fee81b5c7822272ce81115ae8b147b13abb604df
[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.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.lisp,v 1.5 2003/06/08 12:48:55 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   (let ((new-types '())
47         #+ignore (field-vec (mysql-fetch-fields res-ptr)))
48     (dotimes (i num-fields)
49       (declare (fixnum i))
50       (let* ( (field (mysql-fetch-field-direct res-ptr i))
51              #+ignore (field (uffi:deref-array field-vec '(:array mysql-field) i))
52               (type (uffi:get-slot-value field 'mysql-field 'type)))
53         (push
54          (case type
55            ((#.mysql-field-types#tiny 
56              #.mysql-field-types#short
57              #.mysql-field-types#int24
58              #.mysql-field-types#long)
59             :int32)
60            (#.mysql-field-types#longlong
61             :int64)
62            ((#.mysql-field-types#double
63              #.mysql-field-types#float
64              #.mysql-field-types#decimal)
65             :double)
66            (otherwise
67             t))
68          new-types)))
69     (nreverse new-types)))
70
71 (defun canonicalize-types (types num-fields res-ptr)
72   (if (null types)
73       nil
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                              (loop for i fixnum from 0 below num-fields
164                                    collect
165                                    (convert-raw-field
166                                     (uffi:deref-array row '(:array
167                                                             (* :unsigned-char))
168                                                       i)
169                                     types i))))
170                   (mysql-free-result res-ptr))
171                 (error 'clsql-sql-error
172                        :database database
173                        :expression query-expression
174                        :errno (mysql-errno mysql-ptr)
175                        :error (mysql-error-string mysql-ptr))))
176           (error 'clsql-sql-error
177                  :database database
178                  :expression query-expression
179                  :errno (mysql-errno mysql-ptr)
180                  :error (mysql-error-string mysql-ptr))))))
181
182 (defmethod database-execute-command (sql-expression (database mysql-database))
183   (uffi:with-cstring (sql-native sql-expression)
184     (let ((mysql-ptr (database-mysql-ptr database)))
185       (declare (type mysql-mysql-ptr-def mysql-ptr))
186       (if (zerop (mysql-query mysql-ptr sql-native))
187           t
188         (error 'clsql-sql-error
189                :database database
190                :expression sql-expression
191                :errno (mysql-errno mysql-ptr)
192                :error (mysql-error-string mysql-ptr))))))
193
194
195 (defstruct mysql-result-set 
196   (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def)
197   (types nil :type list)
198   (num-fields 0 :type fixnum)
199   (full-set nil :type boolean))
200
201
202 (defmethod database-query-result-set (query-expression 
203                                       (database mysql-database)
204                                       &key full-set types)
205   (uffi:with-cstring (query-native query-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 query-native))
209           (let ((res-ptr (if full-set
210                              (mysql-store-result mysql-ptr)
211                            (mysql-use-result mysql-ptr))))
212             (declare (type mysql-mysql-res-ptr-def res-ptr))
213             (if (not (uffi:null-pointer-p res-ptr))
214                 (let* ((num-fields (mysql-num-fields res-ptr))
215                        (result-set (make-mysql-result-set
216                                     :res-ptr res-ptr
217                                     :num-fields num-fields
218                                     :full-set full-set
219                                     :types
220                                     (canonicalize-types 
221                                      types num-fields
222                                      res-ptr)))) 
223                   (if full-set
224                       (values result-set
225                               num-fields
226                               (mysql-num-rows res-ptr))
227                       (values result-set
228                               num-fields)))
229                 (error 'clsql-sql-error
230                      :database database
231                      :expression query-expression
232                      :errno (mysql-errno mysql-ptr)
233                      :error (mysql-error-string mysql-ptr))))
234         (error 'clsql-sql-error
235                :database database
236                :expression query-expression
237                :errno (mysql-errno mysql-ptr)
238                :error (mysql-error-string mysql-ptr))))))
239
240 (defmethod database-dump-result-set (result-set (database mysql-database))
241   (mysql-free-result (mysql-result-set-res-ptr result-set))
242   t)
243
244
245 (defmethod database-store-next-row (result-set (database mysql-database) list)
246   (let* ((res-ptr (mysql-result-set-res-ptr result-set))
247          (row (mysql-fetch-row res-ptr))
248          (types (mysql-result-set-types result-set)))
249     (declare (type mysql-mysql-res-ptr-def res-ptr)
250              (type mysql-row-def row))
251     (unless (uffi:null-pointer-p row)
252       (loop for i from 0 below (mysql-result-set-num-fields result-set)
253             for rest on list
254             do
255             (setf (car rest) 
256                   (convert-raw-field
257                    (uffi:deref-array row '(:array (* :unsigned-char)) i)
258                    types
259                    i)))
260       list)))
261
262
263 (when (clsql-base-sys:database-type-library-loaded :mysql)
264   (clsql-base-sys:initialize-database-type :database-type :mysql)
265   (pushnew :mysql cl:*features*))