r1798: Initial support for pooled connections
[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.19 2002/04/27 20:58:11 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 :clsql-uffi)
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 make-type-list-for-auto (num-fields res-ptr)
45   (let ((new-types '())
46         #+ignore (field-vec (mysql-fetch-fields res-ptr)))
47     (dotimes (i num-fields)
48       (declare (fixnum i))
49       (let* ( (field (mysql-fetch-field-direct res-ptr i))
50              #+ignore (field (uffi:deref-array field-vec 'mysql-field-vector i))
51               (type (uffi:get-slot-value field 'mysql-field 'type)))
52         (push
53          (case type
54            ((#.mysql-field-types#tiny 
55              #.mysql-field-types#short
56              #.mysql-field-types#int24
57              #.mysql-field-types#long)
58             :int32)
59            (#.mysql-field-types#longlong
60             :int64)
61            ((#.mysql-field-types#double
62              #.mysql-field-types#float
63              #.mysql-field-types#decimal)
64             :double)
65            (otherwise
66             t))
67          new-types)))
68     (nreverse new-types)))
69
70 (defun canonicalize-types (types num-fields res-ptr)
71   (if (null types)
72       nil
73       (let ((auto-list (make-type-list-for-auto num-fields res-ptr)))
74         (cond
75           ((listp types)
76            (canonicalize-type-list types auto-list))
77           ((eq types :auto)
78            auto-list)
79           (t
80            nil)))))
81
82 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
83   t)
84
85 (uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
86 (uffi:def-type mysql-row-def mysql-row)
87 (uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
88
89 (defclass mysql-database (database)
90   ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
91               :type mysql-mysql-ptr-def)))
92
93 (defmethod database-type ((database mysql-database))
94   :mysql)
95
96 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
97   (check-connection-spec connection-spec database-type (host db user password))
98   (destructuring-bind (host db user password) connection-spec
99     (declare (ignore password))
100     (concatenate 'string host "/" db "/" user)))
101
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   (with-slots (mysql-ptr) database
149     (uffi:with-cstring (query-native query-expression)
150        (if (zerop (mysql-query mysql-ptr query-native))
151            (let ((res-ptr (mysql-use-result mysql-ptr)))
152              (if res-ptr
153                  (let ((num-fields (mysql-num-fields res-ptr)))
154                    (setq types (canonicalize-types 
155                                       types num-fields
156                                       res-ptr))
157                    (unwind-protect
158                         (loop for row = (mysql-fetch-row res-ptr)
159                               until (uffi:null-pointer-p row)
160                               collect
161                               (loop for i from 0 below num-fields
162                                     collect
163                                     (convert-raw-field
164                                      (uffi:deref-array row 'mysql-row i)
165                                      types i)))
166                      (mysql-free-result res-ptr)))
167                (error 'clsql-sql-error
168                       :database database
169                       :expression query-expression
170                       :errno (mysql-errno mysql-ptr)
171                       :error (mysql-error-string mysql-ptr))))
172          (error 'clsql-sql-error
173                 :database database
174                 :expression query-expression
175                 :errno (mysql-errno mysql-ptr)
176                 :error (mysql-error-string mysql-ptr))))))
177
178 (defmethod database-execute-command (sql-expression (database mysql-database))
179   (uffi:with-cstring (sql-native sql-expression)
180     (let ((mysql-ptr (database-mysql-ptr database)))
181       (declare (type mysql-mysql-ptr-def mysql-ptr))
182       (if (zerop (mysql-query mysql-ptr sql-native))
183           t
184         (error 'clsql-sql-error
185                :database database
186                :expression sql-expression
187                :errno (mysql-errno mysql-ptr)
188                :error (mysql-error-string mysql-ptr))))))
189
190 (defstruct mysql-result-set
191   (res-ptr (uffi:make-null-pointer 'mysql-mysql-res)
192            :type mysql-mysql-res-ptr-def)
193   (types nil)
194   (num-fields nil :type fixnum)
195   (full-set nil :type boolean))
196
197
198 (defmethod database-query-result-set (query-expression 
199                                       (database mysql-database)
200                                       &key full-set types)
201   (uffi:with-cstring (query-native query-expression)
202     (let ((mysql-ptr (database-mysql-ptr database)))
203      (declare (type mysql-mysql-ptr-def mysql-ptr))
204       (if (zerop (mysql-query mysql-ptr query-native))
205           (let ((res-ptr (if full-set
206                              (mysql-store-result mysql-ptr)
207                            (mysql-use-result mysql-ptr))))
208             (declare (type mysql-mysql-res-ptr-def res-ptr))
209             (if (not (uffi:null-pointer-p res-ptr))
210                 (let* ((num-fields (mysql-num-fields res-ptr))
211                        (result-set (make-mysql-result-set
212                                     :res-ptr res-ptr
213                                     :num-fields num-fields
214                                     :full-set full-set
215                                     :types
216                                     (canonicalize-types 
217                                      types num-fields
218                                      res-ptr)))) 
219                   (if full-set
220                       (values result-set
221                               num-fields
222                               (mysql-num-rows res-ptr))
223                       (values result-set
224                               num-fields)))
225                 (error 'clsql-sql-error
226                      :database database
227                      :expression query-expression
228                      :errno (mysql-errno mysql-ptr)
229                      :error (mysql-error-string mysql-ptr))))
230         (error 'clsql-sql-error
231                :database database
232                :expression query-expression
233                :errno (mysql-errno mysql-ptr)
234                :error (mysql-error-string mysql-ptr))))))
235
236 (defmethod database-dump-result-set (result-set (database mysql-database))
237   (mysql-free-result (mysql-result-set-res-ptr result-set))
238   t)
239
240
241 (defmethod database-store-next-row (result-set (database mysql-database) list)
242   (let* ((res-ptr (mysql-result-set-res-ptr result-set))
243          (row (mysql-fetch-row res-ptr))
244          (types (mysql-result-set-types result-set)))
245     (declare (type mysql-mysql-res-ptr-def res-ptr)
246              (type mysql-row-def row))
247     (unless (uffi:null-pointer-p row)
248       (loop for i from 0 below (mysql-result-set-num-fields result-set)
249             for rest on list
250             do
251             (setf (car rest) 
252                   (convert-raw-field
253                    (uffi:deref-array row 'mysql-row i)
254                    types
255                    i)))
256       list)))
257
258