5
Improved class sqLite,copyright(c) 2002-2003 Marcin Krzetowski
8
simple class interface for SQLite. Hacked in by Ben Hochstrasser (bhoc@surfeu.ch)
9
Thanks to Roger Reghin (RReghin@scelectric.ca) for his idea to ValueList.
16
Classes,db,sysutils,Contnrs;
23
BookmarkFlag: TBookmarkFlag;
27
pBinBookMark = ^tBinBookMark;
34
TSQLiteExecCallback = function(Sender: TObject; Columns: Integer; ColumnValues: Pointer; ColumnNames: Pointer): integer of object; cdecl;
35
TSQLiteBusyCallback = function(Sender: TObject; ObjectName: PChar; BusyCount: integer): integer of object; cdecl;
36
TOnData = Procedure(Sender: TObject; Columns: Integer; ColumnNames, ColumnValues: String) of object;
37
TOnBusy = Procedure(Sender: TObject; ObjectName: String; BusyCount: integer; var Cancel: Boolean) of object;
38
TOnQueryComplete = Procedure(Sender: TObject) of object;
42
tSqliteField = class(tObject)
46
fFieldKind: tFieldKind;
47
fFieldType: tFieldType;
48
{ tIntegerType : Integer;
49
tLongIntegerType : int64;
50
tDateTimeType : tDateTime;}
51
// procedure SetName(const Value: string);
52
procedure SetFieldKind(const Value: tFieldKind);
53
procedure SetFieldType(const Value: tFieldType);
56
constructor create(aOwner : tObject);
57
destructor destroy; override;
58
procedure SetData(pt : pChar; NativeFormat : boolean);
59
function GetData(Buffer: Pointer; NativeFormat : Boolean) : boolean;
60
function GetData(Buffer: Pointer{=True}) : boolean;
61
// property FieldName : string read fName write SetName;
62
property FieldKind : tFieldKind read fFieldKind write SetFieldKind;
63
property FieldType : tFieldType read fFieldType write SetFieldType;
68
tSqliteRows = class (tObject)
70
function getItem(index: integer): tSqliteField;
71
procedure SetItem(index: integer; const Value: tSqliteField);
72
function checkIndex(index : integer) : boolean;
76
BookmarkFlag : tBookmarkFlag;
78
DataPointer : Pointer;
79
constructor Create(fieldCount : integer);
80
destructor destroy; override;
81
procedure Push(item : tSqliteField);
82
function Pop : tSqliteField;
83
property Items[index : integer] : tSqliteField read getItem write SetItem;
85
procedure ClearCalcFields;
86
function add(pt : Pchar; ptName : pCHar) : boolean;
90
fbuffercount : integer;
91
fBuffer : ^tSqliteField;
92
internalCount : integer;
93
procedure clearBuffer;
97
TSQLite = class(TDataSet)
99
maxLengthInit : boolean;
101
maxilcount : integer;
102
fDoExceptions : boolean;
113
fLstName: TStringList;
114
fLstVal: TStringList;
115
// fbuffer : tObjectList;
118
fOnQueryComplete: TOnQueryComplete;
119
fBusyTimeout: integer;
121
fChangeCount: integer;
124
fDataBaseName : string;
128
procedure SetBusyTimeout(Timeout: integer);
129
procedure SetDataBase(DBFileName: String);
130
procedure setTableName(const Value: string);
131
function getIsCancel: boolean;
132
procedure clearBuffer;
134
fCalcFieldsOfs,fRecordSize : integer;
135
fBookMarkOfs,fRecordBufferSize : integer;
136
fCurrentRecord : int64;
137
fRecordCount : int64;
138
fCursorOpen : boolean;
139
fFieldOffset : tList;
140
// procedure internalInsert; override;
141
function getActive: boolean;
142
// procedure setActive(Value: boolean); override;
143
function getRecNo : integer; override;
144
function getBookmarkFlag(Buffer : pChar) : tBookMarkFlag; override;
145
procedure InitBufferPointers;
146
procedure GetBookmarkData(Buffer : pChar; Data : Pointer); override;
147
procedure SetBookMarkData(Buffer : pChar; Data : Pointer); override;
148
procedure InternalGotoBookmark(ABookMark : Pointer) ; override;
149
function FieldDefsStored : boolean;
150
procedure ClearCalcFields(Buffer : pChar); override;
151
procedure OpenCursor(InfoQuery : Boolean); override;
152
function getRecordCount : integer; override;
153
procedure SetRecNo (value : integer); override;
154
function getRecord(Buffer : pChar; GetMode : tGetMode; DoCheck : Boolean): tGetResult; override;
155
procedure InternalInitFieldDefs; override;
156
procedure InternalOpen; override;
157
procedure InternalClose; override;
158
procedure InternalAddRecord(Buffer : Pointer; DoAppend : boolean); override;
159
procedure InternalDelete; override;
160
procedure InternalFirst; override;
161
procedure InternalHandleException; override;
162
procedure InternalInitRecord(Buffer : pChar); override;
163
procedure InternalLast;override;
164
procedure InternalPost;override;
165
procedure InternalSetToRecord (Buffer : pChar); override;
166
function isCursorOpen : Boolean; override;
167
procedure SetBookmarkFlag(Buffer : pChar; value : tBookmarkFlag); override;
168
procedure SetFieldData(Field : tField; Buffer : Pointer); override;
169
function allocRecordBuffer : pChar; override;
170
procedure FreeRecordBuffer(var Buffer : pChar); override;
171
function getRecordSize : Word; override;
172
function getCanModify : boolean; override;
174
fbuffer : tObjectList; //po zakonczeniu debuggowania usunac
175
constructor create(Aowner : tComponent); override;
176
destructor Destroy; override;
177
function getFieldData(Field : tField; Buffer : Pointer) : boolean; override;
178
function Query(ASql: String{table= nil}) : Boolean;
179
Function Query(ASQL: String; Table: TStrings): boolean;
180
function ExecSQL : boolean;
181
function ErrorMessage(ErrNo: Integer): string;
182
function IsComplete(ASql: String): boolean;
183
function LastInsertRow: integer;
184
procedure Cancel; override;
185
function DatabaseDetails(Table: TStrings): boolean;
186
function CreateTable : boolean;
187
procedure countMaxiLength(pt: pChar;index : int64);
188
procedure InitMaxLength(length : integer);
190
property LastErrorMessage: string read fMsg;
191
property LastError: Integer read fError;
192
property Version: String read fVersion;
193
property Encoding: String read fEncoding;
194
property OnData: TOnData read fOnData write fOnData;
195
property OnBusy: TOnBusy read fOnBusy write fOnBusy;
196
property OnQueryComplete: TOnQueryComplete read fOnQueryComplete write fOnQueryComplete;
197
property BusyTimeout: Integer read fBusyTimeout write SetBusyTimeout;
198
property ChangeCount: Integer read fChangeCount;
199
property SQL : tStringlist read fSQL write fSQL;
200
// property Fields : tstringlist read fFields;
201
property DataBase : string read fDataBase write SetDataBase;
202
property TableName : string read fTableName write setTableName;
203
property Active : boolean read getActive write setActive;
204
property isCancel : boolean read getIsCancel;
205
property DoExceptions : boolean read fDoExceptions write fDoExceptions stored true default true;
207
function Pas2SQLStr(const PasString: string): string;
208
function SQL2PasStr(const SQLString: string): string;
209
function QuoteStr(const s: string; QuoteChar: Char): string;
210
function UnQuoteStr(const s: string; QuoteChar: Char): string;
211
function QuoteStr(const s: string{; QuoteChar: Char = #39}): string;
212
function UnQuoteStr(const s: string{; QuoteChar: Char = #39}): string;
213
procedure ValueList(const ColumnNames, ColumnValues: String; NameValuePairs: TStrings);
225
function GetProcAddress(S : String) : Pointer;
232
SQLITE_OK = 0; // Successful result
233
SQLITE_ERROR = 1; // SQL error or missing database
234
SQLITE_INTERNAL = 2; // An internal logic error in SQLite
235
SQLITE_PERM = 3; // Access permission denied
236
SQLITE_ABORT = 4; // Callback routine requested an abort
237
SQLITE_BUSY = 5; // The database file is locked
238
SQLITE_LOCKED = 6; // A table in the database is locked
239
SQLITE_NOMEM = 7; // A malloc() failed
240
SQLITE_READONLY = 8; // Attempt to write a readonly database
241
SQLITE_INTERRUPT = 9; // Operation terminated by sqlite_interrupt()
242
SQLITE_IOERR = 10; // Some kind of disk I/O error occurred
243
SQLITE_CORRUPT = 11; // The database disk image is malformed
244
SQLITE_NOTFOUND = 12; // (Internal Only) Table or record not found
245
SQLITE_FULL = 13; // Insertion failed because database is full
246
SQLITE_CANTOPEN = 14; // Unable to open the database file
247
SQLITE_PROTOCOL = 15; // Database lock protocol error
248
SQLITE_EMPTY = 16; // (Internal Only) Database table is empty
249
SQLITE_SCHEMA = 17; // The database schema changed
250
SQLITE_TOOBIG = 18; // Too much data for one row of a table
251
SQLITE_CONSTRAINT = 19; // Abort due to contraint violation
252
SQLITE_MISMATCH = 20; // Data type mismatch
253
SQLITEDLL: PChar = 'sqlite.dll';
254
DblQuote: Char = '"';
255
SngQuote: Char = #39;
256
Crlf: String = #13#10;
258
_DO_EXCEPTIONS = 1; //Handle or not exceptions in dataset
262
SQLite_Open: function(dbname: PChar; mode: Integer; var ErrMsg: PChar): Pointer; cdecl;
263
SQLite_Close: procedure(db: Pointer); cdecl;
264
SQLite_Exec: function(db: Pointer; SQLStatement: PChar; CallbackPtr: Pointer; Sender: TObject; var ErrMsg: PChar): integer; cdecl;
265
SQLite_Version: function(): PChar; cdecl;
266
SQLite_Encoding: function(): PChar; cdecl;
267
SQLite_ErrorString: function(ErrNo: Integer): PChar; cdecl;
268
SQLite_GetTable: function(db: Pointer; SQLStatement: PChar; var ResultPtr: Pointer; var RowCount: Cardinal; var ColCount: Cardinal; var ErrMsg: PChar): integer; cdecl;
269
SQLite_FreeTable: procedure(Table: PChar); cdecl;
270
SQLite_FreeMem: procedure(P: PChar); cdecl;
271
SQLite_Complete: function(P: PChar): boolean; cdecl;
272
SQLite_LastInsertRow: function(db: Pointer): integer; cdecl;
273
SQLite_Cancel: procedure(db: Pointer); cdecl;
274
SQLite_BusyHandler: procedure(db: Pointer; CallbackPtr: Pointer; Sender: TObject); cdecl;
275
SQLite_BusyTimeout: procedure(db: Pointer; TimeOut: integer); cdecl;
276
SQLite_Changes: function(db: Pointer): integer; cdecl;
284
function QuoteStr(const s: string): string;
286
Result := QuoteStr(S,#39);
289
function QuoteStr(const s: string; QuoteChar: Char): string;
291
Result := Concat(QuoteChar, s, QuoteChar);
294
function UnQuoteStr(const s: string): string;
296
Result := UnQuoteStr(s,#39);
299
function UnQuoteStr(const s: string; QuoteChar: Char): string;
302
if length(Result) > 1 then
304
if Result[1] = QuoteChar then
305
Delete(Result, 1, 1);
306
if Result[Length(Result)] = QuoteChar then
307
Delete(Result, Length(Result), 1);
311
function Pas2SQLStr(const PasString: string): string;
315
Result := SQL2PasStr(PasString);
319
if Result[n] = SngQuote then
320
Insert(SngQuote, Result, n);
323
Result := QuoteStr(Result);
326
function SQL2PasStr(const SQLString: string): string;
328
DblSngQuote: String = #39#39;
333
p := pos(DblSngQuote, Result);
336
Delete(Result, p, 1);
337
p := pos(DblSngQuote, Result);
339
Result := UnQuoteStr(Result);
342
procedure ValueList(const ColumnNames, ColumnValues: String; NameValuePairs: TStrings);
345
lstName, lstValue: TStringList;
347
if NameValuePairs <> nil then
349
lstName := TStringList.Create;
350
lstValue := TStringList.Create;
351
lstName.CommaText := ColumnNames;
352
lstValue.CommaText := ColumnValues;
353
NameValuePairs.Clear;
354
if lstName.Count = LstValue.Count then
355
if lstName.Count > 0 then
356
for n := 0 to lstName.Count - 1 do
357
NameValuePairs.Append(Concat(lstName.Strings[n], '=', lstValue.Strings[n]));
364
function LoadLibs: Boolean;
367
DLLHandle := LoadLibrary(SQLITEDLL);
368
if DLLHandle <> 0 then
370
@SQLite_Open := GetProcAddress(DLLHandle, 'sqlite_open');
371
if not Assigned(@SQLite_Open) then exit;
372
@SQLite_Close := GetProcAddress(DLLHandle, 'sqlite_close');
373
if not Assigned(@SQLite_Close) then exit;
374
@SQLite_Exec := GetProcAddress(DLLHandle, 'sqlite_exec');
375
if not Assigned(@SQLite_Exec) then exit;
376
@SQLite_Version := GetProcAddress(DLLHandle, 'sqlite_libversion');
377
if not Assigned(@SQLite_Version) then exit;
378
@SQLite_Encoding := GetProcAddress(DLLHandle, 'sqlite_libencoding');
379
if not Assigned(@SQLite_Encoding) then exit;
380
@SQLite_ErrorString := GetProcAddress(DLLHandle, 'sqlite_error_string');
381
if not Assigned(@SQLite_ErrorString) then exit;
382
@SQLite_GetTable := GetProcAddress(DLLHandle, 'sqlite_get_table');
383
if not Assigned(@SQLite_GetTable) then exit;
384
@SQLite_FreeTable := GetProcAddress(DLLHandle, 'sqlite_free_table');
385
if not Assigned(@SQLite_FreeTable) then exit;
386
@SQLite_FreeMem := GetProcAddress(DLLHandle, 'sqlite_freemem');
387
if not Assigned(@SQLite_FreeMem) then exit;
388
@SQLite_Complete := GetProcAddress(DLLHandle, 'sqlite_complete');
389
if not Assigned(@SQLite_Complete) then exit;
390
@SQLite_LastInsertRow := GetProcAddress(DLLHandle, 'sqlite_last_insert_rowid');
391
if not Assigned(@SQLite_LastInsertRow) then exit;
392
@SQLite_Cancel := GetProcAddress(DLLHandle, 'sqlite_interrupt');
393
if not Assigned(@SQLite_Cancel) then exit;
394
@SQLite_BusyTimeout := GetProcAddress(DLLHandle, 'sqlite_busy_timeout');
395
if not Assigned(@SQLite_BusyTimeout) then exit;
396
@SQLite_BusyHandler := GetProcAddress(DLLHandle, 'sqlite_busy_handler');
397
if not Assigned(@SQLite_BusyHandler) then exit;
398
@SQLite_Changes := GetProcAddress(DLLHandle, 'sqlite_changes');
399
if not Assigned(@SQLite_Changes) then exit;
405
function SystemErrorMsg(ErrNo: Integer): String;
416
ErrNo := GetLastError;
417
MsgLen := FormatMessage(FORMAT_MESSAGE_FROM_SYSTEM, nil, ErrNo, 0, buf, size, nil);
424
Result := ('SystemErrorMsg Not Implemented');
427
function SystemErrorMsg: String;
433
function BusyCallback(Sender: Pointer; ObjectName: PChar; BusyCount: integer): integer; cdecl;
439
with TSQLite(Sender) do
441
if Assigned(fOnBusy) then
444
sObjName := ObjectName;
445
fOnBusy(Tsqlite(Sender), sObjName, BusyCount, bCancel);
452
function ExecCallback(Sender: TObject; Columns: Integer; ColumnValues: Pointer; ColumnNames: Pointer): integer; cdecl;
459
with Sender as TSQLite do
461
if (Assigned(fOnData) or Assigned(fTable)) then
467
PName := ColumnNames;
468
PVal := ColumnValues;
469
for n := 0 to Columns - 1 do
471
fLstName.Append(PName^);
472
fLstVal.Append(PVal^);
473
if Assigned(fTable) then
475
fTable.Append(PVal^);
481
sVal := fLstVal.CommaText;
482
sName := fLstName.CommaText;
483
if Assigned(fOnData) then
484
fOnData(Sender, Columns, sName, sVal);
491
function ExecCallback2(Sender: TObject; Columns: Integer; ColumnValues: Pointer; ColumnNames: Pointer): integer; cdecl;
502
with Sender as TSQLite do begin
503
if (Assigned(fOnData) or assigned(fBuffer)) then begin
506
if Columns > 0 then begin
507
PName := ColumnNames;
508
PVal := ColumnValues;
509
fBuffer.Add(tSqliteRows.Create(Columns));
510
temp:=fBuffer.count-1;
511
initMaxLength(columns);
512
for n := 0 to Columns - 1 do begin
513
fLstName.Append(PName^);
514
if Assigned(fBuffer) then begin
515
p:=fBuffer.Items[temp];
517
if t=nil then continue;
521
countMaxiLength(PVAL^,n);
525
// at last we add the bookmark info
528
if Assigned(fOnData) then begin
529
sVal := fLstVal.CommaText;
530
sName := fLstName.CommaText;
531
fOnData(Sender, Columns, sName, sVal);
539
procedure TSQLite.SetDataBase(DBFileName: String);
544
fError := SQLITE_ERROR;
548
fOnQueryComplete := nil;
554
fSQLite := SQLite_Open(PChar(DBFileName), 1, @afPMsg);
555
SQLite_FreeMem(afPMsg);
556
if fSQLite <> nil then
559
fVersion := strpas(SQLite_Version);
560
fEncoding := strpas(SQLite_Encoding);
568
fMsg := ErrorMessage(fError);
571
destructor TSQLite.Destroy;
574
if assigned(fSQl) then begin
579
SQLite_Close(fSQLite);
581
if assigned(fLstName) then begin
585
if assigned(fLstVal) then begin
592
fOnQueryComplete := nil;
595
if assigned(fBuffer) then begin
605
function TSQLite.Query(ASql: String): boolean;
608
Result:=Query(ASql,Nil);
611
function TSQLite.Query(ASql: String; Table: TStrings): boolean;
615
maxLengthInit:=false;
616
fError := SQLITE_ERROR;
622
if fTable <> nil then
624
fError := SQLite_Exec(fSQLite, PChar(ASql), @ExecCallback, Self, @fPMsg);
625
SQLite_FreeMem(fPMsg);
626
fChangeCount := SQLite_Changes(fSQLite);
629
if Assigned(fOnQueryComplete) then
630
fOnQueryComplete(Self);
632
fMsg := ErrorMessage(fError);
633
Result := not (fError <> SQLITE_OK);//function should return true, if execution of query ends ok..
634
if result and not active then
640
procedure TSQLite.SetBusyTimeout(Timeout: Integer);
642
fBusyTimeout := Timeout;
645
SQLite_Busy_Timeout(fSQLite, fBusyTimeout);
646
if fBusyTimeout > 0 then
647
SQLite_Busy_Handler(fSQLite, @BusyCallback, Self)
649
SQLite_Busy_Handler(fSQLite, nil, nil);
653
function TSQLite.LastInsertRow: integer;
656
Result := SQLite_Last_Insert_Rowid(fSQLite)
661
function TSQLite.ErrorMessage(ErrNo: Integer): string;
670
Result := SQLite_Error_String(ErrNo);
673
Raise exception.Create('Library "sqlite.dll" not found.');
677
function TSQLite.IsComplete(ASql: String): boolean;
679
Result := SQLite_Complete(PChar(ASql))=0;
682
function TSQLite.DatabaseDetails(Table: TStrings): boolean;
684
Result := Query('SELECT * FROM SQLITE_MASTER;', Table);
687
function TSQLite.ExecSQL: boolean;
692
maxLengthInit:=false;
693
fError := SQLITE_ERROR;
699
if fTable <> nil then
701
for i:=0 to fsql.Count-1 do begin
702
fError := SQLite_Exec(fSQLite, PChar(fSql[i]), @ExecCallback2, Self, @fPMsg);
703
SQLite_FreeMem(fPMsg);
705
fChangeCount := SQLite_Changes(fSQLite);
708
if Assigned(fOnQueryComplete) then
709
fOnQueryComplete(Self);
711
fMsg := ErrorMessage(fError);
712
Result :=not (fError <> SQLITE_OK);
713
if result and not active then
718
constructor TSQLite.Create(Aowner: tComponent);
720
inherited create(Aowner);
721
fLstName := TStringList.Create;
722
fLstVal := TStringList.Create;
724
fsql:=tStringList.Create;
726
fBuffer:=tObjectList.Create(true);
727
if length(fDataBase)>1 then
728
setDataBase(fDataBase);
731
procedure TSQLite.setTableName(const Value: string);
733
if (not active) and (length(value)>0) then begin
736
sql.add('select rowid,* from '+tableName+';');
740
function TSQLite.getActive: boolean;
746
procedure TSQLite.setActive(Value: boolean);
750
//switch for active=true;
758
inherited setActive(value);
762
function TSQLite.getRecNo: integer;
764
result:=self.fCurrentRecord;
767
procedure TSQLite.Cancel;
771
if fBusy and fIsOpen then
773
do_SQLite_interrupt(fSQLite);
780
function TSQLite.getIsCancel: boolean;
785
function TSQLite.getBookmarkFlag(Buffer: pChar): tBookMarkFlag;
787
result:= pRecInfo(Buffer)^.BookmarkFlag;
790
procedure TSQLite.InitBufferPointers;
792
fCalcFieldsOfs :=fRecordSize;
793
//fRecInfoOfs :=fCalcFieldsOfs + CalcFieldsSize;
794
//fBookMarkOfs := fRecInfoOfs+SizeOf(tRecInfo);
795
fRecordBufferSize :=fBookmarkOfs + BookmarkSize;
798
procedure TSQLite.GetBookmarkData(Buffer: pChar; Data: Pointer);
800
Move(Buffer[fBookMarkOfs],Data^,SizeOf(tBinBookMark));
801
//implementacja jest watpliwa
804
procedure TSQLite.SetBookMarkData(Buffer: pChar; Data: Pointer);
806
Move(Data^,Buffer[fBookMarkOfs],SizeOf(tbinBookMark));
810
procedure TSQLite.InternalGotoBookmark(ABookMark: Pointer);
812
with pBinBookMark(ABookMark)^ do begin
813
fCurrentRecord :=RecPtr;
817
function TSQLite.FieldDefsStored: boolean;
822
procedure TSQLite.ClearCalcFields(Buffer: pChar);
837
function TSQLite.getRecordCount: integer;
839
result :=fRecordCount;
842
procedure TSQLite.OpenCursor(InfoQuery: Boolean);
848
procedure TSQLite.SetRecNo(value: integer);
854
function TSQLite.CreateTable: boolean;
859
function TSQLite.getRecord(Buffer: pChar; GetMode: tGetMode;
860
DoCheck: Boolean): tGetResult;
862
if fRecordCount<1 then
868
if fCurrentRecord>= (fRecordCount-1) then
873
if (fCurrentRecord <=0) then
878
if (fCurrentRecord >= fRecordCount) or (fCurrentRecord <0) then
882
if result=grOk then begin
883
self.fRecordBufferSize:=sizeOf(fBuffer[fCurrentRecord]);
884
self.fRecordSize:=self.fRecordBufferSize;
885
// Buffer:=fBuffer.List[fcurrentRecord];
886
//read data from psyh buffer sqlite..;)
887
GetCalcFields(Buffer);
888
{ with fBuffer.Items[fCurrentRecord] as tSqliteRows do begin
889
BookmarkFlag := bfCurrent;
891
with PRecInfo(Buffer)^ do
893
Index := fCurrentRecord;
894
BookmarkFlag := bfCurrent;
895
Bookmark := Integer (fCurrentRecord);
901
if result=grError then begin
902
if DoCheck and DoExceptions then
903
raise edataBaseError.Create('Invalid Record');
907
procedure TSQLite.InternalInitFieldDefs;
911
for i:=0 to fLstname.Count-1 do begin
912
FieldDefs.Add(fLstName[i],ftString,MaxiL[i],false);
916
procedure TSQLite.InternalOpen;
918
if fBUffer<>nil then begin
922
if (length(tableName)>0) and (fSQL.Count<1) then begin
923
fsql.add('select rowid,* from '+fTableName);
927
InternalInitFieldDefs;
929
if ((fLstName.count-1)>0) and (fBuffer<>nil) then
930
fRecordCount:=(fBuffer.Count-1) div (fLstName.Count-1)
934
if (fBuffer<>nil) then
935
fRecordCount:=(fBuffer.Count-1)
938
if DefaultFields then
942
FRecordSize := sizeof (TRecInfo);
943
FCurrentRecord := -1;
944
BookmarkSize := sizeOf (Integer);
947
procedure TSQLite.InternalClose;
952
function TSQLite.allocRecordBuffer: pChar;
955
//now is time to calculate currentRecordSize...
956
GetMem(Result,GetRecordSize);
957
FillChar(Result^,GetRecordSize,0);
960
procedure TSQLite.FreeRecordBuffer(var Buffer: pChar);
962
//FreeMem(Buffer,sizeOf(Buffer));
963
FreeMem(Buffer,GetRecordSize);
966
function TSQLite.getRecordSize: Word;
969
Result:=sizeof(TRecInfo);
973
procedure TSQLite.InternalAddRecord(Buffer: Pointer; DoAppend: boolean);
978
procedure TSQLite.InternalDelete;
983
procedure TSQLite.InternalFirst;
985
self.fCurrentRecord:=0;
988
procedure TSQLite.InternalHandleException;
991
if _DO_EXCEPTIONS=1 then
992
Application.HandleException(Self)
996
procedure TSQLite.InternalInitRecord(Buffer: pChar);
1001
procedure TSQLite.InternalLast;
1003
fCurrentRecord:=fRecordCount;
1006
procedure TSQLite.InternalPost;
1010
procedure TSQLite.InternalSetToRecord(Buffer: pChar);
1016
function TSQLite.isCursorOpen: Boolean;
1022
procedure TSQLite.SetFieldData(Field: tField; Buffer: Pointer);
1026
// aa:=Field.NewValue;
1031
procedure TSQLite.SetBookmarkFlag(Buffer: pChar; value: tBookmarkFlag);
1037
function TSQLite.getFieldData(Field: tField; Buffer: Pointer): boolean;
1045
k:=fieldDefs.Count-1;
1046
self.fLstName.Count;
1047
r:=fBuffer[PRecInfo(ActiveBuffer)^.Index] as tSqliteRows;
1049
for i:=0 to k do begin
1050
if lowercase(fLstName[i])=lowercase(field.FieldName) then begin
1052
if p = nil then break;
1053
p.GetData(Buffer,true);
1062
procedure tSqliteRows.Push(item: tSqliteField);
1064
if internalcount<fBuffercount then begin
1065
fBuffer[internalCount]:=item;
1070
constructor tSqliteRows.Create(fieldCount: integer);
1075
if fieldCount<=0 then
1077
fbuffercount:=fieldcount+1;
1078
getmem(fBuffer,fbuffercount*sizeof(pointer));
1081
destructor tSqliteRows.destroy;
1090
function tSqliteRows.Pop: tSqliteField;
1093
if (internalCount>0) and (internalCount<fBuffercount) then begin
1094
result:=fBuffer[internalCount];
1099
function tSqliteRows.getItem(index: integer): tSqliteField;
1102
if checkIndex(index) then
1103
result:=fBuffer[Index];
1106
procedure tSqliteRows.SetItem(index: integer; const Value: tSqliteField);
1108
if checkIndex(index) then
1109
fBuffer[index]:=Value;
1112
function tSqliteRows.checkIndex(index : integer): boolean;
1115
if (index>=0) and (index<internalCount) then
1119
procedure tSqliteRows.clearBuffer;
1122
if internalcount>0 then begin
1123
for i:=0 to internalCount -1 do begin
1124
if fBuffer[i]<>nil then begin
1139
procedure tSqliteRows.Clear;
1145
procedure tSqliteRows.ClearCalcFields;
1150
function tSqliteRows.Add(pt: pChar;ptName : pChar):boolean;
1153
Push(tSqliteField.Create(nil));
1154
tmp:=internalCount-1;
1155
items[tmp].FieldKind:=fkData;
1156
items[tmp].SetFieldType(ftString);
1157
items[tmp].SetData(pt,true);
1161
procedure tSqlite.countMaxiLength(pt: pChar; index : int64);
1163
if length(pt)>maxil[index] then
1164
maxiL[index]:=length(pt);
1169
constructor tSqliteField.create(aOwner: tObject);
1176
destructor tSqliteField.destroy;
1182
function tSqliteField.GetData(Buffer: Pointer) : boolean;
1185
Result:=GetData(Buffer,True);
1188
function tSqliteField.GetData(Buffer: Pointer;
1189
NativeFormat: Boolean): boolean;
1191
l,tIntegerType : integer;
1192
tDateTimeType : tDateTime;
1197
if not nativeFormat then begin
1198
Move(data,Buffer^,sizeOf(data));
1201
case self.fieldType of
1203
tIntegerType:=StrToInt(data);
1204
Move(tIntegerType,Buffer^,sizeOf(data));
1207
tDateTimeType:=StrToDate(data);
1208
Move(tDateTimeType,Buffer^,sizeOf(data));
1212
// Move(data,Buffer^,l);
1213
StrCopy (Buffer, pchar(data));
1216
Move(data,Buffer^,sizeOf(data));
1225
procedure tSqliteField.SetData(pt: pChar; NativeFormat: boolean);
1230
procedure tSqliteField.SetFieldKind(const Value: tFieldKind);
1232
fFieldKind := Value;
1235
procedure tSqliteField.SetFieldType(const Value: tFieldType);
1237
fFieldType := Value;
1240
procedure tSqliteField.SetName(const Value: string);
1245
function TSQLite.getCanModify: boolean;
1249
if length(fTableName)>0 then
1253
procedure TSQLite.InitMaxLength(length: integer);
1255
if not maxLengthInit and (length>0) then begin
1256
maxLengthInit:=true;
1258
getmem(maxiL,maxilcount*sizeof(integer));
1262
procedure TSQLite.clearBuffer;
1265
if assigned(fBuffer) then begin
1266
if fBuffer.count>0 then begin
1271
if assigned(fLstVal) then begin
1274
if assigned(fLstName) then begin
1280
procedure TSQLite.internalInsert;
1283
if not getCanModify then exit;
1289
RegisterComponents('MK', [tSqlite]);
1294
LibsLoaded := LoadLibs;
1297
MsgNoError := SystemErrorMsg(0);
1299
MsgNoError := 'The operation completed successfully';
1304
if DLLHandle <> 0 then
1305
FreeLibrary(DLLHandle);