878033c6d4a1d7cadb99a7a5f95b47608a4c99c2
[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.3 2002/03/24 04:01:26 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 (defpackage :clsql-mysql
33     (:use :common-lisp :clsql-sys :mysql)
34     (:export #:mysql-database)
35     (:documentation "This is the CLSQL interface to MySQL."))
36
37 (in-package :clsql-mysql)
38
39 (defmethod database-initialize-database-type ((database-type (eql :mysql)))
40   t)
41
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))
45
46 (defclass mysql-database (database)
47   ((mysql-ptr :accessor database-mysql-ptr :initarg :mysql-ptr
48               :type mysql-mysql-ptr-def)))
49
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)))
55
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)))
60           (socket nil))
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)
68                             (user-native user)
69                             (password-native password)
70                             (db-native db)
71                             (socket-native socket))
72           (let ((error-occurred nil))
73             (unwind-protect
74                 (if (uffi:null-pointer-p 
75                      (mysql-real-connect 
76                       mysql-ptr host-native user-native password-native
77                       db-native 0 socket-native 0))
78                     (progn
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
87                                                    database-type)
88                     :mysql-ptr mysql-ptr))
89               (when error-occurred (mysql-close mysql-ptr)))))))))
90
91
92 (defmethod database-disconnect ((database mysql-database))
93   (mysql-close (database-mysql-ptr database))
94   (setf (database-mysql-ptr database) nil)
95   t)
96
97
98 (defstruct mysql-result-set
99   (res-ptr (uffi:make-null-pointer 'mysql-mysql-res)
100            :type mysql-mysql-res-ptr-def)
101   (full-set nil))
102
103 (defmethod database-dump-result-set (result-set (database mysql-database))
104   (mysql-free-result (mysql-result-set-res-ptr result-set))
105   t)
106
107
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)
115           for rest on list
116           do
117             (setf (car rest) 
118               (uffi:convert-from-foreign-string (uffi:deref-array row 'mysql-row i))))
119       list)))
120
121
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))
127           t
128         (error 'clsql-sql-error
129                :database database
130                :expression sql-expression
131                :errno (mysql-errno mysql-ptr)
132                :error (mysql-error-string mysql-ptr))))))
133
134
135
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)))
141              (if res-ptr
142                  (unwind-protect
143                      (loop for row = (mysql-fetch-row res-ptr)
144                          until (uffi:null-pointer-p row)
145                          collect
146                            (loop for i from 0 below (mysql-num-fields res-ptr)
147                                collect
148                                  (uffi:convert-from-cstring
149                                   (uffi:deref-array row 'mysql-row i))))
150                    (mysql-free-result res-ptr))
151                (error 'clsql-sql-error
152                       :database database
153                       :expression query-expression
154                       :errno (mysql-errno mysql-ptr)
155                       :error (mysql-error-string mysql-ptr))))
156          (error 'clsql-sql-error
157                 :database database
158                 :expression query-expression
159                 :errno (mysql-errno mysql-ptr)
160                 :error (mysql-error-string mysql-ptr))))))
161
162
163 (defmethod database-query-result-set (query-expression 
164                                       (database mysql-database)
165                                       &optional full-set)
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))
175                 (if full-set
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
182                      :database database
183                      :expression query-expression
184                      :errno (mysql-errno mysql-ptr)
185                      :error (mysql-error-string mysql-ptr))))
186         (error 'clsql-sql-error
187                :database database
188                :expression query-expression
189                :errno (mysql-errno mysql-ptr)
190                :error (mysql-error-string mysql-ptr))))))
191
192