00001 {*********************************************************}
00002 { }
00003 { Zeos Database Objects }
00004 { Native Plain Drivers for SQLite }
00005 { }
00006 { Originally written by Sergey Seroukhov }
00007 { }
00008 {*********************************************************}
00009
00010 {@********************************************************}
00011 { Copyright (c) 1999-2006 Zeos Development Group }
00012 { }
00013 { License Agreement: }
00014 { }
00015 { This library is distributed in the hope that it will be }
00016 { useful, but WITHOUT ANY WARRANTY; without even the }
00017 { implied warranty of MERCHANTABILITY or FITNESS FOR }
00018 { A PARTICULAR PURPOSE. See the GNU Lesser General }
00019 { Public License for more details. }
00020 { }
00021 { The source code of the ZEOS Libraries and packages are }
00022 { distributed under the Library GNU General Public }
00023 { License (see the file COPYING / COPYING.ZEOS) }
00024 { with the following modification: }
00025 { As a special exception, the copyright holders of this }
00026 { library give you permission to link this library with }
00027 { independent modules to produce an executable, }
00028 { regardless of the license terms of these independent }
00029 { modules, and to copy and distribute the resulting }
00030 { executable under terms of your choice, provided that }
00031 { you also meet, for each linked independent module, }
00032 { the terms and conditions of the license of that module. }
00033 { An independent module is a module which is not derived }
00034 { from or based on this library. If you modify this }
00035 { library, you may extend this exception to your version }
00036 { of the library, but you are not obligated to do so. }
00037 { If you do not wish to do so, delete this exception }
00038 { statement from your version. }
00039 { }
00040 { }
00041 { The project web site is located on: }
00042 { http:
00043 { http:
00044 { svn:
00045 { }
00046 { http:
00047 { http:
00048 { }
00049 { }
00050 { }
00051 { Zeos Development Group. }
00052 {********************************************************@}
00053
00054 unit ZPlainSqLiteDriver;
00055
00056 interface
00057
00058 {$I ZPlain.inc}
00059
00060 uses ZClasses, ZCompatibility, ZPlainDriver, ZPlainSqLite28, ZPlainSqLite3,
00061 SysUtils;
00062
00063 const
00064 MASTER_NAME = 'sqlite_master';
00065 TEMP_MASTER_NAME = 'sqlite_temp_master';
00066
00067 { Return values for sqlite_exec() and sqlite_step() }
00068 SQLITE_OK = 0;
00069 SQLITE_ERROR = 1;
00070 SQLITE_INTERNAL = 2;
00071 SQLITE_PERM = 3;
00072 SQLITE_ABORT = 4;
00073 SQLITE_BUSY = 5;
00074 SQLITE_LOCKED = 6;
00075 SQLITE_NOMEM = 7;
00076 SQLITE_READONLY = 8;
00077 _SQLITE_INTERRUPT = 9;
00078 SQLITE_IOERR = 10;
00079 SQLITE_CORRUPT = 11;
00080 SQLITE_NOTFOUND = 12;
00081 SQLITE_FULL = 13;
00082 SQLITE_CANTOPEN = 14;
00083 SQLITE_PROTOCOL = 15;
00084 SQLITE_EMPTY = 16;
00085 SQLITE_SCHEMA = 17;
00086 SQLITE_TOOBIG = 18;
00087 SQLITE_CONSTRAINT = 19;
00088 SQLITE_MISMATCH = 20;
00089 SQLITE_MISUSE = 21;
00090 SQLITE_NOLFS = 22;
00091 SQLITE_AUTH = 23;
00092 SQLITE_FORMAT = 24;
00093 SQLITE_RANGE = 25;
00094 SQLITE_NOTADB = 26;
00095 SQLITE_ROW = 100;
00096 SQLITE_DONE = 101;
00097
00098 SQLITE_NUMERIC = -1;
00099 SQLITE_TEXT = -2;
00100 SQLITE_ARGS = -3;
00101
00102 {
00103 The second parameter to the access authorization function above will
00104 be one of the values below. These values signify what kind of operation
00105 is to be authorized. The 3rd and 4th parameters to the authorization
00106 function will be parameters or NULL depending on which of the following
00107 codes is used as the second parameter. The 5th parameter is the name
00108 of the database ("main", "temp", etc.) if applicable. The 6th parameter
00109 is the name of the inner-most trigger or view that is responsible for
00110 the access attempt or NULL if this access attempt is directly from
00111 input SQL code.
00112
00113 Arg-3 Arg-4
00114 }
00115 SQLITE_COPY = 0;
00116 SQLITE_CREATE_INDEX = 1;
00117 SQLITE_CREATE_TABLE = 2;
00118 SQLITE_CREATE_TEMP_INDEX = 3;
00119 SQLITE_CREATE_TEMP_TABLE = 4;
00120 SQLITE_CREATE_TEMP_TRIGGER = 5;
00121 SQLITE_CREATE_TEMP_VIEW = 6;
00122 SQLITE_CREATE_TRIGGER = 7;
00123 SQLITE_CREATE_VIEW = 8;
00124 SQLITE_DELETE = 9;
00125 SQLITE_DROP_INDEX = 10;
00126 SQLITE_DROP_TABLE = 11;
00127 SQLITE_DROP_TEMP_INDEX = 12;
00128 SQLITE_DROP_TEMP_TABLE = 13;
00129 SQLITE_DROP_TEMP_TRIGGER = 14;
00130 SQLITE_DROP_TEMP_VIEW = 15;
00131 SQLITE_DROP_TRIGGER = 16;
00132 SQLITE_DROP_VIEW = 17;
00133 SQLITE_INSERT = 18;
00134 SQLITE_PRAGMA = 19;
00135 SQLITE_READ = 20;
00136 SQLITE_SELECT = 21;
00137 SQLITE_TRANSACTION = 22;
00138 SQLITE_UPDATE = 23;
00139 SQLITE_ATTACH = 24;
00140 SQLITE_DETACH = 25;
00141
00142 { The return value of the authorization function should be one of the
00143 following constants: }
00144 SQLITE_DENY = 1;
00145 SQLITE_IGNORE = 2;
00146
00147 type
00148 Psqlite = Pointer;
00149 Psqlite_func = Pointer;
00150 Psqlite_vm = Pointer;
00151
00152 type
00153
00154 {** Represents a generic interface to SQLite native API. }
00155 IZSQLitePlainDriver = interface (IZPlainDriver)
00156 ['{B931C952-3076-4ECB-9630-D900E8DB9869}']
00157
00158 function Open(const filename: PChar; mode: Integer;
00159 var errmsg: PChar): Psqlite;
00160 procedure Close(db: Psqlite);
00161 function Execute(db: Psqlite; const sql: PChar;
00162 sqlite_callback: Tsqlite_callback; arg: Pointer;
00163 var errmsg: PChar): Integer;
00164 function LastInsertRowId(db: Psqlite): Integer;
00165 function Changes(db: Psqlite): Integer;
00166 function LastStatementChanges(db: Psqlite): Integer;
00167 function ErrorString(code: Integer): PChar;
00168 procedure Interrupt(db: Psqlite);
00169 function Complete(const sql: PChar): Integer;
00170
00171 procedure BusyHandler(db: Psqlite; callback: Tsqlite_busy_callback;
00172 ptr: Pointer);
00173 procedure BusyTimeout(db: Psqlite; ms: Integer);
00174
00175 function GetTable(db: Psqlite; const sql: PChar; var resultp: PPChar;
00176 var nrow: Integer; var ncolumn: Integer; var errmsg: PChar): Integer;
00177 procedure FreeTable(var result: PChar);
00178 procedure FreeMem(ptr: Pointer);
00179 function LibVersion: PChar;
00180 function LibEncoding: PChar;
00181
00182 function CreateFunction(db: Psqlite; const zName: PChar;
00183 nArg: Integer; callback: Tsqlite_function_callback;
00184 pUserData: Pointer): Integer;
00185 function CreateAggregate(db: Psqlite; const zName: PChar;
00186 nArg: Integer; callback: Tsqlite_function_callback;
00187 finalize: Tsqlite_finalize_callback; pUserData: Pointer): Integer;
00188 function FunctionType(db: Psqlite; const zName: PChar;
00189 datatype: Integer): Integer;
00190 function SetResultString(func: Psqlite_func; const arg: PChar;
00191 len: Integer): PChar;
00192 procedure SetResultInt(func: Psqlite_func; arg: Integer);
00193 procedure SetResultDouble(func: Psqlite_func; arg: Double);
00194 procedure SetResultError(func: Psqlite_func; const arg: PChar; len: Integer);
00195 function UserData(func: Psqlite_func): Pointer;
00196 function AggregateContext(func: Psqlite_func; nBytes: Integer): Pointer;
00197 function AggregateCount(func: Psqlite_func): Integer;
00198
00199 function SetAuthorizer(db: Psqlite; callback: Tsqlite_auth_callback;
00200 pUserData: Pointer): Integer;
00201 function Trace(db: Psqlite; callback: Tsqlite_trace_callback;
00202 ptr: Pointer): Pointer;
00203
00204 function Compile(db: Psqlite; const zSql: PChar;nBytes: Integer;
00205 var pzTail: PChar; var ppVm: Psqlite_vm; var pzErrmsg: PChar): Integer;
00206 function Step(pVm: Psqlite_vm; var pN: Integer; var pazValue: PPChar;
00207 var pazColName: PPChar): Integer;
00208 function Finalize(vm: Psqlite_vm; var pzErrMsg: PChar): Integer;
00209 function Reset(vm: Psqlite_vm; var pzErrMsg: PChar): Integer;
00210 function Bind(vm: Psqlite_vm; idx: Integer; const value: PChar;
00211 len: Integer; copy: Integer): Integer;
00212
00213 procedure ProgressHandler(db: Psqlite; p1: Integer;
00214 callback: Tsqlite_simple_callback; ptr: Pointer);
00215 function CommitHook(db: Psqlite; callback: Tsqlite_simple_callback;
00216 ptr: Pointer): Pointer;
00217
00218 function OpenEncrypted(const zFilename: PChar; const pKey: PChar;
00219 nKey: Integer; var pErrcode: Integer; var pzErrmsg: PChar): Psqlite;
00220 function ReKey(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
00221 function Key(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
00222 end;
00223
00224 {** Implements a driver for SQLite 2.8 }
00225 TZSQLite28PlainDriver = class (TZAbstractObject, IZPlainDriver,
00226 IZSQLitePlainDriver)
00227 public
00228 constructor Create;
00229
00230 function GetProtocol: string;
00231 function GetDescription: string;
00232 procedure Initialize;
00233
00234 function Open(const filename: PChar; mode: Integer;
00235 var errmsg: PChar): Psqlite;
00236 procedure Close(db: Psqlite);
00237 function Execute(db: Psqlite; const sql: PChar;
00238 sqlite_callback: Tsqlite_callback; arg: Pointer;
00239 var errmsg: PChar): Integer;
00240 function LastInsertRowId(db: Psqlite): Integer;
00241 function Changes(db: Psqlite): Integer;
00242 function LastStatementChanges(db: Psqlite): Integer;
00243 function ErrorString(code: Integer): PChar;
00244 procedure Interrupt(db: Psqlite);
00245 function Complete(const sql: PChar): Integer;
00246
00247 procedure BusyHandler(db: Psqlite; callback: Tsqlite_busy_callback;
00248 ptr: Pointer);
00249 procedure BusyTimeout(db: Psqlite; ms: Integer);
00250
00251 function GetTable(db: Psqlite; const sql: PChar; var resultp: PPChar;
00252 var nrow: Integer; var ncolumn: Integer; var errmsg: PChar): Integer;
00253 procedure FreeTable(var result: PChar);
00254 procedure FreeMem(ptr: Pointer);
00255 function LibVersion: PChar;
00256 function LibEncoding: PChar;
00257
00258 function CreateFunction(db: Psqlite; const zName: PChar;
00259 nArg: Integer; callback: Tsqlite_function_callback;
00260 pUserData: Pointer): Integer;
00261 function CreateAggregate(db: Psqlite; const zName: PChar;
00262 nArg: Integer; callback: Tsqlite_function_callback;
00263 finalize: Tsqlite_finalize_callback; pUserData: Pointer): Integer;
00264 function FunctionType(db: Psqlite; const zName: PChar;
00265 datatype: Integer): Integer;
00266 function SetResultString(func: Psqlite_func; const arg: PChar;
00267 len: Integer): PChar;
00268 procedure SetResultInt(func: Psqlite_func; arg: Integer);
00269 procedure SetResultDouble(func: Psqlite_func; arg: Double);
00270 procedure SetResultError(func: Psqlite_func; const arg: PChar; len: Integer);
00271 function UserData(func: Psqlite_func): Pointer;
00272 function AggregateContext(func: Psqlite_func; nBytes: Integer): Pointer;
00273 function AggregateCount(func: Psqlite_func): Integer;
00274
00275 function SetAuthorizer(db: Psqlite; callback: Tsqlite_auth_callback;
00276 pUserData: Pointer): Integer;
00277 function Trace(db: Psqlite; callback: Tsqlite_trace_callback;
00278 ptr: Pointer): Pointer;
00279
00280 function Compile(db: Psqlite; const zSql: PChar;
00281 nBytes: Integer;var pzTail: PChar;
00282 var ppVm: Psqlite_vm; var pzErrmsg: PChar): Integer;
00283 function Step(pVm: Psqlite_vm; var pN: Integer; var pazValue: PPChar;
00284 var pazColName: PPChar): Integer;
00285 function Finalize(vm: Psqlite_vm; var pzErrMsg: PChar): Integer;
00286 function Reset(vm: Psqlite_vm; var pzErrMsg: PChar): Integer;
00287 function Bind(vm: Psqlite_vm; idx: Integer; const value: PChar;
00288 len: Integer; copy: Integer): Integer;
00289
00290 procedure ProgressHandler(db: Psqlite; p1: Integer;
00291 callback: Tsqlite_simple_callback; ptr: Pointer);
00292 function CommitHook(db: Psqlite; callback: Tsqlite_simple_callback;
00293 ptr: Pointer): Pointer;
00294
00295 function OpenEncrypted(const zFilename: PChar; const pKey: PChar;
00296 nKey: Integer; var pErrcode: Integer; var pzErrmsg: PChar): Psqlite;
00297 function ReKey(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
00298 function Key(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
00299 end;
00300
00301 {** Implements a driver for SQLite 3 }
00302 TZSQLite3PlainDriver = class (TZAbstractObject, IZPlainDriver,
00303 IZSQLitePlainDriver)
00304 public
00305 constructor Create;
00306
00307 function GetProtocol: string;
00308 function GetDescription: string;
00309 procedure Initialize;
00310
00311 function Open(const filename: PChar; mode: Integer;
00312 var errmsg: PChar): Psqlite;
00313 procedure Close(db: Psqlite);
00314 function Execute(db: Psqlite; const sql: PChar;
00315 sqlite_callback: Tsqlite_callback; arg: Pointer;
00316 var errmsg: PChar): Integer;
00317 function LastInsertRowId(db: Psqlite): Integer;
00318 function Changes(db: Psqlite): Integer;
00319 function LastStatementChanges(db: Psqlite): Integer;
00320 function ErrorString(code: Integer): PChar;
00321 procedure Interrupt(db: Psqlite);
00322 function Complete(const sql: PChar): Integer;
00323
00324 procedure BusyHandler(db: Psqlite; callback: Tsqlite_busy_callback;
00325 ptr: Pointer);
00326 procedure BusyTimeout(db: Psqlite; ms: Integer);
00327
00328 function GetTable(db: Psqlite; const sql: PChar; var resultp: PPChar;
00329 var nrow: Integer; var ncolumn: Integer; var errmsg: PChar): Integer;
00330 procedure FreeTable(var result: PChar);
00331 procedure FreeMem(ptr: Pointer);
00332 function LibVersion: PChar;
00333 function LibEncoding: PChar;
00334
00335 function CreateFunction(db: Psqlite; const zName: PChar;
00336 nArg: Integer; callback: Tsqlite_function_callback;
00337 pUserData: Pointer): Integer;
00338 function CreateAggregate(db: Psqlite; const zName: PChar;
00339 nArg: Integer; callback: Tsqlite_function_callback;
00340 finalize: Tsqlite_finalize_callback; pUserData: Pointer): Integer;
00341 function FunctionType(db: Psqlite; const zName: PChar;
00342 datatype: Integer): Integer;
00343 function SetResultString(func: Psqlite_func; const arg: PChar;
00344 len: Integer): PChar;
00345 procedure SetResultInt(func: Psqlite_func; arg: Integer);
00346 procedure SetResultDouble(func: Psqlite_func; arg: Double);
00347 procedure SetResultError(func: Psqlite_func; const arg: PChar; len: Integer);
00348 function UserData(func: Psqlite_func): Pointer;
00349 function AggregateContext(func: Psqlite_func; nBytes: Integer): Pointer;
00350 function AggregateCount(func: Psqlite_func): Integer;
00351
00352 function SetAuthorizer(db: Psqlite; callback: Tsqlite_auth_callback;
00353 pUserData: Pointer): Integer;
00354 function Trace(db: Psqlite; callback: Tsqlite_trace_callback;
00355 ptr: Pointer): Pointer;
00356
00357 function Compile(db: Psqlite; const zSql: PChar;
00358 nBytes: Integer;var pzTail: PChar; var ppVm: Psqlite_vm;
00359 var pzErrmsg: PChar): Integer;
00360 function Step(pVm: Psqlite_vm; var pN: Integer; var pazValue: PPChar;
00361 var pazColName: PPChar): Integer;
00362 function Finalize(vm: Psqlite_vm; var pzErrMsg: PChar): Integer;
00363 function Reset(vm: Psqlite_vm; var pzErrMsg: PChar): Integer;
00364 function Bind(vm: Psqlite_vm; idx: Integer; const value: PChar;
00365 len: Integer; copy: Integer): Integer;
00366
00367 procedure ProgressHandler(db: Psqlite; p1: Integer;
00368 callback: Tsqlite_simple_callback; ptr: Pointer);
00369 function CommitHook(db: Psqlite; callback: Tsqlite_simple_callback;
00370 ptr: Pointer): Pointer;
00371
00372 function OpenEncrypted(const zFilename: PChar; const pKey: PChar;
00373 nKey: Integer; var pErrcode: Integer; var pzErrmsg: PChar): Psqlite;
00374 function ReKey(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
00375 function Key(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
00376 end;
00377
00378 implementation
00379
00380 { TZSQLite28PlainDriver }
00381
00382 constructor TZSQLite28PlainDriver.Create;
00383 begin
00384 end;
00385
00386 function TZSQLite28PlainDriver.GetProtocol: string;
00387 begin
00388 Result := 'sqlite-2.8';
00389 end;
00390
00391 function TZSQLite28PlainDriver.GetDescription: string;
00392 begin
00393 Result := 'Native Plain Driver for SQLite 2.8';
00394 end;
00395
00396 procedure TZSQLite28PlainDriver.Initialize;
00397 begin
00398 ZPlainSQLite28.LibraryLoader.LoadIfNeeded;
00399 end;
00400
00401 function TZSQLite28PlainDriver.AggregateContext(func: Psqlite_func;
00402 nBytes: Integer): Pointer;
00403 begin
00404 Result := ZPlainSqLite28.sqlite_aggregate_context(func, nBytes);
00405 end;
00406
00407 function TZSQLite28PlainDriver.AggregateCount(func: Psqlite_func): Integer;
00408 begin
00409 Result := ZPlainSqLite28.sqlite_aggregate_count(func);
00410 end;
00411
00412 function TZSQLite28PlainDriver.Bind(vm: Psqlite_vm; idx: Integer;
00413 const value: PChar; len, copy: Integer): Integer;
00414 begin
00415 Result := ZPlainSqLite28.sqlite_bind(vm, idx, value, len, copy);
00416 end;
00417
00418 procedure TZSQLite28PlainDriver.BusyHandler(db: Psqlite;
00419 callback: Tsqlite_busy_callback; ptr: Pointer);
00420 begin
00421 ZPlainSqLite28.sqlite_busy_handler(db, callback, ptr);
00422 end;
00423
00424 procedure TZSQLite28PlainDriver.BusyTimeout(db: Psqlite; ms: Integer);
00425 begin
00426 ZPlainSqLite28.sqlite_busy_timeout(db, ms);
00427 end;
00428
00429 function TZSQLite28PlainDriver.Changes(db: Psqlite): Integer;
00430 begin
00431 Result := ZPlainSqLite28.sqlite_changes(db);
00432 end;
00433
00434 function TZSQLite28PlainDriver.CommitHook(db: Psqlite;
00435 callback: Tsqlite_simple_callback; ptr: Pointer): Pointer;
00436 begin
00437 Result := ZPlainSqLite28.sqlite_commit_hook(db, callback, ptr);
00438 end;
00439
00440 function TZSQLite28PlainDriver.Compile(db: Psqlite; const zSql: PChar;
00441 nBytes: Integer;var pzTail: PChar;
00442 var ppVm: Psqlite_vm; var pzErrmsg: PChar): Integer;
00443 begin
00444 Result := ZPlainSqLite28.sqlite_compile(db, zSql, pzTail, ppVm, pzErrmsg);
00445 end;
00446
00447 function TZSQLite28PlainDriver.Complete(const sql: PChar): Integer;
00448 begin
00449 Result := ZPlainSqLite28.sqlite_complete(sql);
00450 end;
00451
00452 function TZSQLite28PlainDriver.CreateAggregate(db: Psqlite;
00453 const zName: PChar; nArg: Integer; callback: Tsqlite_function_callback;
00454 finalize: Tsqlite_finalize_callback; pUserData: Pointer): Integer;
00455 begin
00456 Result := ZPlainSqLite28.sqlite_create_aggregate(db, zName, nArg, callback,
00457 finalize, pUserData);
00458 end;
00459
00460 function TZSQLite28PlainDriver.CreateFunction(db: Psqlite;
00461 const zName: PChar; nArg: Integer; callback: Tsqlite_function_callback;
00462 pUserData: Pointer): Integer;
00463 begin
00464 Result := ZPlainSqLite28.sqlite_create_function(db, zName, nArg, callback,
00465 pUserData);
00466 end;
00467
00468 function TZSQLite28PlainDriver.ErrorString(code: Integer): PChar;
00469 begin
00470 Result := ZPlainSqLite28.sqlite_error_string(code);
00471 end;
00472
00473 function TZSQLite28PlainDriver.Execute(db: Psqlite; const sql: PChar;
00474 sqlite_callback: Tsqlite_callback; arg: Pointer;
00475 var errmsg: PChar): Integer;
00476 begin
00477 Result := ZPlainSqLite28.sqlite_exec(db, sql, sqlite_callback, arg, errmsg);
00478 end;
00479
00480 function TZSQLite28PlainDriver.Finalize(vm: Psqlite_vm;
00481 var pzErrMsg: PChar): Integer;
00482 begin
00483 Result := ZPlainSqLite28.sqlite_finalize(vm, pzErrMsg);
00484 end;
00485
00486 procedure TZSQLite28PlainDriver.FreeMem(ptr: Pointer);
00487 begin
00488 ZPlainSqLite28.sqlite_freemem(ptr);
00489 end;
00490
00491 procedure TZSQLite28PlainDriver.FreeTable(var result: PChar);
00492 begin
00493 ZPlainSqLite28.sqlite_free_table(result);
00494 end;
00495
00496 function TZSQLite28PlainDriver.FunctionType(db: Psqlite;
00497 const zName: PChar; datatype: Integer): Integer;
00498 begin
00499 Result := ZPlainSqLite28.sqlite_function_type(db, zName, datatype);
00500 end;
00501
00502 function TZSQLite28PlainDriver.GetTable(db: Psqlite; const sql: PChar;
00503 var resultp: PPChar; var nrow, ncolumn: Integer;
00504 var errmsg: PChar): Integer;
00505 begin
00506 Result := ZPlainSqLite28.sqlite_get_table(db, sql, resultp, nrow, ncolumn,
00507 errmsg);
00508 end;
00509
00510 procedure TZSQLite28PlainDriver.Interrupt(db: Psqlite);
00511 begin
00512 ZPlainSqLite28.sqlite_interrupt(db);
00513 end;
00514
00515 function TZSQLite28PlainDriver.LastInsertRowId(db: Psqlite): Integer;
00516 begin
00517 Result := ZPlainSqLite28.sqlite_last_insert_rowid(db);
00518 end;
00519
00520 function TZSQLite28PlainDriver.LastStatementChanges(db: Psqlite): Integer;
00521 begin
00522 Result := ZPlainSqLite28.sqlite_last_statement_changes(db);
00523 end;
00524
00525 function TZSQLite28PlainDriver.LibEncoding: PChar;
00526 begin
00527 Result := ZPlainSqLite28.sqlite_libencoding;
00528 end;
00529
00530 function TZSQLite28PlainDriver.LibVersion: PChar;
00531 begin
00532 Result := ZPlainSqLite28.sqlite_libversion;
00533 end;
00534
00535 function TZSQLite28PlainDriver.Open(const filename: PChar; mode: Integer;
00536 var errmsg: PChar): Psqlite;
00537 begin
00538 Result := ZPlainSqLite28.sqlite_open(filename, mode, errmsg);
00539 end;
00540
00541 function TZSQLite28PlainDriver.OpenEncrypted(const zFilename, pKey: PChar;
00542 nKey: Integer; var pErrcode: Integer; var pzErrmsg: PChar): Psqlite;
00543 begin
00544 if @ZPlainSqLite28.sqlite_open_encrypted = nil then
00545 begin
00546 Result := nil;
00547 end
00548 else begin
00549 Result := ZPlainSqLite28.sqlite_open_encrypted(zFilename, pKey, nKey,
00550 pErrcode, pzErrmsg);
00551 end;
00552 end;
00553
00554 procedure TZSQLite28PlainDriver.ProgressHandler(db: Psqlite; p1: Integer;
00555 callback: Tsqlite_simple_callback; ptr: Pointer);
00556 begin
00557 ZPlainSqLite28.sqlite_progress_handler(db, p1, callback, ptr);
00558 end;
00559
00560 function TZSQLite28PlainDriver.ReKey(db: Psqlite; const pKey: Pointer;
00561 nKey: Integer): Integer;
00562 begin
00563 if @ZPlainSqLite28.sqlite_rekey = nil then
00564 begin
00565 Result := SQLITE_OK;
00566 end
00567 else begin
00568 Result := ZPlainSqLite28.sqlite_rekey(db, pKey, nKey);
00569 end;
00570 end;
00571
00572 function TZSQLite28PlainDriver.Key(db: Psqlite; const pKey: Pointer;
00573 nKey: Integer): Integer;
00574 begin
00575 if @ZPlainSqLite28.sqlite_key = nil then
00576 begin
00577 Result := SQLITE_OK;
00578 end
00579 else begin
00580 Result := ZPlainSqLite28.sqlite_key(db, pKey, nKey);
00581 end;
00582 end;
00583
00584 function TZSQLite28PlainDriver.Reset(vm: Psqlite_vm;
00585 var pzErrMsg: PChar): Integer;
00586 begin
00587 Result := ZPlainSqLite28.sqlite_reset(vm, pzErrMsg);
00588 end;
00589
00590 function TZSQLite28PlainDriver.SetAuthorizer(db: Psqlite;
00591 callback: Tsqlite_auth_callback; pUserData: Pointer): Integer;
00592 begin
00593 Result := ZPlainSqLite28.sqlite_set_authorizer(db, callback, pUserData);
00594 end;
00595
00596 procedure TZSQLite28PlainDriver.SetResultDouble(func: Psqlite_func;
00597 arg: Double);
00598 begin
00599 ZPlainSqLite28.sqlite_set_result_double(func, arg);
00600 end;
00601
00602 procedure TZSQLite28PlainDriver.SetResultError(func: Psqlite_func;
00603 const arg: PChar; len: Integer);
00604 begin
00605 ZPlainSqLite28.sqlite_set_result_error(func, arg, len);
00606 end;
00607
00608 procedure TZSQLite28PlainDriver.SetResultInt(func: Psqlite_func;
00609 arg: Integer);
00610 begin
00611 ZPlainSqLite28.sqlite_set_result_int(func, arg);
00612 end;
00613
00614 function TZSQLite28PlainDriver.SetResultString(func: Psqlite_func;
00615 const arg: PChar; len: Integer): PChar;
00616 begin
00617 Result := ZPlainSqLite28.sqlite_set_result_string(func, arg, len);
00618 end;
00619
00620 function TZSQLite28PlainDriver.Step(pVm: Psqlite_vm; var pN: Integer;
00621 var pazValue, pazColName: PPChar): Integer;
00622 begin
00623 Result := ZPlainSqLite28.sqlite_step(pVm, pN, pazValue, pazColName);
00624 end;
00625
00626 function TZSQLite28PlainDriver.Trace(db: Psqlite;
00627 callback: Tsqlite_trace_callback; ptr: Pointer): Pointer;
00628 begin
00629 Result := ZPlainSqLite28.sqlite_trace(db, callback, ptr);
00630 end;
00631
00632 function TZSQLite28PlainDriver.UserData(func: Psqlite_func): Pointer;
00633 begin
00634 Result := ZPlainSqLite28.sqlite_user_data(func);
00635 end;
00636
00637 procedure TZSQLite28PlainDriver.Close(db: Psqlite);
00638 begin
00639 ZPlainSqLite28.sqlite_close(db);
00640 end;
00641
00642 { TZSQLite3PlainDriver }
00643
00644 constructor TZSQLite3PlainDriver.Create;
00645 begin
00646 end;
00647
00648 function TZSQLite3PlainDriver.GetProtocol: string;
00649 begin
00650 Result := 'sqlite-3';
00651 end;
00652
00653 function TZSQLite3PlainDriver.GetDescription: string;
00654 begin
00655 Result := 'Native Plain Driver for SQLite 3';
00656 end;
00657
00658 procedure TZSQLite3PlainDriver.Initialize;
00659 begin
00660 ZPlainSQLite3.LibraryLoader.LoadIfNeeded;
00661 end;
00662
00663 function TZSQLite3PlainDriver.AggregateContext(func: Psqlite_func;
00664 nBytes: Integer): Pointer;
00665 begin
00666 Result := ZPlainSqLite3.sqlite_aggregate_context(func, nBytes);
00667 end;
00668
00669 function TZSQLite3PlainDriver.AggregateCount(func: Psqlite_func): Integer;
00670 begin
00671 Result := ZPlainSqLite3.sqlite_aggregate_count(func);
00672 end;
00673
00674 function TZSQLite3PlainDriver.Bind(vm: Psqlite_vm; idx: Integer;
00675 const value: PChar; len, copy: Integer): Integer;
00676 begin
00677 Result := ZPlainSqLite3.sqlite_bind(vm, idx, value, len, copy);
00678 end;
00679
00680 procedure TZSQLite3PlainDriver.BusyHandler(db: Psqlite;
00681 callback: Tsqlite_busy_callback; ptr: Pointer);
00682 begin
00683 ZPlainSqLite3.sqlite_busy_handler(db, callback, ptr);
00684 end;
00685
00686 procedure TZSQLite3PlainDriver.BusyTimeout(db: Psqlite; ms: Integer);
00687 begin
00688 ZPlainSqLite3.sqlite_busy_timeout(db, ms);
00689 end;
00690
00691 function TZSQLite3PlainDriver.Changes(db: Psqlite): Integer;
00692 begin
00693 Result := ZPlainSqLite3.sqlite_changes(db);
00694 end;
00695
00696 function TZSQLite3PlainDriver.CommitHook(db: Psqlite;
00697 callback: Tsqlite_simple_callback; ptr: Pointer): Pointer;
00698 begin
00699 Result := ZPlainSqLite3.sqlite_commit_hook(db, callback, ptr);
00700 end;
00701
00702 function TZSQLite3PlainDriver.Compile(db: Psqlite; const zSql: PChar;
00703 nBytes: Integer;var pzTail: PChar;
00704 var ppVm: Psqlite_vm; var pzErrmsg: PChar): Integer;
00705
00706 begin
00707 Result := ZPlainSqLite3.sqlite_compile(db, zSql, -1, ppVm, nil);
00708 pzErrmsg := nil;
00709 end;
00710
00711 function TZSQLite3PlainDriver.Complete(const sql: PChar): Integer;
00712 begin
00713 Result := ZPlainSqLite3.sqlite_complete(sql);
00714 end;
00715
00716 function TZSQLite3PlainDriver.CreateAggregate(db: Psqlite;
00717 const zName: PChar; nArg: Integer; callback: Tsqlite_function_callback;
00718 finalize: Tsqlite_finalize_callback; pUserData: Pointer): Integer;
00719 begin
00720 Result := SQLITE_MISUSE;
00721 end;
00722
00723 function TZSQLite3PlainDriver.CreateFunction(db: Psqlite;
00724 const zName: PChar; nArg: Integer; callback: Tsqlite_function_callback;
00725 pUserData: Pointer): Integer;
00726 begin
00727 Result := SQLITE_MISUSE;
00728 end;
00729
00730 function TZSQLite3PlainDriver.ErrorString(code: Integer): PChar;
00731 begin
00732 Case code of
00733 SQLITE_OK: Result := 'not an error';
00734 SQLITE_ERROR: Result := 'SQL logic error or missing database';
00735 SQLITE_INTERNAL: Result := 'internal SQLite implementation flaw';
00736 SQLITE_PERM: Result := 'access permission denied';
00737 SQLITE_ABORT: Result := 'callback requested query abort';
00738 SQLITE_BUSY: Result := 'database is locked';
00739 SQLITE_LOCKED: Result := 'database table is locked';
00740 SQLITE_NOMEM: Result := 'out of memory';
00741 SQLITE_READONLY: Result := 'attempt to write a readonly database';
00742 _SQLITE_INTERRUPT: Result := 'interrupted';
00743 SQLITE_IOERR: Result := 'disk I/O error';
00744 SQLITE_CORRUPT: Result := 'database disk image is malformed';
00745 SQLITE_NOTFOUND: Result := 'table or record not found';
00746 SQLITE_FULL: Result := 'database is full';
00747 SQLITE_CANTOPEN: Result := 'unable to open database file';
00748 SQLITE_PROTOCOL: Result := 'database locking protocol failure';
00749 SQLITE_EMPTY: Result := 'table contains no data';
00750 SQLITE_SCHEMA: Result := 'database schema has changed';
00751 SQLITE_TOOBIG: Result := 'too much data for one table row';
00752 SQLITE_CONSTRAINT: Result := 'constraint failed';
00753 SQLITE_MISMATCH: Result := 'datatype mismatch';
00754 SQLITE_MISUSE: Result := 'library routine called out of sequence';
00755 SQLITE_NOLFS: Result := 'kernel lacks large file support';
00756 SQLITE_AUTH: Result := 'authorization denied';
00757 SQLITE_FORMAT: Result := 'auxiliary database format error';
00758 SQLITE_RANGE: Result := 'bind index out of range';
00759 SQLITE_NOTADB: Result := 'file is encrypted or is not a database';
00760 else Result := 'unknown error';
00761 end;
00762 end;
00763
00764 function TZSQLite3PlainDriver.Execute(db: Psqlite; const sql: PChar;
00765 sqlite_callback: Tsqlite_callback; arg: Pointer;
00766 var errmsg: PChar): Integer;
00767 begin
00768 errmsg:= nil;
00769 Result := ZPlainSqLite3.sqlite_exec(db, sql, sqlite_callback, arg, errmsg);
00770 end;
00771
00772 function TZSQLite3PlainDriver.Finalize(vm: Psqlite_vm;
00773 var pzErrMsg: PChar): Integer;
00774 begin
00775 Result:= ZPlainSqLite3.sqlite_finalize(vm); pzErrMsg:=nil;
00776 end;
00777
00778 procedure TZSQLite3PlainDriver.FreeMem(ptr: Pointer);
00779 begin
00780 ZPlainSqLite3.sqlite_freemem(ptr);
00781 end;
00782
00783 procedure TZSQLite3PlainDriver.FreeTable(var result: PChar);
00784 begin
00785 ZPlainSqLite3.sqlite_free_table(result);
00786 end;
00787
00788 function TZSQLite3PlainDriver.FunctionType(db: Psqlite;
00789 const zName: PChar; datatype: Integer): Integer;
00790 begin
00791 Result := SQLITE_MISUSE;
00792 end;
00793
00794 function TZSQLite3PlainDriver.GetTable(db: Psqlite; const sql: PChar;
00795 var resultp: PPChar; var nrow, ncolumn: Integer;
00796 var errmsg: PChar): Integer;
00797 begin
00798 Result := ZPlainSqLite3.sqlite_get_table(db, sql, resultp, nrow, ncolumn,
00799 errmsg);
00800 end;
00801
00802 procedure TZSQLite3PlainDriver.Interrupt(db: Psqlite);
00803 begin
00804 ZPlainSqLite3.sqlite_interrupt(db);
00805 end;
00806
00807 function TZSQLite3PlainDriver.LastInsertRowId(db: Psqlite): Integer;
00808 begin
00809 Result := ZPlainSqLite3.sqlite_last_insert_rowid(db);
00810 end;
00811
00812 function TZSQLite3PlainDriver.LastStatementChanges(db: Psqlite): Integer;
00813 begin
00814 Result := SQLITE_MISUSE;
00815 end;
00816
00817 function TZSQLite3PlainDriver.LibEncoding: PChar;
00818 begin
00819 Result := nil;
00820 end;
00821
00822 function TZSQLite3PlainDriver.LibVersion: PChar;
00823 begin
00824 Result := ZPlainSqLite3.sqlite_libversion;
00825 end;
00826
00827 function TZSQLite3PlainDriver.Open(const filename: PChar; mode: Integer;
00828 var errmsg: PChar): Psqlite;
00829 var
00830 Result0: Psqlite;
00831 Version: string;
00832 FileNameString: String;
00833 begin
00834 Result0:= nil;
00835 Version := LibVersion;
00836 FileNameString := filename;
00837 {$IFNDEF VER130}
00838 if (Version > '3.2.5') then
00839 ZPlainSqLite3.sqlite_open(PAnsiChar(AnsiToUTF8(FileNameString)), Result0)
00840 else
00841 {$ENDIF}
00842 ZPlainSqLite3.sqlite_open(filename, Result0);
00843 Result := Result0;
00844 end;
00845
00846 function TZSQLite3PlainDriver.OpenEncrypted(const zFilename, pKey: PChar;
00847 nKey: Integer; var pErrcode: Integer; var pzErrmsg: PChar): Psqlite;
00848 begin
00849 pErrcode := SQLITE_MISUSE;
00850 pzErrmsg := 'function is not used in the current version of the library';
00851 Result:= nil;
00852 end;
00853
00854 procedure TZSQLite3PlainDriver.ProgressHandler(db: Psqlite; p1: Integer;
00855 callback: Tsqlite_simple_callback; ptr: Pointer);
00856 begin
00857 ZPlainSqLite3.sqlite_progress_handler(db, p1, callback, ptr);
00858 end;
00859
00860 function TZSQLite3PlainDriver.ReKey(db: Psqlite; const pKey: Pointer;
00861 nKey: Integer): Integer;
00862 begin
00863 if @ZPlainSqLite3.sqlite_rekey = nil then
00864 begin
00865 Result := SQLITE_OK;
00866 end
00867 else begin
00868 Result := ZPlainSqLite3.sqlite_rekey(db, pKey, nKey);
00869 end;
00870 end;
00871
00872 function TZSQLite3PlainDriver.Key(db: Psqlite; const pKey: Pointer;
00873 nKey: Integer): Integer;
00874 begin
00875 if @ZPlainSqLite3.sqlite_key = nil then
00876 begin
00877 Result := SQLITE_OK;
00878 end
00879 else begin
00880 Result := ZPlainSqLite3.sqlite_key(db, pKey, nKey);
00881 end;
00882 end;
00883
00884 function TZSQLite3PlainDriver.Reset(vm: Psqlite_vm;
00885 var pzErrMsg: PChar): Integer;
00886 begin
00887 Result := ZPlainSqLite3.sqlite_reset(vm);
00888 pzErrMsg := nil;
00889 end;
00890
00891 function TZSQLite3PlainDriver.SetAuthorizer(db: Psqlite;
00892 callback: Tsqlite_auth_callback; pUserData: Pointer): Integer;
00893 begin
00894 Result := ZPlainSqLite3.sqlite_set_authorizer(db, callback, pUserData);
00895 end;
00896
00897 procedure TZSQLite3PlainDriver.SetResultDouble(func: Psqlite_func;
00898 arg: Double);
00899 begin
00900 ZPlainSqLite3.sqlite_set_result_double(func, arg);
00901 end;
00902
00903 procedure TZSQLite3PlainDriver.SetResultError(func: Psqlite_func;
00904 const arg: PChar; len: Integer);
00905 begin
00906 ZPlainSqLite3.sqlite_set_result_error(func, arg, len);
00907 end;
00908
00909 procedure TZSQLite3PlainDriver.SetResultInt(func: Psqlite_func;
00910 arg: Integer);
00911 begin
00912 ZPlainSqLite3.sqlite_set_result_int(func, arg);
00913 end;
00914
00915 function TZSQLite3PlainDriver.SetResultString(func: Psqlite_func;
00916 const arg: PChar; len: Integer): PChar;
00917 begin
00918 Result := ZPlainSqLite3.sqlite_set_result_string(func, arg, len, nil);
00919 end;
00920
00921 function TZSQLite3PlainDriver.Step(pVm: Psqlite_vm; var pN: Integer;
00922 var pazValue, pazColName: PPChar): Integer;
00923 var i: Integer;
00924 val,cname,ctype: PChar;
00925 pazValue0, pazColName0, pazColType: PPChar;
00926 begin
00927 pazValue0 := nil;
00928 Result := ZPlainSqLite3.sqlite_step(pVm);
00929 if (Result=SQLITE_ROW) or (Result=SQLITE_DONE) then begin
00930 pN:= ZPlainSqLite3.sqlite_column_count(pVm);
00931 if Result=SQLITE_ROW then begin
00932 pazValue:= AllocMem(SizeOf(PPChar)*(pN+1));
00933 pazValue0:= pazValue;
00934 end;
00935 pazColName:= AllocMem(SizeOf(PPChar)*(pN+1)*2);
00936 pazColName0:= pazColName;
00937 pazColType:= pazColName;
00938
00939 Inc(pazColType, pN);
00940
00941
00942 for i:=0 to pN-1 do begin
00943 if Result = SQLITE_ROW then
00944 begin
00945 val := ZPlainSqLite3.sqlite_column_bytes(pVm, i);
00946 cname:= ZPlainSqLite3.sqlite_column_name(pVm, i);
00947 ctype:= ZPlainSqLite3.sqlite_column_decltype(pVm, i);
00948 pazValue0^ := val; inc(pazValue0);
00949 end
00950 else
00951 begin
00952 cname:= ZPlainSqLite3.sqlite_column_name(pVm, i);
00953 ctype:= ZPlainSqLite3.sqlite_column_decltype(pVm, i);
00954 end;
00955 pazColName0^:= cname;
00956 pazColType^ := ctype;
00957 inc(pazColName0);inc(pazColType);
00958 end;
00959 if Result=SQLITE_ROW then pazValue0^:= nil;
00960 pazColType^:= nil;
00961 if Result=SQLITE_DONE then pazValue:= nil;
00962 end;
00963 end;
00964
00965 function TZSQLite3PlainDriver.Trace(db: Psqlite;
00966 callback: Tsqlite_trace_callback; ptr: Pointer): Pointer;
00967 begin
00968 Result := ZPlainSqLite3.sqlite_trace(db, callback, ptr);
00969 end;
00970
00971 function TZSQLite3PlainDriver.UserData(func: Psqlite_func): Pointer;
00972 begin
00973 Result := ZPlainSqLite3.sqlite_user_data(func);
00974 end;
00975
00976 procedure TZSQLite3PlainDriver.Close(db: Psqlite);
00977 begin
00978 ZPlainSqLite3.sqlite_close(db);
00979 end;
00980
00981 end.
00982