1 {*********************************************************}
3 { Zeos Database Objects }
4 { Native Plain Drivers for SQLite }
6 { Originally written by Sergey Seroukhov }
8 {*********************************************************}
10 {@********************************************************}
11 { Copyright (c) 1999-2012 Zeos Development Group }
13 { License Agreement: }
15 { This library is distributed in the hope that it will be }
16 { useful, but WITHOUT ANY WARRANTY; without even the }
17 { implied warranty of MERCHANTABILITY or FITNESS FOR }
18 { A PARTICULAR PURPOSE. See the GNU Lesser General }
19 { Public License for more details. }
21 { The source code of the ZEOS Libraries and packages are }
22 { distributed under the Library GNU General Public }
23 { License (see the file COPYING / COPYING.ZEOS) }
24 { with the following modification: }
25 { As a special exception, the copyright holders of this }
26 { library give you permission to link this library with }
27 { independent modules to produce an executable, }
28 { regardless of the license terms of these independent }
29 { modules, and to copy and distribute the resulting }
30 { executable under terms of your choice, provided that }
31 { you also meet, for each linked independent module, }
32 { the terms and conditions of the license of that module. }
33 { An independent module is a module which is not derived }
34 { from or based on this library. If you modify this }
35 { library, you may extend this exception to your version }
36 { of the library, but you are not obligated to do so. }
37 { If you do not wish to do so, delete this exception }
38 { statement from your version. }
41 { The project web site is located on: }
42 { http://zeos.firmos.at (FORUM) }
43 { http://sourceforge.net/p/zeoslib/tickets/ (BUGTRACKER)}
44 { svn://svn.code.sf.net/p/zeoslib/code-0/trunk (SVN) }
46 { http://www.sourceforge.net/projects/zeoslib. }
49 { Zeos Development Group. }
50 {********************************************************@}
52 unit ZPlainSqLiteDriver;
58 uses SysUtils, Classes, {$IFDEF MSEgui}mclasses,{$ENDIF} Types,
59 ZClasses, ZCompatibility, ZPlainDriver;
62 WINDOWS_DLL_LOCATION = 'sqlite.dll';
63 WINDOWS_DLL3_LOCATION = 'sqlite3.dll';
64 LINUX_DLL_LOCATION = 'libsqlite'+SharedSuffix;
65 LINUX_DLL3_LOCATION = 'libsqlite3'+SharedSuffix;
68 MASTER_NAME = 'sqlite_master';
69 TEMP_MASTER_NAME = 'sqlite_temp_master';
71 { Return values for sqlite_exec() and sqlite_step() }
72 SQLITE_OK = 0; // Successful result
73 SQLITE_ERROR = 1; // SQL error or missing database
74 SQLITE_INTERNAL = 2; // An internal logic error in SQLite
75 SQLITE_PERM = 3; // Access permission denied
76 SQLITE_ABORT = 4; // Callback routine requested an abort
77 SQLITE_BUSY = 5; // The database file is locked
78 SQLITE_LOCKED = 6; // A table in the database is locked
79 SQLITE_NOMEM = 7; // A malloc() failed
80 SQLITE_READONLY = 8; // Attempt to write a readonly database
81 _SQLITE_INTERRUPT = 9; // Operation terminated by sqlite_interrupt()
82 SQLITE_IOERR = 10; // Some kind of disk I/O error occurred
83 SQLITE_CORRUPT = 11; // The database disk image is malformed
84 SQLITE_NOTFOUND = 12; // (Internal Only) Table or record not found
85 SQLITE_FULL = 13; // Insertion failed because database is full
86 SQLITE_CANTOPEN = 14; // Unable to open the database file
87 SQLITE_PROTOCOL = 15; // Database lock protocol error
88 SQLITE_EMPTY = 16; // (Internal Only) Database table is empty
89 SQLITE_SCHEMA = 17; // The database schema changed
90 SQLITE_TOOBIG = 18; // Too much data for one row of a table
91 SQLITE_CONSTRAINT = 19; // Abort due to contraint violation
92 SQLITE_MISMATCH = 20; // Data type mismatch
93 SQLITE_MISUSE = 21; // Library used incorrectly
94 SQLITE_NOLFS = 22; // Uses OS features not supported on host
95 SQLITE_AUTH = 23; // Authorization denied
96 SQLITE_FORMAT = 24; // Auxiliary database format error
97 SQLITE_RANGE = 25; // 2nd parameter to sqlite_bind out of range
98 SQLITE_NOTADB = 26; // File opened that is not a database file
99 SQLITE_ROW = 100; // sqlite_step() has another row ready
100 SQLITE_DONE = 101; // sqlite_step() has finished executing
107 The second parameter to the access authorization function above will
108 be one of the values below. These values signify what kind of operation
109 is to be authorized. The 3rd and 4th parameters to the authorization
110 function will be parameters or NULL depending on which of the following
111 codes is used as the second parameter. The 5th parameter is the name
112 of the database ("main", "temp", etc.) if applicable. The 6th parameter
113 is the name of the inner-most trigger or view that is responsible for
114 the access attempt or NULL if this access attempt is directly from
119 SQLITE_COPY = 0; // Table Name File Name
120 SQLITE_CREATE_INDEX = 1; // Index Name Table Name
121 SQLITE_CREATE_TABLE = 2; // Table Name NULL
122 SQLITE_CREATE_TEMP_INDEX = 3; // Index Name Table Name
123 SQLITE_CREATE_TEMP_TABLE = 4; // Table Name NULL
124 SQLITE_CREATE_TEMP_TRIGGER = 5; // Trigger Name Table Name
125 SQLITE_CREATE_TEMP_VIEW = 6; // View Name NULL
126 SQLITE_CREATE_TRIGGER = 7; // Trigger Name Table Name
127 SQLITE_CREATE_VIEW = 8; // View Name NULL
128 SQLITE_DELETE = 9; // Table Name NULL
129 SQLITE_DROP_INDEX = 10; // Index Name Table Name
130 SQLITE_DROP_TABLE = 11; // Table Name NULL
131 SQLITE_DROP_TEMP_INDEX = 12; // Index Name Table Name
132 SQLITE_DROP_TEMP_TABLE = 13; // Table Name NULL
133 SQLITE_DROP_TEMP_TRIGGER = 14; // Trigger Name Table Name
134 SQLITE_DROP_TEMP_VIEW = 15; // View Name NULL
135 SQLITE_DROP_TRIGGER = 16; // Trigger Name Table Name
136 SQLITE_DROP_VIEW = 17; // View Name NULL
137 SQLITE_INSERT = 18; // Table Name NULL
138 SQLITE_PRAGMA = 19; // Pragma Name 1st arg or NULL
139 SQLITE_READ = 20; // Table Name Column Name
140 SQLITE_SELECT = 21; // NULL NULL
141 SQLITE_TRANSACTION = 22; // NULL NULL
142 SQLITE_UPDATE = 23; // Table Name Column Name
143 SQLITE_ATTACH = 24; // Filename NULL
144 SQLITE_DETACH = 25; // Database Name NULL
146 { The return value of the authorization function should be one of the
147 following constants: }
148 SQLITE_DENY = 1; // Abort the SQL statement with an error
149 SQLITE_IGNORE = 2; // Don't allow access, but don't generate an error
159 Psqlite_func = Pointer;
160 Psqlite_vm = Pointer;
161 Psqlite3_stmt = Pointer;
162 Psqlite3_value = Pointer;
164 Tsqlite3_destructor_type = procedure(user: pointer); cdecl;
167 SQLITE_STATIC = procedure(User: Pointer = Nil); cdecl;
168 SQLITE_TRANSIENT = procedure(User: pointer = Pointer(-1)); cdecl;
171 { ************** Plain API Function types definition ************* }
173 Tsqlite_callback = function(p1: Pointer; p2: Integer; var p3: PAnsiChar;
174 var p4: PAnsiChar): Integer; cdecl;
175 Tsqlite_simple_callback = function(p1: Pointer): Integer; cdecl;
176 Tsqlite_simple_callback0 = function(p1: Pointer): Pointer; cdecl;
177 Tsqlite_busy_callback = function(p1: Pointer; const p2: PAnsiChar;
178 p3: Integer): Integer; cdecl;
180 Tsqlite_function_callback = procedure(p1: Psqlite_func; p2: Integer;
181 const p3: PPAnsiChar); cdecl;
182 Tsqlite_finalize_callback = procedure(p1: Psqlite_func); cdecl;
183 Tsqlite_auth_callback = function(p1: Pointer; p2: Integer; const p3: PAnsiChar;
184 const p4: PAnsiChar; const p5: PAnsiChar; const p6: PAnsiChar): Integer; cdecl;
185 Tsqlite_trace_callback = procedure(p1: Pointer; const p2: PAnsiChar); cdecl;
187 Tsqlite_open = function(const filename: PAnsiChar;var Qsqlite: Psqlite): Integer; cdecl;
188 Tsqlite_close = function(db: Psqlite): Integer; cdecl;
190 { prepared statment api}
191 Tsqlite3_prepare = function(
192 db: Psqlite; // Database handle
193 const zSql: PAnsiChar; // SQL statement, UTF-8 encoded
194 nBytes: Integer; // Maximum length of zSql in bytes. -1 = null terminated
195 out ppStmt: Psqlite3_stmt; // OUT: Statement handle
196 out pzTail: PPAnsichar // OUT: Pointer to unused portion of zSql
198 Tsqlite3_prepare_v2 = function(
199 db: Psqlite; // Database handle
200 const zSql: PAnsiChar; // SQL statement, UTF-8 encoded
201 nBytes: Integer; // Maximum length of zSql in bytes. -1 = null terminated
202 out ppStmt: Psqlite3_stmt; // OUT: Statement handle
203 out pzTail: PPAnsichar // OUT: Pointer to unused portion of zSql
205 Tsqlite3_prepare16 = function(
206 db: Psqlite; // Database handle
207 const zSql: PWideChar; // SQL statement, UTF-16 encoded
208 nBytes: Integer; // Maximum length of zSql in bytes. -1 = null terminated
209 out ppStmt: Psqlite3_stmt; // OUT: Statement handle
210 out pzTail: ZPPWideChar // OUT: Pointer to unused portion of zSql
212 Tsqlite3_prepare16_v2 = function(
213 db: Psqlite; // Database handle
214 const zSql: PWideChar; // SQL statement, UTF-16 encoded
215 nBytes: Integer; // Maximum length of zSql in bytes. -1 = null terminated
216 out ppStmt: Psqlite3_stmt; // OUT: Statement handle
217 out pzTail: ZPPWideChar // OUT: Pointer to unused portion of zSql
220 Tsqlite3_bind_parameter_count = function(pStmt: Psqlite3_stmt): Integer; cdecl;
221 Tsqlite3_bind_parameter_name = function(pStmt: Psqlite3_stmt; ParamIndex: Integer): PAnsichar; cdecl;
222 Tsqlite3_bind_parameter_index = function(pStmt: Psqlite3_stmt; const zName: PAnsiChar): Integer; cdecl;
224 Tsqlite3_clear_bindings = function(pStmt: Psqlite3_stmt): Integer; cdecl;
225 Tsqlite3_column_count = function(pStmt: Psqlite3_stmt): Integer; cdecl;
226 Tsqlite3_column_name = function(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar; cdecl;
227 Tsqlite3_column_name16 = function(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar; cdecl;
229 Tsqlite3_column_database_name = function(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar; cdecl;
230 Tsqlite3_column_database_name16 = function(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar; cdecl;
231 Tsqlite3_column_table_name = function(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar; cdecl;
232 Tsqlite3_column_table_name16 = function(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar; cdecl;
233 Tsqlite3_column_origin_name = function(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar; cdecl;
234 Tsqlite3_column_origin_name16 = function(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar; cdecl;
236 Tsqlite3_column_decltype = function(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar; cdecl;
237 Tsqlite3_column_decltype16 = function(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar; cdecl;
239 Tsqlite3_step = function(pStmt: Psqlite3_stmt): Integer; cdecl;
240 Tsqlite3_data_count = function (pStmt: Psqlite3_stmt): Integer; cdecl;
242 Tsqlite3_bind_blob = function(pStmt: Psqlite3_stmt; ParamIndex: Integer; const Buffer: Pointer; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer; cdecl;
243 Tsqlite3_bind_double = function(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Double): Integer; cdecl;
244 Tsqlite3_bind_int = function(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Integer): Integer; cdecl;
245 Tsqlite3_bind_int64 = function(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Int64): Integer; cdecl;
246 Tsqlite3_bind_null = function(pStmt: Psqlite3_stmt; ParamIndex: Integer): Integer; cdecl;
247 Tsqlite3_bind_text = function(pStmt: Psqlite3_stmt; ParamIndex: Integer; const Text: PAnsiChar; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer; cdecl;
248 Tsqlite3_bind_text16 = function(pStmt: Psqlite3_stmt; ParamIndex: Integer; const Text: PWideChar; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer; cdecl;
249 Tsqlite3_bind_value = function(pStmt: Psqlite3_stmt; ParamIndex: Integer; const Value: Psqlite3_value): Integer; cdecl;
250 Tsqlite3_bind_zeroblob = function(pStmt: Psqlite3_stmt; ParamIndex: Integer; N: Integer): Integer; cdecl;
252 Tsqlite3_finalize = function(pStmt: Psqlite3_stmt): Integer; cdecl;
253 Tsqlite3_reset = function(pStmt: Psqlite3_stmt): Integer; cdecl;
255 Tsqlite3_column_blob = function(Stmt: Psqlite3_stmt; iCol:integer): Pointer; cdecl;
256 Tsqlite3_column_bytes = function(Stmt: Psqlite3_stmt; iCol: Integer): integer; cdecl;
257 Tsqlite3_column_bytes16 = function(Stmt: Psqlite3_stmt; iCol: Integer): integer; cdecl;
258 Tsqlite3_column_double = function(Stmt: Psqlite3_stmt; iCol: Integer): Double; cdecl;
259 Tsqlite3_column_int = function(Stmt: Psqlite3_stmt; iCol: Integer): Integer; cdecl;
260 Tsqlite3_column_int64 = function(Stmt: Psqlite3_stmt; iCol: Integer): Int64; cdecl;
261 Tsqlite3_column_text = function(Stmt: Psqlite3_stmt; iCol: Integer): PAnsiChar; cdecl;
262 Tsqlite3_column_text16 = function(Stmt: Psqlite3_stmt; iCol: Integer): PWideChar; cdecl;
263 Tsqlite3_column_type = function(Stmt: Psqlite3_stmt; iCol: Integer): Integer; cdecl;
264 Tsqlite3_column_value = function(Stmt: Psqlite3_stmt; iCol: Integer): Psqlite3_value; cdecl;
265 Tsqlite3_last_insert_rowid = function(db: Psqlite): Int64; cdecl;
267 //gets the column type
268 Tsqlite_column_type = function(db:PSqlite;iCol:integer):Integer; cdecl;
270 Tsqlite_exec = function(db: Psqlite; const sql: PAnsiChar;
271 sqlite_callback: Tsqlite_callback; arg: Pointer;
272 var errmsg: PAnsiChar): Integer; cdecl;
273 Tsqlite_errmsg = function(db: Psqlite): PAnsiChar; cdecl;
274 Tsqlite_errstr = function(code: Integer): PAnsiChar; cdecl;
275 //Tsqlite_last_insert_rowid = function(db: Psqlite): Integer; cdecl;
276 Tsqlite_changes = function(db: Psqlite): Integer; cdecl;
277 Tsqlite_last_statement_changes = function(db: Psqlite): Integer; cdecl;
278 Tsqlite_interrupt = procedure(db: Psqlite); cdecl;
279 Tsqlite_complete = function(const sql: PAnsiChar): Integer; cdecl;
280 Tsqlite_busy_handler = procedure(db: Psqlite;
281 callback: Tsqlite_busy_callback; ptr: Pointer); cdecl;
282 Tsqlite_busy_timeout = procedure(db: Psqlite; ms: Integer); cdecl;
283 Tsqlite_get_table = function(db: Psqlite; const sql: PAnsiChar;
284 var resultp: PPAnsiChar; var nrow: Integer; var ncolumn: Integer;
285 var errmsg: PAnsiChar): Integer; cdecl;
286 Tsqlite_free_table = procedure(var result: PAnsiChar); cdecl;
287 Tsqlite_freemem = procedure(ptr: Pointer); cdecl;
288 Tsqlite_libversion = function: PAnsiChar; cdecl;
289 Tsqlite_libencoding = function: PAnsiChar; cdecl;
291 Tsqlite_create_function = function(db: Psqlite; const zName: PAnsiChar;
292 nArg: Integer; callback: Tsqlite_function_callback;
293 pUserData: Pointer): Integer; cdecl;
294 Tsqlite_create_aggregate = function(db: Psqlite; const zName: PAnsiChar;
295 nArg: Integer; callback: Tsqlite_function_callback;
296 finalize: Tsqlite_finalize_callback; pUserData: Pointer): Integer; cdecl;
297 Tsqlite_function_type = function(db: Psqlite; const zName: PAnsiChar;
298 datatype: Integer): Integer; cdecl;
300 Tsqlite_set_result_string = function(func: Psqlite_func; const arg: PAnsiChar;
301 len: Integer; UN: Tsqlite_simple_callback): PAnsiChar; cdecl;
303 Tsqlite_set_result_int = procedure(func: Psqlite_func; arg: Integer); cdecl;
304 Tsqlite_set_result_double = procedure(func: Psqlite_func; arg: Double); cdecl;
305 Tsqlite_set_result_error = procedure(func: Psqlite_func; const arg: PAnsiChar;
306 len: Integer); cdecl;
307 Tsqlite_user_data = function(func: Psqlite_func): Pointer; cdecl;
308 Tsqlite_aggregate_context = function(func: Psqlite_func;
309 nBytes: Integer): Pointer; cdecl;
310 Tsqlite_aggregate_count = function(func: Psqlite_func): Integer; cdecl;
312 Tsqlite_set_authorizer = function(db: Psqlite;
313 callback: Tsqlite_auth_callback; pUserData: Pointer): Integer; cdecl;
314 Tsqlite_trace = function(db: Psqlite; callback: Tsqlite_trace_callback;
315 ptr: Pointer): Pointer; cdecl;
317 Tsqlite_progress_handler = procedure(db: Psqlite; p1: Integer;
318 callback: Tsqlite_simple_callback; ptr: Pointer); cdecl;
319 Tsqlite_commit_hook = function(db: Psqlite; callback: Tsqlite_simple_callback;
320 ptr: Pointer): Pointer; cdecl;
322 Tsqlite_open_encrypted = function(const zFilename: PAnsiChar;
323 const pKey: PAnsiChar; nKey: Integer; var pErrcode: Integer;
324 var pzErrmsg: PAnsiChar): Psqlite; cdecl;
325 Tsqlite_rekey = function(db: Psqlite; const pKey: Pointer;
326 nKey: Integer): Integer; cdecl;
327 Tsqlite_key = function(db: Psqlite; const pKey: Pointer;
328 nKey: Integer): Integer; cdecl;
330 { ************* Plain API Function variables definition ************ }
331 TZSQLite_API = record
332 sqlite_open: Tsqlite_open;
333 sqlite_close: Tsqlite_close;
335 { prepared statement api }
336 sqlite_prepare: Tsqlite3_prepare;
337 sqlite_prepare_v2: Tsqlite3_prepare_v2;
338 sqlite_prepare16: Tsqlite3_prepare16;
339 sqlite_prepare16_v2: Tsqlite3_prepare16_v2;
341 sqlite_bind_parameter_count: Tsqlite3_bind_parameter_count;
342 sqlite_bind_parameter_name: Tsqlite3_bind_parameter_name;
343 sqlite_bind_parameter_index: Tsqlite3_bind_parameter_index;
345 sqlite_clear_bindings: Tsqlite3_clear_bindings;
346 sqlite_column_count: Tsqlite3_column_count;
347 sqlite_column_name: Tsqlite3_column_name;
348 sqlite_column_name16: Tsqlite3_column_name16;
350 sqlite_column_database_name: Tsqlite3_column_database_name;
351 sqlite_column_database_name16: Tsqlite3_column_database_name16;
352 sqlite_column_table_name: Tsqlite3_column_table_name;
353 sqlite_column_table_name16: Tsqlite3_column_table_name16;
354 sqlite_column_origin_name: Tsqlite3_column_origin_name;
355 sqlite_column_origin_name16: Tsqlite3_column_origin_name16;
357 sqlite_column_decltype: Tsqlite3_column_decltype;
358 sqlite_column_decltype16: Tsqlite3_column_decltype16;
360 sqlite_step: Tsqlite3_step;
361 sqlite_data_count: Tsqlite3_data_count;
363 sqlite_bind_blob: Tsqlite3_bind_blob;
364 sqlite_bind_double: Tsqlite3_bind_double;
365 sqlite_bind_int: Tsqlite3_bind_int;
366 sqlite_bind_int64: Tsqlite3_bind_int64;
367 sqlite_bind_null: Tsqlite3_bind_null;
368 sqlite_bind_text: Tsqlite3_bind_text;
369 sqlite_bind_text16: Tsqlite3_bind_text16;
370 sqlite_bind_value: Tsqlite3_bind_value;
371 sqlite_bind_zeroblob: Tsqlite3_bind_zeroblob;
373 sqlite_finalize: Tsqlite3_finalize;
374 sqlite_reset: Tsqlite3_reset;
376 sqlite_column_blob: Tsqlite3_column_blob;
377 sqlite_column_bytes: Tsqlite3_column_bytes;
378 sqlite_column_bytes16: Tsqlite3_column_bytes16;
379 sqlite_column_double: Tsqlite3_column_double;
380 sqlite_column_int: Tsqlite3_column_int;
381 sqlite_column_int64: Tsqlite3_column_int64;
382 sqlite_column_text: Tsqlite3_column_text;
383 sqlite_column_text16: Tsqlite3_column_text16;
384 sqlite_column_type: Tsqlite3_column_type;
385 sqlite_column_value: Tsqlite3_column_value;
387 sqlite_exec: Tsqlite_exec;
388 sqlite_errmsg: Tsqlite_errmsg;
389 sqlite_errstr: Tsqlite_errstr;
390 sqlite_last_insert_rowid: Tsqlite3_last_insert_rowid;
391 sqlite_changes: Tsqlite_changes;
392 sqlite_last_statement_changes: Tsqlite_last_statement_changes;
393 sqlite_interrupt: Tsqlite_interrupt;
394 sqlite_complete: Tsqlite_complete;
395 sqlite_busy_handler: Tsqlite_busy_handler;
396 sqlite_busy_timeout: Tsqlite_busy_timeout;
397 sqlite_get_table: Tsqlite_get_table;
398 sqlite_free_table: Tsqlite_free_table;
399 sqlite_freemem: Tsqlite_freemem;
400 sqlite_libversion: Tsqlite_libversion;
401 sqlite_libencoding: Tsqlite_libencoding;
402 sqlite_create_function: Tsqlite_create_function;
403 sqlite_create_aggregate: Tsqlite_create_aggregate;
404 sqlite_function_type: Tsqlite_function_type;
405 sqlite_set_result_string: Tsqlite_set_result_string;
406 sqlite_set_result_int: Tsqlite_set_result_int;
407 sqlite_set_result_double: Tsqlite_set_result_double;
408 sqlite_set_result_error: Tsqlite_set_result_error;
409 sqlite_user_data: Tsqlite_user_data;
410 sqlite_aggregate_context: Tsqlite_aggregate_context;
411 sqlite_aggregate_count: Tsqlite_aggregate_count;
412 sqlite_set_authorizer: Tsqlite_set_authorizer;
413 sqlite_trace: Tsqlite_trace;
414 sqlite_progress_handler: Tsqlite_progress_handler;
415 sqlite_commit_hook: Tsqlite_commit_hook;
416 sqlite_open_encrypted: Tsqlite_open_encrypted;
417 sqlite_rekey: Tsqlite_rekey;
418 sqlite_key: Tsqlite_key;
423 {** Represents a generic interface to SQLite native API. }
424 IZSQLitePlainDriver = interface (IZPlainDriver)
425 ['{B931C952-3076-4ECB-9630-D900E8DB9869}']
427 function Open(const filename: PAnsiChar; mode: Integer;
428 var errmsg: PAnsiChar): Psqlite;
429 function Close(db: Psqlite): Integer;
430 function Execute(db: Psqlite; const sql: PAnsiChar;
431 sqlite_callback: Tsqlite_callback; arg: Pointer;
432 var errmsg: PAnsiChar): Integer;
433 function LastInsertRowId(db: Psqlite): Int64;
434 function Changes(db: Psqlite): Integer;
435 function LastStatementChanges(db: Psqlite): Integer;
436 function ErrorString(db: Psqlite; code: Integer): String;
437 procedure Interrupt(db: Psqlite);
438 function Complete(const sql: PAnsiChar): Integer;
440 procedure BusyHandler(db: Psqlite; callback: Tsqlite_busy_callback;
442 procedure BusyTimeout(db: Psqlite; ms: Integer);
444 function GetTable(db: Psqlite; const sql: PAnsiChar; var resultp: PPAnsiChar;
445 var nrow: Integer; var ncolumn: Integer; var errmsg: PAnsiChar): Integer;
446 procedure FreeTable(var result: PAnsiChar);
447 procedure FreeMem(ptr: Pointer);
448 function LibVersion: PAnsiChar;
449 function LibEncoding: PAnsiChar;
451 function CreateFunction(db: Psqlite; const zName: PAnsiChar;
452 nArg: Integer; callback: Tsqlite_function_callback;
453 pUserData: Pointer): Integer;
454 function CreateAggregate(db: Psqlite; const zName: PAnsiChar;
455 nArg: Integer; callback: Tsqlite_function_callback;
456 finalize: Tsqlite_finalize_callback; pUserData: Pointer): Integer;
457 function FunctionType(db: Psqlite; const zName: PAnsiChar;
458 datatype: Integer): Integer;
459 function SetResultString(func: Psqlite_func; const arg: PAnsiChar;
460 len: Integer): PAnsiChar;
461 procedure SetResultInt(func: Psqlite_func; arg: Integer);
462 procedure SetResultDouble(func: Psqlite_func; arg: Double);
463 procedure SetResultError(func: Psqlite_func; const arg: PAnsiChar; len: Integer);
464 function UserData(func: Psqlite_func): Pointer;
465 function AggregateContext(func: Psqlite_func; nBytes: Integer): Pointer;
466 function AggregateCount(func: Psqlite_func): Integer;
468 function SetAuthorizer(db: Psqlite; callback: Tsqlite_auth_callback;
469 pUserData: Pointer): Integer;
470 function Trace(db: Psqlite; callback: Tsqlite_trace_callback;
471 ptr: Pointer): Pointer;
473 { Prepared statmenet api }
474 function Prepare(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
475 out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
476 function Prepare_v2(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
477 out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
478 function Prepare16(db: Psqlite; const zSql: PWideChar; nBytes: Integer;
479 out ppStmt: Psqlite3_stmt; pzTail: ZPPWideChar): Integer;
480 function Prepare16_v2(db: Psqlite; const zSql: PWideChar; nBytes: Integer;
481 out ppStmt: Psqlite3_stmt; pzTail: ZPPWideChar): Integer;
483 function bind_parameter_count(pStmt: Psqlite3_stmt): Integer;
484 function bind_parameter_name(pStmt: Psqlite3_stmt; ParamIndex: Integer): PAnsichar;
485 function bind_parameter_index(pStmt: Psqlite3_stmt; const zName: PAnsiChar): Integer;
487 function clear_bindings(pStmt: Psqlite3_stmt): Integer;
488 function column_count(pStmt: Psqlite3_stmt): Integer;
489 function column_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
490 function column_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
492 function column_database_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
493 function column_database_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
494 function column_table_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
495 function column_table_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
496 function column_origin_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
497 function column_origin_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
499 function column_decltype(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
500 function column_decltype16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
502 function Step(Stmt: Psqlite3_stmt; var pN: Integer;
503 var pazValue, pazColName: PPAnsiChar): Integer; overload;
504 function Step(Stmt: Psqlite3_stmt): Integer; overload;
505 function data_count(pStmt: Psqlite3_stmt): Integer;
507 function bind_blob(pStmt: Psqlite3_stmt; ParamIndex: Integer;
508 const Buffer: Pointer; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer;
509 function bind_double(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Double): Integer;
510 function bind_int(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Integer): Integer;
511 function bind_int64(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Int64): Integer;
512 function bind_null(pStmt: Psqlite3_stmt; ParamIndex: Integer): Integer;
513 function bind_text(pStmt: Psqlite3_stmt; ParamIndex: Integer;
514 const Text: PAnsiChar; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer;
515 function bind_text16(pStmt: Psqlite3_stmt; ParamIndex: Integer;
516 const Text: PWideChar; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer;
517 function bind_value(pStmt: Psqlite3_stmt; ParamIndex: Integer; const Value: Psqlite3_value): Integer;
518 function bind_zeroblob(pStmt: Psqlite3_stmt; ParamIndex: Integer; N: Integer): Integer;
520 function finalize(pStmt: Psqlite3_stmt): Integer;
521 function reset(pStmt: Psqlite3_stmt): Integer;
523 function column_blob(Stmt: Psqlite3_stmt; iCol:integer): TStream;
524 function column_bytes(Stmt: Psqlite3_stmt; iCol: Integer): integer;
525 function column_bytes16(Stmt: Psqlite3_stmt; iCol: Integer): integer;
526 function column_double(Stmt: Psqlite3_stmt; iCol: Integer): Double;
527 function column_int(Stmt: Psqlite3_stmt; iCol: Integer): Integer;
528 function column_int64(Stmt: Psqlite3_stmt; iCol: Integer): Int64;
529 function column_text(Stmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
530 function column_text16(Stmt: Psqlite3_stmt; iCol: Integer): PWideChar;
531 function column_type(Stmt: Psqlite3_stmt; iCol: Integer): String;
532 function column_value(Stmt: Psqlite3_stmt; iCol: Integer): Psqlite3_value;
534 procedure ProgressHandler(db: Psqlite; p1: Integer;
535 callback: Tsqlite_simple_callback; ptr: Pointer);
536 function CommitHook(db: Psqlite; callback: Tsqlite_simple_callback;
537 ptr: Pointer): Pointer;
539 function OpenEncrypted(const zFilename: PAnsiChar; const pKey: PAnsiChar;
540 nKey: Integer; var pErrcode: Integer; var pzErrmsg: PAnsiChar): Psqlite;
541 function ReKey(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
542 function Key(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
545 {** Implements a base driver for SQLite}
546 TZSQLiteBaseDriver = class (TZAbstractPlainDriver, IZPlainDriver, IZSQLitePlainDriver)
548 SQLite_API : TZSQLite_API;
549 // procedure LoadApi; override; ->completely done in version dependent child classes
550 function GetUnicodeCodePageName: String; override;
551 procedure LoadCodePages; override;
555 function Open(const filename: PAnsiChar; mode: Integer;
556 var errmsg: PAnsiChar): Psqlite;
557 function Close(db: Psqlite): Integer;
558 function Execute(db: Psqlite; const sql: PAnsiChar;
559 sqlite_callback: Tsqlite_callback; arg: Pointer;
560 var errmsg: PAnsiChar): Integer;
561 function LastInsertRowId(db: Psqlite): Int64;
562 function Changes(db: Psqlite): Integer;
563 function LastStatementChanges(db: Psqlite): Integer;
564 function ErrorString(db: Psqlite; code: Integer): String;
565 procedure Interrupt(db: Psqlite);
566 function Complete(const sql: PAnsiChar): Integer;
568 procedure BusyHandler(db: Psqlite; callback: Tsqlite_busy_callback;
570 procedure BusyTimeout(db: Psqlite; ms: Integer);
572 function GetTable(db: Psqlite; const sql: PAnsiChar; var resultp: PPAnsiChar;
573 var nrow: Integer; var ncolumn: Integer; var errmsg: PAnsiChar): Integer;
574 procedure FreeTable(var result: PAnsiChar);
575 procedure FreeMem(ptr: Pointer);
576 function LibVersion: PAnsiChar;
577 function LibEncoding: PAnsiChar;
579 function CreateFunction(db: Psqlite; const zName: PAnsiChar;
580 nArg: Integer; callback: Tsqlite_function_callback;
581 pUserData: Pointer): Integer; virtual;
582 function CreateAggregate(db: Psqlite; const zName: PAnsiChar;
583 nArg: Integer; callback: Tsqlite_function_callback;
584 finalize: Tsqlite_finalize_callback; pUserData: Pointer): Integer;
585 function FunctionType(db: Psqlite; const zName: PAnsiChar;
586 datatype: Integer): Integer;
587 function SetResultString(func: Psqlite_func; const arg: PAnsiChar;
588 len: Integer): PAnsiChar;
589 procedure SetResultInt(func: Psqlite_func; arg: Integer);
590 procedure SetResultDouble(func: Psqlite_func; arg: Double);
591 procedure SetResultError(func: Psqlite_func; const arg: PAnsiChar; len: Integer);
592 function UserData(func: Psqlite_func): Pointer;
593 function AggregateContext(func: Psqlite_func; nBytes: Integer): Pointer;
594 function AggregateCount(func: Psqlite_func): Integer;
596 function SetAuthorizer(db: Psqlite; callback: Tsqlite_auth_callback;
597 pUserData: Pointer): Integer;
598 function Trace(db: Psqlite; callback: Tsqlite_trace_callback;
599 ptr: Pointer): Pointer;
601 { Prepared statmenet api }
602 function Prepare(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
603 out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
604 function Prepare_v2(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
605 out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
606 function Prepare16(db: Psqlite; const zSql: PWideChar; nBytes: Integer;
607 out ppStmt: Psqlite3_stmt; pzTail: ZPPWideChar): Integer;
608 function Prepare16_v2(db: Psqlite; const zSql: PWideChar; nBytes: Integer;
609 out ppStmt: Psqlite3_stmt; pzTail: ZPPWideChar): Integer;
611 function bind_parameter_count(pStmt: Psqlite3_stmt): Integer;
612 function bind_parameter_name(pStmt: Psqlite3_stmt; ParamIndex: Integer): PAnsichar;
613 function bind_parameter_index(pStmt: Psqlite3_stmt; const zName: PAnsiChar): Integer;
615 function clear_bindings(pStmt: Psqlite3_stmt): Integer;
616 function column_count(pStmt: Psqlite3_stmt): Integer;
617 function column_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
618 function column_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
620 function column_database_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
621 function column_database_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
622 function column_table_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
623 function column_table_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
624 function column_origin_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
625 function column_origin_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
627 function column_decltype(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
628 function column_decltype16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
630 function Step(Stmt: Psqlite3_stmt; var pN: Integer;
631 var pazValue, pazColName: PPAnsiChar): Integer; overload;
632 function Step(Stmt: Psqlite3_stmt): Integer; overload;
633 function data_count(pStmt: Psqlite3_stmt): Integer;
635 function bind_blob(pStmt: Psqlite3_stmt; ParamIndex: Integer;
636 const Buffer: Pointer; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer;
637 function bind_double(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Double): Integer;
638 function bind_int(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Integer): Integer;
639 function bind_int64(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Int64): Integer;
640 function bind_null(pStmt: Psqlite3_stmt; ParamIndex: Integer): Integer;
641 function bind_text(pStmt: Psqlite3_stmt; ParamIndex: Integer; const Text: PAnsiChar; N: Integer;
642 ValDestructor: Tsqlite3_destructor_type): Integer;
643 function bind_text16(pStmt: Psqlite3_stmt; ParamIndex: Integer; const Text: PWideChar; N: Integer;
644 ValDestructor: Tsqlite3_destructor_type): Integer;
645 function bind_value(pStmt: Psqlite3_stmt; ParamIndex: Integer; const Value: Psqlite3_value): Integer;
646 function bind_zeroblob(pStmt: Psqlite3_stmt; ParamIndex: Integer; N: Integer): Integer;
648 function finalize(pStmt: Psqlite3_stmt): Integer;
649 function reset(pStmt: Psqlite3_stmt): Integer;
651 function column_blob(Stmt: Psqlite3_stmt; iCol:integer): TStream;
652 function column_bytes(Stmt: Psqlite3_stmt; iCol: Integer): integer;
653 function column_bytes16(Stmt: Psqlite3_stmt; iCol: Integer): integer;
654 function column_double(Stmt: Psqlite3_stmt; iCol: Integer): Double;
655 function column_int(Stmt: Psqlite3_stmt; iCol: Integer): Integer;
656 function column_int64(Stmt: Psqlite3_stmt; iCol: Integer): Int64;
657 function column_text(Stmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
658 function column_text16(Stmt: Psqlite3_stmt; iCol: Integer): PWideChar;
659 function column_type(Stmt: Psqlite3_stmt; iCol: Integer): String;
660 function column_value(Stmt: Psqlite3_stmt; iCol: Integer): Psqlite3_value;
662 procedure ProgressHandler(db: Psqlite; p1: Integer;
663 callback: Tsqlite_simple_callback; ptr: Pointer);
664 function CommitHook(db: Psqlite; callback: Tsqlite_simple_callback;
665 ptr: Pointer): Pointer;
667 function OpenEncrypted(const zFilename: PAnsiChar; const pKey: PAnsiChar;
668 nKey: Integer; var pErrcode: Integer; var pzErrmsg: PAnsiChar): Psqlite;
669 function ReKey(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
670 function Key(db: Psqlite; const pKey: Pointer; nKey: Integer): Integer;
674 {** Implements a driver for SQLite 3 }
675 TZSQLite3PlainDriver = class (TZSQLiteBaseDriver, IZPlainDriver, IZSQLitePlainDriver)
677 function Clone: IZPlainDriver; override;
678 procedure LoadApi; override;
681 function GetProtocol: string; override;
682 function GetDescription: string; override;
687 uses ZPlainLoader, ZEncoding;
689 { TZSQLiteBaseDriver }
691 function TZSQLiteBaseDriver.GetUnicodeCodePageName: String;
696 procedure TZSQLiteBaseDriver.LoadCodePages; //Egonhugeist
699 AddCodePage('UTF-8', 1, ceUTF8, zCP_UTF8);
700 AddCodePage('UTF-16le', 2, ceUTF16, zCP_UTF16, 'UTF-8'); //Setting this will be ignored by actual Excute of Plaindriver
701 AddCodePage('UTF-16be', 3, ceUTF16, zCP_UTF16BE, 'UTF-8'); //Setting this will be ignored by actual Excute of Plaindriver
702 AddCodePage('UTF-16', 4, ceUTF16, zCP_UTF16, 'UTF-8'); //Setting this will be ignored by actual Excute of Plaindriver
705 constructor TZSQLiteBaseDriver.Create;
708 FLoader := TZNativeLibraryLoader.Create([]);
710 FLoader.AddLocation(WINDOWS_DLL_LOCATION);
712 FLoader.AddLocation(LINUX_DLL_LOCATION);
713 FLoader.AddLocation(LINUX_DLL_LOCATION+'.0');
717 function TZSQLiteBaseDriver.AggregateContext(func: Psqlite_func;
718 nBytes: Integer): Pointer;
720 Result := SQLite_API.sqlite_aggregate_context(func, nBytes);
723 function TZSQLiteBaseDriver.AggregateCount(func: Psqlite_func): Integer;
725 Result := SQLite_API.sqlite_aggregate_count(func);
728 procedure TZSQLiteBaseDriver.BusyHandler(db: Psqlite;
729 callback: Tsqlite_busy_callback; ptr: Pointer);
731 SQLite_API.sqlite_busy_handler(db, callback, ptr);
734 procedure TZSQLiteBaseDriver.BusyTimeout(db: Psqlite; ms: Integer);
736 SQLite_API.sqlite_busy_timeout(db, ms);
739 function TZSQLiteBaseDriver.Changes(db: Psqlite): Integer;
741 Result := SQLite_API.sqlite_changes(db);
744 function TZSQLiteBaseDriver.CommitHook(db: Psqlite;
745 callback: Tsqlite_simple_callback; ptr: Pointer): Pointer;
747 Result := SQLite_API.sqlite_commit_hook(db, callback, ptr);
750 function TZSQLiteBaseDriver.Complete(const sql: PAnsiChar): Integer;
752 Result := SQLite_API.sqlite_complete(sql);
755 function TZSQLiteBaseDriver.CreateAggregate(db: Psqlite;
756 const zName: PAnsiChar; nArg: Integer; callback: Tsqlite_function_callback;
757 finalize: Tsqlite_finalize_callback; pUserData: Pointer): Integer;
759 Result := SQLITE_MISUSE;
762 function TZSQLiteBaseDriver.CreateFunction(db: Psqlite;
763 const zName: PAnsiChar; nArg: Integer; callback: Tsqlite_function_callback;
764 pUserData: Pointer): Integer;
766 Result := SQLITE_MISUSE;
769 function TZSQLiteBaseDriver.ErrorString(db: Psqlite; code: Integer): String;
771 ErrorMessagePointer: PAnsiChar;
772 ErrorMessage: String;
775 if code = SQLITE_OK then
777 Result := 'not an error';
781 if code = SQLITE_NOMEM then
783 Result := 'out of memory';
787 if ( db = nil ) or ( @SQLite_API.sqlite_errstr = nil ) then
790 SQLITE_OK: Result := 'not an error';
791 SQLITE_ERROR: Result := 'SQL logic error or missing database';
792 SQLITE_INTERNAL: Result := 'internal SQLite implementation flaw';
793 SQLITE_PERM: Result := 'access permission denied';
794 SQLITE_ABORT: Result := 'callback requested query abort';
795 SQLITE_BUSY: Result := 'database is locked';
796 SQLITE_LOCKED: Result := 'database table is locked';
797 SQLITE_NOMEM: Result := 'out of memory';
798 SQLITE_READONLY: Result := 'attempt to write a readonly database';
799 _SQLITE_INTERRUPT: Result := 'interrupted';
800 SQLITE_IOERR: Result := 'disk I/O error';
801 SQLITE_CORRUPT: Result := 'database disk image is malformed';
802 SQLITE_NOTFOUND: Result := 'table or record not found';
803 SQLITE_FULL: Result := 'database is full';
804 SQLITE_CANTOPEN: Result := 'unable to open database file';
805 SQLITE_PROTOCOL: Result := 'database locking protocol failure';
806 SQLITE_EMPTY: Result := 'table contains no data';
807 SQLITE_SCHEMA: Result := 'database schema has changed';
808 SQLITE_TOOBIG: Result := 'too much data for one table row';
809 SQLITE_CONSTRAINT: Result := 'constraint failed';
810 SQLITE_MISMATCH: Result := 'datatype mismatch';
811 SQLITE_MISUSE: Result := 'library routine called out of sequence';
812 SQLITE_NOLFS: Result := 'kernel lacks large file support';
813 SQLITE_AUTH: Result := 'authorization denied';
814 SQLITE_FORMAT: Result := 'auxiliary database format error';
815 SQLITE_RANGE: Result := 'bind index out of range';
816 SQLITE_NOTADB: Result := 'file is encrypted or is not a database';
818 Result := 'unknown error';
825 ErrorMessagePointer := Self.SQLite_API.sqlite_errstr(code);
827 ErrorString := Trim(UTF8ToUnicodeString(ErrorMessagePointer));
830 ErrorString := Trim(UTF8ToAnsi(StrPas(ErrorMessagePointer)));
832 ErrorString := Trim(ErrorMessagePointer);
836 ErrorMessagePointer := Self.SQLite_API.sqlite_errmsg(db);
838 ErrorMessage := Trim(UTF8ToUnicodeString(ErrorMessagePointer));
841 ErrorMessage := Trim(UTF8ToAnsi(ErrorMessagePointer));
843 ErrorMessage := Trim(ErrorMessagePointer);
847 Result := ErrorString + ': ' + ErrorMessage;
851 function TZSQLiteBaseDriver.Execute(db: Psqlite; const sql: PAnsiChar;
852 sqlite_callback: Tsqlite_callback; arg: Pointer;
853 var errmsg: PAnsiChar): Integer;
856 Result := SQLite_API.sqlite_exec(db, sql, sqlite_callback, arg, errmsg);
859 procedure TZSQLiteBaseDriver.FreeMem(ptr: Pointer);
861 SQLite_API.sqlite_freemem(ptr);
864 procedure TZSQLiteBaseDriver.FreeTable(var result: PAnsiChar);
866 SQLite_API.sqlite_free_table(result);
869 function TZSQLiteBaseDriver.FunctionType(db: Psqlite;
870 const zName: PAnsiChar; datatype: Integer): Integer;
872 Result := SQLITE_MISUSE;
875 function TZSQLiteBaseDriver.GetTable(db: Psqlite; const sql: PAnsiChar;
876 var resultp: PPAnsiChar; var nrow, ncolumn: Integer;
877 var errmsg: PAnsiChar): Integer;
879 Result := SQLite_API.sqlite_get_table(db, sql, resultp, nrow, ncolumn,
883 procedure TZSQLiteBaseDriver.Interrupt(db: Psqlite);
885 SQLite_API.sqlite_interrupt(db);
888 function TZSQLiteBaseDriver.LastInsertRowId(db: Psqlite): Int64;
890 Result := SQLite_API.sqlite_last_insert_rowid(db);
893 function TZSQLiteBaseDriver.LastStatementChanges(db: Psqlite): Integer;
895 Result := SQLITE_MISUSE;
898 function TZSQLiteBaseDriver.LibEncoding: PAnsiChar;
903 function TZSQLiteBaseDriver.LibVersion: PAnsiChar;
905 Result := SQLite_API.sqlite_libversion;
908 function TZSQLiteBaseDriver.Open(const filename: PAnsiChar; mode: Integer;
909 var errmsg: PAnsiChar): Psqlite;
914 FileNameString: String;
918 (*Note to Windows users: The encoding used for the filename argument of
919 sqlite3_open() and sqlite3_open_v2() must be UTF-8, not whatever codepage
920 is currently defined. Filenames containing international characters must
921 be converted to UTF-8 prior to passing them into sqlite3_open() or
922 sqlite3_open_v2(). *)
925 SQLite_API.sqlite_open(filename, Result0);
927 Version := LibVersion;
928 FileNameString := filename;
929 if (Version > '3.2.5') then
931 SQLite_API.sqlite_open(PAnsiChar(FileNameString), Result0)
933 SQLite_API.sqlite_open(PAnsiChar(AnsiToUTF8(FileNameString)), Result0)
936 SQLite_API.sqlite_open(filename, Result0);
941 function TZSQLiteBaseDriver.OpenEncrypted(const zFilename, pKey: PAnsiChar;
942 nKey: Integer; var pErrcode: Integer; var pzErrmsg: PAnsiChar): Psqlite;
944 pErrcode := SQLITE_MISUSE;
945 pzErrmsg := 'function is not used in the current version of the library';
949 procedure TZSQLiteBaseDriver.ProgressHandler(db: Psqlite; p1: Integer;
950 callback: Tsqlite_simple_callback; ptr: Pointer);
952 SQLite_API.sqlite_progress_handler(db, p1, callback, ptr);
955 function TZSQLiteBaseDriver.ReKey(db: Psqlite; const pKey: Pointer;
956 nKey: Integer): Integer;
958 if @SQLite_API.sqlite_rekey = nil then
964 Result := SQLite_API.sqlite_rekey(db, pKey, nKey);
968 function TZSQLiteBaseDriver.Key(db: Psqlite; const pKey: Pointer;
969 nKey: Integer): Integer;
971 if @SQLite_API.sqlite_key = nil then
977 Result := SQLite_API.sqlite_key(db, pKey, nKey);
981 function TZSQLiteBaseDriver.SetAuthorizer(db: Psqlite;
982 callback: Tsqlite_auth_callback; pUserData: Pointer): Integer;
984 Result := SQLite_API.sqlite_set_authorizer(db, callback, pUserData);
987 procedure TZSQLiteBaseDriver.SetResultDouble(func: Psqlite_func;
990 SQLite_API.sqlite_set_result_double(func, arg);
993 procedure TZSQLiteBaseDriver.SetResultError(func: Psqlite_func;
994 const arg: PAnsiChar; len: Integer);
996 SQLite_API.sqlite_set_result_error(func, arg, len);
999 procedure TZSQLiteBaseDriver.SetResultInt(func: Psqlite_func;
1002 SQLite_API.sqlite_set_result_int(func, arg);
1005 function TZSQLiteBaseDriver.SetResultString(func: Psqlite_func;
1006 const arg: PAnsiChar; len: Integer): PAnsiChar;
1008 Result := SQLite_API.sqlite_set_result_string(func, arg, len, nil);
1011 { Prepared statmenet api }
1012 function TZSQLiteBaseDriver.Prepare(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
1013 out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
1015 Result := SQLite_API.sqlite_prepare(db, zSql, nBytes, ppStmt, pzTail);
1018 function TZSQLiteBaseDriver.Prepare_v2(db: Psqlite; const zSql: PAnsiChar; nBytes: Integer;
1019 out ppStmt: Psqlite3_stmt; pzTail: PPAnsichar): Integer;
1021 Result := SQLite_API.sqlite_prepare_v2(db, zSql, nBytes, ppStmt, pzTail);
1024 function TZSQLiteBaseDriver.Prepare16(db: Psqlite; const zSql: PWideChar; nBytes: Integer;
1025 out ppStmt: Psqlite3_stmt; pzTail: ZPPWideChar): Integer;
1027 Result := SQLite_API.sqlite_prepare16(db, zSql, nBytes, ppStmt, pzTail);
1030 function TZSQLiteBaseDriver.Prepare16_v2(db: Psqlite; const zSql: PWideChar; nBytes: Integer;
1031 out ppStmt: Psqlite3_stmt; pzTail: ZPPWideChar): Integer;
1033 Result := SQLite_API.sqlite_prepare16_v2(db, zSql, nBytes, ppStmt, pzTail);
1036 function TZSQLiteBaseDriver.bind_parameter_count(pStmt: Psqlite3_stmt): Integer;
1038 Result := SQLite_API.sqlite_bind_parameter_count(pStmt);
1041 function TZSQLiteBaseDriver.bind_parameter_name(pStmt: Psqlite3_stmt; ParamIndex: Integer): PAnsichar;
1043 Result := SQLite_API.sqlite_bind_parameter_name(pStmt, ParamIndex);
1046 function TZSQLiteBaseDriver.bind_parameter_index(pStmt: Psqlite3_stmt; const zName: PAnsiChar): Integer;
1048 Result := SQLite_API.sqlite_bind_parameter_index(pStmt, ZName);
1051 function TZSQLiteBaseDriver.clear_bindings(pStmt: Psqlite3_stmt): Integer;
1053 Result := SQLite_API.sqlite_clear_bindings(pStmt);
1056 function TZSQLiteBaseDriver.column_count(pStmt: Psqlite3_stmt): Integer;
1058 Result := SQLite_API.sqlite_column_count(pStmt);
1061 function TZSQLiteBaseDriver.column_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
1063 Result := SQLite_API.sqlite_column_name(pStmt, iCol);
1066 function TZSQLiteBaseDriver.column_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
1068 Result := SQLite_API.sqlite_column_name16(pStmt, iCol);
1071 function TZSQLiteBaseDriver.column_database_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
1073 Result := SQLite_API.sqlite_column_database_name(pStmt, iCol);
1076 function TZSQLiteBaseDriver.column_database_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
1078 Result := SQLite_API.sqlite_column_database_name16(pStmt, iCol);
1081 function TZSQLiteBaseDriver.column_table_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
1083 Result := SQLite_API.sqlite_column_table_name(pStmt, iCol);
1086 function TZSQLiteBaseDriver.column_table_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
1088 Result := SQLite_API.sqlite_column_table_name16(pStmt, iCol);
1091 function TZSQLiteBaseDriver.column_origin_name(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
1093 Result := SQLite_API.sqlite_column_origin_name(pStmt, iCol);
1096 function TZSQLiteBaseDriver.column_origin_name16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
1098 Result := SQLite_API.sqlite_column_origin_name16(pStmt, iCol);
1101 function TZSQLiteBaseDriver.column_decltype(pStmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
1103 Result := SQLite_API.sqlite_column_decltype(pStmt, iCol);
1106 function TZSQLiteBaseDriver.column_decltype16(pStmt: Psqlite3_stmt; iCol: Integer): PWideChar;
1108 Result := SQLite_API.sqlite_column_decltype16(pStmt, iCol);
1111 function TZSQLiteBaseDriver.Step(Stmt: Psqlite3_stmt; var pN: Integer;
1112 var pazValue, pazColName: PPAnsiChar): Integer;
1115 val, cname,ctype: PAnsiChar;
1116 pazValue0, pazColName0, pazColType: PPAnsiChar;
1118 pazValue0 := nil; // satisfy compiler
1119 Result := SQLite_API.sqlite_step(Stmt);
1120 if (Result = SQLITE_ROW) or (Result = SQLITE_DONE) then
1122 pN:= SQLite_API.sqlite_column_count(Stmt);
1123 if Result = SQLITE_ROW then
1125 pazValue:= AllocMem(SizeOf(PPAnsiChar)*(pN+1));
1126 pazValue0:= pazValue;
1128 pazColName:= AllocMem(SizeOf(PPAnsiChar)*(pN+1)*2);
1129 pazColName0:= pazColName;
1130 pazColType:= pazColName;
1132 Inc(pazColType, pN);
1133 for i := 0 to pN - 1 do
1135 if Result = SQLITE_ROW then
1137 cname:= SQLite_API.sqlite_column_name(Stmt, i);
1138 ctype:= SQLite_API.sqlite_column_decltype(Stmt, i);
1139 val := SQLite_API.sqlite_column_text(Stmt, i);
1145 cname:= SQLite_API.sqlite_column_name(Stmt, i);
1146 ctype:= SQLite_API.sqlite_column_decltype(Stmt, i);
1148 pazColName0^:= cname;
1149 pazColType^ := ctype;
1153 if Result = SQLITE_ROW then
1156 if Result = SQLITE_DONE then
1161 function TZSQLiteBaseDriver.Step(Stmt: Psqlite3_stmt): Integer;
1163 Result := SQLite_API.sqlite_step(Stmt);
1166 function TZSQLiteBaseDriver.data_count(pStmt: Psqlite3_stmt): Integer;
1168 Result := SQLite_API.sqlite_data_count(pStmt);
1171 function TZSQLiteBaseDriver.bind_blob(pStmt: Psqlite3_stmt; ParamIndex: Integer;
1172 const Buffer: Pointer; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer;
1174 Result := SQLite_API.sqlite_bind_blob(pStmt, ParamIndex, Buffer, N, ValDestructor);
1177 function TZSQLiteBaseDriver.bind_double(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Double): Integer;
1179 Result := SQLite_API.sqlite_bind_double(pStmt, ParamIndex, Value);
1182 function TZSQLiteBaseDriver.bind_int(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Integer): Integer;
1184 Result := SQLite_API.sqlite_bind_int(pStmt, ParamIndex, Value);
1187 function TZSQLiteBaseDriver.bind_int64(pStmt: Psqlite3_stmt; ParamIndex: Integer; Value: Int64): Integer;
1189 Result := SQLite_API.sqlite_bind_int64(pStmt, ParamIndex, Value);
1192 function TZSQLiteBaseDriver.bind_null(pStmt: Psqlite3_stmt; ParamIndex: Integer): Integer;
1194 Result := SQLite_API.sqlite_bind_null(pStmt, ParamIndex);
1197 function TZSQLiteBaseDriver.bind_text(pStmt: Psqlite3_stmt; ParamIndex: Integer;
1198 const Text: PAnsiChar; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer;
1200 Result := SQLite_API.sqlite_bind_text(pStmt, ParamIndex, Text, N, ValDestructor);
1203 function TZSQLiteBaseDriver.bind_text16(pStmt: Psqlite3_stmt; ParamIndex: Integer;
1204 const Text: PWideChar; N: Integer; ValDestructor: Tsqlite3_destructor_type): Integer;
1206 Result := SQLite_API.sqlite_bind_text16(pStmt, ParamIndex, Text, N, ValDestructor);
1209 function TZSQLiteBaseDriver.bind_value(pStmt: Psqlite3_stmt; ParamIndex: Integer; const Value: Psqlite3_value): Integer;
1211 Result := SQLite_API.sqlite_bind_value(pStmt, ParamIndex, Value);
1214 function TZSQLiteBaseDriver.bind_zeroblob(pStmt: Psqlite3_stmt; ParamIndex: Integer; N: Integer): Integer;
1216 Result := SQLite_API.sqlite_bind_zeroblob(pStmt, ParamIndex, N);
1219 function TZSQLiteBaseDriver.finalize(pStmt: Psqlite3_stmt): Integer;
1221 Result := SQLite_API.sqlite_finalize(pStmt);
1224 function TZSQLiteBaseDriver.reset(pStmt: Psqlite3_stmt): Integer;
1226 Result := SQLite_API.sqlite_reset(pStmt);
1229 function TZSQLiteBaseDriver.column_blob(Stmt: Psqlite3_stmt; iCol:integer): TStream;
1234 result := TMemoryStream.Create;
1235 P := SQLite_API.sqlite_column_blob(Stmt, iCol-1);
1236 len := SQLite_API.sqlite_column_bytes(Stmt, iCol-1);
1237 result.WriteBuffer(P^,len);
1240 function TZSQLiteBaseDriver.column_bytes(Stmt: Psqlite3_stmt; iCol: Integer): integer;
1242 Result := SQLite_API.sqlite_column_bytes(Stmt, iCol);
1245 function TZSQLiteBaseDriver.column_bytes16(Stmt: Psqlite3_stmt; iCol: Integer): integer;
1247 Result := SQLite_API.sqlite_column_bytes16(Stmt, iCol);
1250 function TZSQLiteBaseDriver.column_double(Stmt: Psqlite3_stmt; iCol: Integer): Double;
1252 Result := SQLite_API.sqlite_column_double(Stmt, iCol);
1255 function TZSQLiteBaseDriver.column_int(Stmt: Psqlite3_stmt; iCol: Integer): Integer;
1257 Result := SQLite_API.sqlite_column_int(Stmt, iCol);
1260 function TZSQLiteBaseDriver.column_int64(Stmt: Psqlite3_stmt; iCol: Integer): Int64;
1262 Result := SQLite_API.sqlite_column_int64(Stmt, iCol);
1265 function TZSQLiteBaseDriver.column_text(Stmt: Psqlite3_stmt; iCol: Integer): PAnsiChar;
1267 Result := SQLite_API.sqlite_column_text(Stmt, iCol);
1270 function TZSQLiteBaseDriver.column_text16(Stmt: Psqlite3_stmt; iCol: Integer): PWideChar;
1272 Result := SQLite_API.sqlite_column_text16(Stmt, iCol);
1275 function TZSQLiteBaseDriver.column_type(Stmt: Psqlite3_stmt; iCol: Integer): String;
1277 if Assigned(SQLite_API.sqlite_column_type) then
1278 case SQLite_API.sqlite_column_type(stmt, iCol) of
1282 Result:='FLOAT(16)';
1284 //RESULT := 'CHAR'; //EgonHugeist: Need to boil down this type !
1285 //Else Metadatainformations are not readable !
1289 SQLITE_NULL: Result := '';
1295 function TZSQLiteBaseDriver.column_value(Stmt: Psqlite3_stmt; iCol: Integer): Psqlite3_value;
1297 Result := SQLite_API.sqlite_column_value(stmt, iCol);
1300 function TZSQLiteBaseDriver.Trace(db: Psqlite;
1301 callback: Tsqlite_trace_callback; ptr: Pointer): Pointer;
1303 Result := SQLite_API.sqlite_trace(db, callback, ptr);
1306 function TZSQLiteBaseDriver.UserData(func: Psqlite_func): Pointer;
1308 Result := SQLite_API.sqlite_user_data(func);
1311 function TZSQLiteBaseDriver.Close(db: Psqlite): Integer;
1313 Result := SQLite_API.sqlite_close(db);
1316 { TZSQLite3PlainDriver }
1318 function TZSQLite3PlainDriver.Clone: IZPlainDriver;
1320 Result := TZSQLite3PlainDriver.Create;
1323 procedure TZSQLite3PlainDriver.LoadApi;
1325 { ************** Load adresses of API Functions ************* }
1328 @SQLite_API.sqlite_open := GetAddress('sqlite3_open');
1329 @SQLite_API.sqlite_close := GetAddress('sqlite3_close');
1331 { prepared Statment api }
1332 { prepared statement api }
1333 @SQLite_API.sqlite_prepare := GetAddress('sqlite3_prepare');
1334 @SQLite_API.sqlite_prepare_v2 := GetAddress('sqlite3_prepare_v2');
1335 @SQLite_API.sqlite_prepare16 := GetAddress('sqlite3_prepare16');
1336 @SQLite_API.sqlite_prepare16_v2 := GetAddress('sqlite3_prepare16_v2');
1338 @SQLite_API.sqlite_bind_parameter_count := GetAddress('sqlite3_bind_parameter_count');
1339 @SQLite_API.sqlite_bind_parameter_name := GetAddress('sqlite3_bind_parameter_name');
1340 @SQLite_API.sqlite_bind_parameter_index := GetAddress('sqlite3_bind_parameter_index');
1342 @SQLite_API.sqlite_clear_bindings := GetAddress('sqlite3_clear_bindings');
1344 @SQLite_API.sqlite_column_count := GetAddress('sqlite3_column_count');
1345 @SQLite_API.sqlite_column_bytes := GetAddress('sqlite3_column_bytes');
1346 @SQLite_API.sqlite_column_bytes16 := GetAddress('sqlite3_column_bytes16');
1347 @SQLite_API.sqlite_Column_blob := GetAddress('sqlite3_column_blob');
1348 @SQLite_API.sqlite_column_double := GetAddress('sqlite3_column_double');
1349 @SQLite_API.sqlite_column_int := GetAddress('sqlite3_column_int');
1350 @SQLite_API.sqlite_column_int64 := GetAddress('sqlite3_column_int64');
1351 @SQLite_API.sqlite_column_text := GetAddress('sqlite3_column_text');
1352 @SQLite_API.sqlite_column_text16 := GetAddress('sqlite3_column_text16');
1353 @SQLite_API.sqlite_column_type := GetAddress('sqlite3_column_type');
1354 @SQLite_API.sqlite_column_value := GetAddress('sqlite3_column_value');
1355 @SQLite_API.sqlite_column_name := GetAddress('sqlite3_column_name');
1356 @SQLite_API.sqlite_column_name16 := GetAddress('sqlite3_column_name16');
1358 @SQLite_API.sqlite_column_database_name := GetAddress('sqlite3_column_database_name');
1359 @SQLite_API.sqlite_column_database_name16 := GetAddress('sqlite3_column_database_name16');
1360 @SQLite_API.sqlite_column_table_name := GetAddress('sqlite3_column_table_name');
1361 @SQLite_API.sqlite_column_table_name16 := GetAddress('sqlite3_column_table_name16');
1362 @SQLite_API.sqlite_column_origin_name := GetAddress('sqlite3_column_origin_name');
1363 @SQLite_API.sqlite_column_origin_name16 := GetAddress('sqlite3_column_origin_name16');
1365 @SQLite_API.sqlite_column_decltype := GetAddress('sqlite3_column_decltype');
1366 @SQLite_API.sqlite_column_decltype16 := GetAddress('sqlite3_column_decltype16');
1368 @SQLite_API.sqlite_step := GetAddress('sqlite3_step');
1369 @SQLite_API.sqlite_data_count := GetAddress('sqlite3_data_count');
1371 @SQLite_API.sqlite_bind_blob := GetAddress('sqlite3_bind_blob');
1372 @SQLite_API.sqlite_bind_double := GetAddress('sqlite3_bind_double');
1373 @SQLite_API.sqlite_bind_int := GetAddress('sqlite3_bind_int');
1374 @SQLite_API.sqlite_bind_int64 := GetAddress('sqlite3_bind_int64');
1375 @SQLite_API.sqlite_bind_null := GetAddress('sqlite3_bind_null');
1376 @SQLite_API.sqlite_bind_text := GetAddress('sqlite3_bind_text');
1377 @SQLite_API.sqlite_bind_text16 := GetAddress('sqlite3_bind_text16');
1378 @SQLite_API.sqlite_bind_value := GetAddress('sqlite3_bind_value');
1379 @SQLite_API.sqlite_bind_zeroblob := GetAddress('sqlite3_bind_zeroblob');
1381 @SQLite_API.sqlite_finalize := GetAddress('sqlite3_finalize');
1382 @SQLite_API.sqlite_reset := GetAddress('sqlite3_reset');
1384 @SQLite_API.sqlite_exec := GetAddress('sqlite3_exec');
1385 @SQLite_API.sqlite_last_insert_rowid := GetAddress('sqlite3_last_insert_rowid');
1386 @SQLite_API.sqlite_changes := GetAddress('sqlite3_changes');
1387 // @SQLite_API.sqlite_last_statement_changes := GetAddress('sqlite3_last_statement_changes');
1388 @SQLite_API.sqlite_errmsg := GetAddress('sqlite3_errmsg');
1389 @SQLite_API.sqlite_errstr := GetAddress('sqlite3_errstr');
1390 @SQLite_API.sqlite_interrupt := GetAddress('sqlite3_interrupt');
1391 @SQLite_API.sqlite_complete := GetAddress('sqlite3_complete');
1392 @SQLite_API.sqlite_busy_handler := GetAddress('sqlite3_busy_handler');
1393 @SQLite_API.sqlite_busy_timeout := GetAddress('sqlite3_busy_timeout');
1394 @SQLite_API.sqlite_get_table := GetAddress('sqlite3_get_table');
1395 @SQLite_API.sqlite_free_table := GetAddress('sqlite3_free_table');
1396 @SQLite_API.sqlite_freemem := GetAddress('sqlite3_free');
1397 @SQLite_API.sqlite_libversion := GetAddress('sqlite3_libversion');
1398 // @SQLite_API.sqlite_libencoding := GetAddress('sqlite3_libencoding');
1399 // @SQLite_API.sqlite_create_function := GetAddress('sqlite3_create_function');
1400 // @SQLite_API.sqlite_create_aggregate := GetAddress('sqlite3_create_aggregate');
1401 // @SQLite_API.sqlite_function_type := GetAddress('sqlite3_function_type');
1402 @SQLite_API.sqlite_set_result_string := GetAddress('sqlite3_result_string');
1403 @SQLite_API.sqlite_set_result_int := GetAddress('sqlite3_result_int');
1404 @SQLite_API.sqlite_set_result_double := GetAddress('sqlite3_result_double');
1405 @SQLite_API.sqlite_set_result_error := GetAddress('sqlite3_result_error');
1406 @SQLite_API.sqlite_user_data := GetAddress('sqlite3_user_data');
1407 @SQLite_API.sqlite_aggregate_context := GetAddress('sqlite3_aggregate_context');
1408 @SQLite_API.sqlite_aggregate_count := GetAddress('sqlite3_aggregate_count');
1409 @SQLite_API.sqlite_set_authorizer := GetAddress('sqlite3_set_authorizer');
1410 @SQLite_API.sqlite_trace := GetAddress('sqlite3_trace');
1411 @SQLite_API.sqlite_progress_handler := GetAddress('sqlite3_progress_handler');
1412 @SQLite_API.sqlite_commit_hook := GetAddress('sqlite3_commit_hook');
1413 // @SQLite_API.sqlite_open_encrypted := GetAddress('sqlite3_open_encrypted');
1414 @SQLite_API.sqlite_rekey := GetAddress('sqlite3_rekey');
1415 @SQLite_API.sqlite_key := GetAddress('sqlite3_key');
1419 constructor TZSQLite3PlainDriver.Create;
1423 FLoader.AddLocation(WINDOWS_DLL3_LOCATION);
1425 FLoader.AddLocation(LINUX_DLL3_LOCATION);
1426 FLoader.AddLocation(LINUX_DLL3_LOCATION+'.0');
1431 function TZSQLite3PlainDriver.GetProtocol: string;
1433 Result := 'sqlite-3';
1436 function TZSQLite3PlainDriver.GetDescription: string;
1438 Result := 'Native Plain Driver for SQLite 3';