/lib/sqlitewrap/source/SQLite3.pas

https://code.google.com/p/delphi-orm/ · Pascal · 258 lines · 173 code · 30 blank · 55 comment · 0 complexity · 183c939f677e5fb78fb6a1945f6c4ee0 MD5 · raw file

  1. unit SQLite3;
  2. {
  3. Simplified interface for SQLite.
  4. This version :Ported to D2009 Unicode by Roger Lascelles (support@veecad.com)
  5. V2.0.0 29 June 2010
  6. History
  7. Reworked by Lukáš Gebauer at http://www.ararat.cz/doku.php/en:sqlitewrap.
  8. Updated for Sqlite 3 by Tim Anderson (tim@itwriting.com)
  9. Note: NOT COMPLETE for version 3, just minimal functionality
  10. Adapted from file created by Pablo Pissanetzky (pablo@myhtpc.net)
  11. which was based on SQLite.pas by Ben Hochstrasser (bhoc@surfeu.ch)
  12. Require: Delphi 6+, FreePascal
  13. }
  14. {$IFDEF FPC}
  15. {$MODE DELPHI}
  16. {$H+} (* use AnsiString *)
  17. {$PACKENUM 4} (* use 4-byte enums *)
  18. {$PACKRECORDS C} (* C/C++-compatible record packing *)
  19. {$ELSE}
  20. {$MINENUMSIZE 4} (* use 4-byte enums *)
  21. {$ENDIF}
  22. interface
  23. const
  24. {$IF Defined(MSWINDOWS)}
  25. SQLiteDLL = 'sqlite3.dll';
  26. {$ELSEIF Defined(DARWIN)}
  27. SQLiteDLL = 'libsqlite3.dylib';
  28. {$linklib libsqlite3}
  29. {$ELSEIF Defined(UNIX)}
  30. SQLiteDLL = 'sqlite3.so';
  31. {$IFEND}
  32. // Return values for sqlite3_exec() and sqlite3_step()
  33. const
  34. SQLITE_OK = 0; // Successful result
  35. (* beginning-of-error-codes *)
  36. SQLITE_ERROR = 1; // SQL error or missing database
  37. SQLITE_INTERNAL = 2; // An internal logic error in SQLite
  38. SQLITE_PERM = 3; // Access permission denied
  39. SQLITE_ABORT = 4; // Callback routine requested an abort
  40. SQLITE_BUSY = 5; // The database file is locked
  41. SQLITE_LOCKED = 6; // A table in the database is locked
  42. SQLITE_NOMEM = 7; // A malloc() failed
  43. SQLITE_READONLY = 8; // Attempt to write a readonly database
  44. SQLITE_INTERRUPT = 9; // Operation terminated by sqlite3_interrupt()
  45. SQLITE_IOERR = 10; // Some kind of disk I/O error occurred
  46. SQLITE_CORRUPT = 11; // The database disk image is malformed
  47. SQLITE_NOTFOUND = 12; // (Internal Only) Table or record not found
  48. SQLITE_FULL = 13; // Insertion failed because database is full
  49. SQLITE_CANTOPEN = 14; // Unable to open the database file
  50. SQLITE_PROTOCOL = 15; // Database lock protocol error
  51. SQLITE_EMPTY = 16; // Database is empty
  52. SQLITE_SCHEMA = 17; // The database schema changed
  53. SQLITE_TOOBIG = 18; // Too much data for one row of a table
  54. SQLITE_CONSTRAINT = 19; // Abort due to contraint violation
  55. SQLITE_MISMATCH = 20; // Data type mismatch
  56. SQLITE_MISUSE = 21; // Library used incorrectly
  57. SQLITE_NOLFS = 22; // Uses OS features not supported on host
  58. SQLITE_AUTH = 23; // Authorization denied
  59. SQLITE_FORMAT = 24; // Auxiliary database format error
  60. SQLITE_RANGE = 25; // 2nd parameter to sqlite3_bind out of range
  61. SQLITE_NOTADB = 26; // File opened that is not a database file
  62. SQLITE_ROW = 100; // sqlite3_step() has another row ready
  63. SQLITE_DONE = 101; // sqlite3_step() has finished executing
  64. SQLITE_INTEGER = 1;
  65. SQLITE_FLOAT = 2;
  66. SQLITE_TEXT = 3;
  67. SQLITE_BLOB = 4;
  68. SQLITE_NULL = 5;
  69. SQLITE_UTF8 = 1;
  70. SQLITE_UTF16 = 2;
  71. SQLITE_UTF16BE = 3;
  72. SQLITE_UTF16LE = 4;
  73. SQLITE_ANY = 5;
  74. SQLITE_STATIC {: TSQLite3Destructor} = Pointer(0);
  75. SQLITE_TRANSIENT {: TSQLite3Destructor} = Pointer(-1);
  76. type
  77. TSQLiteDB = Pointer;
  78. TSQLiteResult = ^PAnsiChar;
  79. TSQLiteStmt = Pointer;
  80. type
  81. PPAnsiCharArray = ^TPAnsiCharArray;
  82. TPAnsiCharArray = array[0 .. (MaxInt div SizeOf(PAnsiChar))-1] of PAnsiChar;
  83. type
  84. TSQLiteExecCallback = function(UserData: Pointer; NumCols: integer; ColValues:
  85. PPAnsiCharArray; ColNames: PPAnsiCharArray): integer; cdecl;
  86. TSQLiteBusyHandlerCallback = function(UserData: Pointer; P2: integer): integer; cdecl;
  87. //function prototype for define own collate
  88. TCollateXCompare = function(UserData: pointer; Buf1Len: integer; Buf1: pointer;
  89. Buf2Len: integer; Buf2: pointer): integer; cdecl;
  90. function SQLite3_Initialize(): integer; cdecl; external SQLiteDLL name 'sqlite3_initialize';
  91. function SQLite3_Shutdown(): integer; cdecl; external SQLiteDLL name 'sqlite3_shutdown';
  92. function SQLite3_Open(filename: PAnsiChar; var db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_open';
  93. function SQLite3_Close(db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_close';
  94. function SQLite3_Exec(db: TSQLiteDB; SQLStatement: PAnsiChar; CallbackPtr: TSQLiteExecCallback; UserData: Pointer; var ErrMsg: PAnsiChar): integer; cdecl; external SQLiteDLL name 'sqlite3_exec';
  95. function SQLite3_Version(): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_libversion';
  96. function SQLite3_ErrMsg(db: TSQLiteDB): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_errmsg';
  97. function SQLite3_ErrCode(db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_errcode';
  98. procedure SQlite3_Free(P: PAnsiChar); cdecl; external SQLiteDLL name 'sqlite3_free';
  99. function SQLite3_GetTable(db: TSQLiteDB; SQLStatement: PAnsiChar; var ResultPtr: TSQLiteResult; var RowCount: Cardinal; var ColCount: Cardinal; var ErrMsg: PAnsiChar): integer; cdecl; external SQLiteDLL name 'sqlite3_get_table';
  100. procedure SQLite3_FreeTable(Table: TSQLiteResult); cdecl; external SQLiteDLL name 'sqlite3_free_table';
  101. function SQLite3_Complete(P: PAnsiChar): boolean; cdecl; external SQLiteDLL name 'sqlite3_complete';
  102. function SQLite3_LastInsertRowID(db: TSQLiteDB): int64; cdecl; external SQLiteDLL name 'sqlite3_last_insert_rowid';
  103. procedure SQLite3_Interrupt(db: TSQLiteDB); cdecl; external SQLiteDLL name 'sqlite3_interrupt';
  104. procedure SQLite3_BusyHandler(db: TSQLiteDB; CallbackPtr: TSQLiteBusyHandlerCallback; UserData: Pointer); cdecl; external SQLiteDLL name 'sqlite3_busy_handler';
  105. procedure SQLite3_BusyTimeout(db: TSQLiteDB; TimeOut: integer); cdecl; external SQLiteDLL name 'sqlite3_busy_timeout';
  106. function SQLite3_Changes(db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_changes';
  107. function SQLite3_TotalChanges(db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_total_changes';
  108. function SQLite3_Prepare(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): integer; cdecl; external SQLiteDLL name 'sqlite3_prepare';
  109. function SQLite3_Prepare_v2(db: TSQLiteDB; SQLStatement: PAnsiChar; nBytes: integer; var hStmt: TSqliteStmt; var pzTail: PAnsiChar): integer; cdecl; external SQLiteDLL name 'sqlite3_prepare_v2';
  110. function SQLite3_ColumnCount(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_column_count';
  111. function SQLite3_ColumnName(hStmt: TSqliteStmt; ColNum: integer): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_column_name';
  112. function SQLite3_ColumnDeclType(hStmt: TSqliteStmt; ColNum: integer): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_column_decltype';
  113. function SQLite3_Step(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_step';
  114. function SQLite3_DataCount(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_data_count';
  115. function SQLite3_ColumnBlob(hStmt: TSqliteStmt; ColNum: integer): pointer; cdecl; external SQLiteDLL name 'sqlite3_column_blob';
  116. function SQLite3_ColumnBytes(hStmt: TSqliteStmt; ColNum: integer): integer; cdecl; external SQLiteDLL name 'sqlite3_column_bytes';
  117. function SQLite3_ColumnDouble(hStmt: TSqliteStmt; ColNum: integer): double; cdecl; external SQLiteDLL name 'sqlite3_column_double';
  118. function SQLite3_ColumnInt(hStmt: TSqliteStmt; ColNum: integer): integer; cdecl; external SQLiteDLL name 'sqlite3_column_int';
  119. function SQLite3_ColumnText(hStmt: TSqliteStmt; ColNum: integer): PAnsiChar; cdecl; external SQLiteDLL name 'sqlite3_column_text';
  120. function SQLite3_ColumnType(hStmt: TSqliteStmt; ColNum: integer): integer; cdecl; external SQLiteDLL name 'sqlite3_column_type';
  121. function SQLite3_ColumnInt64(hStmt: TSqliteStmt; ColNum: integer): Int64; cdecl; external SQLiteDLL name 'sqlite3_column_int64';
  122. function SQLite3_Finalize(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_finalize';
  123. function SQLite3_Reset(hStmt: TSqliteStmt): integer; cdecl; external SQLiteDLL name 'sqlite3_reset';
  124. function SQLite3_Get_Autocommit(db: TSQLiteDB): integer; cdecl; external SQLiteDLL name 'sqlite3_get_autocommit';
  125. //
  126. // In the SQL strings input to sqlite3_prepare() and sqlite3_prepare16(),
  127. // one or more literals can be replace by a wildcard "?" or ":N:" where
  128. // N is an integer. These value of these wildcard literals can be set
  129. // using the routines listed below.
  130. //
  131. // In every case, the first parameter is a pointer to the sqlite3_stmt
  132. // structure returned from sqlite3_prepare(). The second parameter is the
  133. // index of the wildcard. The first "?" has an index of 1. ":N:" wildcards
  134. // use the index N.
  135. //
  136. // The fifth parameter to sqlite3_bind_blob(), sqlite3_bind_text(), and
  137. //sqlite3_bind_text16() is a destructor used to dispose of the BLOB or
  138. //text after SQLite has finished with it. If the fifth argument is the
  139. // special value SQLITE_STATIC, then the library assumes that the information
  140. // is in static, unmanaged space and does not need to be freed. If the
  141. // fifth argument has the value SQLITE_TRANSIENT, then SQLite makes its
  142. // own private copy of the data.
  143. //
  144. // The sqlite3_bind_* routine must be called before sqlite3_step() after
  145. // an sqlite3_prepare() or sqlite3_reset(). Unbound wildcards are interpreted
  146. // as NULL.
  147. //
  148. type
  149. TSQLite3Destructor = procedure(Ptr: Pointer); cdecl;
  150. function sqlite3_bind_blob(hStmt: TSqliteStmt; ParamNum: integer;
  151. ptrData: pointer; numBytes: integer; ptrDestructor: TSQLite3Destructor): integer;
  152. cdecl; external SQLiteDLL name 'sqlite3_bind_blob';
  153. function sqlite3_bind_text(hStmt: TSqliteStmt; ParamNum: integer;
  154. Text: PAnsiChar; numBytes: integer; ptrDestructor: TSQLite3Destructor): integer;
  155. cdecl; external SQLiteDLL name 'sqlite3_bind_text';
  156. function sqlite3_bind_double(hStmt: TSqliteStmt; ParamNum: integer; Data: Double): integer;
  157. cdecl; external SQLiteDLL name 'sqlite3_bind_double';
  158. function sqlite3_bind_int(hStmt: TSqLiteStmt; ParamNum: integer; Data: integer): integer;
  159. cdecl; external SQLiteDLL name 'sqlite3_bind_int';
  160. function sqlite3_bind_int64(hStmt: TSqliteStmt; ParamNum: integer; Data: int64): integer;
  161. cdecl; external SQLiteDLL name 'sqlite3_bind_int64';
  162. function sqlite3_bind_null(hStmt: TSqliteStmt; ParamNum: integer): integer;
  163. cdecl; external SQLiteDLL name 'sqlite3_bind_null';
  164. function sqlite3_bind_parameter_index(hStmt: TSqliteStmt; zName: PAnsiChar): integer;
  165. cdecl; external SQLiteDLL name 'sqlite3_bind_parameter_index';
  166. function sqlite3_clear_bindings(hStmt: TSqliteStmt): integer;
  167. cdecl; external SQLiteDLL name 'sqlite3_clear_bindings';
  168. function sqlite3_enable_shared_cache(Value: integer): integer; cdecl; external SQLiteDLL name 'sqlite3_enable_shared_cache';
  169. //user collate definiton
  170. function SQLite3_create_collation(db: TSQLiteDB; Name: PAnsiChar; eTextRep: integer;
  171. UserData: pointer; xCompare: TCollateXCompare): integer; cdecl; external SQLiteDLL name 'sqlite3_create_collation';
  172. function SQLiteFieldType(SQLiteFieldTypeCode: Integer): String;
  173. function SQLiteErrorStr(SQLiteErrorCode: Integer): String;
  174. implementation
  175. uses
  176. SysUtils;
  177. function SQLiteFieldType(SQLiteFieldTypeCode: Integer): String;
  178. begin
  179. case SQLiteFieldTypeCode of
  180. SQLITE_INTEGER: Result := 'Integer';
  181. SQLITE_FLOAT: Result := 'Float';
  182. SQLITE_TEXT: Result := 'Text';
  183. SQLITE_BLOB: Result := 'Blob';
  184. SQLITE_NULL: Result := 'Null';
  185. else
  186. Result := 'Unknown SQLite Field Type Code "' + IntToStr(SQLiteFieldTypeCode) + '"';
  187. end;
  188. end;
  189. function SQLiteErrorStr(SQLiteErrorCode: Integer): String;
  190. begin
  191. case SQLiteErrorCode of
  192. SQLITE_OK: Result := 'Successful result';
  193. SQLITE_ERROR: Result := 'SQL error or missing database';
  194. SQLITE_INTERNAL: Result := 'An internal logic error in SQLite';
  195. SQLITE_PERM: Result := 'Access permission denied';
  196. SQLITE_ABORT: Result := 'Callback routine requested an abort';
  197. SQLITE_BUSY: Result := 'The database file is locked';
  198. SQLITE_LOCKED: Result := 'A table in the database is locked';
  199. SQLITE_NOMEM: Result := 'A malloc() failed';
  200. SQLITE_READONLY: Result := 'Attempt to write a readonly database';
  201. SQLITE_INTERRUPT: Result := 'Operation terminated by sqlite3_interrupt()';
  202. SQLITE_IOERR: Result := 'Some kind of disk I/O error occurred';
  203. SQLITE_CORRUPT: Result := 'The database disk image is malformed';
  204. SQLITE_NOTFOUND: Result := '(Internal Only) Table or record not found';
  205. SQLITE_FULL: Result := 'Insertion failed because database is full';
  206. SQLITE_CANTOPEN: Result := 'Unable to open the database file';
  207. SQLITE_PROTOCOL: Result := 'Database lock protocol error';
  208. SQLITE_EMPTY: Result := 'Database is empty';
  209. SQLITE_SCHEMA: Result := 'The database schema changed';
  210. SQLITE_TOOBIG: Result := 'Too much data for one row of a table';
  211. SQLITE_CONSTRAINT: Result := 'Abort due to contraint violation';
  212. SQLITE_MISMATCH: Result := 'Data type mismatch';
  213. SQLITE_MISUSE: Result := 'Library used incorrectly';
  214. SQLITE_NOLFS: Result := 'Uses OS features not supported on host';
  215. SQLITE_AUTH: Result := 'Authorization denied';
  216. SQLITE_FORMAT: Result := 'Auxiliary database format error';
  217. SQLITE_RANGE: Result := '2nd parameter to sqlite3_bind out of range';
  218. SQLITE_NOTADB: Result := 'File opened that is not a database file';
  219. SQLITE_ROW: Result := 'sqlite3_step() has another row ready';
  220. SQLITE_DONE: Result := 'sqlite3_step() has finished executing';
  221. else
  222. Result := 'Unknown SQLite Error Code "' + IntToStr(SQLiteErrorCode) + '"';
  223. end;
  224. end;
  225. end.