7bd3bb9a0560a49512fe0896f0c0621932e51899
[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.17 2002/03/29 14:03:27 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           types)
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-name-from-spec (connection-spec (database-type (eql :mysql)))
95   (check-connection-spec connection-spec database-type (host db user password))
96   (destructuring-bind (host db user password) connection-spec
97     (declare (ignore password))
98     (concatenate 'string host "/" db "/" user)))
99
100
101 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
102   (check-connection-spec connection-spec database-type (host db user password))
103   (destructuring-bind (host db user password) connection-spec
104     (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
105           (socket nil))
106       (if (uffi:null-pointer-p mysql-ptr)
107           (error 'clsql-connect-error
108                  :database-type database-type
109                  :connection-spec connection-spec
110                  :errno (mysql-errno mysql-ptr)
111                  :error (mysql-error-string mysql-ptr))
112         (uffi:with-cstrings ((host-native host)
113                             (user-native user)
114                             (password-native password)
115                             (db-native db)
116                             (socket-native socket))
117           (let ((error-occurred nil))
118             (unwind-protect
119                 (if (uffi:null-pointer-p 
120                      (mysql-real-connect 
121                       mysql-ptr host-native user-native password-native
122                       db-native 0 socket-native 0))
123                     (progn
124                       (setq error-occurred t)
125                       (error 'clsql-connect-error
126                              :database-type database-type
127                              :connection-spec connection-spec
128                              :errno (mysql-errno mysql-ptr)
129                              :error (mysql-error-string mysql-ptr)))
130                   (make-instance 'mysql-database
131                     :name (database-name-from-spec connection-spec
132                                                    database-type)
133                     :mysql-ptr mysql-ptr))
134               (when error-occurred (mysql-close mysql-ptr)))))))))
135
136
137 (defmethod database-disconnect ((database mysql-database))
138   (mysql-close (database-mysql-ptr database))
139   (setf (database-mysql-ptr database) nil)
140   t)
141
142
143 (defmethod database-query (query-expression (database mysql-database) 
144                            types)
145   (with-slots (mysql-ptr) database
146     (uffi:with-cstring (query-native query-expression)
147        (if (zerop (mysql-query mysql-ptr query-native))
148            (let ((res-ptr (mysql-use-result mysql-ptr)))
149              (if res-ptr
150                  (let ((num-fields (mysql-num-fields res-ptr)))
151                    (setq types (canonicalize-types 
152                                       types num-fields
153                                       res-ptr))
154                    (unwind-protect
155                         (loop for row = (mysql-fetch-row res-ptr)
156                               until (uffi:null-pointer-p row)
157                               collect
158                               (loop for i from 0 below num-fields
159                                     collect
160                                     (convert-raw-field
161                                      (uffi:deref-array row 'mysql-row i)
162                                      types i)))
163                      (mysql-free-result res-ptr)))
164                (error 'clsql-sql-error
165                       :database database
166                       :expression query-expression
167                       :errno (mysql-errno mysql-ptr)
168                       :error (mysql-error-string mysql-ptr))))
169          (error 'clsql-sql-error
170                 :database database
171                 :expression query-expression
172                 :errno (mysql-errno mysql-ptr)
173                 :error (mysql-error-string mysql-ptr))))))
174
175 (defmethod database-execute-command (sql-expression (database mysql-database))
176   (uffi:with-cstring (sql-native sql-expression)
177     (let ((mysql-ptr (database-mysql-ptr database)))
178       (declare (type mysql-mysql-ptr-def mysql-ptr))
179       (if (zerop (mysql-query mysql-ptr sql-native))
180           t
181         (error 'clsql-sql-error
182                :database database
183                :expression sql-expression
184                :errno (mysql-errno mysql-ptr)
185                :error (mysql-error-string mysql-ptr))))))
186
187 (defstruct mysql-result-set
188   (res-ptr (uffi:make-null-pointer 'mysql-mysql-res)
189            :type mysql-mysql-res-ptr-def)
190   (types nil)
191   (num-fields nil :type fixnum)
192   (full-set nil :type boolean))
193
194
195 (defmethod database-query-result-set (query-expression 
196                                       (database mysql-database)
197                                       &key full-set types)
198   (uffi:with-cstring (query-native query-expression)
199     (let ((mysql-ptr (database-mysql-ptr database)))
200      (declare (type mysql-mysql-ptr-def mysql-ptr))
201       (if (zerop (mysql-query mysql-ptr query-native))
202           (let ((res-ptr (if full-set
203                              (mysql-store-result mysql-ptr)
204                            (mysql-use-result mysql-ptr))))
205             (declare (type mysql-mysql-res-ptr-def res-ptr))
206             (if (not (uffi:null-pointer-p res-ptr))
207                 (let* ((num-fields (mysql-num-fields res-ptr))
208                        (result-set (make-mysql-result-set
209                                     :res-ptr res-ptr
210                                     :num-fields num-fields
211                                     :full-set full-set
212                                     :types
213                                     (canonicalize-types 
214                                      types num-fields
215                                      res-ptr)))) 
216                   (if full-set
217                       (values result-set
218                               num-fields
219                               (mysql-num-rows res-ptr))
220                       (values result-set
221                               num-fields)))
222                 (error 'clsql-sql-error
223                      :database database
224                      :expression query-expression
225                      :errno (mysql-errno mysql-ptr)
226                      :error (mysql-error-string mysql-ptr))))
227         (error 'clsql-sql-error
228                :database database
229                :expression query-expression
230                :errno (mysql-errno mysql-ptr)
231                :error (mysql-error-string mysql-ptr))))))
232
233 (defmethod database-dump-result-set (result-set (database mysql-database))
234   (mysql-free-result (mysql-result-set-res-ptr result-set))
235   t)
236
237
238 (defmethod database-store-next-row (result-set (database mysql-database) list)
239   (let* ((res-ptr (mysql-result-set-res-ptr result-set))
240          (row (mysql-fetch-row res-ptr))
241          (types (mysql-result-set-types result-set)))
242     (declare (type mysql-mysql-res-ptr-def res-ptr)
243              (type mysql-row-def row))
244     (unless (uffi:null-pointer-p row)
245       (loop for i from 0 below (mysql-result-set-num-fields result-set)
246             for rest on list
247             do
248             (setf (car rest) 
249                   (convert-raw-field
250                    (uffi:deref-array row 'mysql-row i)
251                    types
252                    i)))
253       list)))
254
255