1 ! Copyright (C) 2005 Chris Double, Doug Coleman.
2 ! See http://factorcode.org/license.txt for BSD license.
3 ! An interface to the sqlite database. Tested against sqlite v3.1.3.
4 ! Not all functions have been wrapped.
5 USING: alien compiler kernel math namespaces sequences strings alien.syntax
6 system combinators alien.c-types ;
10 { [ os winnt? ] [ "sqlite3.dll" ] }
11 { [ os macosx? ] [ "/usr/lib/libsqlite3.dylib" ] }
12 { [ os unix? ] [ "libsqlite3.so" ] }
13 } cond "cdecl" add-library >>
15 ! Return values from sqlite functions
16 : SQLITE_OK 0 ; inline ! Successful result
17 : SQLITE_ERROR 1 ; inline ! SQL error or missing database
18 : SQLITE_INTERNAL 2 ; inline ! An internal logic error in SQLite
19 : SQLITE_PERM 3 ; inline ! Access permission denied
20 : SQLITE_ABORT 4 ; inline ! Callback routine requested an abort
21 : SQLITE_BUSY 5 ; inline ! The database file is locked
22 : SQLITE_LOCKED 6 ; inline ! A table in the database is locked
23 : SQLITE_NOMEM 7 ; inline ! A malloc() failed
24 : SQLITE_READONLY 8 ; inline ! Attempt to write a readonly database
25 : SQLITE_INTERRUPT 9 ; inline ! Operation terminated by sqlite_interrupt()
26 : SQLITE_IOERR 10 ; inline ! Some kind of disk I/O error occurred
27 : SQLITE_CORRUPT 11 ; inline ! The database disk image is malformed
28 : SQLITE_NOTFOUND 12 ; inline ! (Internal Only) Table or record not found
29 : SQLITE_FULL 13 ; inline ! Insertion failed because database is full
30 : SQLITE_CANTOPEN 14 ; inline ! Unable to open the database file
31 : SQLITE_PROTOCOL 15 ; inline ! Database lock protocol error
32 : SQLITE_EMPTY 16 ; inline ! (Internal Only) Database table is empty
33 : SQLITE_SCHEMA 17 ; inline ! The database schema changed
34 : SQLITE_TOOBIG 18 ; inline ! Too much data for one row of a table
35 : SQLITE_CONSTRAINT 19 ; inline ! Abort due to contraint violation
36 : SQLITE_MISMATCH 20 ; inline ! Data type mismatch
37 : SQLITE_MISUSE 21 ; inline ! Library used incorrectly
38 : SQLITE_NOLFS 22 ; inline ! Uses OS features not supported on host
39 : SQLITE_AUTH 23 ; inline ! Authorization denied
40 : SQLITE_FORMAT 24 ; inline ! Auxiliary database format error
41 : SQLITE_RANGE 25 ; inline ! 2nd parameter to sqlite3_bind out of range
42 : SQLITE_NOTADB 26 ; inline ! File opened that is not a database file
44 : sqlite-error-messages ( -- seq ) {
46 "SQL error or missing database"
47 "An internal logic error in SQLite"
48 "Access permission denied"
49 "Callback routine requested an abort"
50 "The database file is locked"
51 "A table in the database is locked"
53 "Attempt to write a readonly database"
54 "Operation terminated by sqlite_interrupt()"
55 "Some kind of disk I/O error occurred"
56 "The database disk image is malformed"
57 "(Internal Only) Table or record not found"
58 "Insertion failed because database is full"
59 "Unable to open the database file"
60 "Database lock protocol error"
61 "(Internal Only) Database table is empty"
62 "The database schema changed"
63 "Too much data for one row of a table"
64 "Abort due to contraint violation"
66 "Library used incorrectly"
67 "Uses OS features not supported on host"
68 "Authorization denied"
69 "Auxiliary database format error"
70 "2nd parameter to sqlite3_bind out of range"
71 "File opened that is not a database file"
74 ! Return values from sqlite3_step
75 : SQLITE_ROW 100 ; inline
76 : SQLITE_DONE 101 ; inline
78 ! Return values from the sqlite3_column_type function
79 : SQLITE_INTEGER 1 ; inline
80 : SQLITE_FLOAT 2 ; inline
81 : SQLITE_TEXT 3 ; inline
82 : SQLITE_BLOB 4 ; inline
83 : SQLITE_NULL 5 ; inline
85 ! Values for the 'destructor' parameter of the 'bind' routines.
86 : SQLITE_STATIC 0 ; inline
87 : SQLITE_TRANSIENT -1 ; inline
89 : SQLITE_OPEN_READONLY HEX: 00000001 ; inline
90 : SQLITE_OPEN_READWRITE HEX: 00000002 ; inline
91 : SQLITE_OPEN_CREATE HEX: 00000004 ; inline
92 : SQLITE_OPEN_DELETEONCLOSE HEX: 00000008 ; inline
93 : SQLITE_OPEN_EXCLUSIVE HEX: 00000010 ; inline
94 : SQLITE_OPEN_MAIN_DB HEX: 00000100 ; inline
95 : SQLITE_OPEN_TEMP_DB HEX: 00000200 ; inline
96 : SQLITE_OPEN_TRANSIENT_DB HEX: 00000400 ; inline
97 : SQLITE_OPEN_MAIN_JOURNAL HEX: 00000800 ; inline
98 : SQLITE_OPEN_TEMP_JOURNAL HEX: 00001000 ; inline
99 : SQLITE_OPEN_SUBJOURNAL HEX: 00002000 ; inline
100 : SQLITE_OPEN_MASTER_JOURNAL HEX: 00004000 ; inline
102 TYPEDEF: void sqlite3
103 TYPEDEF: void sqlite3_stmt
104 TYPEDEF: longlong sqlite3_int64
105 TYPEDEF: ulonglong sqlite3_uint64
108 FUNCTION: int sqlite3_open ( char* filename, void* ppDb ) ;
109 FUNCTION: int sqlite3_close ( sqlite3* pDb ) ;
110 FUNCTION: char* sqlite3_errmsg ( sqlite3* pDb ) ;
111 FUNCTION: int sqlite3_prepare ( sqlite3* pDb, char* zSql, int nBytes, void* ppStmt, void* pzTail ) ;
112 FUNCTION: int sqlite3_prepare_v2 ( sqlite3* pDb, char* zSql, int nBytes, void* ppStmt, void* pzTail ) ;
113 FUNCTION: int sqlite3_finalize ( sqlite3_stmt* pStmt ) ;
114 FUNCTION: int sqlite3_reset ( sqlite3_stmt* pStmt ) ;
115 FUNCTION: int sqlite3_step ( sqlite3_stmt* pStmt ) ;
116 FUNCTION: sqlite3_uint64 sqlite3_last_insert_rowid ( sqlite3* pStmt ) ;
117 FUNCTION: int sqlite3_bind_blob ( sqlite3_stmt* pStmt, int index, void* ptr, int len, int destructor ) ;
118 FUNCTION: int sqlite3_bind_double ( sqlite3_stmt* pStmt, int index, double x ) ;
119 FUNCTION: int sqlite3_bind_int ( sqlite3_stmt* pStmt, int index, int n ) ;
120 FUNCTION: int sqlite3_bind_int64 ( sqlite3_stmt* pStmt, int index, sqlite3_int64 n ) ;
121 ! Bind the same function as above, but for unsigned 64bit integers
122 : sqlite3-bind-uint64 ( pStmt index in64 -- int )
123 "int" "sqlite" "sqlite3_bind_int64"
124 { "sqlite3_stmt*" "int" "sqlite3_uint64" } alien-invoke ;
125 FUNCTION: int sqlite3_bind_null ( sqlite3_stmt* pStmt, int n ) ;
126 FUNCTION: int sqlite3_bind_text ( sqlite3_stmt* pStmt, int index, char* text, int len, int destructor ) ;
127 FUNCTION: int sqlite3_bind_parameter_index ( sqlite3_stmt* pStmt, char* name ) ;
128 FUNCTION: int sqlite3_clear_bindings ( sqlite3_stmt* pStmt ) ;
129 FUNCTION: int sqlite3_column_count ( sqlite3_stmt* pStmt ) ;
130 FUNCTION: void* sqlite3_column_blob ( sqlite3_stmt* pStmt, int col ) ;
131 FUNCTION: int sqlite3_column_bytes ( sqlite3_stmt* pStmt, int col ) ;
132 FUNCTION: char* sqlite3_column_decltype ( sqlite3_stmt* pStmt, int col ) ;
133 FUNCTION: int sqlite3_column_int ( sqlite3_stmt* pStmt, int col ) ;
134 FUNCTION: sqlite3_int64 sqlite3_column_int64 ( sqlite3_stmt* pStmt, int col ) ;
135 ! Bind the same function as above, but for unsigned 64bit integers
136 : sqlite3-column-uint64 ( pStmt col -- uint64 )
137 "sqlite3_uint64" "sqlite" "sqlite3_column_int64"
138 { "sqlite3_stmt*" "int" } alien-invoke ;
139 FUNCTION: double sqlite3_column_double ( sqlite3_stmt* pStmt, int col ) ;
140 FUNCTION: char* sqlite3_column_name ( sqlite3_stmt* pStmt, int col ) ;
141 FUNCTION: char* sqlite3_column_text ( sqlite3_stmt* pStmt, int col ) ;
142 FUNCTION: int sqlite3_column_type ( sqlite3_stmt* pStmt, int col ) ;