16f1925ba4d35c80f49ab4d20001030853ffdb0e
[clsql.git] / db-mysql / mysql-sql.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          mysql-sql.lisp
6 ;;;; Purpose:       High-level MySQL interface using UFFI
7 ;;;; Programmers:   Kevin M. Rosenberg based on
8 ;;;;                Original code by Pierre R. Mai 
9 ;;;; Date Started:  Feb 2002
10 ;;;;
11 ;;;; $Id: mysql-sql.lisp,v 1.8 2003/07/21 01:45:45 kevin Exp $
12 ;;;;
13 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
15 ;;;;
16 ;;;; CLSQL users are granted the rights to distribute and use this software
17 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
18 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
19 ;;;; *************************************************************************
20
21 (eval-when (:compile-toplevel)
22   (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0))))
23
24 ;;;; Modified by Kevin Rosenberg, Feb 20002
25 ;;;; -- Added support for Allegro CL and Lispworks using UFFI layer
26 ;;;; -- Changed database-connect to use mysql-real-connect. This way,
27 ;;;;    can avoid using double (unwind-protect)
28 ;;;; -- Changed database-connect to have MySQL library allocate space
29 ;;;;    for MYSQL structure. This will make the code more robust in
30 ;;;;    the event that MySQL library changes the size of the mysql-mysql
31 ;;;;    structure.
32 ;;;;
33 ;;;; Mar 2002
34 ;;;; Added field types
35
36 (defpackage #:clsql-mysql
37     (:use #:common-lisp #:clsql-base-sys #:mysql #:clsql-uffi)
38     (:export #:mysql-database)
39     (:documentation "This is the CLSQL interface to MySQL."))
40
41 (in-package #:clsql-mysql)
42
43 ;;; Field conversion functions
44
45 (defun make-type-list-for-auto (num-fields res-ptr)
46   (declare (fixnum num-fields))
47   (let ((new-types '())
48         #+ignore (field-vec (mysql-fetch-fields res-ptr)))
49     (dotimes (i num-fields)
50       (declare (fixnum i))
51       (let* ( (field (mysql-fetch-field-direct res-ptr i))
52              #+ignore (field (uffi:deref-array field-vec '(:array mysql-field) i))
53               (type (uffi:get-slot-value field 'mysql-field 'type)))
54         (push
55          (case type
56            ((#.mysql-field-types#tiny 
57              #.mysql-field-types#short
58              #.mysql-field-types#int24
59              #.mysql-field-types#long)
60             :int32)
61            (#.mysql-field-types#longlong
62             :int64)
63            ((#.mysql-field-types#double
64              #.mysql-field-types#float
65              #.mysql-field-types#decimal)
66             :double)
67            (otherwise
68             t))
69          new-types)))
70     (nreverse new-types)))
71
72 (defun canonicalize-types (types num-fields res-ptr)
73   (when types
74     (let ((auto-list (make-type-list-for-auto num-fields res-ptr)))
75       (cond
76         ((listp types)
77          (canonicalize-type-list types auto-list))
78         ((eq types :auto)
79          auto-list)
80         (t
81          nil)))))
82
83 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
84   t)
85
86 (uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
87 (uffi:def-type mysql-row-def mysql-row)
88 (uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
89
90 (defclass mysql-database (database)
91   ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
92               :type mysql-mysql-ptr-def)))
93
94 (defmethod database-type ((database mysql-database))
95   :mysql)
96
97 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
98   (check-connection-spec connection-spec database-type (host db user password))
99   (destructuring-bind (host db user password) connection-spec
100     (declare (ignore password))
101     (concatenate 'string host "/" db "/" user)))
102
103 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
104   (check-connection-spec connection-spec database-type (host db user password))
105   (destructuring-bind (host db user password) connection-spec
106     (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
107           (socket nil))
108       (if (uffi:null-pointer-p mysql-ptr)
109           (error 'clsql-connect-error
110                  :database-type database-type
111                  :connection-spec connection-spec
112                  :errno (mysql-errno mysql-ptr)
113                  :error (mysql-error-string mysql-ptr))
114         (uffi:with-cstrings ((host-native host)
115                             (user-native user)
116                             (password-native password)
117                             (db-native db)
118                             (socket-native socket))
119           (let ((error-occurred nil))
120             (unwind-protect
121                 (if (uffi:null-pointer-p 
122                      (mysql-real-connect 
123                       mysql-ptr host-native user-native password-native
124                       db-native 0 socket-native 0))
125                     (progn
126                       (setq error-occurred t)
127                       (error 'clsql-connect-error
128                              :database-type database-type
129                              :connection-spec connection-spec
130                              :errno (mysql-errno mysql-ptr)
131                              :error (mysql-error-string mysql-ptr)))
132                   (make-instance 'mysql-database
133                     :name (database-name-from-spec connection-spec
134                                                    database-type)
135                     :connection-spec connection-spec
136                     :mysql-ptr mysql-ptr))
137               (when error-occurred (mysql-close mysql-ptr)))))))))
138
139
140 (defmethod database-disconnect ((database mysql-database))
141   (mysql-close (database-mysql-ptr database))
142   (setf (database-mysql-ptr database) nil)
143   t)
144
145
146 (defmethod database-query (query-expression (database mysql-database) 
147                            types)
148   (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
149   (let ((mysql-ptr (database-mysql-ptr database)))
150     (uffi:with-cstring (query-native query-expression)
151       (if (zerop (mysql-query mysql-ptr query-native))
152           (let ((res-ptr (mysql-use-result mysql-ptr)))
153             (if res-ptr
154                 (unwind-protect
155                      (let ((num-fields (mysql-num-fields res-ptr)))
156                        (declare (fixnum num-fields))
157                        (setq types (canonicalize-types 
158                                     types num-fields
159                                     res-ptr))
160                        (loop for row = (mysql-fetch-row res-ptr)
161                              until (uffi:null-pointer-p row)
162                            collect
163                              (do* ((rlist (make-list num-fields))
164                                    (i 0 (1+ i))
165                                    (pos rlist (cdr pos)))
166                                  ((= i num-fields) rlist)
167                                (declare (fixnum i))
168                                (setf (car pos)  
169                                  (convert-raw-field
170                                   (uffi:deref-array row '(:array
171                                                           (* :unsigned-char))
172                                                     i)
173                                   types i)))))
174                   (mysql-free-result res-ptr))
175                 (error 'clsql-sql-error
176                        :database database
177                        :expression query-expression
178                        :errno (mysql-errno mysql-ptr)
179                        :error (mysql-error-string mysql-ptr))))
180           (error 'clsql-sql-error
181                  :database database
182                  :expression query-expression
183                  :errno (mysql-errno mysql-ptr)
184                  :error (mysql-error-string mysql-ptr))))))
185
186 #+ignore
187 (defmethod database-query (query-expression (database mysql-database) 
188                            types)
189   (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
190   (let ((mysql-ptr (database-mysql-ptr database)))
191     (uffi:with-cstring (query-native query-expression)
192       (if (zerop (mysql-query mysql-ptr query-native))
193           (let ((res-ptr (mysql-use-result mysql-ptr)))
194             (if res-ptr
195                 (unwind-protect
196                      (let ((num-fields (mysql-num-fields res-ptr)))
197                        (declare (fixnum num-fields))
198                        (setq types (canonicalize-types 
199                                     types num-fields
200                                     res-ptr))
201                        (loop for row = (mysql-fetch-row res-ptr)
202                              until (uffi:null-pointer-p row)
203                              collect
204                              (loop for i fixnum from 0 below num-fields
205                                    collect
206                                    (convert-raw-field
207                                     (uffi:deref-array row '(:array
208                                                             (* :unsigned-char))
209                                                       i)
210                                     types i))))
211                   (mysql-free-result res-ptr))
212                 (error 'clsql-sql-error
213                        :database database
214                        :expression query-expression
215                        :errno (mysql-errno mysql-ptr)
216                        :error (mysql-error-string mysql-ptr))))
217           (error 'clsql-sql-error
218                  :database database
219                  :expression query-expression
220                  :errno (mysql-errno mysql-ptr)
221                  :error (mysql-error-string mysql-ptr))))))
222
223 (defmethod database-execute-command (sql-expression (database mysql-database))
224   (uffi:with-cstring (sql-native sql-expression)
225     (let ((mysql-ptr (database-mysql-ptr database)))
226       (declare (type mysql-mysql-ptr-def mysql-ptr))
227       (if (zerop (mysql-query mysql-ptr sql-native))
228           t
229         (error 'clsql-sql-error
230                :database database
231                :expression sql-expression
232                :errno (mysql-errno mysql-ptr)
233                :error (mysql-error-string mysql-ptr))))))
234
235
236 (defstruct mysql-result-set 
237   (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def)
238   (types nil :type list)
239   (num-fields 0 :type fixnum)
240   (full-set nil :type boolean))
241
242
243 (defmethod database-query-result-set (query-expression 
244                                       (database mysql-database)
245                                       &key full-set types)
246   (uffi:with-cstring (query-native query-expression)
247     (let ((mysql-ptr (database-mysql-ptr database)))
248      (declare (type mysql-mysql-ptr-def mysql-ptr))
249       (if (zerop (mysql-query mysql-ptr query-native))
250           (let ((res-ptr (if full-set
251                              (mysql-store-result mysql-ptr)
252                            (mysql-use-result mysql-ptr))))
253             (declare (type mysql-mysql-res-ptr-def res-ptr))
254             (if (not (uffi:null-pointer-p res-ptr))
255                 (let* ((num-fields (mysql-num-fields res-ptr))
256                        (result-set (make-mysql-result-set
257                                     :res-ptr res-ptr
258                                     :num-fields num-fields
259                                     :full-set full-set
260                                     :types
261                                     (canonicalize-types 
262                                      types num-fields
263                                      res-ptr)))) 
264                   (if full-set
265                       (values result-set
266                               num-fields
267                               (mysql-num-rows res-ptr))
268                       (values result-set
269                               num-fields)))
270                 (error 'clsql-sql-error
271                      :database database
272                      :expression query-expression
273                      :errno (mysql-errno mysql-ptr)
274                      :error (mysql-error-string mysql-ptr))))
275         (error 'clsql-sql-error
276                :database database
277                :expression query-expression
278                :errno (mysql-errno mysql-ptr)
279                :error (mysql-error-string mysql-ptr))))))
280
281 (defmethod database-dump-result-set (result-set (database mysql-database))
282   (mysql-free-result (mysql-result-set-res-ptr result-set))
283   t)
284
285
286 (defmethod database-store-next-row (result-set (database mysql-database) list)
287   (let* ((res-ptr (mysql-result-set-res-ptr result-set))
288          (row (mysql-fetch-row res-ptr))
289          (types (mysql-result-set-types result-set)))
290     (declare (type mysql-mysql-res-ptr-def res-ptr)
291              (type mysql-row-def row))
292     (unless (uffi:null-pointer-p row)
293       (loop for i from 0 below (mysql-result-set-num-fields result-set)
294             for rest on list
295             do
296             (setf (car rest) 
297                   (convert-raw-field
298                    (uffi:deref-array row '(:array (* :unsigned-char)) i)
299                    types
300                    i)))
301       list)))
302
303
304 (when (clsql-base-sys:database-type-library-loaded :mysql)
305   (clsql-base-sys:initialize-database-type :database-type :mysql))