1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Low-level interface for CLSQL ODBC backend
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; $Id: odbc-sql.lisp 8983 2004-04-12 21:16:48Z kevin $
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
19 (defpackage #:clsql-odbc
20 (:use #:common-lisp #:clsql-base-sys)
21 (:export #:odbc-database)
22 (:documentation "This is the CLSQL interface to ODBC."))
24 (in-package #:clsql-odbc)
28 (defclass odbc-database (database)
29 ((odbc-conn :accessor database-odbc-conn :initarg :odbc-conn)
30 (odbc-db-type :accessor database-odbc-db-type)))
32 (defmethod database-name-from-spec (connection-spec
33 (database-type (eql :odbc)))
34 (check-connection-spec connection-spec database-type (dsn user password))
35 (destructuring-bind (dsn user password) connection-spec
36 (declare (ignore password))
37 (concatenate 'string dsn "/" user)))
39 (defmethod database-connect (connection-spec (database-type (eql :odbc)))
40 (check-connection-spec connection-spec database-type (dsn user password))
41 (destructuring-bind (dsn user password) connection-spec
44 (make-instance 'odbc-database
45 :name (database-name-from-spec connection-spec :odbc)
48 (odbc-dbi:connect :user user
50 :data-source-name dsn))))
51 (store-type-of-connected-database db)
56 (error () ;; Init or Connect failed
57 (error 'clsql-connect-error
58 :database-type database-type
59 :connection-spec connection-spec
61 :error "Connection failed")))))
63 (defmethod database-underlying-type ((database odbc-database))
64 (odbc-db-type database))
66 (defun store-type-of-connected-database (db)
67 (let* ((odbc-conn (database-odbc-conn db))
68 (server-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_SERVER_NAME))
69 (dbms-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_DBMS_NAME))
71 ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
73 ((or (search "postgresql" server-name :test #'char-equal)
74 (search "postgresql" dbms-name :test #'char-equal))
76 ((or (search "mysql" server-name :test #'char-equal)
77 (search "mysql" dbms-name :test #'char-equal))
79 ((or (search "oracle" server-name :test #'char-equal)
80 (search "oracle" dbms-name :test #'char-equal))
82 (setf (database-odbc-db-type db) type)))
84 (defmethod database-disconnect ((database odbc-database))
85 (odbc-dbi:disconnect (database-odbc-conn database))
86 (setf (database-odbc-conn database) nil)
89 (defmethod database-query (query-expression (database odbc-database)
92 (odbc-dbi:sql query-expression :db (database-odbc-conn database)
93 :result-types result-types)
98 (error 'clsql-sql-error
100 :expression query-expression
102 :error "Query failed"))))
104 (defmethod database-execute-command (sql-expression
105 (database odbc-database))
107 (odbc-dbi:sql sql-expression :db (database-odbc-conn database))
112 (error 'clsql-sql-error
114 :expression sql-expression
116 :error "Execute command failed"))))
118 (defstruct odbc-result-set
121 (full-set nil :type boolean))
123 (defmethod database-query-result-set ((query-expression string)
124 (database odbc-database)
125 &key full-set result-types)
127 (multiple-value-bind (query column-names)
128 (odbc-dbi:sql query-expression
129 :db (database-odbc-conn database)
133 :result-types result-types)
135 (make-odbc-result-set :query query :full-set full-set
137 (length column-names)
138 nil ;; not able to return number of rows with odbc
142 (error 'clsql-sql-error
144 :expression query-expression
146 :error "Query result set failed"))))
148 (defmethod database-dump-result-set (result-set (database odbc-database))
149 (odbc-dbi:close-query (odbc-result-set-query result-set))
152 (defmethod database-store-next-row (result-set
153 (database odbc-database)
155 (let ((row (odbc-dbi:fetch-row (odbc-result-set-query result-set) nil 'eof)))
159 (loop for elem in row
162 (setf (car rest) elem))
165 ;;; Sequence functions
167 (defun %sequence-name-to-table (sequence-name)
168 (concatenate 'string "_clsql_seq_" (sql-escape sequence-name)))
170 (defun %table-name-to-sequence-name (table-name)
171 (and (>= (length table-name) 11)
172 (string= (subseq table-name 0 11) "_clsql_seq_")
173 (subseq table-name 11)))
175 (defmethod database-create-sequence (sequence-name
176 (database odbc-database))
177 (let ((table-name (%sequence-name-to-table sequence-name)))
178 (database-execute-command
179 (concatenate 'string "CREATE TABLE " table-name
180 " (last_value int NOT NULL PRIMARY KEY, increment_by int, min_value int, is_called char(1))")
182 (database-execute-command
183 (concatenate 'string "INSERT INTO " table-name
184 " VALUES (1,1,1,'f')")
187 (defmethod database-drop-sequence (sequence-name
188 (database odbc-database))
189 (database-execute-command
190 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
193 (defmethod database-list-sequences ((database odbc-database)
195 (declare (ignore owner))
196 (mapcar #'(lambda (s) (%table-name-to-sequence-name (car s)))
197 (database-query "SHOW TABLES LIKE '%clsql_seq%'"
200 (defmethod database-list-tables ((database odbc-database)
202 (declare (ignore owner))
203 (multiple-value-bind (rows col-names)
204 (odbc-dbi:list-all-database-tables :db (database-odbc-conn database))
205 (declare (ignore col-names))
206 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
207 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
208 (loop for row in rows
209 when (and (not (string-equal "information_schema" (nth 1 row)))
210 (string-equal "TABLE" (nth 3 row)))
211 collect (nth 2 row))))
213 (defmethod database-list-attributes ((table string) (database odbc-database)
215 (declare (ignore owner))
216 (multiple-value-bind (rows col-names)
217 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
218 (let ((pos (position "COLUMN_NAME" col-names :test #'string-equal)))
220 (loop for row in rows
221 collect (nth pos row))))))
223 (defmethod database-attribute-type ((attribute string) (table string) (database odbc-database)
225 (declare (ignore owner))
226 (multiple-value-bind (rows col-names)
227 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
228 (let ((pos (position "TYPE_NAME" col-names :test #'string-equal)))
230 (loop for row in rows
231 collect (nth pos row))))))
233 (defmethod database-set-sequence-position (sequence-name
235 (database odbc-database))
236 (database-execute-command
237 (format nil "UPDATE ~A SET last_value=~A,is_called='t'"
238 (%sequence-name-to-table sequence-name)
243 (defmethod database-sequence-next (sequence-name (database odbc-database))
245 (let* ((table-name (%sequence-name-to-table sequence-name))
248 (concatenate 'string "SELECT last_value,is_called FROM "
253 ((char-equal (schar (second tuple) 0) #\f)
254 (database-execute-command
255 (format nil "UPDATE ~A SET is_called='t'" table-name)
259 (let ((new-pos (1+ (car tuple))))
260 (database-execute-command
261 (format nil "UPDATE ~A SET last_value=~D" table-name new-pos)
265 (defmethod database-sequence-last (sequence-name (database odbc-database))
267 (caar (database-query
268 (concatenate 'string "SELECT last_value FROM "
269 (%sequence-name-to-table sequence-name))
273 (defmethod database-create (connection-spec (type (eql :odbc)))
274 (warn "Not implemented."))
276 (defmethod database-destroy (connection-spec (type (eql :odbc)))
277 (warn "Not implemented."))
279 (defmethod database-probe (connection-spec (type (eql :odbc)))
280 (when (find (car connection-spec) (database-list connection-spec type)
281 :test #'string-equal)
284 (defmethod database-list (connection-spec (type (eql :odbc)))
285 (declare (ignore connection-spec))
286 (odbc-dbi:list-all-data-sources))
288 (defmethod database-list-indexes ((database odbc-database)
291 (dolist (table (database-list-tables database :owner owner) result)
293 (append (database-list-table-indexes table database :owner owner)
296 (defmethod database-list-table-indexes (table (database odbc-database)
298 (declare (ignore owner))
299 (multiple-value-bind (rows col-names)
300 (odbc-dbi:list-table-indexes
301 (string-downcase table)
302 :db (database-odbc-conn database))
303 (declare (ignore col-names))
304 ;; INDEX_NAME is hard-coded in sixth position by ODBC driver
305 ;; FIXME: ??? is hard-coded in the fourth position
307 (loop-rows rows (cdr loop-rows)))
308 ((null loop-rows) (nreverse results))
309 (let* ((row (car loop-rows))
312 (unless (or (find col results :test #'string-equal)
313 #+ignore (equal "0" type))
314 (push col results))))))
316 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
320 (when (clsql-base-sys:database-type-library-loaded :odbc)
321 (clsql-base-sys:initialize-database-type :database-type :odbc))