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