r8887: pre 2.4.0
[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-row-pointer-type
56            #:sqlite-vm-pointer))
57
58 (in-package #:sqlite)
59
60 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
61 ;;;;
62 ;;;; Return values for sqlite_exec() and sqlite_step()
63 ;;;;
64 (defconstant SQLITE-OK            0  "Successful result")
65 (defconstant SQLITE-ERROR         1  "SQL error or missing database")
66 (defconstant SQLITE-ROW         100  "sqlite_step() has another row ready")
67 (defconstant SQLITE-DONE        101  "sqlite_step() has finished executing")
68
69 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
70 ;;;;
71 ;;;; Conditions.
72 ;;;;
73 (define-condition sqlite-error ()
74   ((message :initarg :message :reader sqlite-error-message :initform "")
75    (code :initarg :code :reader sqlite-error-code))
76   (:report (lambda (condition stream)
77              (let ((code (sqlite-error-code condition)))
78                (format stream "SQLite error [~A]: ~A"
79                        code (sqlite-error-message condition))))))
80
81 (defun signal-sqlite-error (code &optional message)
82   (let ((condition
83          (make-condition 'sqlite-error
84                          :code code
85                          :message (if message
86                                       message
87                                       (sqlite-error-string code)))))
88     (unless (signal condition)
89       (invoke-debugger condition))))
90
91 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 ;;;;
93 ;;;; Foreign types definitions.
94 ;;;;
95 (def-foreign-type errmsg (* :char))
96 (def-foreign-type sqlite-db :pointer-void)
97 (def-foreign-type sqlite-vm :pointer-void)
98 (def-foreign-type string-pointer (* (* :char)))
99 (def-foreign-type sqlite-row-pointer (* string-pointer))
100
101 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
102 ;;;
103 ;;; Lisp types used in declarations.
104 ;;;;
105 (def-type sqlite-db sqlite-db)
106 (def-type sqlite-row string-pointer)
107 (def-type sqlite-row-pointer-type (* string-pointer))
108 (def-type sqlite-vm-pointer (* sqlite-vm))
109
110 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
111 ;;;;
112 ;;;; Library functions.
113 ;;;;
114 (defmacro def-sqlite-function (name args &key (returning :void))
115   `(def-function ,name ,args
116     :module "sqlite"
117     :returning ,returning))
118
119 (def-sqlite-function
120     "sqlite_error_string"
121     ((error-code :int))
122   :returning :cstring)
123
124 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
125 ;;;;
126 ;;;; Core API.
127 ;;;;
128 (declaim (inline %open))
129 (def-sqlite-function
130     ("sqlite_open" %open)
131     ((dbname :cstring)
132      (mode :int)
133      (error-message (* errmsg)))
134   :returning sqlite-db)
135
136 (declaim (inline sqlite-close))
137 (def-sqlite-function
138     "sqlite_close"
139     ((db sqlite-db)))
140
141 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
142 ;;;;
143 ;;;; New API.
144 ;;;;
145 (declaim (inline %compile))
146 (def-sqlite-function
147     ("sqlite_compile" %compile)
148     ((db sqlite-db)
149      (sql :cstring)
150      (sql-tail (* (* :char)))
151      (vm (* sqlite-vm))
152      (error-message (* errmsg)))
153   :returning :int)
154
155 (declaim (inline %step))
156 (def-sqlite-function
157     ("sqlite_step" %step)
158     ((vm sqlite-vm)
159      (cols-n (* :int))
160      (cols (* (* (* :char))))
161      (col-names (* (* (* :char)))))
162   :returning :int)
163
164 (declaim (inline %finalize))
165 (def-sqlite-function
166     ("sqlite_finalize" %finalize)
167     ((vm sqlite-vm)
168      (error-message (* errmsg)))
169   :returning :int)
170
171 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
172 ;;;;
173 ;;;; Extended API.
174 ;;;;
175 (declaim (inline sqlite-last-insert-rowid))
176 (def-sqlite-function
177     "sqlite_last_insert_rowid"
178     ((db sqlite-db))
179   :returning :int)
180
181 (declaim (inline %get-table))
182 (def-sqlite-function
183     ("sqlite_get_table" %get-table)
184     ((db sqlite-db)
185      (sql :cstring)
186      (result (* (* (* :char))))
187      (rows-n (* :int))
188      (cols-n (* :int))
189      (error-message (* errmsg)))
190   :returning :int)
191
192 (declaim (inline %free-table))
193 (def-sqlite-function
194     ("sqlite_free_table" %free-table)
195     ((rows :pointer-void)))
196
197 (declaim (inline sqlite-libversion))
198 (def-sqlite-function
199     "sqlite_libversion"
200     ()
201   :returning :cstring)
202
203 (declaim (inline sqlite-libencoding))
204 (def-sqlite-function
205     "sqlite_libencoding"
206     ()
207   :returning :cstring)
208
209 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
210 ;;;;
211 ;;;; Wrapper functions.
212 ;;;;
213 (defparameter sqlite-version (sqlite-libversion))
214 (defparameter sqlite-encoding (sqlite-libencoding))
215
216 (defun sqlite-open (db-name &optional (mode 0))
217   (with-cstring (db-name-native db-name) 
218     (let ((db (%open db-name-native mode nil)))
219       (if (null-pointer-p db)
220           (signal-sqlite-error SQLITE-ERROR
221                                (format nil "unable to open ~A" db-name))
222           db))))
223
224 (defun sqlite-compile (db sql)
225   (with-cstring (sql-native sql)
226     (let ((vm (allocate-foreign-object 'sqlite-vm)))
227       (with-foreign-object (sql-tail '(* :char))
228         (let ((result (%compile db sql-native sql-tail vm nil)))
229           (if (= result SQLITE-OK)
230               vm
231               (progn
232                 (free-foreign-object vm)
233                 (signal-sqlite-error result))))))))
234
235 (defun sqlite-step (vm)
236   (declare (type sqlite-vm-pointer vm))
237   (with-foreign-object (cols-n :int)
238     (let ((cols (allocate-foreign-object '(* (* :char))))
239           (col-names (allocate-foreign-object '(* (* :char)))))
240       (declare (type sqlite-row-pointer-type cols col-names))
241       (let ((result (%step (deref-pointer vm 'sqlite-vm)
242                            cols-n cols col-names)))
243         (cond
244           ((= result SQLITE-ROW)
245            (let ((n (deref-pointer cols-n :int)))
246              (values n cols col-names)))
247           ((= result SQLITE-DONE)
248            (free-foreign-object cols)
249            (free-foreign-object col-names)
250            (values 0 (make-null-pointer 'string-pointer)
251                    (make-null-pointer 'string-pointer)))
252           (t
253            (free-foreign-object cols)
254            (free-foreign-object col-names)
255            (signal-sqlite-error result)))))))
256
257 (defun sqlite-finalize (vm)
258   (declare (type sqlite-vm-pointer vm))
259   (let ((result (%finalize (deref-pointer vm 'sqlite-vm) nil)))
260     (if (= result SQLITE-OK)
261         (progn
262           (free-foreign-object vm)
263           t)
264         (signal-sqlite-error result))))
265
266 (defun sqlite-get-table (db sql)
267   (declare (type sqlite-db db))
268   (with-cstring (sql-native sql)
269     (let ((rows (allocate-foreign-object '(* (* :char)))))
270       (declare (type sqlite-row-pointer-type rows))
271       (with-foreign-object (rows-n :int)
272         (with-foreign-object (cols-n :int)
273           (let ((result (%get-table db sql-native rows rows-n cols-n nil)))
274             (if (= result SQLITE-OK)
275                 (let ((cn (deref-pointer cols-n :int))
276                       (rn (deref-pointer rows-n :int)))
277                   (values rows rn cn))
278                 (progn
279                   (free-foreign-object rows)
280                   (signal-sqlite-error result)))))))))
281
282 (declaim (inline sqlite-free-table))
283 (defun sqlite-free-table (table)
284   (declare (type sqlite-row-pointer-type table))
285   (%free-table (deref-pointer table 'sqlite-row-pointer)))
286
287 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
288 ;;;;
289 ;;;; Utility functions.
290 ;;;;
291 (declaim (inline make-null-row))
292 (defun make-null-row ()
293   (uffi:make-null-pointer 'string-pointer))
294
295 (declaim (inline make-null-vm))
296 (defun make-null-vm ()
297   (uffi:make-null-pointer 'sqlite-vm))
298
299 (declaim (inline null-row-p))
300 (defun null-row-p (row)
301   (null-pointer-p row))
302
303 (declaim (inline sqlite-aref))
304 (defun sqlite-aref (a n)
305   (declare (type sqlite-row-pointer-type a))
306   (convert-from-foreign-string (deref-array  (deref-pointer a 'sqlite-row-pointer) '(:array :char) n)))
307
308 (declaim (inline sqlite-free-row))
309 (defun sqlite-free-row (row)
310   (declare (type sqlite-row-pointer-type row))
311   (free-foreign-object row))