Skip to content

Commit 8ba0112

Browse files
author
aafent
committed
New statement RETRIEVE for SQL Data Adapter
1 parent 2c40728 commit 8ba0112

12 files changed

Lines changed: 372 additions & 9 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
REM Word Templating - ApplicationForm (example5)
2+
REM (This example is similar to example1 at wordTest1.bas)
3+
REM Create a Loan Application Form by populating with data the template and
4+
REM attaching a second document at the end of the first. The example demonstrating
5+
REM how to retrieve only the necessary for the template data from the database.
6+
REM
7+
8+
rem Prepare template documents
9+
FILESTREAM template, inmemory, "", "other", "LoanTemplate.docx" ' Streams Library
10+
FILESTREAM final, out, "", "Output", "example5.docx"
11+
WORDEXTRACTBODY template, text ' Templating Library
12+
13+
rem Extract metadata
14+
PHVSDATA allNames text ' TextReplacer Library
15+
PHSdata cnames text ' TextReplacer Library
16+
17+
rem Orchestrate the needed data
18+
print "Load data on demand..."
19+
let AppID=1010 ' Input from the caller application
20+
let Today = date()
21+
22+
foreach cnames
23+
loginfo "Setting up...."+[cnames.ph]
24+
phgosub [cnames.ph] else notfound ' TextReplacer Library
25+
endforeach cnames
26+
27+
28+
rem (v) Do the replacement and save the document
29+
loginfo "Creating the new document"
30+
WORDREPALCEBODY template, ALL, allNames ' Templating Library
31+
32+
33+
rem (v) Append Consent
34+
FILESTREAM Consent, in, "", "other", "Consent.docx"
35+
WORDAPPEND Consent, template
36+
37+
SCOPY template,final ' Streams Library
38+
39+
halt
40+
41+
rem
42+
rem ...............DATA SOURCES...................
43+
rem
44+
Appl:
45+
loginfo "Loading Application: "+AppID
46+
retrieve Appl,NEW,1, "select CustomerID,RequestedAmount,LoanTerms,Purpose,ApplicationDate from Applications where ApplicationID="+sql(AppID) ' SQLData Adapter
47+
48+
return
49+
50+
Cust:
51+
loginfo "Loading Customer:"+[Appl.CustomerID]
52+
retrieve Cust,NEW,1, "select Name,VatNumber,Email,Address,City from Customers where CustomerID="+sql([Appl.CustomerID])
53+
54+
return
55+
56+
notfound:
57+
print "***NOT FOUND***"
58+
return
59+
60+
End
29.1 KB
Binary file not shown.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
rem
2+
3+
' RETRIEVE array_name, NEW|APPEND, number|*, SQL Data retrieval statement
4+
retrieve Cust, APPEND, *, "select CustomerID, Name, Email,City from Customers where CustomerID=1"
5+
retrieve Cust, APPEND, 1, "select CustomerID, Name, Email,City from Customers"
6+
retrieve Cust, APPEND, 2, "select CustomerID, Name, Email,City from Customers"
7+
8+
print "Count="+ubound("Cust")
9+
print [Cust.CustomerID]+": Name:"+[Cust.Name]+", Email: "+[Cust.Email]
10+
11+
12+
13+
halt
14+

FAST.FBasic.LibraryToolkit/Interfaces/IInterpreter.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public interface IInterpreter
2323
InterpretationState GetState();
2424
FBasicArray GetArray(string name);
2525
IBasicCollection GetCollection(string name);
26+
void DropCollection(string name);
27+
2628
Marker GetCurrentInstructionMarker();
2729
T GetDataAdapter<T>(string name) where T : IFBasicDataAdapter;
2830
Value GetVar(string name);

FAST.FBasic.LibraryToolkit/ToolKit/ToolKitHelper.cs

Lines changed: 78 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public static class ToolKitHelper
1111
/// <param name="value"></param>
1212
/// <param name="placeholder"></param>
1313
/// <returns></returns>
14-
public static string FormatStringValue(string placeholder, string value )
14+
public static string FormatStringValue(string placeholder, string value)
1515
{
1616
// Check for formatter: {name:specifier,modifiers}
1717
string name = placeholder;
@@ -69,11 +69,11 @@ public static string FormatStringValue(string placeholder, string value )
6969
if (modifiers.Contains("D"))
7070
{
7171
// Date: YYYY-MM-DD to DD-MM-YYYY
72-
if (value.Length >= 10 )
72+
if (value.Length >= 10)
7373
{
7474
value = value.Substring(8, 2) + value[4]
75-
+ value.Substring(5,2) + value[4]
76-
+ value.Substring(0,4)
75+
+ value.Substring(5, 2) + value[4]
76+
+ value.Substring(0, 4)
7777
+ value.Substring(10);
7878
}
7979
}
@@ -151,5 +151,79 @@ public static string ToProperCase(string input)
151151
return firstCharUpper + restOfStringLower;
152152
}
153153

