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 (database-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-equal (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 ;; FIXME: Underlying database backend stuff should come from that backend
198 (case (database-odbc-db-type database)
200 (mapcan #'(lambda (s)
201 (let ((sn (%table-name-to-sequence-name (car s))))
203 (database-query "SHOW TABLES" database nil)))
204 ((:postgresql :postgresql-socket)
205 (mapcar #'(lambda (s) (%table-name-to-sequence-name (car s)))
206 (database-query "SELECT RELNAME FROM pg_class WHERE RELNAME LIKE '%clsql_seq%'"
209 (defmethod database-list-tables ((database odbc-database)
211 (declare (ignore owner))
212 (multiple-value-bind (rows col-names)
213 (odbc-dbi:list-all-database-tables :db (database-odbc-conn database))
214 (declare (ignore col-names))
215 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
216 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
217 (loop for row in rows
218 when (and (not (string-equal "information_schema" (nth 1 row)))
219 (string-equal "TABLE" (nth 3 row)))
220 collect (nth 2 row))))
222 (defmethod database-list-views ((database odbc-database)
224 (declare (ignore owner))
225 (multiple-value-bind (rows col-names)
226 (odbc-dbi:list-all-database-tables :db (database-odbc-conn database))
227 (declare (ignore col-names))
228 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
229 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
230 (loop for row in rows
231 when (and (not (string-equal "information_schema" (nth 1 row)))
232 (string-equal "VIEW" (nth 3 row)))
233 collect (nth 2 row))))
235 (defmethod database-list-attributes ((table string) (database odbc-database)
237 (declare (ignore owner))
238 (multiple-value-bind (rows col-names)
239 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
240 (let ((pos (position "COLUMN_NAME" col-names :test #'string-equal)))
242 (loop for row in rows
243 collect (nth pos row))))))
245 (defmethod database-attribute-type ((attribute string) (table string) (database odbc-database)
247 (declare (ignore owner))
248 (multiple-value-bind (rows col-names)
249 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
250 (let ((pos (position "TYPE_NAME" col-names :test #'string-equal)))
252 (loop for row in rows
253 collect (nth pos row))))))
255 (defmethod database-set-sequence-position (sequence-name
257 (database odbc-database))
258 (database-execute-command
259 (format nil "UPDATE ~A SET last_value=~A,is_called='t'"
260 (%sequence-name-to-table sequence-name)
265 (defmethod database-sequence-next (sequence-name (database odbc-database))
267 (let* ((table-name (%sequence-name-to-table sequence-name))
270 (concatenate 'string "SELECT last_value,is_called FROM "
275 ((char-equal (schar (second tuple) 0) #\f)
276 (database-execute-command
277 (format nil "UPDATE ~A SET is_called='t'" table-name)
281 (let ((new-pos (1+ (car tuple))))
282 (database-execute-command
283 (format nil "UPDATE ~A SET last_value=~D" table-name new-pos)
287 (defmethod database-sequence-last (sequence-name (database odbc-database))
289 (caar (database-query
290 (concatenate 'string "SELECT last_value FROM "
291 (%sequence-name-to-table sequence-name))
295 (defmethod database-create (connection-spec (type (eql :odbc)))
296 (warn "Not implemented."))
298 (defmethod database-destroy (connection-spec (type (eql :odbc)))
299 (warn "Not implemented."))
301 (defmethod database-probe (connection-spec (type (eql :odbc)))
302 (when (find (car connection-spec) (database-list connection-spec type)
303 :test #'string-equal)
306 (defmethod database-list (connection-spec (type (eql :odbc)))
307 (declare (ignore connection-spec))
308 (odbc-dbi:list-all-data-sources))
310 (defmethod database-list-indexes ((database odbc-database)
313 (dolist (table (database-list-tables database :owner owner) result)
315 (append (database-list-table-indexes table database :owner owner)
318 (defmethod database-list-table-indexes (table (database odbc-database)
320 (declare (ignore owner))
321 (odbc-list-table-indexes table database))
323 (defun odbc-list-table-indexes (table database)
324 (multiple-value-bind (rows col-names)
325 (odbc-dbi:list-table-indexes
327 :db (database-odbc-conn database))
328 (declare (ignore col-names))
329 ;; INDEX_NAME is hard-coded in sixth position by ODBC driver
330 ;; FIXME: ??? is hard-coded in the fourth position
332 (loop-rows rows (cdr loop-rows)))
333 ((null loop-rows) (nreverse results))
334 (let* ((row (car loop-rows))
336 (unless (find col results :test #'string-equal)
337 (push col results))))))
339 ;;; Database capabilities
341 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :odbc)))
345 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
349 (when (clsql-base-sys:database-type-library-loaded :odbc)
350 (clsql-base-sys:initialize-database-type :database-type :odbc))