eda/sqlite: Removed check on creation of eda_table_fields table
[sdk] / eda / drivers / sqlite / EDASQLite.ec
1 #ifdef ECERE_STATIC
2 public import static "ecere"
3 public import static "EDA"
4 #else
5 public import "ecere"
6 public import "EDA"
7 #endif
8
9 #ifdef __linux__
10 #include <sqlite3.h>
11 #else
12 #include "sqlite3.h"
13 #endif
14
15 #define uint _uint
16 #include "ffi.h"
17 #undef uint
18
19 static void UnusedFunction()
20 {
21    int a;
22    a.OnGetString(0,0,0);
23    a.OnFree();
24    a.OnCopy(null);
25    a.OnCompare(null);
26    a.OnSaveEdit(null,0);
27    a.OnEdit(null,null,0,0,0,0,0);
28    a.OnDisplay(null,0,0,0,0,0,0);
29    a.OnGetDataFromString(null);
30    a.OnUnserialize(null);
31    a.OnSerialize(null);
32 }
33
34 default:
35 extern int __ecereVMethodID_class_OnGetString;
36 extern int __ecereVMethodID_class_OnGetDataFromString;
37 extern int __ecereVMethodID_class_OnCompare;
38 extern int __ecereVMethodID_class_OnSerialize;
39 extern int __ecereVMethodID_class_OnUnserialize;
40 extern int __ecereVMethodID_class_OnFree;
41 private:
42
43 int CollationCompare(Class type, int count1, void * data1, int count2, void * data2)
44 {
45    if(type.type == normalClass || type.type ==  noHeadClass)
46    {
47       Instance inst1, inst2;
48       int result;
49       SerialBuffer buffer1 { size = count1, count = count1, buffer = data1 };
50       SerialBuffer buffer2 { size = count2, count = count2, buffer = data2 };
51
52       ((void (*)(void *, void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnUnserialize])(type, &inst1, buffer1);
53       ((void (*)(void *, void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnUnserialize])(type, &inst2, buffer2);
54
55       result = ((int (*)(void *, void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnCompare])(type, inst1, inst2);
56
57       buffer1.buffer = null;
58       buffer2.buffer = null;
59       delete buffer1;
60       delete buffer2;
61       inst1.OnFree();
62       inst2.OnFree();
63       return result;
64    }
65    else if(type.type == structClass)
66    {
67       void * inst1, * inst2;
68       int result;
69       SerialBuffer buffer1 { size = count1, count = count1, buffer = data1 };
70       SerialBuffer buffer2 { size = count2, count = count2, buffer = data2 };
71
72       inst1 = new0 byte[type.structSize];
73       inst2 = new0 byte[type.structSize];
74       ((void (*)(void *, void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnUnserialize])(type, inst1, buffer1);
75       ((void (*)(void *, void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnUnserialize])(type, inst2, buffer2);
76
77       result = ((int (*)(void *, void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnCompare])(type, inst1, inst2);
78
79       buffer1.buffer = null;
80       buffer2.buffer = null;
81       delete buffer1;
82       delete buffer2;
83       delete inst1;
84       delete inst2;
85       return result;
86    }
87    else
88       return ((int (*)(void *, void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnCompare])(type, data1, data2);
89 }
90
91 public class SQLiteStaticLink { }   // Until .imp generation is fixed
92
93 class SQLiteDataSource : DirFilesDataSourceDriver
94 {
95    class_property(name) = "SQLite";
96    class_property(databaseFileExtension) = "sqlite";
97
98    Database OpenDatabase(const String name, CreateOptions createOptions, DataSource ds)
99    {
100       Database result = null;
101       if(name && name[0])
102       {
103          String path = MakeDatabasePath(name);
104          sqlite3 * db;
105
106          // sqlite3_open(path, &db);
107          // sqlite3_open_v2(path, &db, SQLITE_OPEN_READONLY /*SQLITE_OPEN_READWRITE*/ /*SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE*/, null );
108
109          if(sqlite3_open_v2(path, &db, (createOptions == readOnly) ? SQLITE_OPEN_READONLY :
110             (SQLITE_OPEN_READWRITE | ((createOptions == create) ? SQLITE_OPEN_CREATE : 0)), null))
111          {
112             // fprintf(stderr, "%s\n", s); // interesting
113             printf($"EDASQLite: Can't open database (%s): %s\n", path, sqlite3_errmsg(db));
114             sqlite3_close(db);
115          }
116          else
117          {
118             bool success = true;
119             char command[1024];
120             sprintf(command, "CREATE TABLE eda_table_fields(Table_Name TEXT, Name TEXT, Type TEXT, Length INT);");
121             sqlite3_exec(db, command, null, null, null);
122
123             if(createOptions != readOnly)
124             {
125                sqlite3_exec(db, "PRAGMA locking_mode=exclusive", null, null, null);
126                sqlite3_exec(db, "DELETE FROM eda_table_fields WHERE Name = 'lockDummy'", null, null, null);
127                if(sqlite3_exec(db, "INSERT INTO eda_table_fields (Table_Name, Name, Type, Length) VALUES ('lockDummy', 'lockDummy', 'lockDummy', 'lockDummy')", null, null, null))
128                   success = false;
129                else
130                   sqlite3_exec(db, "DELETE FROM eda_table_fields WHERE Name = 'lockDummy'", null, null, null);
131             }
132
133             if(success)
134                result = SQLiteDatabase { db = db };
135          }
136          delete path;
137       }
138       return result;
139    }
140 }
141
142 class SQLiteField : Field
143 {
144    char * name;
145    Class type;
146    int length;
147    public LinkElement<SQLiteField> link;
148    int num;
149    int sqliteType;
150    SQLiteTable tbl;
151
152    ~SQLiteField()
153    {
154       delete name;
155    }
156
157    String GetName()
158    {
159       return name;
160    }
161    Class GetType()
162    {
163       return type;
164    }
165    int GetLength() { return length; }
166    Field GetPrev()
167    {
168       return link.prev;
169    }
170    Field GetNext()
171    {
172       return link.next;
173    }
174    Table GetTable()
175    {
176       return tbl;
177    }
178 }
179
180 class SQLiteDatabase : Database
181 {
182    sqlite3 * db;
183    AVLTree<String> collations { };
184
185    ~SQLiteDatabase()
186    {
187       sqlite3_exec(db, "PRAGMA locking_mode=normal", null, null, null);
188       sqlite3_close(db);
189    }
190
191    uint ObjectsCount(ObjectType type)
192    {
193       // TODO
194       return 0;
195    }
196
197    bool RenameObject(ObjectType type, const String name, const String rename)
198    {
199       // TODO
200       return false;
201    }
202
203    bool DeleteObject(ObjectType type, const String name)
204    {
205       // TODO
206       return false;
207    }
208
209    Table OpenTable(const String name, OpenOptions options)
210    {
211       char command[1024];
212       int result;
213       int nRows = 0, nCols = 0;
214       char ** t;
215       SQLiteTable table = null;
216       if(options.type == tablesList)
217       {
218          SQLiteField field;
219          strcpy(command, "SELECT name FROM sqlite_master WHERE type='table' AND name!='eda_table_fields';");
220          table = SQLiteTable { db = this, specialStatement = CopyString(command) };
221          field = { tbl = table, name = CopyString("Name"), type = class(String), num = -1, sqliteType = SQLITE_TEXT };
222          LinkTable(table);
223          incref field;
224          table._fields.Add(field);
225       }
226       else if(options.type == fieldsList)
227       {
228          SQLiteField field;
229
230          sprintf(command, "SELECT Name, Type, Length FROM eda_table_fields WHERE Table_Name='%s';", name);
231          table = SQLiteTable { db = this, specialStatement = CopyString(command) };
232          LinkTable(table);
233          field = { tbl = table, name = CopyString("Name"), type = class(String), num = -1, sqliteType = SQLITE_TEXT };
234          incref field;
235          table._fields.Add(field);
236          field = { tbl = table, name = CopyString("Type"), type = class(Class), num = 0, sqliteType = SQLITE_TEXT };
237          incref field;
238          table._fields.Add(field);
239          field = { tbl = table, name = CopyString("Length"), type = class(int), num = 1, sqliteType = SQLITE_INTEGER };
240          incref field;
241          table._fields.Add(field);
242       }
243       else if(options.type == tableRows)
244       {
245          bool addFields = false;
246
247          sprintf(command, "SELECT Name FROM eda_table_fields WHERE Table_Name='%s';", name);
248          result = sqlite3_get_table(db, command, &t, &nRows, &nCols, null);
249          if(!nRows && !nCols)
250             addFields = true;
251
252          sqlite3_free_table(t);
253
254          sprintf(command, "SELECT sql FROM sqlite_master WHERE type='table' AND name='%s';", name);
255          nCols = 0, nRows = 0;
256          result = sqlite3_get_table(db, command, &t, &nRows, &nCols, null);
257
258          if((nCols || nRows) || options.create)
259          {
260             table = SQLiteTable { db = this, name = CopyString(name) };
261             LinkTable(table);
262             if(!nCols && !nRows)
263                table.mustCreate = true;
264             else
265             {
266                if(addFields)
267                {
268                   int r;
269                   for(r = 1; r <= nRows; r++)      // There should be only 1 row here
270                   {
271                      char * sql = t[nCols * r];
272                      char * bracket = strchr(sql, '(');
273                      if(bracket)
274                      {
275                         int c = 0;
276                         bracket++;
277                         while(true)
278                         {
279                            char ch;
280                            char fieldName[256];
281                            char dataType[256];
282                            int d;
283                            int start = c;
284                            int sqliteType = SQLITE_BLOB;
285                            Class type = class(int);
286                            fieldName[0] = 0;
287                            dataType[0] = 0;
288
289                            while((ch = bracket[c++]))
290                            {
291                               if(ch == ',' || ch == ')')
292                                  break;
293                            }
294                            for(d = c-1; d >= 0 && bracket[d] != ' '; d--);
295
296                            memcpy(fieldName, bracket + start, d - start);
297                            fieldName[d - start] = 0;
298
299                            memcpy(dataType, bracket + d + 1, c - d - 2);
300                            dataType[c - d - 2] = 0;
301
302                            while(ch && bracket[c] == ' ') c++;
303
304                            if(!strcmp(dataType, "REAL")) { sqliteType = SQLITE_FLOAT; type = class(double); }
305                            else if(!strcmp(dataType, "TEXT")) { sqliteType = SQLITE_TEXT; type = class(String); }
306                            else if(!strcmp(dataType, "INTEGER")) { sqliteType = SQLITE_INTEGER; type = class(int); }
307                            else if(!strcmp(dataType, "BLOB")) { sqliteType = SQLITE_BLOB; type = class(char *); } //class(byte *);
308
309                            sprintf(command, "INSERT INTO eda_table_fields (Table_Name, Name, Type, Length) VALUES ('%s', '%s', '%s', %d);", name,
310                               fieldName, type.name, 0);
311                            result = sqlite3_exec(db, command, null, null, null);
312
313                            {
314                               SQLiteField field { tbl = table, name = CopyString(fieldName), type = type, num = table._fields.count, sqliteType = sqliteType };
315                               incref field;
316                               table._fields.Add(field);
317                            }
318
319                            if(!ch || ch == ')') break;
320                         }
321                      }
322                   }
323                }
324                else
325                {
326                   Table refTable = null;
327                   sqlite3_stmt * statement;
328
329                   sprintf(command, "SELECT Name, Type, Length FROM eda_table_fields WHERE Table_Name='%s';", name);
330                   result = sqlite3_prepare_v2(db, command, -1, &statement, null);
331
332                   while(sqlite3_step(statement) != SQLITE_DONE)
333                   {
334                      char * fieldName = sqlite3_column_text(statement, 0);
335                      char * typeName = sqlite3_column_text(statement, 1);
336                      int length = sqlite3_column_int(statement, 2);
337                      Class type = null;
338                      int sqliteType = SQLITE_BLOB;
339
340                      ((Class)(&type)).OnGetDataFromString(typeName);    // TODO: THIS REQUIRES A FIX SOMEWHERE ELSE
341
342                      if(type)
343                      {
344                         if(!strcmp(type.dataTypeString, "int") || !strcmp(type.dataTypeString, "unsigned int") ||
345                            !strcmp(type.dataTypeString, "long") || !strcmp(type.dataTypeString, "long int") ||
346                            !strcmp(type.dataTypeString, "uint") || !strcmp(type.dataTypeString, "uint32") ||
347                            !strcmp(type.dataTypeString, "int64") || !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64") ||
348                            !strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
349                            !strcmp(type.dataTypeString, "char") || !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
350                            sqliteType = SQLITE_INTEGER;
351                         else if(!strcmp(type.dataTypeString, "double") || !strcmp(type.dataTypeString, "float"))
352                            sqliteType = SQLITE_FLOAT;
353                         else if(!strcmp(type.dataTypeString, "String") || !strcmp(type.dataTypeString, "char *"))
354                            sqliteType = SQLITE_TEXT;
355                         else
356                         {
357                            if(strcmp(type.fullName, "CIString") && !collations.Find(type.fullName))
358                            {
359                               collations.Add(type.fullName);
360                               sqlite3_create_collation_v2(table.db.db, type.fullName, SQLITE_UTF8, type, CollationCompare, null);
361                            }
362                            sqliteType = SQLITE_BLOB;
363                         }
364                      }
365
366                      {
367                         Table * fTable = (Table *)eClass_GetProperty(type, "table");
368                         SQLiteField field { tbl = table, name = CopyString(fieldName), type = type, length = length, num = table._fields.count, sqliteType = sqliteType };
369                         incref field;
370                         if(fTable) refTable = *fTable;
371                         if(!table.primaryKey && refTable && !strcmp(refTable.name, table.name))
372                            table.primaryKey = field;
373
374                         table._fields.Add(field);
375                      }
376                   }
377                   sqlite3_finalize(statement);
378                }
379             }
380          }
381          sqlite3_free_table(t);
382       }
383       return (Table)table;
384    }
385
386    bool Begin()
387    {
388       char command[1024];
389       int result;
390       sprintf(command, "BEGIN;");
391       result = sqlite3_exec(db, command, null, null, null);
392       if(result)
393          PrintLn($"BEGIN FAILED!");
394       return result == SQLITE_OK;
395    }
396
397    bool Commit()
398    {
399       char command[1024];
400       int result;
401       sprintf(command, "COMMIT;");
402       result = sqlite3_exec(db, command, null, null, null);
403       if(result)
404          PrintLn($"COMMIT FAILED!");
405       return result == SQLITE_OK;
406    }
407
408    bool CreateCustomFunction(char * name, SQLCustomFunction customFunction)
409    {
410       bool result = false;
411       Class cfClass = customFunction._class;
412       customFunction.method = eClass_FindMethod(cfClass, "function", cfClass.module);
413       if(customFunction.method)
414       {
415          String typeString = CopyString(customFunction.method.dataTypeString);
416          char * tokens[256];
417          int count = TokenizeWith(typeString, sizeof(tokens)/sizeof(tokens[0]), tokens, "(,)", false);
418          int c;
419          bool variadic = false;
420
421          for(c = 0; c < count; c++)
422          {
423             Class type = null;
424             bool pointer = false;
425             String arg = tokens[c];
426             char * space;
427             TrimLSpaces(arg, arg);
428             if(strchr(arg, '*')) pointer = true;
429             if(pointer)
430                // Using String for generic pointer...
431                type = class(String);
432             else
433             {
434                if((space = strchr(arg, ' '))) *space = 0;
435                if(!strcmp(arg, "void"))
436                   type = null;
437                else if(!strcmp(arg, "..."))
438                   variadic = true;
439                else
440                {
441                   if(cfClass.templateParams.count)
442                   {
443                      ClassTemplateParameter p;
444                      int id = 0;
445                      for(p = cfClass.templateParams.first; p; p = p.next, id++)
446                      {
447                         if(!strcmp(p.name, arg))
448                            break;
449                      }
450                      if(p && cfClass.templateArgs)
451                         arg = cfClass.templateArgs[id].dataTypeString;
452                   }
453                   type = eSystem_FindClass(customFunction._class.module, arg);
454                   if(!type)
455                      type = eSystem_FindClass(customFunction._class.module.application, arg);
456                }
457             }
458             if(c == 0)
459                customFunction.returnType = type;
460             else
461                customFunction.args.Add(type);
462          }
463          delete typeString;
464          if(variadic)
465          {
466             result = false;
467             // Variadic args don't make sense for SQL custom functions
468             // Note that different CIF must be prepared for different set of arguments
469             // ffi_prep_cif_var(&customFunction.cif, FFI_DEFAULT_ABI, args.count-1, rType, argTypes);
470          }
471          else
472          {
473             customFunction.rType = FFIGetType(customFunction.returnType, true);
474             customFunction.argTypes.Add((void *)&ffi_type_pointer);    // This pointer for SQLCustomFunction object
475             for(a : customFunction.args) customFunction.argTypes.Add((void *)FFIGetType(a, false));
476             ffi_prep_cif(&customFunction.cif, FFI_DEFAULT_ABI, customFunction.argTypes.count, customFunction.rType, (ffi_type **) customFunction.argTypes.array);
477             result = sqlite3_create_function(db, name, customFunction.args.count, SQLITE_UTF8, customFunction, SQLiteFunctionProcessor, null, null) == SQLITE_OK;
478          }
479       }
480       return result;
481    }
482 }
483
484 static class FFITypesHolder : Map<Class, String> { ~FFITypesHolder() { Free(); } }
485 FFITypesHolder structFFITypes { };
486
487 public ffi_type * FFIGetType(Class type, bool structByValue)
488 {
489    if(type)
490       switch(type.type)
491       {
492          // Pointer Types
493          case structClass:
494             if(structByValue)
495             {
496                MapIterator<Class, String> it { map = structFFITypes };
497                ffi_type * ffiType = null;
498                if(it.Index(type, false))
499                   ffiType = (void *)it.data;
500                else
501                {
502                   /*
503                   DataMember member;
504                   Array<String> memberTypes { };
505                   for(member = type.membersAndProperties.first; member; member = member.next)
506                   {
507                      if(!member.isProperty)
508                      {
509                         memberTypes.Add(FFIGetType(member.dataType
510                      }
511                   }
512                   */
513                   ffiType = new0 ffi_type[1];
514                   ffiType->size = type.structSize;
515                   ffiType->type = FFI_TYPE_STRUCT;
516                   structFFITypes[type] = (void *)ffiType;
517                }
518                return ffiType;
519             }
520          case normalClass:
521          case noHeadClass:
522          case unionClass:
523             return &ffi_type_pointer;
524          // Scalar Types
525          case bitClass:
526          case enumClass:
527          case systemClass:
528          case unitClass:
529                  if(!strcmp(type.dataTypeString, "float"))  return &ffi_type_float;
530             else if(!strcmp(type.dataTypeString, "double")) return &ffi_type_double;
531             else
532                switch(type.typeSize)
533                {
534                   case 1: return &ffi_type_uint8;
535                   case 2: return &ffi_type_uint16;
536                   case 4: return &ffi_type_uint32;
537                   case 8: return &ffi_type_uint64;
538                }
539       }
540    else
541       return &ffi_type_void;
542    return null;
543 }
544
545 static SerialBuffer staticBuffer { };
546 void SQLiteFunctionProcessor(sqlite3_context* context, int n, sqlite3_value** values)
547 {
548    SQLCustomFunction sqlFunction = sqlite3_user_data(context);
549
550    /*  // Simple 1 pointer param returning a string
551    void * p = sqlFunction.method.function(sqlFunction, sqlite3_value_text(values[0]));
552    sqlite3_result_text(context, p, strlen(p), SQLITE_TRANSIENT);
553    */
554    int64 retData = 0;
555    void * ret = &retData;
556    Array<String> args { size = sqlFunction.args.count + 1 };
557    Iterator<String> ffiArg { sqlFunction.argTypes };
558    Iterator<String> arg { args };
559    int i = 0;
560
561    // this * for the SQLCustomFunction
562    args[0] = (void *)&sqlFunction;
563    ffiArg.Next();
564    // Get the arguments from SQLite
565    for(a : sqlFunction.args)
566    {
567       ffi_type * type = (ffi_type *)sqlFunction.argTypes[i+1];
568       if(i >= n) break;
569       switch(a.type)
570       {
571          case normalClass:
572          case noHeadClass:
573          case structClass:
574          case unionClass:
575          {
576             void ** data = new void *[1];
577             args[i+1] = (void *)data;
578             if(a == class(String))
579             {
580                int numBytes = sqlite3_value_bytes(values[i]);
581                char * text = sqlite3_value_text(values[i]);
582                *(char **)data = text ? new byte[numBytes+1] : null;
583                if(text)
584                   memcpy(*(char **)data, text, numBytes+1);
585             }
586             else
587             {
588                SerialBuffer buffer = staticBuffer; //{ };
589                buffer.pos = 0;
590                buffer._size = sqlite3_value_bytes(values[i]);
591                buffer._buffer = sqlite3_value_text(values[i]);
592                //buffer._buffer = sqlite3_value_blob(curStatement);
593                buffer.count = buffer._size;
594                if(a.type == structClass)
595                   *data = new byte[a.structSize];
596                ((void (*)(void *, void *, void *))(void *)a._vTbl[__ecereVMethodID_class_OnUnserialize])(a, (a.type == structClass) ? *data : data, buffer);
597                buffer._buffer = null;
598                //delete buffer;
599             }
600             break;
601          }
602          case bitClass:
603          case enumClass:
604          case systemClass:
605          case unitClass:
606             if(type == &ffi_type_double || type == &ffi_type_float)
607             {
608                double d = sqlite3_value_double(values[i]);
609                if(a.typeSize == 8)
610                {
611                   double * data = new double[1];
612                   args[i+1] = (void *)data;
613                   *data = d;
614                }
615                else
616                {
617                   float * data = new float[1];
618                   args[i+1] = (void *)data;
619                   *data = (float)d;
620                }
621             }
622             else
623             {
624                switch(a.typeSize)
625                {
626                   case 8:
627                   {
628                      int64 * data = new int64[1];
629                      args[i+1] = (void *)data;
630                      *data = sqlite3_value_int64(values[i]);
631                      break;
632                   }
633                   case 4:
634                   {
635                      int * data = new int[1];
636                      args[i+1] = (void *)data;
637                      *data = sqlite3_value_int(values[i]);
638                      break;
639                   }
640                   case 2:
641                   {
642                      short * data = new short[1];
643                      int value;
644                      args[i+1] = (void *)data;
645                      value = sqlite3_value_int(values[i]);
646                      if(value < 0)
647                         *data = (short)value;
648                      else
649                         *(uint16 *)data = (uint16)value;
650                      break;
651                   }
652                   case 1:
653                   {
654                      char * data = new char[1];
655                      int value;
656                      args[i+1] = data;
657                      value = sqlite3_value_int(values[i]);
658                      if(value < 0)
659                         *data = (char)value;
660                      else
661                         *(byte *)data = (byte)value;
662                      break;
663                   }
664                }
665             }
666             break;
667       }
668       i++;
669       ffiArg.Next();
670    }
671    if(sqlFunction.returnType && sqlFunction.returnType.type == structClass)
672       ret = new byte[sqlFunction.returnType.typeSize];
673    ffi_call(&sqlFunction.cif, (void *)sqlFunction.method.function, ret, args.array);
674    // Give SQLite the return value
675    if(sqlFunction.returnType)
676    {
677       ffi_type * type = sqlFunction.rType;
678       Class r = sqlFunction.returnType;
679       switch(r.type)
680       {
681          case normalClass:
682          case noHeadClass:
683          case structClass:
684          case unionClass:
685          {
686             void * data = ret ? *(void **)ret : null;
687             if(r.type == structClass)
688                data = ret;
689             if(r == class(String))
690             {
691                if(data)
692                   sqlite3_result_text(context, (char *)data, strlen((char *)data), SQLITE_TRANSIENT);
693                else
694                   sqlite3_result_text(context, null, 0, SQLITE_TRANSIENT);
695             }
696             else
697             {
698                SerialBuffer buffer { };
699                ((void (*)(void *, void *, void *))(void *)r._vTbl[__ecereVMethodID_class_OnSerialize])(r, data, buffer);
700                sqlite3_result_text(context, buffer._buffer, buffer.count, SQLITE_TRANSIENT);
701                delete buffer;
702
703                // Avoid destroying Strings for now... (Returning memory owned by the Custom Function)
704                ((void (*)(void *, void *))(void *)r._vTbl[__ecereVMethodID_class_OnFree])(r, data);
705             }
706
707             if(r.type == structClass)
708                delete ret;
709             break;
710          }
711          case bitClass:
712          case enumClass:
713          case systemClass:
714          case unitClass:
715             if(type == &ffi_type_double || type == &ffi_type_float)
716             {
717                if(r.typeSize == 8)
718                   sqlite3_result_double(context, *(double *)ret);
719                else
720                   sqlite3_result_double(context, (double)*(float *)ret);
721             }
722             else
723             {
724                switch(r.typeSize)
725                {
726                   case 8:
727                      sqlite3_result_int64(context, (sqlite3_int64)*(int64 *)ret);
728                      break;
729                   case 4:
730                      sqlite3_result_int(context, *(int *)ret);
731                      break;
732                   case 2:
733                   {
734                      int value;
735                      //if((int)data < 0)
736                         value = (int)*(short *)ret;
737                      //else
738                         //value = (int)*(uint16 *)ret;
739                      sqlite3_result_int(context, value);
740                      break;
741                   }
742                   case 1:
743                   {
744                      int value;
745                      //if((int)data < 0)
746                         value = (int)*(char *)ret;
747                      //else
748                         //value = (int)*(byte *)ret;
749                      sqlite3_result_int(context, value);
750                      break;
751                   }
752                }
753             }
754             break;
755       }
756    }
757
758    // Free Stuff up
759    arg.Next();
760    for(type : sqlFunction.args; arg.Next())
761    {
762       // Free instance
763       void * data = *(void **)arg.data;
764       ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, data);
765       if(type.type == structClass)
766          delete data;
767       // Free arg holder
768       data = arg.data;
769       delete data;
770    }
771    delete args;
772 }
773
774 class SQLiteTable : Table
775 {
776    char * name;
777    bool mustCreate;
778    SQLiteDatabase db;
779    LinkList<SQLiteField> _fields { };
780    char * specialStatement;
781    SQLiteField primaryKey;
782    FieldIndex * indexFields;
783    int indexFieldsCount;
784    int64 lastID;
785
786    Field AddField(const String fieldName, Class type, int length)
787    {
788       SQLiteField field;
789       char command[1024];
790       char dataType[256];
791       int sqliteType;
792       int result;
793       Table refTable = null;
794       Field idField = null;
795       command[0] = 0;
796
797       if(FindField(fieldName)) return null;
798
799       if(!strcmp(type.dataTypeString, "int") || !strcmp(type.dataTypeString, "unsigned int") ||
800          !strcmp(type.dataTypeString, "long") || !strcmp(type.dataTypeString, "long int") ||
801          !strcmp(type.dataTypeString, "uint") || !strcmp(type.dataTypeString, "uint32") ||
802          !strcmp(type.dataTypeString, "int64") || !strcmp(type.dataTypeString, "unsigned int64") || !strcmp(type.dataTypeString, "uint64") ||
803          !strcmp(type.dataTypeString, "short") || !strcmp(type.dataTypeString, "unsigned short") || !strcmp(type.dataTypeString, "uint16") ||
804          !strcmp(type.dataTypeString, "char") || !strcmp(type.dataTypeString, "unsigned char") || !strcmp(type.dataTypeString, "byte"))
805       {
806          strcpy(dataType, "INTEGER");
807          sqliteType = SQLITE_INTEGER;
808       }
809       else if(!strcmp(type.dataTypeString, "double") || !strcmp(type.dataTypeString, "float"))
810       {
811          strcpy(dataType, "REAL");
812          sqliteType = SQLITE_FLOAT;
813       }
814       else if(!strcmp(type.name, "CIString"))
815       {
816          strcpy(dataType, "TEXT");
817          sqliteType = SQLITE_BLOB;
818       }
819       else if(!strcmp(type.dataTypeString, "String") || !strcmp(type.dataTypeString, "char *"))
820       {
821          strcpy(dataType, "TEXT");
822          sqliteType = SQLITE_TEXT;
823       }
824       else
825       {
826          //strcpy(dataType, "BLOB");
827          strcpy(dataType, "TEXT");
828          sqliteType = SQLITE_BLOB;
829
830          if(!db.collations.Find(type.fullName))
831          {
832             db.collations.Add(type.fullName);
833             result = sqlite3_create_collation_v2(db.db, type.fullName, SQLITE_UTF8, type, CollationCompare, null);
834          }
835       }
836       if(sqliteType != SQLITE_BLOB && eClass_IsDerived(type, class(eda::Id)))
837       {
838          Table * table = (Table *)eClass_GetProperty(type, "table");
839          if(table) refTable = *table;
840          if(refTable)
841          {
842             if(primaryKey || refTable != this)
843             {
844                for(idField = refTable.firstField; idField; idField = idField.next)
845                   if(eClass_IsDerived(type, idField.type)) break;
846
847                if(!idField)
848                   PrintLn("WARNING: field not yet created for class ", (String)type.name);
849             }
850             else
851                idField = primaryKey;
852          }
853          else
854          {
855             PrintLn($"WARNING: Table not yet created for class ", (String)type.name);
856          }
857       }
858
859       if(mustCreate)
860       {
861          if(sqliteType == SQLITE_BLOB)
862          {
863             if(!strcmp(type.name, "CIString"))
864                sprintf(command, "CREATE TABLE `%s`(%s %s COLLATE NOCASE);", name, fieldName, dataType);
865             else
866                sprintf(command, "CREATE TABLE `%s`(%s %s COLLATE '%s');", name, fieldName, dataType, type.fullName);
867          }
868          else if(refTable)
869          {
870             if(!idField && refTable == this)
871                sprintf(command, "CREATE TABLE `%s`(`%s` %s PRIMARY KEY);", name, fieldName, dataType);
872             else if(idField)
873                sprintf(command, "CREATE TABLE `%s`(`%s` %s REFERENCES `%s`(`%s`));", name, fieldName, dataType, refTable.name, idField.name);
874          }
875          else
876             sprintf(command, "CREATE TABLE `%s`(`%s` %s);", name, fieldName, dataType);
877          result = sqlite3_exec(db.db, command, null, null, null);
878          if(result) return null;
879          mustCreate = false;
880       }
881       else
882       {
883          if(sqliteType == SQLITE_BLOB)
884          {
885             if(!strcmp(type.name, "CIString"))
886                sprintf(command, "ALTER TABLE `%s` ADD `%s` %s COLLATE NOCASE;", name, fieldName, dataType);
887             else
888                sprintf(command, "ALTER TABLE `%s` ADD `%s` %s COLLATE `%s`;", name, fieldName, dataType, type.fullName);
889          }
890          else if(refTable)
891          {
892             if(!idField && refTable == this)
893             {
894                PrintLn($"WARNING: ALTER TABLE DOESN'T WORK WITH PRIMARY KEY FOR ", (String)name);
895                sprintf(command, "ALTER TABLE `%s` ADD `%s` %s PRIMARY KEY;", name, fieldName, dataType);
896             }
897             else if(idField)
898                sprintf(command, "ALTER TABLE `%s` ADD `%s` %s REFERENCES `%s`(`%s`);", name, fieldName, dataType, refTable.name, idField.name);
899          }
900          else
901             sprintf(command, "ALTER TABLE `%s` ADD `%s` %s;", name, fieldName, dataType);
902          result = sqlite3_exec(db.db, command, null, null, null);
903          if(result) return null;
904       }
905
906       sprintf(command, "INSERT INTO eda_table_fields (Table_Name, Name, Type, Length) VALUES ('%s', '%s', '%s', %d);", name,
907          fieldName, type.name, length);
908       result = sqlite3_exec(db.db, command, null, null, null);
909
910       field = { name = CopyString(fieldName), type = type, num = _fields.count, sqliteType = sqliteType };
911       incref field;
912       _fields.Add(field);
913       if(!primaryKey && refTable == this)
914          primaryKey = field;
915       return (Field)field;
916    }
917
918    Field FindField(const String name)
919    {
920       for(f : _fields; !strcmp(f.name, name))
921       {
922          if(!primaryKey)
923          {
924             if(f.sqliteType != SQLITE_BLOB && eClass_IsDerived(f.type, class(eda::Id)))
925             {
926
927                Table * tablePtr = (Table *)eClass_GetProperty(f.type, "table");
928                if(tablePtr && *tablePtr == this)
929                   primaryKey = f;
930             }
931          }
932          return (Field)f;
933       }
934       return null;
935    }
936
937    bool GenerateIndex(int count, FieldIndex * fieldIndexes, bool init)
938    {
939       char command[1024];
940       int c;
941       int result;
942       char indexName[4096];
943
944       delete indexFields;
945       indexFieldsCount = count;
946       indexFields = new FieldIndex[count];
947       memcpy(indexFields, fieldIndexes, count * sizeof(FieldIndex));
948
949       // TODO: USE CODED INDEX NAME INSTEAD?
950       strcpy(indexName, "index_");
951       strcat(indexName, name);
952       strcat(indexName, "_");
953       for(c = 0; c<count; c++)
954       {
955          if(fieldIndexes[c].field)
956          {
957             if(count == 1 && fieldIndexes[c].field == primaryKey)
958                return true;
959             strcat(indexName, fieldIndexes[c].field.name);
960             if(fieldIndexes[c].memberField)
961             {
962                strcat(indexName, ".");
963                strcat(indexName, fieldIndexes[c].memberField.name);
964             }
965             strcat(indexName, (fieldIndexes[c].order == ascending) ? "+" : "-");
966          }
967          else
968             return false;
969       }
970
971       sprintf(command, "CREATE INDEX IF NOT EXISTS `%s` ON `%s` (", indexName, name);
972       for(c = 0; c<count; c++)
973       {
974          char columnName[1024];
975          sprintf(columnName, "`%s` %s", fieldIndexes[c].field.name, (fieldIndexes[c].order == ascending) ? "ASC" : "DESC");
976          if(c > 0) strcat(command, ", ");
977          strcat(command, columnName);
978       }
979       strcat(command, ");");
980       result = sqlite3_exec(db.db, command, null, null, null);
981
982       return result == SQLITE_OK;
983    }
984
985    String GetName()
986    {
987       return name;
988    }
989
990    Field GetFirstField()
991    {
992       return _fields.first;
993    }
994
995    Field GetPrimaryKey()
996    {
997       return primaryKey;
998    }
999
1000    uint GetFieldsCount()
1001    {
1002       return _fields.count;
1003    }
1004
1005    uint GetRowsCount()
1006    {
1007       char command[1024];
1008       char **t;
1009       int nCols, nRows;
1010       int result;
1011       uint rowCount = 0;
1012       sprintf(command, "SELECT COUNT(*) FROM `%s`;", name);
1013       result = sqlite3_get_table(db.db, command, &t, &nRows, &nCols, null);
1014       if(result == SQLITE_OK)
1015       {
1016          rowCount = atoi(t[1]);
1017          sqlite3_free_table(t);
1018       }
1019       return rowCount;
1020    }
1021
1022    // Returns true if not ordered by row ID
1023    bool GetIndexOrder(char * fullOrder, bool flip)
1024    {
1025       if(!flip && (!indexFields || (indexFieldsCount == 1 && indexFields[0].field == primaryKey && indexFields[0].order == ascending)))
1026       {
1027          strcpy(fullOrder, " ORDER BY ROWID");
1028          return false;
1029       }
1030       else
1031       {
1032          int c;
1033          strcpy(fullOrder, " ORDER BY ");
1034          for(c = flip ? indexFieldsCount-1 : 0; flip ? (c >= 0) : (c < indexFieldsCount); flip ? c-- : c++)
1035          {
1036             char order[1024];
1037             FieldIndex * fIndex = &indexFields[c];
1038             order[0] = 0;
1039             if(c) strcat(order, ", ");
1040             strcat(order, "`");
1041             strcat(order, fIndex->field.name);
1042             strcat(order, "`");
1043             if(fIndex->order == (flip ? ascending : descending)) strcat(order, " DESC");
1044             strcat(fullOrder, order);
1045          }
1046          return true;
1047       }
1048    }
1049
1050    Container<Field> GetFields()
1051    {
1052       return (Container<Field>)_fields;
1053    }
1054
1055    DriverRow CreateRow()
1056    {
1057       char command[1024];
1058       sqlite3_stmt * statement;
1059       sqlite3_stmt * sysIDStmt = null, * insertStmt = null, * deleteStmt = null, * selectRowIDsStmt = null, * setRowIDStmt = null;
1060       sqlite3_stmt * prevStmt = null, * nextStmt = null, * lastStmt = null, * insertIDStmt = null;
1061
1062       if(specialStatement)
1063          strcpy(command, specialStatement);
1064       else
1065       {
1066          char order[1024];
1067          /*sprintf(command, "SELECT ROWID, * FROM `%s` WHERE ? = ?;", name);
1068          sqlite3_prepare_v2(db.db, command, -1, &findStmt, null);*/
1069          sprintf(command, "SELECT ROWID, * FROM `%s` WHERE ROWID = ?;", name);
1070          sqlite3_prepare_v2(db.db, command, -1, &sysIDStmt, null);
1071
1072          sprintf(command, "INSERT INTO `%s` DEFAULT VALUES;", name);
1073          sqlite3_prepare_v2(db.db, command, -1, &insertStmt, null);
1074
1075          sprintf(command, "INSERT INTO `%s` (ROWID) VALUES(?);", name);
1076          sqlite3_prepare_v2(db.db, command, -1, &insertIDStmt, null);
1077
1078          sprintf(command, "DELETE FROM `%s` WHERE ROWID = ?;", name);
1079          sqlite3_prepare_v2(db.db, command, -1, &deleteStmt, null);
1080
1081          sprintf(command, "SELECT ROWID, * FROM `%s` WHERE ROWID < ? ORDER BY ROWID DESC LIMIT 1;", name);
1082          sqlite3_prepare_v2(db.db, command, -1, &prevStmt, null);
1083
1084          sprintf(command, "SELECT ROWID, * FROM `%s` WHERE ROWID > ? ORDER BY ROWID LIMIT 1;", name);
1085          sqlite3_prepare_v2(db.db, command, -1, &nextStmt, null);
1086
1087          sprintf(command, "SELECT MAX(ROWID), * FROM `%s`", name);
1088          sqlite3_prepare_v2(db.db, command, -1, &lastStmt, null);
1089
1090          /*sprintf(command, "UPDATE `%s` SET ? = ? WHERE ROWID = ?;", name);
1091
1092          sqlite3_prepare_v2(db.db, command, -1, &updateStmt, null);*/
1093
1094          GetIndexOrder(order, false);
1095          sprintf(command, "SELECT ROWID, * FROM `%s`%s;", name, order);
1096       }
1097       sqlite3_prepare_v2(db.db, command, -1, &statement, null);
1098
1099       sprintf(command, "SELECT ROWID FROM `%s` WHERE ROWID > ?", name);
1100       sqlite3_prepare_v2(db.db, command, -1, &selectRowIDsStmt, null);
1101
1102       sprintf(command, "UPDATE `%s` SET ROWID = ? WHERE ROWID = ?", name);
1103       sqlite3_prepare_v2(db.db, command, -1, &setRowIDStmt, null);
1104
1105       return SQLiteRow
1106          { tbl = this, defaultStatement = statement, curStatement = statement, sysIDStatement = sysIDStmt,
1107            insertStatement = insertStmt, deleteStatement = deleteStmt, selectRowIDsStmt = selectRowIDsStmt, setRowIDStmt = setRowIDStmt,
1108            previousStatement = prevStmt, nextStatement = nextStmt, lastStatement = lastStmt, insertIDStatement = insertIDStmt };
1109    }
1110
1111    ~SQLiteTable()
1112    {
1113       delete name;
1114       delete specialStatement;
1115       delete indexFields;
1116       _fields.Free();
1117    }
1118 }
1119
1120 class SQLiteRow : DriverRow
1121 {
1122    SQLiteTable tbl;
1123    sqlite3_stmt * curStatement;
1124
1125    sqlite3_stmt * defaultStatement;
1126    sqlite3_stmt * findStatement;
1127    sqlite3_stmt * prevFindStatement, * lastFindStatement;
1128    sqlite3_stmt * nextFindStatement;
1129    sqlite3_stmt * sysIDStatement;
1130    sqlite3_stmt * queryStatement;
1131    sqlite3_stmt * selectRowIDsStmt;
1132    sqlite3_stmt * setRowIDStmt;
1133    sqlite3_stmt * lastStatement;
1134    sqlite3_stmt * previousStatement;
1135    sqlite3_stmt * nextStatement;
1136
1137    sqlite3_stmt * insertStatement;
1138    sqlite3_stmt * deleteStatement;
1139    sqlite3_stmt * updateStatement;
1140    sqlite3_stmt * insertIDStatement;
1141    bool done;
1142    done = true;
1143    int64 rowID;
1144    // Because we use GoToSysID() and the sysIDStatement when searching for a primary key with Find(),
1145    // this flag is used to distinguish between a Find() and a GoToSysID() for Select(next) purposes:
1146    bool findSysID;
1147    int findBindId;
1148
1149    bool Nil()
1150    {
1151       return done;
1152    }
1153
1154    ~SQLiteRow()
1155    {
1156       if(defaultStatement) sqlite3_finalize(defaultStatement);
1157       if(findStatement)    sqlite3_finalize(findStatement);
1158       if(prevFindStatement)sqlite3_finalize(prevFindStatement);
1159       if(lastFindStatement)sqlite3_finalize(lastFindStatement);
1160       if(nextFindStatement)sqlite3_finalize(nextFindStatement);
1161       if(sysIDStatement)   sqlite3_finalize(sysIDStatement);
1162       if(insertStatement)  sqlite3_finalize(insertStatement);
1163       if(deleteStatement)  sqlite3_finalize(deleteStatement);
1164       if(updateStatement)  sqlite3_finalize(updateStatement);
1165       if(queryStatement)   sqlite3_finalize(queryStatement);
1166       if(selectRowIDsStmt) sqlite3_finalize(selectRowIDsStmt);
1167       if(setRowIDStmt)     sqlite3_finalize(setRowIDStmt);
1168       if(previousStatement)sqlite3_finalize(previousStatement);
1169       if(nextStatement)    sqlite3_finalize(nextStatement);
1170       if(lastStatement)    sqlite3_finalize(lastStatement);
1171       if(insertIDStatement)    sqlite3_finalize(insertIDStatement);
1172    }
1173
1174    bool Select(MoveOptions move)
1175    {
1176       int result;
1177       bool stepping = curStatement == previousStatement || curStatement == nextStatement || curStatement == lastStatement;
1178       if(!curStatement)
1179          curStatement = defaultStatement;
1180       switch(move)
1181       {
1182          case first:
1183          {
1184             sqlite3_reset(curStatement);
1185             result = sqlite3_step(curStatement);
1186             done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1187             if(done) { rowID = 0; sqlite3_reset(curStatement); return false; }
1188             rowID = sqlite3_column_int64(curStatement, 0);
1189             break;
1190          }
1191          case last:
1192          {
1193             sqlite3_reset(curStatement);
1194             curStatement = lastStatement;
1195             result = sqlite3_step(curStatement);
1196             done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1197             if(done) { rowID = 0; sqlite3_reset(curStatement); return false; }
1198             rowID = sqlite3_column_int64(curStatement, 0);
1199             break;
1200          }
1201          case middle:
1202             break;
1203          case next:
1204          case previous:
1205          {
1206             // For sysID statement, for a Find() we want to go through next/previous in order, otherwise we just go to nil
1207             if((move == next && curStatement != prevFindStatement && curStatement != lastFindStatement && !stepping && (curStatement != sysIDStatement || findSysID)) ||
1208                (move == previous && (curStatement == prevFindStatement || curStatement == lastFindStatement)))
1209             {
1210                result = sqlite3_step(curStatement);
1211                done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1212                if(done) { rowID = 0; sqlite3_reset(curStatement); return false; }
1213                rowID = sqlite3_column_int64(curStatement, 0);
1214             }
1215             else if(curStatement == prevFindStatement || curStatement == findStatement || curStatement == nextFindStatement || curStatement == lastFindStatement)
1216             {
1217                if(rowID)
1218                {
1219                   int bindId = findBindId;
1220                   sqlite3_reset((move == next) ? nextFindStatement : prevFindStatement);
1221                   BindCursorData((move == next) ? nextFindStatement : prevFindStatement, move,
1222                      (move == next && (!tbl.indexFields || (tbl.indexFieldsCount == 1 && tbl.indexFields[0].field == tbl.primaryKey && tbl.indexFields[0].order == ascending))) ? false : true, &bindId);
1223                   sqlite3_reset(curStatement);
1224                   curStatement = (move == next) ? nextFindStatement : prevFindStatement;
1225                   result = sqlite3_step(curStatement);
1226                   done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1227                   if(done) { rowID = 0; sqlite3_reset(curStatement); return false; }
1228                   rowID = sqlite3_column_int64(curStatement, 0);
1229                }
1230                else
1231                {
1232                   int bindId = findBindId;
1233                   sqlite3_reset((move == next) ? findStatement : lastFindStatement);
1234                   sqlite3_reset(curStatement);
1235                   curStatement = (move == next) ? findStatement : lastFindStatement;
1236                   result = sqlite3_step(curStatement);
1237                   done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1238                   if(done) { rowID = 0; sqlite3_reset(curStatement); return false; }
1239                   rowID = sqlite3_column_int64(curStatement, 0);
1240                }
1241             }
1242             else
1243             {
1244                sqlite3_reset(curStatement);
1245                curStatement = (move == previous) ? (rowID ? previousStatement : lastStatement) : (rowID ? nextStatement : defaultStatement);
1246                sqlite3_bind_int64(curStatement, 1, (sqlite3_int64)rowID);
1247                result = sqlite3_step(curStatement);
1248                done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1249                if(done) { rowID = 0; sqlite3_reset(curStatement); return false; }
1250                rowID = sqlite3_column_int64(curStatement, 0);
1251             }
1252             break;
1253          }
1254          case nil:
1255             sqlite3_reset(curStatement);
1256             rowID = 0;
1257             done = true;
1258             break;
1259          case here:
1260             break;
1261       }
1262       return true;
1263    }
1264
1265    bool Query(char * queryString)
1266    {
1267       bool status = true;
1268       int result;
1269
1270       if(curStatement)
1271          sqlite3_reset(curStatement);
1272       if(queryStatement)
1273       {
1274          sqlite3_finalize(queryStatement);
1275          queryStatement = null;
1276       }
1277
1278       if(queryString)
1279       {
1280          result = sqlite3_prepare_v2(tbl.db.db, queryString, -1, &queryStatement, null);
1281          if(!result)
1282          {
1283             curStatement = queryStatement;
1284             if(!strchr(queryString, '?'))
1285             {
1286                result = sqlite3_step(queryStatement);
1287
1288                done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1289                if(done) { rowID = 0; sqlite3_reset(queryStatement); return false; }
1290
1291                rowID = sqlite3_column_int64(queryStatement, 0);
1292             }
1293          }
1294          else
1295             status = false;
1296       }
1297       else
1298          curStatement = null;
1299       return status;
1300    }
1301
1302    bool BindData(sqlite3_stmt * statement, int pos, SQLiteField fld, typed_object data, SerialBuffer * bufferOut)
1303    {
1304       int result = 1;
1305       Class dataType = fld.type;
1306       SerialBuffer buffer = null;
1307       switch(fld.sqliteType)
1308       {
1309          case SQLITE_INTEGER:
1310          {
1311             switch(dataType.typeSize)
1312             {
1313                case 8:
1314                   result = sqlite3_bind_int64(statement, pos, (sqlite3_int64)*(int64 *)data);
1315                   break;
1316                case 4:
1317                   result = sqlite3_bind_int(statement, pos, *(int *)data);
1318                   break;
1319                case 2:
1320                {
1321                   int value;
1322                   if((int)data < 0)
1323                      value = (int)*(short *)data;
1324                   else
1325                      value = (int)*(uint16 *)data;
1326                   result = sqlite3_bind_int(statement, pos, value);
1327                   break;
1328                }
1329                case 1:
1330                {
1331                   int value;
1332                   if((int)data < 0)
1333                      value = (int)*(char *)data;
1334                   else
1335                      value = (int)*(byte *)data;
1336                   result = sqlite3_bind_int(statement, pos, value);
1337                   break;
1338                }
1339             }
1340             break;
1341          }
1342          case SQLITE_FLOAT:
1343          {
1344             if(dataType.typeSize == 8)
1345                result = sqlite3_bind_double(statement, pos, *(double *)data);
1346             else
1347                result = sqlite3_bind_double(statement, pos, (double)*(float *)data);
1348             break;
1349          }
1350          case SQLITE_TEXT:
1351          {
1352             if((char *)data)
1353                result = sqlite3_bind_text(statement, pos, (char *)data, strlen((char *)data), SQLITE_TRANSIENT);
1354             else
1355                result = sqlite3_bind_null(statement, pos);
1356             break;
1357          }
1358          case SQLITE_BLOB:
1359          case SQLITE_NULL:
1360          {
1361             if((void *)data)
1362             {
1363                buffer = SerialBuffer { };
1364                ((void (*)(void *, void *, void *))(void *)dataType._vTbl[__ecereVMethodID_class_OnSerialize])(dataType, data, buffer);
1365                result = sqlite3_bind_text(statement, pos, buffer._buffer, buffer.count, SQLITE_TRANSIENT);
1366             }
1367             else
1368                result = sqlite3_bind_null(statement, pos);
1369             break;
1370          }
1371       }
1372       if(bufferOut)
1373          *bufferOut = buffer;
1374       else
1375          delete buffer;
1376       return !result;
1377    }
1378
1379    void AddCursorWhereClauses(char * command, MoveOptions move, bool useIndex)
1380    {
1381       if(move == next || move == previous)
1382       {
1383          // Where clauses for index
1384          if(useIndex)
1385          {
1386             int c;
1387             bool gotPrimaryKey = false;
1388
1389             strcatf(command, " AND (");
1390             for(c = ((move == next) ? 0 : tbl.indexFieldsCount-1); (move == next) ? c < tbl.indexFieldsCount : c >= 0; (move == next) ? c++ : c--)
1391             {
1392                char where[1024];
1393                FieldIndex * fIndex = &tbl.indexFields[c];
1394                where[0] = 0;
1395
1396                strcat(where, "`");
1397                strcat(where, fIndex->field.name);
1398                strcat(where, "` ");
1399                strcat(where, (fIndex->order == ((move == next) ? descending : ascending)) ? "<" : ">");
1400                strcat(where, " ? OR (");
1401                strcat(where, fIndex->field.name);
1402                if(fIndex->field == tbl.primaryKey)
1403                   gotPrimaryKey = true;
1404                strcat(where, " = ? AND (");
1405                strcat(command, where);
1406             }
1407             strcat(command, gotPrimaryKey ? "1)" : ((move == next) ? "ROWID > ?)" : "ROWID < ?)"));
1408             for(c = 0; c < tbl.indexFieldsCount; c++)
1409                strcat(command, "))");
1410          }
1411          else
1412             strcatf(command, (move == next) ? " AND ROWID > ?" : " AND ROWID < ?");
1413       }
1414    }
1415
1416    void BindCursorData(sqlite3_stmt * stmt, MoveOptions move, bool useIndex, int * bindId)
1417    {
1418       if(move == next || move == previous)
1419       {
1420          // The binds for the Extra ordering Where clauses
1421          if(useIndex)
1422          {
1423             int c;
1424             /* // Code to not rely on curStatement being set up
1425             SQLiteRow dataRow = (SQLiteRow)tbl.CreateRow();
1426             dataRow.GoToSysID((uint)rowID);
1427             */
1428             for(c = ((move == next) ? 0 : tbl.indexFieldsCount-1); (move == next) ? c < tbl.indexFieldsCount : c >= 0; (move == next) ? c++ : c--)
1429             {
1430                FieldIndex * fIndex = &tbl.indexFields[c];
1431                int64 data;
1432                SQLiteField fld = (SQLiteField)fIndex->field;
1433                Class type = fld.type;
1434                void * dataPtr;
1435                SerialBuffer buffer;
1436
1437                if(type.type == unitClass && !type.typeSize)
1438                {
1439                   Class dataType = eSystem_FindClass(type.module, type.dataTypeString);
1440                   if(dataType)
1441                      type = dataType;
1442                }
1443                if(type.type == structClass)
1444                {
1445                   data = (int64)new0 byte[type.structSize];
1446                   dataPtr = (void *) data;
1447                }
1448                // ((bool (*)())(void *)dataRow.GetData)(dataRow, fld, type, (type.type == structClass) ? (void *)data : &data);
1449                ((bool (*)())(void *)this.GetData)(this, fld, type, (type.type == structClass) ? (void *)data : &data);
1450                if(type.type == normalClass || type.type == noHeadClass)
1451                   dataPtr = (void *) data;
1452                else
1453                   dataPtr = &data;
1454                ((bool (*)())(void *)this.BindData)(this, stmt, (*bindId)++, fld, type, dataPtr, &buffer);
1455                // NOTE: The data is bound twice, for there are 2x '?' in the query from AddCursorWhereClauses
1456                // Reuse the buffer for Blobs...
1457                if(fld.sqliteType == SQLITE_BLOB || fld.sqliteType == SQLITE_NULL)
1458                {
1459                   sqlite3_bind_text(stmt, (*bindId)++, buffer._buffer, buffer.count, SQLITE_TRANSIENT);
1460                   delete buffer;
1461                }
1462                else
1463                   ((bool (*)())(void *)this.BindData)(this, stmt, (*bindId)++, fld, type, dataPtr, null);
1464
1465                ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, dataPtr);
1466             }
1467             // delete dataRow;
1468          }
1469
1470          // Bind for the rowid
1471          sqlite3_bind_int64(stmt, (*bindId)++, (sqlite3_int64)rowID);
1472       }
1473    }
1474
1475    bool Find(Field fld, MoveOptions move, MatchOptions match, typed_object data)
1476    {
1477       char order[1024], command[2048];
1478       int result;
1479       bool useIndex;
1480       sqlite3_stmt * stmt = null;
1481       int bindId = 1;
1482
1483       if(fld == tbl.primaryKey)
1484       {
1485          if(curStatement) { sqlite3_reset(curStatement); curStatement = null; }
1486          if(findStatement) { sqlite3_finalize(findStatement); findStatement = null; }
1487          if(nextFindStatement) { sqlite3_finalize(nextFindStatement); nextFindStatement = null; }
1488          if(prevFindStatement) { sqlite3_finalize(prevFindStatement); prevFindStatement = null; }
1489          if(lastFindStatement) { sqlite3_finalize(lastFindStatement); lastFindStatement = null; }
1490          result = GoToSysID(*(int *)data);
1491          if(result)
1492             findSysID = true;
1493          return result;
1494       }
1495
1496       useIndex = tbl.GetIndexOrder(order, false);
1497       // Basic Find
1498       sprintf(command, "SELECT ROWID, * FROM `%s` WHERE `%s` = ?", tbl.name, fld.name);
1499       AddCursorWhereClauses(command, move, useIndex);
1500       strcat(command, order);
1501       strcat(command, ";");
1502       result = sqlite3_prepare_v2(tbl.db.db, command, -1, &stmt, null);
1503       BindData(stmt, bindId++, (SQLiteField)fld, data, null);
1504       BindCursorData(stmt, move, useIndex, &bindId);
1505
1506       // Currently, we can't reset curStatement until after BindCursorData, as current data is read from it
1507       if(curStatement) { sqlite3_reset(curStatement); curStatement = null; }
1508       if(findStatement) { sqlite3_finalize(findStatement); findStatement = null; }
1509       if(nextFindStatement) { sqlite3_finalize(nextFindStatement); nextFindStatement = null; }
1510       if(prevFindStatement) { sqlite3_finalize(prevFindStatement); prevFindStatement = null; }
1511       if(lastFindStatement) { sqlite3_finalize(lastFindStatement); lastFindStatement = null; }
1512
1513       curStatement = findStatement = stmt;
1514       findBindId = bindId;
1515
1516       // For going back to forward find
1517       bindId = 1;
1518       sprintf(command, "SELECT ROWID, * FROM `%s` WHERE `%s` = ?", tbl.name, fld.name);
1519       AddCursorWhereClauses(command, next, useIndex);
1520       strcat(command, order);
1521       strcat(command, ";");
1522       result = sqlite3_prepare_v2(tbl.db.db, command, -1, &stmt, null);
1523       BindData(stmt, bindId++, (SQLiteField)fld, data, null);
1524       nextFindStatement = stmt;
1525
1526       // Backwards
1527       tbl.GetIndexOrder(order, true);
1528       // For tracing back finds
1529       bindId = 1;
1530       sprintf(command, "SELECT ROWID, * FROM `%s` WHERE `%s` = ?", tbl.name, fld.name);
1531       AddCursorWhereClauses(command, previous, true);
1532       strcat(command, order);
1533       strcat(command, ";");
1534       result = sqlite3_prepare_v2(tbl.db.db, command, -1, &stmt, null);
1535       BindData(stmt, bindId++, (SQLiteField)fld, data, null);
1536       prevFindStatement = stmt;
1537
1538       // For tracing back from last
1539       bindId = 1;
1540       sprintf(command, "SELECT ROWID, * FROM `%s` WHERE `%s` = ?", tbl.name, fld.name);
1541       strcat(command, order);
1542       strcat(command, ";");
1543       result = sqlite3_prepare_v2(tbl.db.db, command, -1, &stmt, null);
1544       BindData(stmt, bindId++, (SQLiteField)fld, data, null);
1545       lastFindStatement = stmt;
1546
1547       result = sqlite3_step(findStatement);
1548
1549       done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1550       if(done)
1551       {
1552          rowID = 0;
1553          sqlite3_reset(findStatement);
1554       }
1555       else
1556          rowID = sqlite3_column_int64(findStatement, 0);
1557       return !done;
1558    }
1559
1560    bool FindMultiple(FieldFindData * findData, MoveOptions move, int numFields)
1561    {
1562 #define BINDDATA \
1563          for(c = 0; c < numFields; c++) \
1564          { \
1565             FieldFindData * fieldFind = &findData[c]; \
1566             SQLiteField sqlFld = (SQLiteField)findData->field; \
1567             Class dataType = sqlFld.type; \
1568             BindData(stmt, bindId++, sqlFld, (dataType.type == structClass || dataType.type == noHeadClass || dataType.type == normalClass) ? fieldFind->value.p : &fieldFind->value.i, null); \
1569          }
1570
1571       if(numFields)
1572       {
1573          char criterias[4096], command[4096], order[1024];
1574          int result;
1575          int c;
1576          bool useIndex;
1577          sqlite3_stmt * stmt = null;
1578          int bindId = 1;
1579
1580          // Criterias
1581          sprintf(criterias, "SELECT ROWID, * FROM `%s` WHERE `", tbl.name);
1582          for(c = 0; c < numFields; c++)
1583          {
1584             FieldFindData * fieldFind = &findData[c];
1585
1586             if(c) strcat(criterias, " AND `");
1587             strcat(criterias, fieldFind->field.name);
1588             strcat(criterias, "` = ?");
1589          }
1590
1591          useIndex = tbl.GetIndexOrder(order, false);
1592          // Basic Find (multiple)
1593          strcpy(command, criterias);
1594          AddCursorWhereClauses(command, move, useIndex);
1595          strcat(command, order);
1596          strcat(command, ";");
1597          result = sqlite3_prepare_v2(tbl.db.db, command, -1, &stmt, null);
1598          BINDDATA;
1599          BindCursorData(stmt, move, useIndex, &bindId);
1600
1601          // Currently, we can't reset curStatement until after BindCursorData, as current data is read from it
1602          if(curStatement) { sqlite3_reset(curStatement); curStatement = null; }
1603          if(findStatement) { sqlite3_finalize(findStatement); findStatement = null; }
1604          if(nextFindStatement) { sqlite3_finalize(nextFindStatement); nextFindStatement = null; }
1605          if(prevFindStatement) { sqlite3_finalize(prevFindStatement); prevFindStatement = null; }
1606          if(lastFindStatement) { sqlite3_finalize(lastFindStatement); lastFindStatement = null; }
1607
1608          curStatement = findStatement = stmt;
1609          findBindId = bindId;
1610
1611          // For tracing back forward finds
1612          bindId = 1;
1613          strcpy(command, criterias);
1614          AddCursorWhereClauses(command, previous, true);
1615          strcat(command, order);
1616          strcat(command, ";");
1617          result = sqlite3_prepare_v2(tbl.db.db, command, -1, &stmt, null);
1618          BINDDATA;
1619          nextFindStatement = stmt;
1620
1621          // Backwards
1622          tbl.GetIndexOrder(order, true);
1623          // For tracing back finds
1624          bindId = 1;
1625          strcpy(command, criterias);
1626          AddCursorWhereClauses(command, next, useIndex);
1627          strcat(command, order);
1628          strcat(command, ";");
1629          result = sqlite3_prepare_v2(tbl.db.db, command, -1, &stmt, null);
1630          BINDDATA;
1631          prevFindStatement = stmt;
1632
1633          // For tracing back from last
1634          bindId = 1;
1635          strcpy(command, criterias);
1636          strcat(command, order);
1637          strcat(command, ";");
1638          result = sqlite3_prepare_v2(tbl.db.db, command, -1, &stmt, null);
1639          BINDDATA;
1640          lastFindStatement = stmt;
1641
1642          result = sqlite3_step(findStatement);
1643          done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1644          if(done)
1645          {
1646             rowID = 0;
1647             sqlite3_reset(findStatement);
1648          }
1649          else
1650             rowID = sqlite3_column_int64(findStatement, 0);
1651          return !done;
1652       }
1653       return false;
1654    }
1655
1656    bool Synch(DriverRow to)
1657    {
1658       SQLiteRow rowTo = (SQLiteRow)to;
1659       if(tbl && rowTo.tbl && !strcmp(tbl.name, rowTo.tbl.name))
1660          return GoToSysID((uint)rowTo.rowID);
1661       return false;
1662    }
1663
1664    bool Add(uint64 id)
1665    {
1666       int result;
1667       //char command[1024];
1668       //sprintf(command, "INSERT INTO `%s` DEFAULT VALUES;", tbl.name);
1669       //result = sqlite3_exec(tbl.db.db, command, null, null, null);
1670       if(id)
1671       {
1672          sqlite3_bind_int64(insertIDStatement, 1, (sqlite3_int64)id);
1673          result = sqlite3_step(insertIDStatement);
1674       }
1675       else
1676          result = sqlite3_step(insertStatement);
1677       if(result == SQLITE_DONE)     // if(result == SQLITE_OK)
1678       {
1679          rowID = sqlite3_last_insert_rowid(tbl.db.db);
1680          if(rowID > MAXDWORD)
1681          {
1682             int64 lastID = tbl.lastID;
1683
1684             sqlite3_bind_int64(selectRowIDsStmt, 1, (sqlite3_int64)lastID);
1685             while(true)
1686             {
1687                int64 id;
1688                result = sqlite3_step(selectRowIDsStmt);
1689                if(result == SQLITE_DONE || result != SQLITE_ROW) break;
1690                id = sqlite3_column_int64(selectRowIDsStmt, 0);
1691                if(id - lastID > 1) break;
1692                lastID = id;
1693             }
1694             sqlite3_reset(selectRowIDsStmt);
1695
1696             sqlite3_bind_int64(setRowIDStmt, 2, (sqlite3_int64)rowID);
1697             rowID = lastID + 1;
1698             tbl.lastID = rowID;
1699             sqlite3_bind_int64(setRowIDStmt, 1, (sqlite3_int64)rowID);
1700             result = sqlite3_step(setRowIDStmt);
1701             sqlite3_reset(setRowIDStmt);
1702          }
1703          sqlite3_reset(id ? insertIDStatement : insertStatement);
1704          curStatement = sysIDStatement;
1705          findSysID = false;
1706          sqlite3_reset(curStatement);
1707          sqlite3_bind_int64(sysIDStatement, 1, (sqlite3_int64)rowID);
1708          result = sqlite3_step(curStatement);
1709          done = false; // Make sure 'nil' is false
1710          return true;
1711       }
1712       sqlite3_reset(insertStatement);
1713       return false;
1714    }
1715
1716    bool Delete()
1717    {
1718       int result;
1719       //char command[1024];
1720       //sprintf(command, "DELETE FROM `%s` WHERE ROWID = %d;", tbl.name, rowID);
1721       //result = sqlite3_exec(tbl.db.db, command, null, null, null);
1722       sqlite3_bind_int64(deleteStatement, 1, (sqlite3_int64)rowID);
1723       result = sqlite3_step(deleteStatement);
1724       sqlite3_reset(deleteStatement);
1725       rowID = 0;
1726       return result == SQLITE_OK || result == SQLITE_DONE;
1727    }
1728
1729    bool GetData(Field fld, typed_object &data)
1730    {
1731       SQLiteField sqlFld = (SQLiteField)fld;
1732       int num = sqlFld.num + 1;
1733       Class dataType = sqlFld.type;
1734
1735
1736       switch(sqlFld.sqliteType)
1737       {
1738          case SQLITE_INTEGER:
1739          {
1740             switch(dataType.typeSize)
1741             {
1742                case 8:
1743                   if(fld == tbl.primaryKey)
1744                      *(int64 *)data = rowID;
1745                   else
1746                      *(int64 *)data = sqlite3_column_int64(curStatement, num);
1747                   break;
1748                case 4:
1749                   if(fld == tbl.primaryKey)
1750                      *(int *)data = (int)(uint)rowID;
1751                   else
1752                      *(int *)data = sqlite3_column_int(curStatement, num);
1753                   break;
1754                case 2:
1755                {
1756                   int value;
1757                   if(fld == tbl.primaryKey)
1758                      value = (int)(uint)rowID;
1759                   else
1760                      value = sqlite3_column_int(curStatement, num);
1761                   if(value < 0)
1762                      *(short *)data = (short)value;
1763                   else
1764                      *(uint16 *)data = (uint16)value;
1765                   break;
1766                }
1767                case 1:
1768                {
1769                   int value;
1770                   if(fld == tbl.primaryKey)
1771                      value = (int)(uint)rowID;
1772                   else
1773                      value = sqlite3_column_int(curStatement, num);
1774                   if(value < 0)
1775                      *(char *)data = (char)value;
1776                   else
1777                      *(byte *)data = (byte)value;
1778                   break;
1779                }
1780             }
1781             break;
1782          }
1783          case SQLITE_FLOAT:
1784          {
1785             double d = sqlite3_column_double(curStatement, num);
1786             if(dataType.typeSize == 8)
1787                *(double *)data = d;
1788             else
1789                *(float *)data = (float)d;
1790             break;
1791          }
1792          case SQLITE_TEXT:
1793          {
1794             int numBytes = sqlite3_column_bytes(curStatement, num);
1795             char * text = sqlite3_column_text(curStatement, num);
1796             *(char **)data = text ? new byte[numBytes+1] : null;
1797             if(text)
1798                memcpy(*(char **)data, text, numBytes+1);
1799             break;
1800          }
1801          case SQLITE_BLOB:
1802          {
1803             SerialBuffer buffer { };
1804             //buffer._buffer = sqlite3_column_blob(curStatement, num);
1805             buffer._size = sqlite3_column_bytes(curStatement, num);
1806             buffer._buffer = sqlite3_column_text(curStatement, num);
1807             buffer.count = buffer._size;
1808
1809             ((void (*)(void *, void *, void *))(void *)dataType._vTbl[__ecereVMethodID_class_OnUnserialize])(dataType, data, buffer);
1810
1811             buffer._buffer = null;
1812             delete buffer;
1813             break;
1814          }
1815       }
1816       return true;
1817    }
1818
1819    bool SetData(Field fld, typed_object data)
1820    {
1821       SQLiteField sqlFld = (SQLiteField)fld;
1822       int result;
1823       int num = sqlFld.num + 1;
1824       char command[1024];
1825
1826       if(updateStatement)
1827          sqlite3_finalize(updateStatement);
1828       sprintf(command, "UPDATE `%s` SET `%s` = ? WHERE ROWID = ?;", tbl.name, sqlFld.name);
1829       result = sqlite3_prepare_v2(tbl.db.db, command, -1, &updateStatement, null);
1830       sqlite3_bind_int64(updateStatement, 2, (sqlite3_int64)rowID);
1831       BindData(updateStatement, 1, (SQLiteField)fld, data, null);
1832       result = sqlite3_step(updateStatement);
1833       sqlite3_reset(updateStatement);
1834       if(fld == tbl.primaryKey)
1835          rowID = *(uint *)data;
1836       return result == SQLITE_DONE;
1837    }
1838
1839    int GetSysID()
1840    {
1841       return (int)(uint)rowID;
1842    }
1843
1844    bool GoToSysID(uint id)
1845    {
1846       //char command[1024];
1847       int result;
1848       rowID = (uint)id;
1849       //if(statement)
1850          //sqlite3_finalize(statement);
1851       //sprintf(command, "SELECT ROWID, * FROM `%s` WHERE ROWID = ?;", tbl.name);
1852       //result = sqlite3_prepare_v2(tbl.db.db, command, -1, &statement, null);
1853
1854       findSysID = false;
1855       if(curStatement)
1856          sqlite3_reset(curStatement);
1857
1858       curStatement = sysIDStatement;
1859       sqlite3_reset(sysIDStatement);
1860       sqlite3_bind_int64(curStatement, 1, (sqlite_int64)rowID);
1861       result = sqlite3_step(curStatement);
1862       done = result == SQLITE_DONE || (result && result != SQLITE_ROW);
1863       if(done) { rowID = 0; sqlite3_reset(curStatement); return false; }
1864       return !done;
1865    }
1866
1867    bool SetQueryParam(int paramID, int value)
1868    {
1869       int result;
1870       if(curStatement != queryStatement)
1871       {
1872          if(curStatement) sqlite3_reset(curStatement);
1873          curStatement = queryStatement;
1874       }
1875       sqlite3_reset(queryStatement);
1876       result = sqlite3_bind_int(queryStatement, paramID, value);
1877       return !result;
1878    }
1879
1880    bool SetQueryParam64(int paramID, int64 value)
1881    {
1882       int result;
1883       if(curStatement != queryStatement)
1884       {
1885          if(curStatement) sqlite3_reset(curStatement);
1886          curStatement = queryStatement;
1887       }
1888       sqlite3_reset(queryStatement);
1889       result = sqlite3_bind_int64(queryStatement, paramID, (sqlite_int64)value);
1890       return !result;
1891    }
1892
1893    bool SetQueryParamText(int paramID, char * data)
1894    {
1895       int result;
1896       if(curStatement != queryStatement)
1897       {
1898          if(curStatement) sqlite3_reset(curStatement);
1899          curStatement = queryStatement;
1900       }
1901       sqlite3_reset(queryStatement);
1902       if(data)
1903          result = sqlite3_bind_text(queryStatement, paramID, (char *)data, strlen((char *)data), SQLITE_TRANSIENT);
1904       else
1905          result = sqlite3_bind_text(queryStatement, paramID, null, 0, SQLITE_TRANSIENT);
1906       return !result;
1907    }
1908
1909    bool SetQueryParamObject(int paramID, void * data, Class type)
1910    {
1911       int result;
1912       if(curStatement != queryStatement)
1913       {
1914          if(curStatement) sqlite3_reset(curStatement);
1915          curStatement = queryStatement;
1916       }
1917       sqlite3_reset(queryStatement);
1918       {
1919          SerialBuffer buffer { };
1920          ((void (*)(void *, void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnSerialize])(type, data, buffer);
1921          result = sqlite3_bind_text(queryStatement, paramID, buffer._buffer, buffer.count, SQLITE_TRANSIENT);
1922          delete buffer;
1923       }
1924       return !result;
1925    }
1926
1927    bool BindQueryData(int pos, SQLiteField fld, typed_object data)
1928    {
1929       if(curStatement != queryStatement)
1930       {
1931          if(curStatement) sqlite3_reset(curStatement);
1932          curStatement = queryStatement;
1933       }
1934       sqlite3_reset(queryStatement);
1935       return BindData(queryStatement, pos, fld, data, null);
1936    }
1937
1938    /*char * GetExtraColumn(int paramID)
1939    {
1940       SQLiteField lastFld = tbl._fields.last;
1941       return sqlite3_column_text(curStatement, lastFld.num + 1 + paramID);
1942    }*/
1943    char * GetColumn(int paramID)
1944    {
1945       return sqlite3_column_text(curStatement, paramID);
1946    }
1947 }