r8886: fix for type declaration
[clsql.git] / db-sqlite / sqlite-api-uffi.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          sqlite-api-uffi.lisp
6 ;;;; Purpose:       Low-level SQLite interface using UFFI
7 ;;;; Programmers:   Aurelio Bignoli and Kevin Rosenberg
8 ;;;; Date Started:  Nov 2003
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2003 by Aurelio Bignoli
13 ;;;;
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
18
19 (in-package #:cl-user)
20
21 (defpackage #:sqlite
22   (:use #:common-lisp #:uffi)
23     (:export
24            ;;; Conditions
25            #:sqlite-error
26            #:sqlite-error-code
27            #:sqlite-error-message
28            
29            ;;; Core API.
30            #:sqlite-open
31            #:sqlite-close
32
33            ;;; New API.
34            #:sqlite-compile
35            #:sqlite-step
36            #:sqlite-finalize
37            
38            ;;; Extended API.
39            #:sqlite-get-table
40            #:sqlite-free-table
41            #:sqlite-version             ; Defined as constant.
42            #:sqlite-encoding            ; Defined as constant.
43            #:sqlite-last-insert-rowid
44
45            ;;; Utility functions.
46            #:make-null-row
47            #:make-null-vm
48            #:null-row-p
49            #:sqlite-aref
50            #:sqlite-free-row
51            
52            ;;; Types.
53            #:sqlite-row
54            #:sqlite-row-pointer
55            #:sqlite-vm-pointer))
56
57 (in-package #:sqlite)
58
59 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
60 ;;;;
61 ;;;; Return values for sqlite_exec() and sqlite_step()
62 ;;;;
63 (defconstant SQLITE-OK            0  "Successful result")
64 (defconstant SQLITE-ERROR         1  "SQL error or missing database")
65 (defconstant SQLITE-ROW         100  "sqlite_step() has another row ready")
66 (defconstant SQLITE-DONE        101  "sqlite_step() has finished executing")
67
68 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
69 ;;;;
70 ;;;; Conditions.
71 ;;;;
72 (define-condition sqlite-error ()
73   ((message :initarg :message :reader sqlite-error-message :initform "")
74    (code :initarg :code :reader sqlite-error-code))
75   (:report (lambda (condition stream)
76              (let ((code (sqlite-error-code condition)))
77                (format stream "SQLite error [~A]: ~A"
78                        code (sqlite-error-message condition))))))
79
80 (defun signal-sqlite-error (code &optional message)
81   (let ((condition
82          (make-condition 'sqlite-error
83                          :code code
84                          :message (if message
85                                       message
86                                       (sqlite-error-string code)))))
87     (unless (signal condition)
88       (invoke-debugger condition))))
89
90 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
91 ;;;;
92 ;;;; Foreign types definitions.
93 ;;;;
94 (def-foreign-type errmsg (* :char))
95 (def-foreign-type sqlite-db :pointer-void)
96 (def-foreign-type sqlite-vm :pointer-void)
97 (def-foreign-type string-pointer (* (* :char)))
98 (def-foreign-type sqlite-row-pointer (* string-pointer))
99
100 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
101 ;;;
102 ;;; Lisp types used in declarations.
103 ;;;;
104 (def-type sqlite-db sqlite-db)
105 (def-type sqlite-row string-pointer)
106 (def-type sqlite-row-pointer-type (* string-pointer))
107 (def-type sqlite-vm-pointer (* sqlite-vm))
108
109 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
110 ;;;;
111 ;;;; Library functions.
112 ;;;;
113 (defmacro def-sqlite-function (name args &key (returning :void))
114   `(def-function ,name ,args
115     :module "sqlite"
116     :returning ,returning))
117
118 (def-sqlite-function
119     "sqlite_error_string"
120     ((error-code :int))
121   :returning :cstring)
122
123 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
124 ;;;;
125 ;;;; Core API.
126 ;;;;
127 (declaim (inline %open))
128 (def-sqlite-function
129     ("sqlite_open" %open)
130     ((dbname :cstring)
131      (mode :int)
132      (error-message (* errmsg)))
133   :returning sqlite-db)
134
135 (declaim (inline sqlite-close))
136 (def-sqlite-function
137     "sqlite_close"
138     ((db sqlite-db)))
139
140 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
141 ;;;;
142 ;;;; New API.
143 ;;;;
144 (declaim (inline %compile))
145 (def-sqlite-function
146     ("sqlite_compile" %compile)
147     ((db sqlite-db)
148      (sql :cstring)
149      (sql-tail (* (* :char)))
150      (vm (* sqlite-vm))
151      (error-message (* errmsg)))
152   :returning :int)
153
154 (declaim (inline %step))
155 (def-sqlite-function
156     ("sqlite_step" %step)
157     ((vm sqlite-vm)
158      (cols-n (* :int))
159      (cols (* (* (* :char))))
160      (col-names (* (* (* :char)))))
161   :returning :int)
162
163 (declaim (inline %finalize))
164 (def-sqlite-function
165     ("sqlite_finalize" %finalize)
166     ((vm sqlite-vm)
167      (error-message (* errmsg)))
168   :returning :int)
169
170 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
171 ;;;;
172 ;;;; Extended API.
173 ;;;;
174 (declaim (inline sqlite-last-insert-rowid))
175 (def-sqlite-function
176     "sqlite_last_insert_rowid"
177     ((db sqlite-db))
178   :returning :int)
179
180 (declaim (inline %get-table))
181 (def-sqlite-function
182     ("sqlite_get_table" %get-table)
183     ((db sqlite-db)
184      (sql :cstring)
185      (result (* (* (* :char))))
186      (rows-n (* :int))
187      (cols-n (* :int))
188      (error-message (* errmsg)))
189   :returning :int)
190
191 (declaim (inline %free-table))
192 (def-sqlite-function
193     ("sqlite_free_table" %free-table)
194     ((rows :pointer-void)))
195
196 (declaim (inline sqlite-libversion))
197 (def-sqlite-function
198     "sqlite_libversion"
199     ()
200   :returning :cstring)
201
202 (declaim (inline sqlite-libencoding))
203 (def-sqlite-function
204     "sqlite_libencoding"
205     ()
206   :returning :cstring)
207
208 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
209 ;;;;
210 ;;;; Wrapper functions.
211 ;;;;
212 (defparameter sqlite-version (sqlite-libversion))
213 (defparameter sqlite-encoding (sqlite-libencoding))
214
215 (defun sqlite-open (db-name &optional (mode 0))
216   (with-cstring (db-name-native db-name) 
217     (let ((db (%open db-name-native mode nil)))
218       (if (null-pointer-p db)
219           (signal-sqlite-error SQLITE-ERROR
220                                (format nil "unable to open ~A" db-name))
221           db))))
222
223 (defun sqlite-compile (db sql)
224   (with-cstring (sql-native sql)
225     (let ((vm (allocate-foreign-object 'sqlite-vm)))
226       (with-foreign-object (sql-tail '(* :char))
227         (let ((result (%compile db sql-native sql-tail vm nil)))
228           (if (= result SQLITE-OK)
229               vm
230               (progn
231                 (free-foreign-object vm)
232                 (signal-sqlite-error result))))))))
233
234 (defun sqlite-step (vm)
235   (declare (type sqlite-vm-pointer vm))
236   (with-foreign-object (cols-n :int)
237     (let ((cols (allocate-foreign-object '(* (* :char))))
238           (col-names (allocate-foreign-object '(* (* :char)))))
239       (declare (type sqlite-row-pointer-type cols col-names))
240       (let ((result (%step (deref-pointer vm 'sqlite-vm)
241                            cols-n cols col-names)))
242         (cond
243           ((= result SQLITE-ROW)
244            (let ((n (deref-pointer cols-n :int)))
245              (values n cols col-names)))
246           ((= result SQLITE-DONE)
247            (free-foreign-object cols)
248            (free-foreign-object col-names)
249            (values 0 (make-null-pointer 'string-pointer)
250                    (make-null-pointer 'string-pointer)))
251           (t
252            (free-foreign-object cols)
253            (free-foreign-object col-names)
254            (signal-sqlite-error result)))))))
255
256 (defun sqlite-finalize (vm)
257   (declare (type sqlite-vm-pointer vm))
258   (let ((result (%finalize (deref-pointer vm 'sqlite-vm) nil)))
259     (if (= result SQLITE-OK)
260         (progn
261           (free-foreign-object vm)
262           t)
263         (signal-sqlite-error result))))
264
265 (defun sqlite-get-table (db sql)
266   (declare (type sqlite-db db))
267   (with-cstring (sql-native sql)
268     (let ((rows (allocate-foreign-object '(* (* :char)))))
269       (declare (type sqlite-row-pointer-type rows))
270       (with-foreign-object (rows-n :int)
271         (with-foreign-object (cols-n :int)
272           (let ((result (%get-table db sql-native rows rows-n cols-n nil)))
273             (if (= result SQLITE-OK)
274                 (let ((cn (deref-pointer cols-n :int))
275                       (rn (deref-pointer rows-n :int)))
276                   (values rows rn cn))
277                 (progn
278                   (free-foreign-object rows)
279                   (signal-sqlite-error result)))))))))
280
281 (declaim (inline sqlite-free-table))
282 (defun sqlite-free-table (table)
283   (declare (type sqlite-row-pointer-type table))
284   (%free-table (deref-pointer table 'sqlite-row-pointer)))
285
286 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
287 ;;;;
288 ;;;; Utility functions.
289 ;;;;
290 (declaim (inline make-null-row))
291 (defun make-null-row ()
292   (uffi:make-null-pointer 'string-pointer))
293
294 (declaim (inline make-null-vm))
295 (defun make-null-vm ()
296   (uffi:make-null-pointer 'sqlite-vm))
297
298 (declaim (inline null-row-p))
299 (defun null-row-p (row)
300   (null-pointer-p row))
301
302 (declaim (inline sqlite-aref))
303 (defun sqlite-aref (a n)
304   (declare (type sqlite-row-pointer-type a))
305   (convert-from-foreign-string (deref-array  (deref-pointer a 'sqlite-row-pointer) '(:array :char) n)))
306
307 (declaim (inline sqlite-free-row))
308 (defun sqlite-free-row (row)
309   (declare (type sqlite-row-pointer-type row))
310   (free-foreign-object row))