sqlite3conn.pp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741
  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. StrDispose(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. end;
  253. Const
  254. FieldMapCount = 15;
  255. FieldMap : Array [1..FieldMapCount] of TFieldMap = (
  256. (n:'INT'; t: ftInteger),
  257. (n:'LARGEINT'; t:ftlargeInt),
  258. (n:'WORD'; t: ftWord),
  259. (n:'SMALLINT'; t: ftSmallint),
  260. (n:'BOOLEAN'; t: ftBoolean),
  261. (n:'REAL'; t: ftFloat),
  262. (n:'DOUBLE'; t: ftFloat),
  263. (n:'DATETIME'; t: ftDateTime), // MUST be before date
  264. (n:'DATE'; t: ftDate),
  265. (n:'TIME'; t: ftTime),
  266. (n:'CURRENCY'; t: ftCurrency),
  267. (n:'VARCHAR'; t: ftString),
  268. (n:'NUMERIC'; t: ftBCD),
  269. (n:'TEXT'; t: ftmemo),
  270. (n:'BLOB'; t: ftBlob)
  271. { Template:
  272. (n:''; t: ft)
  273. }
  274. );
  275. procedure TSQLite3Connection.AddFieldDefs(cursor: TSQLCursor;
  276. FieldDefs: TfieldDefs);
  277. var
  278. i : integer;
  279. FN,FD : string;
  280. ft1 : tfieldtype;
  281. size1 : word;
  282. ar1 : TStringArray;
  283. fi : integer;
  284. st : psqlite3_stmt;
  285. begin
  286. st:=TSQLite3Cursor(cursor).fstatement;
  287. for i:= 0 to sqlite3_column_count(st) - 1 do
  288. begin
  289. FN:=sqlite3_column_name(st,i);
  290. FD:=uppercase(sqlite3_column_decltype(st,i));
  291. ft1:= ftUnknown;
  292. size1:= 0;
  293. for fi := 1 to FieldMapCount do if pos(FieldMap[fi].N,FD)=1 then
  294. begin
  295. ft1:=FieldMap[fi].t;
  296. break;
  297. end;
  298. // handle some specials.
  299. size1:=0;
  300. case ft1 of
  301. ftString: begin
  302. fi:=pos('(',FD);
  303. if (fi>0) then
  304. begin
  305. System.Delete(FD,1,fi);
  306. fi:=pos(')',FD);
  307. size1:=StrToIntDef(trim(copy(FD,1,fi-1)),255);
  308. if size1 > dsMaxStringSize then size1 := dsMaxStringSize;
  309. end
  310. else size1 := 255;
  311. end;
  312. ftBCD: begin
  313. fi:=pos(',',FD);
  314. if (fi>0) then
  315. begin
  316. System.Delete(FD,1,fi);
  317. fi:=pos(')',FD);
  318. size1:=StrToIntDef(trim(copy(FD,1,fi-1)),255);
  319. end
  320. else size1 := 4;
  321. end;
  322. ftUnknown : DatabaseError('Unknown record type: '+FN);
  323. end; // Case
  324. tfielddef.create(fielddefs,FN,ft1,size1,false,i+1);
  325. end;
  326. end;
  327. procedure TSQLite3Connection.Execute(cursor: TSQLCursor; atransaction: tsqltransaction; AParams: TParams);
  328. var
  329. SC : TSQLite3Cursor;
  330. begin
  331. SC:=TSQLite3Cursor(cursor);
  332. If (AParams<>Nil) and (AParams.count > 0) then
  333. SC.BindParams(AParams);
  334. SC.Execute;
  335. end;
  336. Function NextWord(Var S : ShortString; Sep : Char) : String;
  337. Var
  338. P : Integer;
  339. begin
  340. P:=Pos(Sep,S);
  341. If (P=0) then
  342. P:=Length(S)+1;
  343. Result:=Copy(S,1,P-1);
  344. Delete(S,1,P);
  345. end;
  346. Function ParseSQLiteDate(S : ShortString) : TDateTime;
  347. Var
  348. Year, Month, Day : Integer;
  349. begin
  350. Result:=0;
  351. If TryStrToInt(NextWord(S,'-'),Year) then
  352. if TryStrToInt(NextWord(S,'-'),Month) then
  353. if TryStrToInt(NextWord(S,'-'),Day) then
  354. Result:=EncodeDate(Year,Month,Day);
  355. end;
  356. Function ParseSQLiteTime(S : ShortString) : TDateTime;
  357. Var
  358. Hour, Min, Sec : Integer;
  359. begin
  360. Result:=0;
  361. If TryStrToInt(NextWord(S,':'),Hour) then
  362. if TryStrToInt(NextWord(S,':'),Min) then
  363. if TryStrToInt(NextWord(S,':'),Sec) then
  364. Result:=EncodeTime(Hour,Min,Sec,0);
  365. end;
  366. Function ParseSQLiteDateTime(S : String) : TDateTime;
  367. var
  368. P : Integer;
  369. DS,TS : ShortString;
  370. begin
  371. DS:='';
  372. TS:='';
  373. P:=Pos(' ',S);
  374. If (P<>0) then
  375. begin
  376. DS:=Copy(S,1,P-1);
  377. TS:=S;
  378. Delete(TS,1,P);
  379. end
  380. else
  381. begin
  382. If (Pos('-',S)<>0) then
  383. DS:=S
  384. else if (Pos(':',S)<>0) then
  385. TS:=S;
  386. end;
  387. Result:=ParseSQLiteDate(DS)+ParseSQLiteTime(TS);
  388. end;
  389. function TSQLite3Connection.LoadField(cursor : TSQLCursor;FieldDef : TfieldDef;buffer : pointer; out CreateBlob : boolean) : boolean;
  390. var
  391. st1: TStorageType;
  392. fnum: integer;
  393. i: integer;
  394. i64: int64;
  395. int1,int2: integer;
  396. str1: string;
  397. ar1,ar2: TStringArray;
  398. st : psqlite3_stmt;
  399. begin
  400. st:=TSQLite3Cursor(cursor).fstatement;
  401. fnum:= FieldDef.fieldno - 1;
  402. st1:= TStorageType(sqlite3_column_type(st,fnum));
  403. CreateBlob:=false;
  404. result:= st1 <> stnull;
  405. if Not result then
  406. Exit;
  407. case FieldDef.datatype of
  408. ftInteger : pinteger(buffer)^ := sqlite3_column_int(st,fnum);
  409. ftSmallInt : psmallint(buffer)^ := sqlite3_column_int(st,fnum);
  410. ftWord : pword(buffer)^ := sqlite3_column_int(st,fnum);
  411. ftBoolean : pwordbool(buffer)^ := sqlite3_column_int(st,fnum)<>0;
  412. ftLargeInt,
  413. ftBCD : PInt64(buffer)^:= sqlite3_column_int64(st,fnum);
  414. ftFloat,
  415. ftCurrency : pdouble(buffer)^:= sqlite3_column_double(st,fnum);
  416. ftDateTime,
  417. ftDate,
  418. ftTime: if st1 = sttext then
  419. begin
  420. result:= false;
  421. setlength(str1,sqlite3_column_bytes(st,fnum));
  422. move(sqlite3_column_text(st,fnum)^,str1[1],length(str1));
  423. PDateTime(Buffer)^:=ParseSqliteDateTime(str1)
  424. end
  425. else
  426. Pdatetime(buffer)^:= sqlite3_column_double(st,fnum);
  427. ftString: begin
  428. int1:= sqlite3_column_bytes(st,fnum);
  429. if int1>FieldDef.Size then
  430. int1:=FieldDef.Size;
  431. if int1 > 0 then
  432. move(sqlite3_column_text(st,fnum)^,buffer^,int1);
  433. end;
  434. ftMemo,
  435. ftBlob: begin
  436. CreateBlob:=True;
  437. int2:= sqlite3_column_bytes(st,fnum);
  438. {$WARNING Blob data not handled correctly }
  439. // int1:= addblobdata(sqlite3_column_text(st,fnum),int2);
  440. move(int1,buffer^,sizeof(int1)); //save id
  441. end;
  442. else { Case }
  443. result:= false; // unknown
  444. end; { Case }
  445. end;
  446. function TSQLite3Connection.Fetch(cursor: TSQLCursor): boolean;
  447. begin
  448. Result:=TSQLite3Cursor(cursor).Fetch;
  449. end;
  450. procedure TSQLite3Connection.FreeFldBuffers(cursor: TSQLCursor);
  451. begin
  452. //dummy
  453. end;
  454. function TSQLite3Connection.GetTransactionHandle(trans: TSQLHandle): pointer;
  455. begin
  456. result:= nil;
  457. end;
  458. function TSQLite3Connection.Commit(trans: TSQLHandle): boolean;
  459. begin
  460. execsql('COMMIT');
  461. result:= true;
  462. end;
  463. function TSQLite3Connection.RollBack(trans: TSQLHandle): boolean;
  464. begin
  465. execsql('ROLLBACK');
  466. result:= true;
  467. end;
  468. function TSQLite3Connection.StartdbTransaction(trans: TSQLHandle;
  469. aParams: string): boolean;
  470. begin
  471. execsql('BEGIN');
  472. result:= true;
  473. end;
  474. procedure TSQLite3Connection.CommitRetaining(trans: TSQLHandle);
  475. begin
  476. commit(trans);
  477. execsql('BEGIN');
  478. end;
  479. procedure TSQLite3Connection.RollBackRetaining(trans: TSQLHandle);
  480. begin
  481. rollback(trans);
  482. execsql('BEGIN');
  483. end;
  484. procedure TSQLite3Connection.DoInternalConnect;
  485. var
  486. str1: string;
  487. begin
  488. if Length(databasename)=0 then
  489. DatabaseError(SErrNoDatabaseName,self);
  490. initialisesqlite;
  491. str1:= databasename;
  492. checkerror(sqlite3_open(pchar(str1),@fhandle));
  493. end;
  494. procedure TSQLite3Connection.DoInternalDisconnect;
  495. begin
  496. if fhandle <> nil then
  497. begin
  498. checkerror(sqlite3_close(fhandle));
  499. fhandle:= nil;
  500. releasesqlite;
  501. end;
  502. end;
  503. function TSQLite3Connection.GetHandle: pointer;
  504. begin
  505. result:= fhandle;
  506. end;
  507. procedure TSQLite3Connection.checkerror(const aerror: integer);
  508. Var
  509. S : String;
  510. begin
  511. if (aerror<>sqlite_ok) then
  512. begin
  513. S:=strpas(sqlite3_errmsg(fhandle));
  514. DatabaseError(S,Self);
  515. end;
  516. end;
  517. procedure TSQLite3Connection.execsql(const asql: string);
  518. var
  519. err : pchar;
  520. str1 : string;
  521. res : integer;
  522. begin
  523. err:= nil;
  524. Res := sqlite3_exec(fhandle,pchar(asql),nil,nil,@err);
  525. if err <> nil then
  526. begin
  527. str1:= strpas(err);
  528. sqlite3_free(err);
  529. end;
  530. if (res<>sqlite_ok) then
  531. databaseerror(str1);
  532. end;
  533. function TSQLite3Connection.blobscached: boolean;
  534. begin
  535. result:= true;
  536. end;
  537. function execcallback(adata: pointer; ncols: longint; //adata = PStringArray
  538. avalues: PPchar; anames: PPchar):longint; cdecl;
  539. var
  540. P : PStringArray;
  541. i : integer;
  542. begin
  543. P:=PStringArray(adata);
  544. SetLength(P^,ncols);
  545. for i:= 0 to ncols - 1 do
  546. P^[i]:= strPas(avalues[i]);
  547. result:= 0;
  548. end;
  549. function TSQLite3Connection.stringquery(const asql: string): TStringArray;
  550. begin
  551. SetLength(result,0);
  552. CheckError(sqlite3_exec(fhandle,pchar(asql),@execcallback,@result,nil));
  553. end;
  554. function execscallback(adata: pointer; ncols: longint; //adata = PArrayStringArray
  555. avalues: PPchar; anames: PPchar):longint; cdecl;
  556. var
  557. I,N : integer;
  558. PP : PArrayStringArray;
  559. p : PStringArray;
  560. begin
  561. PP:=PArrayStringArray(adata);
  562. N:=high(PP^); // Length-1;
  563. setlength(PP^,N+2); // increase with 1;
  564. p:= @(PP^[N]); // newly added array, fill with data.
  565. setlength(p^,ncols);
  566. for i:= 0 to ncols - 1 do
  567. p^[i]:= strPas(avalues[i]);
  568. result:= 0;
  569. end;
  570. function TSQLite3Connection.stringsquery(const asql: string): TArrayStringArray;
  571. begin
  572. SetLength(result,0);
  573. checkerror(sqlite3_exec(fhandle,pchar(asql),@execscallback,@result,nil));
  574. end;
  575. function TSQLite3Connection.getprimarykeyfield(const atablename: string;
  576. const acursor: tsqlcursor): string;
  577. var
  578. int1,int2: integer;
  579. ar1: TArrayStringArray;
  580. str1: string;
  581. begin
  582. result:= '';
  583. if atablename <> '' then
  584. begin
  585. ar1:= stringsquery('PRAGMA table_info('+atablename+');');
  586. for int1:= 0 to high(ar1) do
  587. begin
  588. if (high(ar1[int1]) >= 5) and (ar1[int1][5] <> '0') then
  589. begin
  590. result:= ar1[int1][1];
  591. break;
  592. end;
  593. end;
  594. end;
  595. end;
  596. procedure TSQLite3Connection.UpdateIndexDefs(var IndexDefs: TIndexDefs;
  597. const TableName: string);
  598. var
  599. str1: string;
  600. begin
  601. str1:= getprimarykeyfield(tablename,nil);
  602. if str1 <> '' then
  603. begin
  604. indexdefs.add('$PRIMARY_KEY$',str1,[ixPrimary,ixUnique]);
  605. end;
  606. end;
  607. {
  608. procedure TSQLite3Connection.UpdateIndexDefs(var IndexDefs: TIndexDefs;
  609. const TableName: string);
  610. var
  611. int1,int2: integer;
  612. ar1: TArrayStringArray;
  613. str1: string;
  614. begin
  615. ar1:= stringsquery('PRAGMA table_info('+tablename+');');
  616. for int1:= 0 to high(ar1) do begin
  617. if (high(ar1[int1]) >= 5) and (ar1[int1][5] <> '0') then begin
  618. indexdefs.add('$PRIMARY_KEY$',ar1[int1][1],[ixPrimary,ixUnique]);
  619. break;
  620. end;
  621. end;
  622. end;
  623. }
  624. function TSQLite3Connection.getinsertid: int64;
  625. begin
  626. result:= sqlite3_last_insert_rowid(fhandle);
  627. end;
  628. procedure TSQLite3Connection.setoptions(const avalue: tsqliteoptions);
  629. begin
  630. if avalue <> foptions then
  631. begin
  632. checkdisconnected;
  633. foptions:= avalue;
  634. end;
  635. end;
  636. end.