138cd7df1587da8220642039504aff40a89a0bf6
[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.14 2002/03/27 08:09:25 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 canonicalize-types (types num-fields res-ptr)
45   (cond
46    ((listp types)
47     (canonicalize-type-list types num-fields))
48    ((eq types :auto)
49     (let ((new-types '())
50           #+ignore (field-vec (mysql-fetch-fields res-ptr)))
51       (dotimes (i num-fields)
52         (declare (fixnum i))
53         (let* ( (field (mysql-fetch-field-direct res-ptr i))
54                 #+ignore (field (uffi:deref-array field-vec 'mysql-field-vector i))
55                 (type (uffi:get-slot-value field 'mysql-field 'type)))
56           (push
57            (case type
58              ((#.mysql-field-types#tiny 
59                #.mysql-field-types#short
60                #.mysql-field-types#int24
61                #.mysql-field-types#long)
62               :int)
63              (#.mysql-field-types#longlong
64               :longlong)
65              ((#.mysql-field-types#double
66                #.mysql-field-types#float
67                #.mysql-field-types#decimal)
68               :double)
69              (otherwise
70               t))
71            new-types)))
72       (nreverse new-types)))
73    (t
74     nil)))
75
76 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
77   t)
78
79 (uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
80 (uffi:def-type mysql-row-def mysql-row)
81 (uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
82
83 (defclass mysql-database (database)
84   ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
85               :type mysql-mysql-ptr-def)))
86
87 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
88   (check-connection-spec connection-spec database-type (host db user password))
89   (destructuring-bind (host db user password) connection-spec
90     (declare (ignore password))
91     (concatenate 'string host "/" db "/" user)))
92
93
94 (defmethod database-connect (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     (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
98           (socket nil))
99       (if (uffi:null-pointer-p mysql-ptr)
100           (error 'clsql-connect-error
101                  :database-type database-type
102                  :connection-spec connection-spec
103                  :errno (mysql-errno mysql-ptr)
104                  :error (mysql-error-string mysql-ptr))
105         (uffi:with-cstrings ((host-native host)
106                             (user-native user)
107                             (password-native password)
108                             (db-native db)
109                             (socket-native socket))
110           (let ((error-occurred nil))
111             (unwind-protect
112                 (if (uffi:null-pointer-p 
113                      (mysql-real-connect 
114                       mysql-ptr host-native user-native password-native
115                       db-native 0 socket-native 0))
116                     (progn
117                       (setq error-occurred t)
118                       (error 'clsql-connect-error
119                              :database-type database-type
120                              :connection-spec connection-spec
121                              :errno (mysql-errno mysql-ptr)
122                              :error (mysql-error-string mysql-ptr)))
123                   (make-instance 'mysql-database
124                     :name (database-name-from-spec connection-spec
125                                                    database-type)
126                     :mysql-ptr mysql-ptr))
127               (when error-occurred (mysql-close mysql-ptr)))))))))
128
129
130 (defmethod database-disconnect ((database mysql-database))
131   (mysql-close (database-mysql-ptr database))
132   (setf (database-mysql-ptr database) nil)
133   t)
134
135
136 (defmethod database-query (query-expression (database mysql-database) 
137                            types)
138   (with-slots (mysql-ptr) database
139     (uffi:with-cstring (query-native query-expression)
140        (if (zerop (mysql-query mysql-ptr query-native))
141            (let ((res-ptr (mysql-use-result mysql-ptr)))
142              (if res-ptr
143                  (let ((num-fields (mysql-num-fields res-ptr)))
144                    (setq types (canonicalize-types 
145                                       types num-fields
146                                       res-ptr))
147                    (unwind-protect
148                         (loop for row = (mysql-fetch-row res-ptr)
149                               until (uffi:null-pointer-p row)
150                               collect
151                               (loop for i from 0 below num-fields
152                                     collect
153                                     (convert-raw-field
154                                      (uffi:deref-array row 'mysql-row i)
155                                      types i)))
156                      (mysql-free-result res-ptr)))
157                (error 'clsql-sql-error
158                       :database database
159                       :expression query-expression
160                       :errno (mysql-errno mysql-ptr)
161                       :error (mysql-error-string mysql-ptr))))
162          (error 'clsql-sql-error
163                 :database database
164                 :expression query-expression
165                 :errno (mysql-errno mysql-ptr)
166                 :error (mysql-error-string mysql-ptr))))))
167
168 (defmethod database-execute-command (sql-expression (database mysql-database))
169   (uffi:with-cstring (sql-native sql-expression)
170     (let ((mysql-ptr (database-mysql-ptr database)))
171       (declare (type mysql-mysql-ptr-def mysql-ptr))
172       (if (zerop (mysql-query mysql-ptr sql-native))
173           t
174         (error 'clsql-sql-error
175                :database database
176                :expression sql-expression
177                :errno (mysql-errno mysql-ptr)
178                :error (mysql-error-string mysql-ptr))))))
179
180 (defstruct mysql-result-set
181   (res-ptr (uffi:make-null-pointer 'mysql-mysql-res)
182            :type mysql-mysql-res-ptr-def)
183   (types nil)
184   (num-fields nil :type fixnum)
185   (full-set nil :type boolean))
186
187
188 (defmethod database-query-result-set (query-expression 
189                                       (database mysql-database)
190                                       &key full-set types)
191   (uffi:with-cstring (query-native query-expression)
192     (let ((mysql-ptr (database-mysql-ptr database)))
193      (declare (type mysql-mysql-ptr-def mysql-ptr))
194       (if (zerop (mysql-query mysql-ptr query-native))
195           (let ((res-ptr (if full-set
196                              (mysql-store-result mysql-ptr)
197                            (mysql-use-result mysql-ptr))))
198             (declare (type mysql-mysql-res-ptr-def res-ptr))
199             (if (not (uffi:null-pointer-p res-ptr))
200                 (let* ((num-fields (mysql-num-fields res-ptr))
201                        (result-set (make-mysql-result-set
202                                     :res-ptr res-ptr
203                                     :num-fields num-fields
204                                     :full-set full-set
205                                     :types
206                                     (canonicalize-types 
207                                      types num-fields
208                                      res-ptr)))) 
209                   (if full-set
210                       (values result-set
211                               num-fields
212                               (mysql-num-rows res-ptr))
213                       (values result-set
214                               num-fields)))
215                 (error 'clsql-sql-error
216                      :database database
217                      :expression query-expression
218                      :errno (mysql-errno mysql-ptr)
219                      :error (mysql-error-string mysql-ptr))))
220         (error 'clsql-sql-error
221                :database database
222                :expression query-expression
223                :errno (mysql-errno mysql-ptr)
224                :error (mysql-error-string mysql-ptr))))))
225
226 (defmethod database-dump-result-set (result-set (database mysql-database))
227   (mysql-free-result (mysql-result-set-res-ptr result-set))
228   t)
229
230
231 (defmethod database-store-next-row (result-set (database mysql-database) list)
232   (let* ((res-ptr (mysql-result-set-res-ptr result-set))
233          (row (mysql-fetch-row res-ptr))
234          (types (mysql-result-set-types result-set)))
235     (declare (type mysql-mysql-res-ptr-def res-ptr)
236              (type mysql-row-def row))
237     (unless (uffi:null-pointer-p row)
238       (loop for i from 0 below (mysql-result-set-num-fields result-set)
239             for rest on list
240             do
241             (setf (car rest) 
242                   (convert-raw-field
243                    (uffi:deref-array row 'mysql-row i)
244                    types
245                    i)))
246       list)))
247
248