PageRenderTime 27ms CodeModel.GetById 1ms app.highlight 19ms RepoModel.GetById 2ms app.codeStats 0ms

/sqlite-ffi.lisp

http://github.com/dmitryvk/cl-sqlite
Lisp | 200 lines | 162 code | 38 blank | 0 comment | 0 complexity | 69b91177fea7ecffa1a9d3af6fceaeec MD5 | raw file
  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           :sqlite3-busy-timeout
  9           :p-sqlite3-stmt
 10           :sqlite3-prepare
 11           :sqlite3-finalize
 12           :sqlite3-step
 13           :sqlite3-reset
 14           :sqlite3-clear-bindings
 15           :sqlite3-column-count
 16           :sqlite3-column-type
 17           :sqlite3-column-text
 18           :sqlite3-column-int64
 19           :sqlite3-column-double
 20           :sqlite3-column-bytes
 21           :sqlite3-column-blob
 22           :sqlite3-column-name
 23           :sqlite3-bind-parameter-count
 24           :sqlite3-bind-parameter-name
 25           :sqlite3-bind-parameter-index
 26           :sqlite3-bind-double
 27           :sqlite3-bind-int64
 28           :sqlite3-bind-null
 29           :sqlite3-bind-text
 30           :sqlite3-bind-blob
 31           :destructor-transient
 32           :destructor-static
 33           :sqlite3-last-insert-rowid))
 34
 35(in-package :sqlite-ffi)
 36
 37(define-foreign-library sqlite3-lib
 38  (:darwin (:default "libsqlite3"))
 39  (:unix (:or "libsqlite3.so.0" "libsqlite3.so"))
 40  (t (:or (:default "libsqlite3") (:default "sqlite3"))))
 41
 42(use-foreign-library sqlite3-lib)
 43
 44(defcenum error-code
 45  (:OK 0)
 46  (:ERROR 1)
 47  (:INTERNAL 2)
 48  (:PERM 3)
 49  (:ABORT 4)
 50  (:BUSY 5)
 51  (:LOCKED 6)
 52  (:NOMEM 7)
 53  (:READONLY 8)
 54  (:INTERRUPT 9)
 55  (:IOERR 10)
 56  (:CORRUPT 11)
 57  (:NOTFOUND 12)
 58  (:FULL 13)
 59  (:CANTOPEN 14)
 60  (:PROTOCOL 15)
 61  (:EMPTY 16)
 62  (:SCHEMA 17)
 63  (:TOOBIG 18)
 64  (:CONSTRAINT 19)
 65  (:MISMATCH 20)
 66  (:MISUSE 21)
 67  (:NOLFS 22)
 68  (:AUTH 23)
 69  (:FORMAT 24)
 70  (:RANGE 25)
 71  (:NOTADB 26)
 72  (:ROW 100)
 73  (:DONE 101))
 74
 75(defcstruct sqlite3)
 76
 77(defctype p-sqlite3 (:pointer sqlite3))
 78
 79(defcfun sqlite3-open error-code
 80  (filename :string)
 81  (db (:pointer p-sqlite3)))
 82
 83(defcfun sqlite3-close error-code
 84  (db p-sqlite3))
 85
 86(defcfun sqlite3-errmsg :string
 87  (db p-sqlite3))
 88
 89(defcfun sqlite3-busy-timeout :int
 90  (db p-sqlite3)
 91  (ms :int))
 92
 93(defcstruct sqlite3-stmt)
 94
 95(defctype p-sqlite3-stmt (:pointer sqlite3-stmt))
 96
 97(defcfun (sqlite3-prepare "sqlite3_prepare_v2") error-code
 98  (db p-sqlite3)
 99  (sql :string)
100  (sql-length-bytes :int)
101  (stmt (:pointer p-sqlite3-stmt))
102  (tail (:pointer (:pointer :char))))
103
104(defcfun sqlite3-finalize error-code
105  (statement p-sqlite3-stmt))
106
107(defcfun sqlite3-step error-code
108  (statement p-sqlite3-stmt))
109
110(defcfun sqlite3-reset error-code
111  (statement p-sqlite3-stmt))
112
113(defcfun sqlite3-clear-bindings error-code
114  (statement p-sqlite3-stmt))
115
116(defcfun sqlite3-column-count :int
117  (statement p-sqlite3-stmt))
118
119(defcenum type-code
120  (:integer 1)
121  (:float 2)
122  (:text 3)
123  (:blob 4)
124  (:null 5))
125
126(defcfun sqlite3-column-type type-code
127  (statement p-sqlite3-stmt)
128  (column-number :int))
129
130(defcfun sqlite3-column-text :string
131  (statement p-sqlite3-stmt)
132  (column-number :int))
133
134(defcfun sqlite3-column-int64 :int64
135  (statement p-sqlite3-stmt)
136  (column-number :int))
137
138(defcfun sqlite3-column-double :double
139  (statement p-sqlite3-stmt)
140  (column-number :int))
141
142(defcfun sqlite3-column-bytes :int
143  (statement p-sqlite3-stmt)
144  (column-number :int))
145
146(defcfun sqlite3-column-blob :pointer
147  (statement p-sqlite3-stmt)
148  (column-number :int))
149
150(defcfun sqlite3-column-name :string
151  (statement p-sqlite3-stmt)
152  (column-number :int))
153
154(defcfun sqlite3-bind-parameter-count :int
155  (statement p-sqlite3-stmt))
156
157(defcfun sqlite3-bind-parameter-name :string
158  (statement p-sqlite3-stmt)
159  (column-number :int))
160
161(defcfun sqlite3-bind-parameter-index :int
162  (statement p-sqlite3-stmt)
163  (name :string))
164
165(defcfun sqlite3-bind-double error-code
166  (statement p-sqlite3-stmt)
167  (parameter-index :int)
168  (value :double))
169
170(defcfun sqlite3-bind-int64 error-code
171  (statement p-sqlite3-stmt)
172  (parameter-index :int)
173  (value :int64))
174
175(defcfun sqlite3-bind-null error-code
176  (statement p-sqlite3-stmt)
177  (parameter-index :int))
178
179(defcfun sqlite3-bind-text error-code
180  (statement p-sqlite3-stmt)
181  (parameter-index :int)
182  (value :string)
183  (octets-count :int)
184  (destructor :pointer))
185
186(defcfun sqlite3-bind-blob error-code
187  (statement p-sqlite3-stmt)
188  (parameter-index :int)
189  (value :pointer)
190  (bytes-count :int)
191  (destructor :pointer))
192
193(defconstant destructor-transient-address (mod -1 (expt 2 (* 8 (cffi:foreign-type-size :pointer)))))
194
195(defun destructor-transient () (cffi:make-pointer destructor-transient-address))
196
197(defun destructor-static () (cffi:make-pointer 0))
198
199(defcfun sqlite3-last-insert-rowid :int64
200  (db p-sqlite3))