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.7 2002/03/25 06:07:06 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)
37 (:export #:mysql-database)
38 (:documentation "This is the CLSQL interface to MySQL."))
40 (in-package :clsql-mysql)
42 ;;; Field conversion functions
44 (defun canonicalize-field-types (types num-fields)
46 (let ((length-types (length types))
48 (loop for i from 0 below num-fields
50 (if (>= i length-types)
51 (push t new-types) ;; types is shorted than num-fields
54 ((:int :long :double t)
64 (uffi:def-function "atoi"
68 (uffi:def-function "atol"
72 (uffi:def-function "atof"
76 (defun convert-raw-field (char-ptr types index)
77 (let ((type (if (listp types)
88 (uffi:convert-from-foreign-string char-ptr)))))
90 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
93 (uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
94 (uffi:def-type mysql-row-def mysql-row)
95 (uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
97 (defclass mysql-database (database)
98 ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
99 :type mysql-mysql-ptr-def)))
101 (defmethod database-name-from-spec (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 (declare (ignore password))
105 (concatenate 'string host "/" db "/" user)))
108 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
109 (check-connection-spec connection-spec database-type (host db user password))
110 (destructuring-bind (host db user password) connection-spec
111 (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
113 (if (uffi:null-pointer-p mysql-ptr)
114 (error 'clsql-connect-error
115 :database-type database-type
116 :connection-spec connection-spec
117 :errno (mysql-errno mysql-ptr)
118 :error (mysql-error-string mysql-ptr))
119 (uffi:with-cstrings ((host-native host)
121 (password-native password)
123 (socket-native socket))
124 (let ((error-occurred nil))
126 (if (uffi:null-pointer-p
128 mysql-ptr host-native user-native password-native
129 db-native 0 socket-native 0))
131 (setq error-occurred t)
132 (error 'clsql-connect-error
133 :database-type database-type
134 :connection-spec connection-spec
135 :errno (mysql-errno mysql-ptr)
136 :error (mysql-error-string mysql-ptr)))
137 (make-instance 'mysql-database
138 :name (database-name-from-spec connection-spec
140 :mysql-ptr mysql-ptr))
141 (when error-occurred (mysql-close mysql-ptr)))))))))
144 (defmethod database-disconnect ((database mysql-database))
145 (mysql-close (database-mysql-ptr database))
146 (setf (database-mysql-ptr database) nil)
150 (defmethod database-query (query-expression (database mysql-database)
152 (with-slots (mysql-ptr) database
153 (uffi:with-cstring (query-native query-expression)
154 (if (zerop (mysql-query mysql-ptr query-native))
155 (let ((res-ptr (mysql-use-result mysql-ptr)))
157 (let ((num-fields (mysql-num-fields res-ptr)))
158 (setq field-types (canonicalize-field-types
159 field-types num-fields))
161 (loop for row = (mysql-fetch-row res-ptr)
162 until (uffi:null-pointer-p row)
164 (loop for i from 0 below num-fields
166 (uffi:convert-from-foreign-string
167 (uffi:deref-array row 'mysql-row i))))
168 (mysql-free-result res-ptr)))
169 (error 'clsql-sql-error
171 :expression query-expression
172 :errno (mysql-errno mysql-ptr)
173 :error (mysql-error-string mysql-ptr))))
174 (error 'clsql-sql-error
176 :expression query-expression
177 :errno (mysql-errno mysql-ptr)
178 :error (mysql-error-string mysql-ptr))))))
180 (defmethod database-execute-command (sql-expression (database mysql-database))
181 (uffi:with-cstring (sql-native sql-expression)
182 (let ((mysql-ptr (database-mysql-ptr database)))
183 (declare (type mysql-mysql-ptr-def mysql-ptr))
184 (if (zerop (mysql-query mysql-ptr sql-native))
186 (error 'clsql-sql-error
188 :expression sql-expression
189 :errno (mysql-errno mysql-ptr)
190 :error (mysql-error-string mysql-ptr))))))
192 (defstruct mysql-result-set
193 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res)
194 :type mysql-mysql-res-ptr-def)
196 (num-fields nil :type fixnum)
197 (full-set nil :type boolean))
200 (defmethod database-query-result-set (query-expression
201 (database mysql-database)
202 &key full-set field-types)
203 (uffi:with-cstring (query-native query-expression)
204 (let ((mysql-ptr (database-mysql-ptr database)))
205 (declare (type mysql-mysql-ptr-def mysql-ptr))
206 (if (zerop (mysql-query mysql-ptr query-native))
207 (let ((res-ptr (if full-set
208 (mysql-store-result mysql-ptr)
209 (mysql-use-result mysql-ptr))))
210 (declare (type mysql-mysql-res-ptr-def res-ptr))
211 (if (not (uffi:null-pointer-p res-ptr))
212 (let* ((num-fields (mysql-num-fields res-ptr))
213 (result-set (make-mysql-result-set
215 :num-fields num-fields
218 (canonicalize-field-types
219 field-types num-fields))))
223 (mysql-num-rows res-ptr))
226 (error 'clsql-sql-error
228 :expression query-expression
229 :errno (mysql-errno mysql-ptr)
230 :error (mysql-error-string mysql-ptr))))
231 (error 'clsql-sql-error
233 :expression query-expression
234 :errno (mysql-errno mysql-ptr)
235 :error (mysql-error-string mysql-ptr))))))
237 (defmethod database-dump-result-set (result-set (database mysql-database))
238 (mysql-free-result (mysql-result-set-res-ptr result-set))
243 (defmethod database-store-next-row (result-set (database mysql-database) list)
244 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
245 (row (mysql-fetch-row res-ptr))
246 (field-types (mysql-result-set-field-types result-set)))
247 (declare (type mysql-mysql-res-ptr-def res-ptr)
248 (type mysql-row-def row))
249 (unless (uffi:null-pointer-p row)
250 (loop for i from 0 below (mysql-result-set-num-fields result-set)
255 (uffi:deref-array row 'mysql-row i)