154+
/// <summary>
155+
/// Convert any variable to FBasic Value
156+
/// Does not support arrays
157+
/// </summary>
158+
/// <param name="obj">Input value</param>
159+
/// <param name="acceptObjects">Set to true, if object as value type is supported. Default is false</param>
160+
/// <returns>Value</returns>
161+
public static Value ToValue(object obj, bool acceptObjects = false)
162+
{
163+
if (obj == null) return Value.Zero;
164+
switch (obj)
165+
{
166+
case char c:
167+
return new Value(c);
168+
169+
case string s:
170+
return new Value(s);
171+
172+
case double d:
173+
return new Value(d);
174+
175+
case float f:
176+
return new Value(f);
177+
178+
case decimal d:
179+
return new Value(d);
180+
181+
case int i:
182+
return new Value(i);
183+
case uint i:
184+
return new Value(i);
185+
case long l:
186+
return new Value(l);
187+
case ulong ul:
188+
return new Value(ul);
189+
190+
case byte b:
191+
return new Value(b);
192+
193+
case sbyte sb:
194+
return new Value(sb);
195+
196+
case short sh:
197+
return new Value(sh);
198+
199+
case ushort sh:
200+
return new Value(sh);
201+
202+
case DateTime dt:
203+
return new Value(DateOnly.FromDateTime(dt));
204+
205+
case DateOnly dt:
206+
return new Value(dt);
207+
208+
case TimeOnly dt:
209+
return new Value(dt);
210+
211+
case null: // Handle the case where obj is null
212+
throw new ArgumentNullException(nameof(obj));
213+
214+
default: // Handle any other type
215+
if (!acceptObjects)
216+
{
217+
throw new NotSupportedException($"Type {obj.GetType().Name} is not supported.");
218+
}
219+
220+
return new Value(obj, obj.ToString()!);
221+
222+
}
223+
224+
}
225+
226+
154227
}
155228
}
229+

FAST.FBasic.LibraryToolkit/Types/FBasicArray_ToolKitExtensions.cs

Lines changed: 45 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1-
namespace FAST.FBasicInterpreter
1+
using System.Data;
2+
3+
namespace FAST.FBasicInterpreter
24
{
35
public static class FBasicArray_ToolKitExtensions
46
{
5-
6-
public static Dictionary<T1, T2> ConvertToDictionary<T1,T2>(this FBasicArray array, int keyIndex=0, int valueIndex=1)
7+
/// <summary>
8+
/// Convert a FBasic Array to Dictionary
9+
/// </summary>
10+
/// <typeparam name="T1">Key type (not nullable)</typeparam>
11+
/// <typeparam name="T2">Value type</typeparam>
12+
/// <param name="array">The array to convert</param>
13+
/// <param name="keyIndex">The index in the array of the key value</param>
14+
/// <param name="valueIndex">The index in the array for the value</param>
15+
/// <returns>Dictionary</returns>
16+
public static Dictionary<T1, T2> ConvertToDictionary<T1,T2>(this FBasicArray array, int keyIndex=0, int valueIndex=1)
17+
where T1:notnull
718
{
819
var dictionary = new Dictionary<T1, T2>();
920

@@ -27,5 +38,36 @@ public static Dictionary<T1, T2> ConvertToDictionary<T1,T2>(this FBasicArray arr
2738
return dictionary;
2839
}
2940

41+
42+
/// <summary>
43+
/// Set column names from an IDataRecord (DataReader is IDataRecord)
44+
/// Existing column names will be cleared
45+
/// </summary>
46+
/// <param name="array">Array to set</param>
47+
/// <param name="rec">The IDataRecord</param>
48+
public static void SetColumnNamesFrom(this FBasicArray array, IDataRecord rec)
49+
{
50+
array.ResetColumnNames();
51+
for (int inx = 0; inx < rec.FieldCount; inx++)
52+
{
53+
array.SetColumnName(1+inx,rec.GetName(inx)); // 1+ as the FBasic arrays are 1-based
54+
}
55+
}
56+
57+
/// <summary>
58+
/// Set column names from another FBasic Array
59+
/// Existing column names will be cleared
60+
/// </summary>
61+
/// <param name="array">Array to set</param>
62+
/// <param name="src">The source array</param>
63+
public static void SetColumnNamesFrom(this FBasicArray array, FBasicArray src)
64+
{
65+
array.ResetColumnNames();
66+
for (int inx = 0; inx < src.ColumnNamesCount; inx++)
67+
{
68+
array.SetColumnName(1 + inx, src.GetColumnName(1+inx)); // 1+ as the FBasic arrays are 1-based
69+
}
70+
}
71+
3072
}
3173
}

FAST.FBasic.LibraryToolkit/Types/Value.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,16 @@ public Value(double real) : this()
8181
this.Real = real;
8282
}
8383

