sqlite3conn.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734
  1. {
  2. This file is part of the Free Pascal Classes Library (FCL).
  3. Copyright (c) 2006 by the Free Pascal development team
  4. SQLite3 connection for SQLDB
  5. See the File COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. {
  12. Based on an implementation by Martin Schreiber, part of MSEIDE.
  13. Reworked all code so it conforms to FCL coding standards.
  14. }
  15. unit sqlite3conn;
  16. {$mode objfpc}
  17. {$h+}
  18. interface
  19. uses
  20. classes, db, bufdataset, sqldb, sqlite3dyn, types;
  21. const
  22. sqliteerrormax = 99;
  23. type
  24. PDateTime = ^TDateTime;
  25. TSqliteOption = (sloTransactions,sloDesignTransactions);
  26. TSqliteOptions = set of TSqliteOption;
  27. TStringArray = Array of string;
  28. PStringArray = ^TStringArray;
  29. TArrayStringArray = Array of TStringArray;
  30. PArrayStringArray = ^TArrayStringArray;
  31. TSQLite3Connection = class(TSQLConnection)
  32. private
  33. fhandle: psqlite3;
  34. foptions: TSQLiteOptions;
  35. function blobscached: boolean;
  36. procedure setoptions(const avalue: tsqliteoptions);
  37. protected
  38. function stringquery(const asql: string): TStringArray;
  39. function stringsquery(const asql: string): TArrayStringArray;
  40. procedure checkerror(const aerror: integer);
  41. procedure DoInternalConnect; override;
  42. procedure DoInternalDisconnect; override;
  43. function GetHandle : pointer; override;
  44. Function AllocateCursorHandle : TSQLCursor; override;
  45. //aowner used as blob cache
  46. Procedure DeAllocateCursorHandle(var cursor : TSQLCursor); override;
  47. Function AllocateTransactionHandle : TSQLHandle; override;
  48. procedure PrepareStatement(cursor: TSQLCursor; ATransaction : TSQLTransaction;
  49. buf: string; AParams : TParams); override;
  50. procedure Execute(cursor: TSQLCursor;atransaction:tSQLtransaction; AParams : TParams); override;
  51. function Fetch(cursor : TSQLCursor) : boolean; override;
  52. procedure AddFieldDefs(cursor: TSQLCursor; FieldDefs : TfieldDefs); override;
  53. procedure UnPrepareStatement(cursor : TSQLCursor); override;
  54. procedure FreeFldBuffers(cursor : TSQLCursor); override;
  55. function LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean; override;
  56. //if bufsize < 0 -> buffer was to small, should be -bufsize
  57. function GetTransactionHandle(trans : TSQLHandle): pointer; override;
  58. function Commit(trans : TSQLHandle) : boolean; override;
  59. function RollBack(trans : TSQLHandle) : boolean; override;
  60. function StartdbTransaction(trans : TSQLHandle; aParams : string) : boolean; override;
  61. procedure CommitRetaining(trans : TSQLHandle); override;
  62. procedure RollBackRetaining(trans : TSQLHandle); override;
  63. procedure LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction); override;
  64. // New methods
  65. procedure execsql(const asql: string);
  66. procedure UpdateIndexDefs(var IndexDefs : TIndexDefs; const TableName : string); // Differs from SQLDB.
  67. function getprimarykeyfield(const atablename: string; const acursor: tsqlcursor): string;
  68. public
  69. function GetInsertID: int64;
  70. published
  71. property Options: TSqliteOptions read FOptions write SetOptions;
  72. end;
  73. implementation
  74. uses
  75. dbconst, sysutils, typinfo, dateutils;
  76. type
  77. TStorageType = (stNone,stInteger,stFloat,stText,stBlob,stNull);
  78. TSQLite3Cursor = class(tsqlcursor)
  79. private
  80. fhandle : psqlite3;
  81. fstatement: psqlite3_stmt;
  82. ftail: pchar;
  83. fstate: integer;
  84. fparambinding: array of Integer;
  85. procedure checkerror(const aerror: integer);
  86. procedure bindparams(AParams : TParams);
  87. Procedure Prepare(Buf : String; APArams : TParams);
  88. Procedure UnPrepare;
  89. Procedure Execute;
  90. Function Fetch : Boolean;
  91. end;
  92. procedure freebindstring(astring: pointer); cdecl;
  93. begin
  94. FreeMem(AString);
  95. end;
  96. procedure TSQLite3Cursor.checkerror(const aerror: integer);
  97. Var
  98. S : String;
  99. begin
  100. if (aerror<>sqlite_ok) then
  101. begin
  102. S:=strpas(sqlite3_errmsg(fhandle));
  103. DatabaseError(S);
  104. end;
  105. end;
  106. Procedure TSQLite3Cursor.bindparams(AParams : TParams);
  107. Function PCharStr(Const S : String) : PChar;
  108. begin
  109. Result:=StrAlloc(Length(S)+1);
  110. If (Result<>Nil) then
  111. StrPCopy(Result,S);
  112. end;
  113. Var
  114. I : Integer;
  115. P : TParam;
  116. pc : pchar;
  117. str1: string;
  118. cu1: currency;
  119. do1: double;
  120. parms : array of Integer;
  121. begin
  122. for I:=1 to high(fparambinding)+1 do
  123. begin
  124. P:=aparams[fparambinding[I-1]];
  125. if P.isnull then
  126. checkerror(sqlite3_bind_null(fstatement,I))
  127. else
  128. case P.datatype of
  129. ftinteger,
  130. ftboolean,
  131. ftsmallint: checkerror(sqlite3_bind_int(fstatement,I,p.asinteger));
  132. ftword: checkerror(sqlite3_bind_int(fstatement,I,P.asword));
  133. ftlargeint: checkerror(sqlite3_bind_int64(fstatement,I,P.aslargeint));
  134. ftbcd: begin
  135. cu1:= P.ascurrency;
  136. checkerror(sqlite3_bind_int64(fstatement,I,pint64(@cu1)^));
  137. end;
  138. ftfloat,
  139. ftcurrency,
  140. ftdatetime,
  141. ftdate,
  142. fttime: begin
  143. do1:= P.asfloat;
  144. checkerror(sqlite3_bind_double(fstatement,I,do1));
  145. end;
  146. ftstring: begin
  147. str1:= p.asstring;
  148. checkerror(sqlite3_bind_text(fstatement,I,pcharstr(str1), length(str1),@freebindstring));
  149. end;
  150. ftblob: begin
  151. str1:= P.asstring;
  152. checkerror(sqlite3_bind_blob(fstatement,I,pcharstr(str1), length(str1),@freebindstring));
  153. end;
  154. else
  155. databaseerror('Parameter type '+getenumname(typeinfo(tfieldtype),ord(P.datatype))+' not supported.');
  156. end; { Case }
  157. end;
  158. end;
  159. Procedure TSQLite3Cursor.Prepare(Buf : String; APArams : TParams);
  160. begin
  161. if assigned(aparams) and (aparams.count > 0) then
  162. buf := aparams.parsesql(buf,false,false,false,psinterbase,fparambinding);
  163. checkerror(sqlite3_prepare(fhandle,pchar(buf),length(buf),@fstatement,@ftail));
  164. end;
  165. Procedure TSQLite3Cursor.UnPrepare;
  166. begin
  167. sqlite3_finalize(fstatement); // No check.
  168. end;
  169. Procedure TSQLite3Cursor.Execute;
  170. var
  171. wo1: word;
  172. begin
  173. {$ifdef i386}
  174. wo1:= get8087cw;
  175. set8087cw(wo1 or $1f); //mask exceptions, Sqlite3 has overflow
  176. Try // Why do people always forget this ??
  177. {$endif}
  178. fstate:= sqlite3_step(fstatement);
  179. {$ifdef i386}
  180. finally
  181. set8087cw(wo1); //restore
  182. end;
  183. {$endif}
  184. if (fstate<=sqliteerrormax) then
  185. checkerror(sqlite3_reset(fstatement));
  186. if (fstate=sqlite_row) then
  187. fstate:= sqliteerrormax; //first row
  188. end;
  189. Function TSQLite3Cursor.Fetch : Boolean;
  190. begin
  191. if (fstate=sqliteerrormax) then
  192. fstate:=sqlite_row //first row;
  193. else if (fstate=sqlite_row) then
  194. begin
  195. fstate:=sqlite3_step(fstatement);
  196. if (fstate<=sqliteerrormax) then
  197. checkerror(sqlite3_reset(fstatement)); //right error returned??
  198. end;
  199. result:=(fstate=sqlite_row);
  200. end;
  201. { TSQLite3Connection }
  202. procedure TSQLite3Connection.LoadBlobIntoBuffer(FieldDef: TFieldDef;ABlobBuf: PBufBlobField; cursor: TSQLCursor; ATransaction : TSQLTransaction);
  203. var
  204. blobid: integer;
  205. int1,int2: integer;
  206. str1: string;
  207. bo1: boolean;
  208. begin
  209. {$WARNING TSQLite3Connection.LoadBlobIntoBuffer not implemented !}
  210. { if (mode = bmwrite) and (field.dataset is tmsesqlquery) then begin
  211. result:= tmsebufdataset(field.dataset).createblobbuffer(field);
  212. end
  213. else begin
  214. result:= nil;
  215. if mode = bmread then begin
  216. if field.getData(@blobId) then begin
  217. result:= acursor.getcachedblob(blobid);
  218. end;
  219. end;
  220. end;
  221. }
  222. end;
  223. function TSQLite3Connection.AllocateTransactionHandle: TSQLHandle;
  224. begin
  225. result:= tsqlhandle.create;
  226. end;
  227. function TSQLite3Connection.AllocateCursorHandle: TSQLCursor;
  228. Var
  229. Res : TSQLite3Cursor;
  230. begin
  231. Res:= TSQLite3Cursor.create;
  232. Res.fhandle:=self.fhandle;
  233. Result:=Res;
  234. end;
  235. procedure TSQLite3Connection.DeAllocateCursorHandle(var cursor: TSQLCursor);
  236. begin
  237. freeandnil(cursor);
  238. end;
  239. procedure TSQLite3Connection.PrepareStatement(cursor: TSQLCursor;
  240. ATransaction: TSQLTransaction; buf: string; AParams: TParams);
  241. begin
  242. TSQLite3Cursor(cursor).Prepare(Buf,AParams);
  243. end;
  244. procedure TSQLite3Connection.UnPrepareStatement(cursor: TSQLCursor);
  245. begin
  246. TSQLite3Cursor(cursor).UnPrepare;
  247. end;
  248. Type
  249. TFieldMap = Record
  250. N : String;
  251. T : TFieldType;
  252. S : Integer;
  253. end;
  254. Const
  255. FieldMapCount = 15;
  256. FieldMap : Array [1..FieldMapCount] of TFieldMap = (
  257. (n:'INT'; t: ftInteger; S: SizeOf(Integer)),
  258. (n:'LARGEINT'; t:ftlargeInt; S: SizeOf(Int64)),
  259. (n:'WORD'; t: ftWord; S: SizeOf(Word)),
  260. (n:'SMALLINT'; t: ftSmallint; S: SizeOf(SmallInt)),
  261. (n:'BOOLEAN'; t: ftBoolean; S: SizeOf(WordBool)),
  262. (n:'REAL'; t: ftFloat; S: SizeOf(Double)),
  263. (n:'DOUBLE'; t: ftFloat; S: SizeOf(Double)),
  264. (n:'DATETIME'; t: ftDateTime; S: SizeOf(TDateTime)), // MUST be before date
  265. (n:'DATE'; t: ftDate; S: SizeOf(TDateTime)),
  266. (n:'TIME'; t: ftTime; S: SizeOf(TDateTime)),
  267. (n:'CURRENCY'; t: ftCurrency; S: SizeOf(double)),
  268. (n:'VARCHAR'; t: ftString; S: 255),
  269. (n:'NUMERIC'; t: ftBCD; S: SizeOf(Currency)),
  270. (n:'TEXT'; t: ftmemo; S: 0),
  271. (n:'BLOB'; t: ftBlob; S: 0)
  272. { Template:
  273. (n:''; t: ft; S: SizeOf())
  274. }
  275. );
  276. procedure TSQLite3Connection.AddFieldDefs(cursor: TSQLCursor;
  277. FieldDefs: TfieldDefs);
  278. var
  279. i : integer;
  280. FN,FD : string;
  281. ft1 : tfieldtype;
  282. size1 : word;
  283. ar1 : TStringArray;
  284. fi : integer;
  285. st : psqlite3_stmt;
  286. begin
  287. st:=TSQLite3Cursor(cursor).fstatement;
  288. for i:= 0 to sqlite3_column_count(st) - 1 do
  289. begin
  290. FN:=sqlite3_column_name(st,i);
  291. FD:=uppercase(sqlite3_column_decltype(st,i));
  292. ft1:= ftUnknown;
  293. size1:= 0;
  294. fi:=1;
  295. While (ft1=ftUnknown) and (fi<=FieldMapCount) do
  296. begin
  297. if pos(FieldMap[fi].N,FD)=1 then
  298. begin
  299. ft1:=FieldMap[fi].t;
  300. size1:=FieldMap[fi].S;
  301. end;
  302. Inc(fi);
  303. end;
  304. // handle some specials.
  305. case ft1 of
  306. ftString: begin
  307. fi:=pos('(',FD);
  308. if (fi>0) then
  309. begin
  310. System.Delete(FD,1,fi);
  311. fi:=pos(')',FD);
  312. size1:=StrToIntDef(trim(copy(FD,1,fi-1)),255);
  313. end;
  314. end;
  315. ftUnknown : DatabaseError('Unknown record type: '+FN);
  316. end; // Case
  317. tfielddef.create(fielddefs,FN,ft1,size1,false,i+1);
  318. end;
  319. end;
  320. procedure TSQLite3Connection.Execute(cursor: TSQLCursor; atransaction: tsqltransaction; AParams: TParams);
  321. var
  322. SC : TSQLite3Cursor;
  323. begin
  324. SC:=TSQLite3Cursor(cursor);
  325. If (AParams<>Nil) then
  326. SC.BindParams(AParams);
  327. SC.Execute;
  328. end;
  329. Function NextWord(Var S : ShortString; Sep : Char) : String;
  330. Var
  331. P : Integer;
  332. begin
  333. P:=Pos(Sep,S);
  334. If (P=0) then
  335. P:=Length(S)+1;
  336. Result:=Copy(S,1,P-1);
  337. Delete(S,1,P);
  338. end;
  339. Function ParseSQLiteDate(S : ShortString) : TDateTime;
  340. Var
  341. Year, Month, Day : Integer;
  342. begin
  343. Result:=0;
  344. If TryStrToInt(NextWord(S,'-'),Year) then
  345. if TryStrToInt(NextWord(S,'-'),Month) then
  346. if TryStrToInt(NextWord(S,'-'),Day) then
  347. Result:=EncodeDate(Year,Month,Day);
  348. end;
  349. Function ParseSQLiteTime(S : ShortString) : TDateTime;
  350. Var
  351. Hour, Min, Sec : Integer;
  352. begin
  353. Result:=0;
  354. If TryStrToInt(NextWord(S,':'),Hour) then
  355. if TryStrToInt(NextWord(S,':'),Min) then
  356. if TryStrToInt(NextWord(S,':'),Sec) then
  357. Result:=EncodeTime(Hour,Min,Sec,0);
  358. end;
  359. Function ParseSQLiteDateTime(S : String) : TDateTime;
  360. var
  361. P : Integer;
  362. DS,TS : ShortString;
  363. begin
  364. DS:='';
  365. TS:='';
  366. P:=Pos(' ',S);
  367. If (P<>0) then
  368. begin
  369. DS:=Copy(S,1,P-1);
  370. TS:=S;
  371. Delete(TS,1,P);
  372. end
  373. else
  374. begin
  375. If (Pos('-',S)<>0) then
  376. DS:=S
  377. else if (Pos(':',S)<>0) then
  378. TS:=S;
  379. end;
  380. Result:=ParseSQLiteDate(DS)+ParseSQLiteTime(TS);
  381. end;
  382. function TSQLite3Connection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean;
  383. var
  384. st1: TStorageType;
  385. fnum: integer;
  386. i: integer;
  387. i64: int64;
  388. int1,int2: integer;
  389. str1: string;
  390. ar1,ar2: TStringArray;
  391. st : psqlite3_stmt;
  392. begin
  393. st:=TSQLite3Cursor(cursor).fstatement;
  394. fnum:= FieldDef.fieldno - 1;
  395. st1:= TStorageType(sqlite3_column_type(st,fnum));
  396. CreateBlob:=false;
  397. result:= st1 <> stnull;
  398. if Not result then
  399. Exit;
  400. case FieldDef.datatype of
  401. ftInteger : pinteger(buffer)^ := sqlite3_column_int(st,fnum);
  402. ftSmallInt : psmallint(buffer)^ := sqlite3_column_int(st,fnum);
  403. ftWord : pword(buffer)^ := sqlite3_column_int(st,fnum);
  404. ftBoolean : pwordbool(buffer)^ := sqlite3_column_int(st,fnum)<>0;
  405. ftLargeInt,
  406. ftBCD : PInt64(buffer)^:= sqlite3_column_int64(st,fnum);
  407. ftFloat,
  408. ftCurrency : pdouble(buffer)^:= sqlite3_column_double(st,fnum);
  409. ftDateTime,
  410. ftDate,
  411. ftTime: if st1 = sttext then
  412. begin
  413. result:= false;
  414. setlength(str1,sqlite3_column_bytes(st,fnum));
  415. move(sqlite3_column_text(st,fnum)^,str1[1],length(str1));
  416. PDateTime(Buffer)^:=ParseSqliteDateTime(str1)
  417. end
  418. else
  419. Pdatetime(buffer)^:= sqlite3_column_double(st,fnum);
  420. ftString: begin
  421. int1:= sqlite3_column_bytes(st,fnum);
  422. if int1>FieldDef.Size then
  423. int1:=FieldDef.Size;
  424. if int1 > 0 then
  425. move(sqlite3_column_text(st,fnum)^,buffer^,int1);
  426. end;
  427. ftMemo,
  428. ftBlob: begin
  429. CreateBlob:=True;
  430. int2:= sqlite3_column_bytes(st,fnum);
  431. {$WARNING Blob data not handled correctly }
  432. // int1:= addblobdata(sqlite3_column_text(st,fnum),int2);
  433. move(int1,buffer^,sizeof(int1)); //save id
  434. end;
  435. else { Case }
  436. result:= false; // unknown
  437. end; { Case }
  438. end;
  439. function TSQLite3Connection.Fetch(cursor: TSQLCursor): boolean;
  440. begin
  441. Result:=TSQLite3Cursor(cursor).Fetch;
  442. end;
  443. procedure TSQLite3Connection.FreeFldBuffers(cursor: TSQLCursor);
  444. begin
  445. //dummy
  446. end;
  447. function TSQLite3Connection.GetTransactionHandle(trans: TSQLHandle): pointer;
  448. begin
  449. result:= nil;
  450. end;
  451. function TSQLite3Connection.Commit(trans: TSQLHandle): boolean;
  452. begin
  453. execsql('COMMIT');
  454. result:= true;
  455. end;
  456. function TSQLite3Connection.RollBack(trans: TSQLHandle): boolean;
  457. begin
  458. execsql('ROLLBACK');
  459. result:= true;
  460. end;
  461. function TSQLite3Connection.StartdbTransaction(trans: TSQLHandle;
  462. aParams: string): boolean;
  463. begin
  464. execsql('BEGIN');
  465. result:= true;
  466. end;
  467. procedure TSQLite3Connection.CommitRetaining(trans: TSQLHandle);
  468. begin
  469. commit(trans);
  470. execsql('BEGIN');
  471. end;
  472. procedure TSQLite3Connection.RollBackRetaining(trans: TSQLHandle);
  473. begin
  474. rollback(trans);
  475. execsql('BEGIN');
  476. end;
  477. procedure TSQLite3Connection.DoInternalConnect;
  478. var
  479. str1: string;
  480. begin
  481. if Length(databasename)=0 then
  482. DatabaseError(SErrNoDatabaseName,self);
  483. initialisesqlite;
  484. str1:= databasename;
  485. checkerror(sqlite3_open(pchar(str1),@fhandle));
  486. end;
  487. procedure TSQLite3Connection.DoInternalDisconnect;
  488. begin
  489. if fhandle <> nil then
  490. begin
  491. checkerror(sqlite3_close(fhandle));
  492. fhandle:= nil;
  493. releasesqlite;
  494. end;
  495. end;
  496. function TSQLite3Connection.GetHandle: pointer;
  497. begin
  498. result:= fhandle;
  499. end;
  500. procedure TSQLite3Connection.checkerror(const aerror: integer);
  501. Var
  502. S : String;
  503. begin
  504. if (aerror<>sqlite_ok) then
  505. begin
  506. S:=strpas(sqlite3_errmsg(fhandle));
  507. DatabaseError(S,Self);
  508. end;
  509. end;
  510. procedure TSQLite3Connection.execsql(const asql: string);
  511. var
  512. err : pchar;
  513. str1 : string;
  514. res : integer;
  515. begin
  516. err:= nil;
  517. Res := sqlite3_exec(fhandle,pchar(asql),nil,nil,@err);
  518. if err <> nil then
  519. begin
  520. str1:= strpas(err);
  521. sqlite3_free(err);
  522. end;
  523. if (res<>sqlite_ok) then
  524. databaseerror(str1);
  525. end;
  526. function TSQLite3Connection.blobscached: boolean;
  527. begin
  528. result:= true;
  529. end;
  530. function execcallback(adata: pointer; ncols: longint; //adata = PStringArray
  531. avalues: PPchar; anames: PPchar):longint; cdecl;
  532. var
  533. P : PStringArray;
  534. i : integer;
  535. begin
  536. P:=PStringArray(adata);
  537. SetLength(P^,ncols);
  538. for i:= 0 to ncols - 1 do
  539. P^[i]:= strPas(avalues[i]);
  540. result:= 0;
  541. end;
  542. function TSQLite3Connection.stringquery(const asql: string): TStringArray;
  543. begin
  544. SetLength(result,0);
  545. CheckError(sqlite3_exec(fhandle,pchar(asql),@execcallback,@result,nil));
  546. end;
  547. function execscallback(adata: pointer; ncols: longint; //adata = PArrayStringArray
  548. avalues: PPchar; anames: PPchar):longint; cdecl;
  549. var
  550. I,N : integer;
  551. PP : PArrayStringArray;
  552. p : PStringArray;
  553. begin
  554. PP:=PArrayStringArray(adata);
  555. N:=high(PP^); // Length-1;
  556. setlength(PP^,N+2); // increase with 1;
  557. p:= @(PP^[N]); // newly added array, fill with data.
  558. setlength(p^,ncols);
  559. for i:= 0 to ncols - 1 do
  560. p^[i]:= strPas(avalues[i]);
  561. result:= 0;
  562. end;
  563. function TSQLite3Connection.stringsquery(const asql: string): TArrayStringArray;
  564. begin
  565. SetLength(result,0);
  566. checkerror(sqlite3_exec(fhandle,pchar(asql),@execscallback,@result,nil));
  567. end;
  568. function TSQLite3Connection.getprimarykeyfield(const atablename: string;
  569. const acursor: tsqlcursor): string;
  570. var
  571. int1,int2: integer;
  572. ar1: TArrayStringArray;
  573. str1: string;
  574. begin
  575. result:= '';
  576. if atablename <> '' then
  577. begin
  578. ar1:= stringsquery('PRAGMA table_info('+atablename+');');
  579. for int1:= 0 to high(ar1) do
  580. begin
  581. if (high(ar1[int1]) >= 5) and (ar1[int1][5] <> '0') then
  582. begin
  583. result:= ar1[int1][1];
  584. break;
  585. end;
  586. end;
  587. end;
  588. end;
  589. procedure TSQLite3Connection.UpdateIndexDefs(var IndexDefs: TIndexDefs;
  590. const TableName: string);
  591. var
  592. str1: string;
  593. begin
  594. str1:= getprimarykeyfield(tablename,nil);
  595. if str1 <> '' then
  596. begin
  597. indexdefs.add('$PRIMARY_KEY$',str1,[ixPrimary,ixUnique]);
  598. end;
  599. end;
  600. {
  601. procedure TSQLite3Connection.UpdateIndexDefs(var IndexDefs: TIndexDefs;
  602. const TableName: string);
  603. var
  604. int1,int2: integer;
  605. ar1: TArrayStringArray;
  606. str1: string;
  607. begin
  608. ar1:= stringsquery('PRAGMA table_info('+tablename+');');
  609. for int1:= 0 to high(ar1) do begin
  610. if (high(ar1[int1]) >= 5) and (ar1[int1][5] <> '0') then begin
  611. indexdefs.add('$PRIMARY_KEY$',ar1[int1][1],[ixPrimary,ixUnique]);
  612. break;
  613. end;
  614. end;
  615. end;
  616. }
  617. function TSQLite3Connection.getinsertid: int64;
  618. begin
  619. result:= sqlite3_last_insert_rowid(fhandle);
  620. end;
  621. procedure TSQLite3Connection.setoptions(const avalue: tsqliteoptions);
  622. begin
  623. if avalue <> foptions then
  624. begin
  625. checkdisconnected;
  626. foptions:= avalue;
  627. end;
  628. end;
  629. end.