1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
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
11 ;;;; $Id: mysql-sql.cl,v 1.15 2002/03/27 12:09:39 kevin Exp $
13 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; and Copyright (c) 1999-2001 by Pierre R. Mai
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 ;;;; *************************************************************************
21 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0)))
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
33 ;;;; Added field types
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."))
40 (in-package :clsql-mysql)
42 ;;; Field conversion functions
44 (defun make-type-list-for-auto (num-fields res-ptr)
46 #+ignore (field-vec (mysql-fetch-fields res-ptr)))
47 (dotimes (i num-fields)
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)))
54 ((#.mysql-field-types#tiny
55 #.mysql-field-types#short
56 #.mysql-field-types#int24
57 #.mysql-field-types#long)
59 (#.mysql-field-types#longlong
61 ((#.mysql-field-types#double
62 #.mysql-field-types#float
63 #.mysql-field-types#decimal)
68 (nreverse new-types)))
70 (defun canonicalize-types (types num-fields res-ptr)
71 (let ((auto-list (make-type-list-for-auto num-fields res-ptr)))
74 (canonicalize-type-list types auto-list))
80 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
83 (uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
84 (uffi:def-type mysql-row-def mysql-row)
85 (uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
87 (defclass mysql-database (database)
88 ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
89 :type mysql-mysql-ptr-def)))
91 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
92 (check-connection-spec connection-spec database-type (host db user password))
93 (destructuring-bind (host db user password) connection-spec
94 (declare (ignore password))
95 (concatenate 'string host "/" db "/" user)))
98 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
99 (check-connection-spec connection-spec database-type (host db user password))
100 (destructuring-bind (host db user password) connection-spec
101 (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
103 (if (uffi:null-pointer-p mysql-ptr)
104 (error 'clsql-connect-error
105 :database-type database-type
106 :connection-spec connection-spec
107 :errno (mysql-errno mysql-ptr)
108 :error (mysql-error-string mysql-ptr))
109 (uffi:with-cstrings ((host-native host)
111 (password-native password)
113 (socket-native socket))
114 (let ((error-occurred nil))
116 (if (uffi:null-pointer-p
118 mysql-ptr host-native user-native password-native
119 db-native 0 socket-native 0))
121 (setq error-occurred t)
122 (error 'clsql-connect-error
123 :database-type database-type
124 :connection-spec connection-spec
125 :errno (mysql-errno mysql-ptr)
126 :error (mysql-error-string mysql-ptr)))
127 (make-instance 'mysql-database
128 :name (database-name-from-spec connection-spec
130 :mysql-ptr mysql-ptr))
131 (when error-occurred (mysql-close mysql-ptr)))))))))
134 (defmethod database-disconnect ((database mysql-database))
135 (mysql-close (database-mysql-ptr database))
136 (setf (database-mysql-ptr database) nil)
140 (defmethod database-query (query-expression (database mysql-database)
142 (with-slots (mysql-ptr) database
143 (uffi:with-cstring (query-native query-expression)
144 (if (zerop (mysql-query mysql-ptr query-native))
145 (let ((res-ptr (mysql-use-result mysql-ptr)))
147 (let ((num-fields (mysql-num-fields res-ptr)))
148 (setq types (canonicalize-types
152 (loop for row = (mysql-fetch-row res-ptr)
153 until (uffi:null-pointer-p row)
155 (loop for i from 0 below num-fields
158 (uffi:deref-array row 'mysql-row i)
160 (mysql-free-result res-ptr)))
161 (error 'clsql-sql-error
163 :expression query-expression
164 :errno (mysql-errno mysql-ptr)
165 :error (mysql-error-string mysql-ptr))))
166 (error 'clsql-sql-error
168 :expression query-expression
169 :errno (mysql-errno mysql-ptr)
170 :error (mysql-error-string mysql-ptr))))))
172 (defmethod database-execute-command (sql-expression (database mysql-database))
173 (uffi:with-cstring (sql-native sql-expression)
174 (let ((mysql-ptr (database-mysql-ptr database)))
175 (declare (type mysql-mysql-ptr-def mysql-ptr))
176 (if (zerop (mysql-query mysql-ptr sql-native))
178 (error 'clsql-sql-error
180 :expression sql-expression
181 :errno (mysql-errno mysql-ptr)
182 :error (mysql-error-string mysql-ptr))))))
184 (defstruct mysql-result-set
185 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res)
186 :type mysql-mysql-res-ptr-def)
188 (num-fields nil :type fixnum)
189 (full-set nil :type boolean))
192 (defmethod database-query-result-set (query-expression
193 (database mysql-database)
195 (uffi:with-cstring (query-native query-expression)
196 (let ((mysql-ptr (database-mysql-ptr database)))
197 (declare (type mysql-mysql-ptr-def mysql-ptr))
198 (if (zerop (mysql-query mysql-ptr query-native))
199 (let ((res-ptr (if full-set
200 (mysql-store-result mysql-ptr)
201 (mysql-use-result mysql-ptr))))
202 (declare (type mysql-mysql-res-ptr-def res-ptr))
203 (if (not (uffi:null-pointer-p res-ptr))
204 (let* ((num-fields (mysql-num-fields res-ptr))
205 (result-set (make-mysql-result-set
207 :num-fields num-fields
216 (mysql-num-rows res-ptr))
219 (error 'clsql-sql-error
221 :expression query-expression
222 :errno (mysql-errno mysql-ptr)
223 :error (mysql-error-string mysql-ptr))))
224 (error 'clsql-sql-error
226 :expression query-expression
227 :errno (mysql-errno mysql-ptr)
228 :error (mysql-error-string mysql-ptr))))))
230 (defmethod database-dump-result-set (result-set (database mysql-database))
231 (mysql-free-result (mysql-result-set-res-ptr result-set))
235 (defmethod database-store-next-row (result-set (database mysql-database) list)
236 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
237 (row (mysql-fetch-row res-ptr))
238 (types (mysql-result-set-types result-set)))
239 (declare (type mysql-mysql-res-ptr-def res-ptr)
240 (type mysql-row-def row))
241 (unless (uffi:null-pointer-p row)
242 (loop for i from 0 below (mysql-result-set-num-fields result-set)
247 (uffi:deref-array row 'mysql-row i)