r1667: Fixed type specifier
[clsql.git] / interfaces / mysql / mysql-sql.cl
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          mysql-sql.cl
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.cl,v 1.11 2002/03/27 00:25:03 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 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
22
23 ;;;; Modified by Kevin Rosenberg, Feb 20002
24 ;;;; -- Added support for Allegro CL and Lispworks using UFFI layer
25 ;;;; -- Changed database-connect to use mysql-real-connect. This way,
26 ;;;;    can avoid using double (unwind-protect)
27 ;;;; -- Changed database-connect to have MySQL library allocate space
28 ;;;;    for MYSQL structure. This will make the code more robust in
29 ;;;;    the event that MySQL library changes the size of the mysql-mysql
30 ;;;;    structure.
31 ;;;;
32 ;;;; Mar 2002
33 ;;;; Added field types
34
35 (defpackage :clsql-mysql
36     (:use :common-lisp :clsql-sys :mysql)
37     (:export #:mysql-database)
38     (:documentation "This is the CLSQL interface to MySQL."))
39
40 (in-package :clsql-mysql)
41
42 ;;; Field conversion functions
43
44 (defun canonicalize-types (types num-fields res-ptr)
45   (cond
46    ((if (listp types)
47         (let ((length-types (length types))
48               (new-types '()))
49           (loop for i from 0 below num-fields
50               do
51                 (if (>= i length-types)
52                     (push t new-types) ;; types is shorted than num-fields
53                   (push
54                    (case (nth i types)
55                      ((:int :long :double t)
56                       (nth i types))
57                      (t
58                       t))
59                    new-types)))
60           (nreverse new-types))))
61    ((eq types :auto)
62     (let ((new-types '())
63           #+ignore (field-vec (mysql-fetch-fields res-ptr)))
64       (dotimes (i num-fields)
65         (declare (fixnum i))
66         (let* ( (field (mysql-fetch-field-direct res-ptr i))
67                 #+ignore (field (uffi:deref-array field-vec 'mysql-field-vector i))
68                 (type (uffi:get-slot-value field 'mysql-field 'type)))
69           (push
70            (case type
71              ((#.mysql-field-types#tiny 
72                #.mysql-field-types#short
73                #.mysql-field-types#int24
74                #.mysql-field-types#long)
75               :int)
76              ((#.mysql-field-types#double
77                #.mysql-field-types#float
78                #.mysql-field-types#decimal)
79               :double)
80              (otherwise
81               t))
82            new-types)))
83       (nreverse new-types)))
84    (t
85     nil)))
86
87 (uffi:def-function "atoi"
88     ((str (* :unsigned-char)))
89   :returning :int)
90
91 (uffi:def-function "atol"
92     ((str (* :unsigned-char)))
93   :returning :long)
94
95 (uffi:def-function "atof"
96     ((str (* :unsigned-char)))
97   :returning :double)
98
99 (defun convert-raw-field (char-ptr types index)
100   (let ((type (if (listp types)
101                   (nth index types)
102                   types)))
103     (case type
104       (:int
105        (atoi char-ptr))
106       (:long
107        (atol char-ptr))
108       (:double
109        (atof char-ptr))
110       (otherwise
111        (uffi:convert-from-foreign-string char-ptr)))))
112
113 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
114   t)
115
116 (uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
117 (uffi:def-type mysql-row-def mysql-row)
118 (uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
119
120 (defclass mysql-database (database)
121   ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
122               :type mysql-mysql-ptr-def)))
123
124 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
125   (check-connection-spec connection-spec database-type (host db user password))
126   (destructuring-bind (host db user password) connection-spec
127     (declare (ignore password))
128     (concatenate 'string host "/" db "/" user)))
129
130
131 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
132   (check-connection-spec connection-spec database-type (host db user password))
133   (destructuring-bind (host db user password) connection-spec
134     (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
135           (socket nil))
136       (if (uffi:null-pointer-p mysql-ptr)
137           (error 'clsql-connect-error
138                  :database-type database-type
139                  :connection-spec connection-spec
140                  :errno (mysql-errno mysql-ptr)
141                  :error (mysql-error-string mysql-ptr))
142         (uffi:with-cstrings ((host-native host)
143                             (user-native user)
144                             (password-native password)
145                             (db-native db)
146                             (socket-native socket))
147           (let ((error-occurred nil))
148             (unwind-protect
149                 (if (uffi:null-pointer-p 
150                      (mysql-real-connect 
151                       mysql-ptr host-native user-native password-native
152                       db-native 0 socket-native 0))
153                     (progn
154                       (setq error-occurred t)
155                       (error 'clsql-connect-error
156                              :database-type database-type
157                              :connection-spec connection-spec
158                              :errno (mysql-errno mysql-ptr)
159                              :error (mysql-error-string mysql-ptr)))
160                   (make-instance 'mysql-database
161                     :name (database-name-from-spec connection-spec
162                                                    database-type)
163                     :mysql-ptr mysql-ptr))
164               (when error-occurred (mysql-close mysql-ptr)))))))))
165
166
167 (defmethod database-disconnect ((database mysql-database))
168   (mysql-close (database-mysql-ptr database))
169   (setf (database-mysql-ptr database) nil)
170   t)
171
172
173 (defmethod database-query (query-expression (database mysql-database) 
174                            types)
175   (with-slots (mysql-ptr) database
176     (uffi:with-cstring (query-native query-expression)
177        (if (zerop (mysql-query mysql-ptr query-native))
178            (let ((res-ptr (mysql-use-result mysql-ptr)))
179              (if res-ptr
180                  (let ((num-fields (mysql-num-fields res-ptr)))
181                    (setq types (canonicalize-types 
182                                       types num-fields
183                                       res-ptr))
184                    (unwind-protect
185                         (loop for row = (mysql-fetch-row res-ptr)
186                               until (uffi:null-pointer-p row)
187                               collect
188                               (loop for i from 0 below num-fields
189                                     collect
190                                     (uffi:convert-from-foreign-string
191                                      (uffi:deref-array row 'mysql-row i))))
192                      (mysql-free-result res-ptr)))
193                (error 'clsql-sql-error
194                       :database database
195                       :expression query-expression
196                       :errno (mysql-errno mysql-ptr)
197                       :error (mysql-error-string mysql-ptr))))
198          (error 'clsql-sql-error
199                 :database database
200                 :expression query-expression
201                 :errno (mysql-errno mysql-ptr)
202                 :error (mysql-error-string mysql-ptr))))))
203
204 (defmethod database-execute-command (sql-expression (database mysql-database))
205   (uffi:with-cstring (sql-native sql-expression)
206     (let ((mysql-ptr (database-mysql-ptr database)))
207       (declare (type mysql-mysql-ptr-def mysql-ptr))
208       (if (zerop (mysql-query mysql-ptr sql-native))
209           t
210         (error 'clsql-sql-error
211                :database database
212                :expression sql-expression
213                :errno (mysql-errno mysql-ptr)
214                :error (mysql-error-string mysql-ptr))))))
215
216 (defstruct mysql-result-set
217   (res-ptr (uffi:make-null-pointer 'mysql-mysql-res)
218            :type mysql-mysql-res-ptr-def)
219   (types nil)
220   (num-fields nil :type fixnum)
221   (full-set nil :type boolean))
222
223
224 (defmethod database-query-result-set (query-expression 
225                                       (database mysql-database)
226                                       &key full-set types)
227   (uffi:with-cstring (query-native query-expression)
228     (let ((mysql-ptr (database-mysql-ptr database)))
229      (declare (type mysql-mysql-ptr-def mysql-ptr))
230       (if (zerop (mysql-query mysql-ptr query-native))
231           (let ((res-ptr (if full-set
232                              (mysql-store-result mysql-ptr)
233                            (mysql-use-result mysql-ptr))))
234             (declare (type mysql-mysql-res-ptr-def res-ptr))
235             (if (not (uffi:null-pointer-p res-ptr))
236                 (let* ((num-fields (mysql-num-fields res-ptr))
237                        (result-set (make-mysql-result-set
238                                     :res-ptr res-ptr
239                                     :num-fields num-fields
240                                     :full-set full-set
241                                     :types
242                                     (canonicalize-types 
243                                      types num-fields
244                                      res-ptr)))) 
245                   (if full-set
246                       (values result-set
247                               num-fields
248                               (mysql-num-rows res-ptr))
249                       (values result-set
250                               num-fields)))
251                 (error 'clsql-sql-error
252                      :database database
253                      :expression query-expression
254                      :errno (mysql-errno mysql-ptr)
255                      :error (mysql-error-string mysql-ptr))))
256         (error 'clsql-sql-error
257                :database database
258                :expression query-expression
259                :errno (mysql-errno mysql-ptr)
260                :error (mysql-error-string mysql-ptr))))))
261
262 (defmethod database-dump-result-set (result-set (database mysql-database))
263   (mysql-free-result (mysql-result-set-res-ptr result-set))
264   t)
265
266
267
268 (defmethod database-store-next-row (result-set (database mysql-database) list)
269   (let* ((res-ptr (mysql-result-set-res-ptr result-set))
270          (row (mysql-fetch-row res-ptr))
271          (types (mysql-result-set-types result-set)))
272     (declare (type mysql-mysql-res-ptr-def res-ptr)
273              (type mysql-row-def row))
274     (unless (uffi:null-pointer-p row)
275       (loop for i from 0 below (mysql-result-set-num-fields result-set)
276             for rest on list
277             do
278             (setf (car rest) 
279                   (convert-raw-field
280                    (uffi:deref-array row 'mysql-row i)
281                    types
282                    i)))
283       list)))
284
285