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.lisp,v 1.5 2003/06/08 12:48:55 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 (eval-when (:compile-toplevel)
22 (declaim (optimize (debug 3) (speed 3) (safety 1) (compilation-speed 0))))
24 ;;;; Modified by Kevin Rosenberg, Feb 20002
25 ;;;; -- Added support for Allegro CL and Lispworks using UFFI layer
26 ;;;; -- Changed database-connect to use mysql-real-connect. This way,
27 ;;;; can avoid using double (unwind-protect)
28 ;;;; -- Changed database-connect to have MySQL library allocate space
29 ;;;; for MYSQL structure. This will make the code more robust in
30 ;;;; the event that MySQL library changes the size of the mysql-mysql
34 ;;;; Added field types
36 (defpackage #:clsql-mysql
37 (:use #:common-lisp #:clsql-base-sys #:mysql #:clsql-uffi)
38 (:export #:mysql-database)
39 (:documentation "This is the CLSQL interface to MySQL."))
41 (in-package #:clsql-mysql)
43 ;;; Field conversion functions
45 (defun make-type-list-for-auto (num-fields res-ptr)
47 #+ignore (field-vec (mysql-fetch-fields res-ptr)))
48 (dotimes (i num-fields)
50 (let* ( (field (mysql-fetch-field-direct res-ptr i))
51 #+ignore (field (uffi:deref-array field-vec '(:array mysql-field) i))
52 (type (uffi:get-slot-value field 'mysql-field 'type)))
55 ((#.mysql-field-types#tiny
56 #.mysql-field-types#short
57 #.mysql-field-types#int24
58 #.mysql-field-types#long)
60 (#.mysql-field-types#longlong
62 ((#.mysql-field-types#double
63 #.mysql-field-types#float
64 #.mysql-field-types#decimal)
69 (nreverse new-types)))
71 (defun canonicalize-types (types num-fields res-ptr)
74 (let ((auto-list (make-type-list-for-auto num-fields res-ptr)))
77 (canonicalize-type-list types auto-list))
83 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
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))
90 (defclass mysql-database (database)
91 ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
92 :type mysql-mysql-ptr-def)))
94 (defmethod database-type ((database mysql-database))
97 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
98 (check-connection-spec connection-spec database-type (host db user password))
99 (destructuring-bind (host db user password) connection-spec
100 (declare (ignore password))
101 (concatenate 'string host "/" db "/" user)))
103 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
104 (check-connection-spec connection-spec database-type (host db user password))
105 (destructuring-bind (host db user password) connection-spec
106 (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
108 (if (uffi:null-pointer-p mysql-ptr)
109 (error 'clsql-connect-error
110 :database-type database-type
111 :connection-spec connection-spec
112 :errno (mysql-errno mysql-ptr)
113 :error (mysql-error-string mysql-ptr))
114 (uffi:with-cstrings ((host-native host)
116 (password-native password)
118 (socket-native socket))
119 (let ((error-occurred nil))
121 (if (uffi:null-pointer-p
123 mysql-ptr host-native user-native password-native
124 db-native 0 socket-native 0))
126 (setq error-occurred t)
127 (error 'clsql-connect-error
128 :database-type database-type
129 :connection-spec connection-spec
130 :errno (mysql-errno mysql-ptr)
131 :error (mysql-error-string mysql-ptr)))
132 (make-instance 'mysql-database
133 :name (database-name-from-spec connection-spec
135 :connection-spec connection-spec
136 :mysql-ptr mysql-ptr))
137 (when error-occurred (mysql-close mysql-ptr)))))))))
140 (defmethod database-disconnect ((database mysql-database))
141 (mysql-close (database-mysql-ptr database))
142 (setf (database-mysql-ptr database) nil)
146 (defmethod database-query (query-expression (database mysql-database)
148 (declare (optimize (speed 3) (safety 0) (debug 0) (space 0)))
149 (let ((mysql-ptr (database-mysql-ptr database)))
150 (uffi:with-cstring (query-native query-expression)
151 (if (zerop (mysql-query mysql-ptr query-native))
152 (let ((res-ptr (mysql-use-result mysql-ptr)))
155 (let ((num-fields (mysql-num-fields res-ptr)))
156 (declare (fixnum num-fields))
157 (setq types (canonicalize-types
160 (loop for row = (mysql-fetch-row res-ptr)
161 until (uffi:null-pointer-p row)
163 (loop for i fixnum from 0 below num-fields
166 (uffi:deref-array row '(:array
170 (mysql-free-result res-ptr))
171 (error 'clsql-sql-error
173 :expression query-expression
174 :errno (mysql-errno mysql-ptr)
175 :error (mysql-error-string mysql-ptr))))
176 (error 'clsql-sql-error
178 :expression query-expression
179 :errno (mysql-errno mysql-ptr)
180 :error (mysql-error-string mysql-ptr))))))
182 (defmethod database-execute-command (sql-expression (database mysql-database))
183 (uffi:with-cstring (sql-native sql-expression)
184 (let ((mysql-ptr (database-mysql-ptr database)))
185 (declare (type mysql-mysql-ptr-def mysql-ptr))
186 (if (zerop (mysql-query mysql-ptr sql-native))
188 (error 'clsql-sql-error
190 :expression sql-expression
191 :errno (mysql-errno mysql-ptr)
192 :error (mysql-error-string mysql-ptr))))))
195 (defstruct mysql-result-set
196 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res) :type mysql-mysql-res-ptr-def)
197 (types nil :type list)
198 (num-fields 0 :type fixnum)
199 (full-set nil :type boolean))
202 (defmethod database-query-result-set (query-expression
203 (database mysql-database)
205 (uffi:with-cstring (query-native query-expression)
206 (let ((mysql-ptr (database-mysql-ptr database)))
207 (declare (type mysql-mysql-ptr-def mysql-ptr))
208 (if (zerop (mysql-query mysql-ptr query-native))
209 (let ((res-ptr (if full-set
210 (mysql-store-result mysql-ptr)
211 (mysql-use-result mysql-ptr))))
212 (declare (type mysql-mysql-res-ptr-def res-ptr))
213 (if (not (uffi:null-pointer-p res-ptr))
214 (let* ((num-fields (mysql-num-fields res-ptr))
215 (result-set (make-mysql-result-set
217 :num-fields num-fields
226 (mysql-num-rows res-ptr))
229 (error 'clsql-sql-error
231 :expression query-expression
232 :errno (mysql-errno mysql-ptr)
233 :error (mysql-error-string mysql-ptr))))
234 (error 'clsql-sql-error
236 :expression query-expression
237 :errno (mysql-errno mysql-ptr)
238 :error (mysql-error-string mysql-ptr))))))
240 (defmethod database-dump-result-set (result-set (database mysql-database))
241 (mysql-free-result (mysql-result-set-res-ptr result-set))
245 (defmethod database-store-next-row (result-set (database mysql-database) list)
246 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
247 (row (mysql-fetch-row res-ptr))
248 (types (mysql-result-set-types result-set)))
249 (declare (type mysql-mysql-res-ptr-def res-ptr)
250 (type mysql-row-def row))
251 (unless (uffi:null-pointer-p row)
252 (loop for i from 0 below (mysql-result-set-num-fields result-set)
257 (uffi:deref-array row '(:array (* :unsigned-char)) i)
263 (when (clsql-base-sys:database-type-library-loaded :mysql)
264 (clsql-base-sys:initialize-database-type :database-type :mysql)
265 (pushnew :mysql cl:*features*))