Initial import
[cl-sqlite.git] / sqlite-ffi.lisp
blob4b3c1ea7430412734db49dd59c282ec13661735b
1 (defpackage :sqlite-ffi
2 (:use :cl :cffi)
3 (:export :error-code
4 :p-sqlite3
5 :sqlite3-open
6 :sqlite3-close
7 :sqlite3-errmsg
8 :p-sqlite3-stmt
9 :sqlite3-prepare
10 :sqlite3-finalize
11 :sqlite3-step
12 :sqlite3-reset
13 :sqlite3-clear-bindings
14 :sqlite3-column-count
15 :sqlite3-column-type
16 :sqlite3-column-text
17 :sqlite3-column-int64
18 :sqlite3-column-double
19 :sqlite3-column-bytes
20 :sqlite3-column-blob
21 :sqlite3-column-name
22 :sqlite3-bind-parameter-count
23 :sqlite3-bind-parameter-name
24 :sqlite3-bind-parameter-index
25 :sqlite3-bind-double
26 :sqlite3-bind-int64
27 :sqlite3-bind-null
28 :sqlite3-bind-text
29 :sqlite3-bind-blob
30 :destructor-transient
31 :destructor-static
32 :sqlite3-last-insert-rowid))
34 (in-package :sqlite-ffi)
36 (define-foreign-library sqlite3-lib
37 (:unix (:or "libsqlite3.so.0" "libsqlite3.so"))
38 (t (:default "libsqlite3")))
40 (use-foreign-library sqlite3-lib)
42 (defcenum error-code
43 (:OK 0)
44 (:ERROR 1)
45 (:INTERNAL 2)
46 (:PERM 3)
47 (:ABORT 4)
48 (:BUSY 5)
49 (:LOCKED 6)
50 (:NOMEM 7)
51 (:READONLY 8)
52 (:INTERRUPT 9)
53 (:IOERR 10)
54 (:CORRUPT 11)
55 (:NOTFOUND 12)
56 (:FULL 13)
57 (:CANTOPEN 14)
58 (:PROTOCOL 15)
59 (:EMPTY 16)
60 (:SCHEMA 17)
61 (:TOOBIG 18)
62 (:CONSTRAINT 19)
63 (:MISMATCH 20)
64 (:MISUSE 21)
65 (:NOLFS 22)
66 (:AUTH 23)
67 (:FORMAT 24)
68 (:RANGE 25)
69 (:NOTADB 26)
70 (:ROW 100)
71 (:DONE 101))
73 (defcstruct sqlite3)
75 (defctype p-sqlite3 (:pointer sqlite3))
77 (defcfun sqlite3-open error-code
78 (filename :string)
79 (db (:pointer p-sqlite3)))
81 (defcfun sqlite3-close error-code
82 (db p-sqlite3))
84 (defcfun sqlite3-errmsg :string
85 (db p-sqlite3))
87 (defcstruct sqlite3-stmt)
89 (defctype p-sqlite3-stmt (:pointer sqlite3-stmt))
91 (defcfun (sqlite3-prepare "sqlite3_prepare_v2") error-code
92 (db p-sqlite3)
93 (sql :string)
94 (sql-length-bytes :int)
95 (stmt (:pointer p-sqlite3-stmt))
96 (tail (:pointer (:pointer :char))))
98 (defcfun sqlite3-finalize error-code
99 (statement p-sqlite3-stmt))
101 (defcfun sqlite3-step error-code
102 (statement p-sqlite3-stmt))
104 (defcfun sqlite3-reset error-code
105 (statement p-sqlite3-stmt))
107 (defcfun sqlite3-clear-bindings error-code
108 (statement p-sqlite3-stmt))
110 (defcfun sqlite3-column-count :int
111 (statement p-sqlite3-stmt))
113 (defcenum type-code
114 (:integer 1)
115 (:float 2)
116 (:text 3)
117 (:blob 4)
118 (:null 5))
120 (defcfun sqlite3-column-type type-code
121 (statement p-sqlite3-stmt)
122 (column-number :int))
124 (defcfun sqlite3-column-text :string
125 (statement p-sqlite3-stmt)
126 (column-number :int))
128 (defcfun sqlite3-column-int64 :int64
129 (statement p-sqlite3-stmt)
130 (column-number :int))
132 (defcfun sqlite3-column-double :double
133 (statement p-sqlite3-stmt)
134 (column-number :int))
136 (defcfun sqlite3-column-bytes :int
137 (statement p-sqlite3-stmt)
138 (column-number :int))
140 (defcfun sqlite3-column-blob :pointer
141 (statement p-sqlite3-stmt)
142 (column-number :int))
144 (defcfun sqlite3-column-name :string
145 (statement p-sqlite3-stmt)
146 (column-number :int))
148 (defcfun sqlite3-bind-parameter-count :int
149 (statement p-sqlite3-stmt))
151 (defcfun sqlite3-bind-parameter-name :string
152 (statement p-sqlite3-stmt)
153 (column-number :int))
155 (defcfun sqlite3-bind-parameter-index :int
156 (statement p-sqlite3-stmt)
157 (name :string))
159 (defcfun sqlite3-bind-double error-code
160 (statement p-sqlite3-stmt)
161 (parameter-index :int)
162 (value :double))
164 (defcfun sqlite3-bind-int64 error-code
165 (statement p-sqlite3-stmt)
166 (parameter-index :int)
167 (value :int64))
169 (defcfun sqlite3-bind-null error-code
170 (statement p-sqlite3-stmt)
171 (parameter-index :int))
173 (defcfun sqlite3-bind-text error-code
174 (statement p-sqlite3-stmt)
175 (parameter-index :int)
176 (value :string)
177 (octets-count :int)
178 (destructor :pointer))
180 (defcfun sqlite3-bind-blob error-code
181 (statement p-sqlite3-stmt)
182 (parameter-index :int)
183 (value :pointer)
184 (bytes-count :int)
185 (destructor :pointer))
187 (defconstant destructor-transient-address (mod -1 (expt 2 (* 8 (cffi:foreign-type-size :pointer)))))
189 (defun destructor-transient () (cffi:make-pointer destructor-transient-address))
191 (defun destructor-static () (cffi:make-pointer 0))
193 (defcfun sqlite3-last-insert-rowid :int64
194 (db p-sqlite3))