@@ -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}
0 commit comments