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.3 2002/03/24 04:01:26 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
32 (defpackage :clsql-mysql
33 (:use :common-lisp :clsql-sys :mysql)
34 (:export #:mysql-database)
35 (:documentation "This is the CLSQL interface to MySQL."))
37 (in-package :clsql-mysql)
39 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
42 (uffi:def-type mysql-mysql-ptr-def (* mysql-mysql))
43 (uffi:def-type mysql-row-def mysql-row)
44 (uffi:def-type mysql-mysql-res-ptr-def (* mysql-mysql-res))
46 (defclass mysql-database (database)
47 ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
48 :type mysql-mysql-ptr-def)))
50 (defmethod database-name-from-spec (connection-spec (database-type (eql :mysql)))
51 (check-connection-spec connection-spec database-type (host db user password))
52 (destructuring-bind (host db user password) connection-spec
53 (declare (ignore password))
54 (concatenate 'string host "/" db "/" user)))
56 (defmethod database-connect (connection-spec (database-type (eql :mysql)))
57 (check-connection-spec connection-spec database-type (host db user password))
58 (destructuring-bind (host db user password) connection-spec
59 (let ((mysql-ptr (mysql-init (uffi:make-null-pointer 'mysql-mysql)))
61 (if (uffi:null-pointer-p mysql-ptr)
62 (error 'clsql-connect-error
63 :database-type database-type
64 :connection-spec connection-spec
65 :errno (mysql-errno mysql-ptr)
66 :error (mysql-error-string mysql-ptr))
67 (uffi:with-cstrings ((host-native host)
69 (password-native password)
71 (socket-native socket))
72 (let ((error-occurred nil))
74 (if (uffi:null-pointer-p
76 mysql-ptr host-native user-native password-native
77 db-native 0 socket-native 0))
79 (setq error-occurred t)
80 (error 'clsql-connect-error
81 :database-type database-type
82 :connection-spec connection-spec
83 :errno (mysql-errno mysql-ptr)
84 :error (mysql-error-string mysql-ptr)))
85 (make-instance 'mysql-database
86 :name (database-name-from-spec connection-spec
88 :mysql-ptr mysql-ptr))
89 (when error-occurred (mysql-close mysql-ptr)))))))))
92 (defmethod database-disconnect ((database mysql-database))
93 (mysql-close (database-mysql-ptr database))
94 (setf (database-mysql-ptr database) nil)
98 (defstruct mysql-result-set
99 (res-ptr (uffi:make-null-pointer 'mysql-mysql-res)
100 :type mysql-mysql-res-ptr-def)
103 (defmethod database-dump-result-set (result-set (database mysql-database))
104 (mysql-free-result (mysql-result-set-res-ptr result-set))
108 (defmethod database-store-next-row (result-set (database mysql-database) list)
109 (let* ((res-ptr (mysql-result-set-res-ptr result-set))
110 (row (mysql-fetch-row res-ptr)))
111 (declare (type mysql-mysql-res-ptr-def res-ptr)
112 (type mysql-row-def row))
113 (unless (uffi:null-pointer-p row)
114 (loop for i from 0 below (mysql-num-fields res-ptr)
118 (uffi:convert-from-foreign-string (uffi:deref-array row 'mysql-row i))))
122 (defmethod database-execute-command (sql-expression (database mysql-database))
123 (uffi:with-cstring (sql-native sql-expression)
124 (let ((mysql-ptr (database-mysql-ptr database)))
125 (declare (type mysql-mysql-ptr-def mysql-ptr))
126 (if (zerop (mysql-query mysql-ptr sql-native))
128 (error 'clsql-sql-error
130 :expression sql-expression
131 :errno (mysql-errno mysql-ptr)
132 :error (mysql-error-string mysql-ptr))))))
136 (defmethod database-query (query-expression (database mysql-database))
137 (with-slots (mysql-ptr) database
138 (uffi:with-cstring (query-native query-expression)
139 (if (zerop (mysql-query mysql-ptr query-native))
140 (let ((res-ptr (mysql-use-result mysql-ptr)))
143 (loop for row = (mysql-fetch-row res-ptr)
144 until (uffi:null-pointer-p row)
146 (loop for i from 0 below (mysql-num-fields res-ptr)
148 (uffi:convert-from-cstring
149 (uffi:deref-array row 'mysql-row i))))
150 (mysql-free-result res-ptr))
151 (error 'clsql-sql-error
153 :expression query-expression
154 :errno (mysql-errno mysql-ptr)
155 :error (mysql-error-string mysql-ptr))))
156 (error 'clsql-sql-error
158 :expression query-expression
159 :errno (mysql-errno mysql-ptr)
160 :error (mysql-error-string mysql-ptr))))))
163 (defmethod database-query-result-set (query-expression
164 (database mysql-database)
166 (uffi:with-cstring (query-native query-expression)
167 (let ((mysql-ptr (database-mysql-ptr database)))
168 (declare (type mysql-mysql-ptr-def mysql-ptr))
169 (if (zerop (mysql-query mysql-ptr query-native))
170 (let ((res-ptr (if full-set
171 (mysql-store-result mysql-ptr)
172 (mysql-use-result mysql-ptr))))
173 (declare (type mysql-mysql-res-ptr-def res-ptr))
174 (if (not (uffi:null-pointer-p res-ptr))
176 (values (make-mysql-result-set :res-ptr res-ptr :full-set t)
177 (mysql-num-fields res-ptr)
178 (mysql-num-rows res-ptr))
179 (values (make-mysql-result-set :res-ptr res-ptr)
180 (mysql-num-fields res-ptr)))
181 (error 'clsql-sql-error
183 :expression query-expression
184 :errno (mysql-errno mysql-ptr)
185 :error (mysql-error-string mysql-ptr))))
186 (error 'clsql-sql-error
188 :expression query-expression
189 :errno (mysql-errno mysql-ptr)
190 :error (mysql-error-string mysql-ptr))))))