84+
/// <summary>
85+
/// Constructor with a decimal as initial value
86+
/// </summary>
87+
/// <param name="decimal">the decimal number</param>
88+
public Value(decimal val) : this()
89+
{
90+
this.Type = ValueType.Real;
91+
this.Real = decimal.ToDouble(val);
92+
}
93+
8494
/// <summary>
8595
/// Constructor with a value an integer casted to Real as initial value
8696
/// </summary>
@@ -91,6 +101,27 @@ public Value(int value)
91101
this.Real = value;
92102
}
93103

104+
105+
/// <summary>
106+
/// Constructor with a value an long casted to Real as initial value
107+
/// </summary>
108+
/// <param name="value">The long</param>
109+
public Value(long value)
110+
{
111+
this.Type = ValueType.Real;
112+
this.Real = value;
113+
}
114+
115+
/// <summary>
116+
/// Constructor with a value an ulong casted to Real as initial value
117+
/// </summary>
118+
/// <param name="value">The ulong</param>
119+
public Value(ulong value)
120+
{
121+
this.Type = ValueType.Real;
122+
this.Real = value;
123+
}
124+
94125
/// <summary>
95126
/// Constructor with a boolean as initial value that it is casted to the appropriate Real.
96127
/// </summary>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+

2+
namespace FAST.FBasicInterpreter.Types
3+
{
4+
/// <summary>
5+
/// Extensions for Value class
6+
/// </summary>
7+
public static class Value_Extensions
8+
{
9+
// Future code here
10+
}
11+
}

FAST.FBasicInterpreter/Core/Interpreter_Methods.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public T GetDataAdapter<T>(string name) where T : IFBasicDataAdapter
207207
/// <param name="collection">The collection handler</param>
208208
public void AddCollection(string name, IBasicCollection collection)
209209
{
210-
if (!dataAdapters.ContainsKey(name))
210+
if (!collections.ContainsKey(name))
211211
{
212212
collections.Add(name, collection);
213213
}
@@ -217,6 +217,20 @@ public void AddCollection(string name, IBasicCollection collection)
217217
}
218218
}
219219

220+
/// <summary>
221+
/// Drop and forget a collection of the interpreter
222+
/// </summary>
223+
/// <param name="name">The collection name</param>
224+
/// <param name="collection">The collection handler</param>
225+
public void DropCollection(string name)
226+
{
227+
if (!collections.ContainsKey(name)) return;
228+
229+
collections[name].ClearCollection(); // prepare collection to remove it
230+
collections[name].Reset();
231+
collections.Remove(name);
232+
}
233+
220234
/// <summary>
221235
/// Request For Object
222236
/// </summary>

FAST.FBasicInterpreter/DataProviders/dataReaderCollection.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ public bool MoveNext()
5959
return !this.endOfData;
6060
}
6161

62+
/// <summary>
63+
/// Reset the Data Reader
64+
/// Is the collection is Open, will close it.
65+
/// </summary>
6266
public void Reset()
6367
{
6468
if (this.isOpen)
@@ -142,6 +146,11 @@ private static bool isNumericType(Type type)
142146
}
143147
}
144148

149+
150+
/// <summary>
151+
/// Clear Collection.
152+
/// It is synonym to Reset()
153+
/// </summary>
145154
public void ClearCollection()
146155
{
147156
Reset();

0 commit comments

Comments
 (0)