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-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 'sql-connection-error
58 :database-type database-type
59 :connection-spec connection-spec
60 :message "Connection failed")))))
62 (defmethod database-underlying-type ((database odbc-database))
63 (database-odbc-db-type database))
65 (defun store-type-of-connected-database (db)
66 (let* ((odbc-conn (database-odbc-conn db))
67 (server-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_SERVER_NAME))
68 (dbms-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_DBMS_NAME))
70 ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
72 ((or (search "postgresql" server-name :test #'char-equal)
73 (search "postgresql" dbms-name :test #'char-equal))
75 ((or (search "mysql" server-name :test #'char-equal)
76 (search "mysql" dbms-name :test #'char-equal))
78 ((or (search "oracle" server-name :test #'char-equal)
79 (search "oracle" dbms-name :test #'char-equal))
81 (setf (database-odbc-db-type db) type)))
83 (defmethod database-disconnect ((database odbc-database))
84 (odbc-dbi:disconnect (database-odbc-conn database))
85 (setf (database-odbc-conn database) nil)
88 (defmethod database-query (query-expression (database odbc-database)
89 result-types field-names)
91 (odbc-dbi:sql query-expression :db (database-odbc-conn database)
92 :result-types result-types
93 :column-names field-names)
98 (error 'sql-database-data-error
100 :expression query-expression
101 :message "Query failed"))))
103 (defmethod database-execute-command (sql-expression
104 (database odbc-database))
106 (odbc-dbi:sql sql-expression :db (database-odbc-conn database))
111 (error 'sql-database-data-error
113 :expression sql-expression
114 :message "Execute command failed"))))
116 (defstruct odbc-result-set
119 (full-set nil :type boolean))
121 (defmethod database-query-result-set ((query-expression string)
122 (database odbc-database)
123 &key full-set result-types)
125 (multiple-value-bind (query column-names)
126 (odbc-dbi:sql query-expression
127 :db (database-odbc-conn database)
131 :result-types result-types)
133 (make-odbc-result-set :query query :full-set full-set
135 (length column-names)
136 nil ;; not able to return number of rows with odbc
139 (error 'sql-database-data-error
141 :expression query-expression
142 :message "Query result set failed"))))
144 (defmethod database-dump-result-set (result-set (database odbc-database))
145 (odbc-dbi:close-query (odbc-result-set-query result-set))
148 (defmethod database-store-next-row (result-set
149 (database odbc-database)
151 (let ((row (odbc-dbi:fetch-row (odbc-result-set-query result-set) nil 'eof)))
155 (loop for elem in row
158 (setf (car rest) elem))
161 ;;; Sequence functions
163 (defun %sequence-name-to-table (sequence-name)
164 (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name)))
166 (defun %table-name-to-sequence-name (table-name)
167 (and (>= (length table-name) 11)
168 (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_")
169 (subseq table-name 11)))
171 (defmethod database-create-sequence (sequence-name
172 (database odbc-database))
173 (let ((table-name (%sequence-name-to-table sequence-name)))
174 (database-execute-command
175 (concatenate 'string "CREATE TABLE " table-name
176 " (last_value int NOT NULL PRIMARY KEY, increment_by int, min_value int, is_called char(1))")
178 (database-execute-command
179 (concatenate 'string "INSERT INTO " table-name
180 " VALUES (1,1,1,'f')")
183 (defmethod database-drop-sequence (sequence-name
184 (database odbc-database))
185 (database-execute-command
186 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
189 (defmethod database-list-sequences ((database odbc-database)
191 (declare (ignore owner))
192 ;; FIXME: Underlying database backend stuff should come from that backend
194 (case (database-odbc-db-type database)
196 (mapcan #'(lambda (s)
197 (let ((sn (%table-name-to-sequence-name (car s))))
199 (database-query "SHOW TABLES" database nil nil)))
200 ((:postgresql :postgresql-socket)
201 (mapcar #'(lambda (s) (%table-name-to-sequence-name (car s)))
202 (database-query "SELECT RELNAME FROM pg_class WHERE RELNAME LIKE '%clsql_seq%'"
203 database nil nil)))))
205 (defmethod database-list-tables ((database odbc-database)
207 (declare (ignore owner))
208 (multiple-value-bind (rows col-names)
209 (odbc-dbi:list-all-database-tables :db (database-odbc-conn database))
210 (declare (ignore col-names))
211 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
212 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
213 (loop for row in rows
214 when (and (not (string-equal "information_schema" (nth 1 row)))
215 (string-equal "TABLE" (nth 3 row)))
216 collect (nth 2 row))))
218 (defmethod database-list-views ((database odbc-database)
220 (declare (ignore owner))
221 (multiple-value-bind (rows col-names)
222 (odbc-dbi:list-all-database-tables :db (database-odbc-conn database))
223 (declare (ignore col-names))
224 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
225 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
226 (loop for row in rows
227 when (and (not (string-equal "information_schema" (nth 1 row)))
228 (string-equal "VIEW" (nth 3 row)))
229 collect (nth 2 row))))
231 (defmethod database-list-attributes ((table string) (database odbc-database)
233 (declare (ignore owner))
234 (multiple-value-bind (rows col-names)
235 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
236 (declare (ignore col-names))
237 ;; COLUMN_NAME is hard-coded by odbc spec as fourth position
238 (loop for row in rows
239 collect (fourth row))))
241 (defmethod database-attribute-type ((attribute string) (table string) (database odbc-database)
243 (declare (ignore owner))
244 (multiple-value-bind (rows col-names)
245 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
246 (declare (ignore col-names))
247 ;; COLUMN_NAME is hard-coded by odbc spec as fourth position
248 ;; TYPE_NAME is the sixth column
249 ;; PRECISION/COLUMN_SIZE is the seventh column
250 ;; SCALE/DECIMAL_DIGITS is the ninth column
251 ;; NULLABLE is the eleventh column
252 (loop for row in rows
253 when (string-equal attribute (fourth row))
255 (let ((size (seventh row))
256 (precision (ninth row))
257 (scale (nth 10 row)))
258 (return (values (ensure-keyword (sixth row))
259 (when size (parse-integer size))
260 (when precision (parse-integer precision))
261 (when scale (parse-integer scale))))))))
263 (defmethod database-set-sequence-position (sequence-name
265 (database odbc-database))
266 (database-execute-command
267 (format nil "UPDATE ~A SET last_value=~A,is_called='t'"
268 (%sequence-name-to-table sequence-name)
273 (defmethod database-sequence-next (sequence-name (database odbc-database))
275 (let* ((table-name (%sequence-name-to-table sequence-name))
278 (concatenate 'string "SELECT last_value,is_called FROM "
280 database :auto nil))))
282 ((char-equal (schar (second tuple) 0) #\f)
283 (database-execute-command
284 (format nil "UPDATE ~A SET is_called='t'" table-name)
288 (let ((new-pos (1+ (car tuple))))
289 (database-execute-command
290 (format nil "UPDATE ~A SET last_value=~D" table-name new-pos)
294 (defmethod database-sequence-last (sequence-name (database odbc-database))
296 (caar (database-query
297 (concatenate 'string "SELECT last_value FROM "
298 (%sequence-name-to-table sequence-name))
299 database :auto nil))))
301 (defmethod database-create (connection-spec (type (eql :odbc)))
302 (declare (ignore connection-spec))
303 (warn "Not implemented."))
305 (defmethod database-destroy (connection-spec (type (eql :odbc)))
306 (declare (ignore connection-spec))
307 (warn "Not implemented."))
309 (defmethod database-probe (connection-spec (type (eql :odbc)))
310 (when (find (car connection-spec) (database-list connection-spec type)
311 :test #'string-equal)
314 (defmethod database-list (connection-spec (type (eql :odbc)))
315 (declare (ignore connection-spec))
316 (odbc-dbi:list-all-data-sources))
318 (defmethod database-list-indexes ((database odbc-database)
321 (dolist (table (database-list-tables database :owner owner) result)
323 (append (database-list-table-indexes table database :owner owner)
326 (defmethod database-list-table-indexes (table (database odbc-database)
328 (declare (ignore owner))
329 (odbc-list-table-indexes table database))
331 (defun odbc-list-table-indexes (table database)
332 (multiple-value-bind (rows col-names)
333 (odbc-dbi:list-table-indexes
335 :db (database-odbc-conn database))
336 (declare (ignore col-names))
337 ;; INDEX_NAME is hard-coded in sixth position by ODBC driver
338 ;; FIXME: ??? is hard-coded in the fourth position
340 (loop-rows rows (cdr loop-rows)))
341 ((null loop-rows) (nreverse results))
342 (let* ((row (car loop-rows))
344 (unless (find col results :test #'string-equal)
345 (push col results))))))
347 ;;; Database capabilities
349 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :odbc)))
353 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
357 (when (clsql-sys:database-type-library-loaded :odbc)
358 (clsql-sys:initialize-database-type :database-type :odbc))