Skip to content

Commit b62ca70

Browse files
author
aafent
committed
Support of the Lazy Initialization Pattern for Arrarys
1 parent 42bc610 commit b62ca70

File tree

6 files changed

+82
-33
lines changed

6 files changed

+82
-33
lines changed

FAST.FBasic.InteractiveConsole/Tests/ApplicationForm.bas

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,22 @@ WORDEXTRACTBODY template, text ' Templating Library
1212

1313
rem Extract metadata
1414
PHVSDATA allNames text ' TextReplacer Library
15-
PHSdata cnames text ' TextReplacer Library
1615

1716
rem Orchestrate the needed data
1817
print "Load data on demand..."
1918
let AppID=1010 ' Input from the caller application
2019
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-
20+
retrieve Appl,NEW,1, "select CustomerID,RequestedAmount,LoanTerms,Purpose,ApplicationDate from Applications where ApplicationID="+sql(AppID) ' SQLData Adapter
21+
retrieve Cust,NEW,1, "select Name,VatNumber,Email,Address,City from Customers where CustomerID="+sql([Appl.CustomerID])
2722

2823
rem (v) Do the replacement and save the document
2924
loginfo "Creating the new document"
3025
WORDREPALCEBODY template, ALL, allNames ' Templating Library
3126

32-
3327
rem (v) Append Consent
3428
FILESTREAM Consent, in, "", "other", "Consent.docx"
3529
WORDAPPEND Consent, template
3630

3731
SCOPY template,final ' Streams Library
3832

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-
6033
End

FAST.FBasic.LibraryToolkit/Types/FBasicArray.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@ public class FBasicArray : Types.JaggedArray2D<Value>, IBasicCollection
77
{
88
private string[] columnNames = new string[0];
99

10+
public Action<object[]> LazyInitializingCallback = null!;
11+
1012
/// <summary>
1113
/// Default is false. If it is true, on delete row statement, the row order will prevented
1214
/// but the operation will be slow, as all the rows will shifted one up.

FAST.FBasic.TemplatingLibrary/FBasicTemplatingLibrary.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ private void WordReplaceBody(IInterpreter interpreter)
356356
var replacements=array.ConvertToDictionary<string,string>(0,1);
357357
foreach (var item in replacements)
358358
{
359-
Console.WriteLine(item.Value);
360359
replacements[item.Key] = ToolKitHelper.FormatStringValue(item.Key, interpreter.GetValue(item.Value).ToString());
361360
}
362361
new WordPlaceHolder(placeHolderPrefix,placeHolderSuffix).ReplacePlaceholders(((Stream)stream.Object), replacements, policy);

FAST.FBasicInterpreter/Core/Interpreter_Methods.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,14 @@ public FBasicArray GetArray(string name)
8181
Error(Errors.E112_UndeclaredEntity("Array",name));
8282
return null; // unnecessary but the compiler will like it...
8383
}
84-
return this.arrays[name];
84+
var array= this.arrays[name];
85+
if (array.LazyInitializingCallback == null) return array;
86+
87+
// (v) init
88+
array.LazyInitializingCallback(null);
89+
array.LazyInitializingCallback=null;
90+
91+
return array;
8592
}
8693

8794
/// <summary>

FAST.FBasicInterpreter/DataProviders/sqlFBasicDataProvider.cs

Lines changed: 68 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,21 @@ public static void RETRIEVE(IInterpreter interpreter)
152152
interpreter.GetNextToken();
153153
string sql = interpreter.Expr().ToString();
154154

155+
// (v) add the array with lazy initializing method
156+
FBasicArray array;
157+
if (interpreter.IsArray(name))
158+
{
159+
array = interpreter.GetArray(name);
160+
}
161+
else
162+
{
163+
array = new();
164+
interpreter.AddArray(name,array);
165+
}
166+
array.LazyInitializingCallback=(a)=>lazyRetrieve(interpreter, array, name, rowSet, maxRows, sql);
167+
168+
169+
/*
155170
// (v) do the statement
156171
var adapter = interpreter.GetDataAdapter<sqlFBasicDataProvider>(adapterName);
157172
IBasicCollection collection;
@@ -209,7 +224,7 @@ public static void RETRIEVE(IInterpreter interpreter)
209224
210225
interpreter.DropCollection(name);
211226
}
212-
227+
*/
213228
}
214229
public static Value SQL(IInterpreter interpreter, List<Value> args)
215230
{
@@ -235,6 +250,58 @@ public static Value SQL(IInterpreter interpreter, List<Value> args)
235250
}
236251

237252

253+
private static void lazyRetrieve(IInterpreter interpreter, FBasicArray array, string name, string rowSet, int maxRows, string sql)
254+
{
255+
// (v) do the statement
256+
var adapter = interpreter.GetDataAdapter<sqlFBasicDataProvider>(adapterName);
257+
IBasicCollection collection;
258+
if (!adapter.cursors.ContainsKey(name))
259+
{
260+
adapter.cursors.Add(name, new() { sqlText = sql });
261+
collection = new cursorCollection(name, adapter);
262+
interpreter.AddCollection(name, collection);
263+
}
264+
else
265+
{
266+
adapter.cursors[name].sqlText = sql; //reset the sql command
267+
collection = new cursorCollection(name, adapter);
268+
collection.ClearCollection();
269+
}
270+
collection.MoveNext();
271+
272+
if (collection.Current is IDataRecord data)
273+
{
274+
if (rowSet == "NEW") array.ResetArray();
275+
276+
array.SetColumnNamesFrom(data);
277+
278+
int row;
279+
if (rowSet == "APPEND")
280+
{
281+
row = array.Length + 1;
282+
}
283+
else
284+
{
285+
row = array.GetCurrentRow();
286+
}
287+
288+
if (maxRows == 0) maxRows = int.MaxValue;
289+
for (int times = 1; times <= maxRows; times++)
290+
{
291+
for (int inx = 0; inx < array.ColumnNamesCount; inx++)
292+
{
293+
array[row - 1 + (times - 1), inx] = ToolKitHelper.ToValue(data[inx]);
294+
}
295+
296+
collection.MoveNext();
297+
if (collection.endOfData) break; // stop early (before maxRow) as no more datas
298+
}
299+
300+
interpreter.DropCollection(name);
301+
}
302+
303+
}
304+
238305
}
239306

240307
}

FAST.FBasicInterpreter/ReleaseNotes.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ For **Business and Technical** documentation read the [Wiki page](https://github
99

1010
| When | Description |
1111
|------------|--------------------------------------------------------|
12-
| 2025-11-16 | New statement RETRIEVE for SQL Data Adapter |
12+
| 2025-11-16 | Now Arrays supporting the Lazy Initialization Pattern |
13+
| 2025-11-16 | New statement RETRIEVE for SQL Data Adapter using lazy initialization |
1314
| 2025-11-15 | New format specifier D for dates |
1415
| 2025-11-13 | Support for .Net.10.0 |
1516
| 2025-11-03 | New function singlewhite() to Text Replacer Library |

0 commit comments

Comments
 (0)