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)
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)
90 result-types field-names)
92 (odbc-dbi:sql query-expression :db (database-odbc-conn database)
93 :result-types result-types
94 :column-names field-names)
99 (error 'clsql-sql-error
101 :expression query-expression
103 :error "Query failed"))))
105 (defmethod database-execute-command (sql-expression
106 (database odbc-database))
108 (odbc-dbi:sql sql-expression :db (database-odbc-conn database))
113 (error 'clsql-sql-error
115 :expression sql-expression
117 :error "Execute command failed"))))
119 (defstruct odbc-result-set
122 (full-set nil :type boolean))
124 (defmethod database-query-result-set ((query-expression string)
125 (database odbc-database)
126 &key full-set result-types)
128 (multiple-value-bind (query column-names)
129 (odbc-dbi:sql query-expression
130 :db (database-odbc-conn database)
134 :result-types result-types)
136 (make-odbc-result-set :query query :full-set full-set
138 (length column-names)
139 nil ;; not able to return number of rows with odbc
143 (error 'clsql-sql-error
145 :expression query-expression
147 :error "Query result set failed"))))
149 (defmethod database-dump-result-set (result-set (database odbc-database))
150 (odbc-dbi:close-query (odbc-result-set-query result-set))
153 (defmethod database-store-next-row (result-set
154 (database odbc-database)
156 (let ((row (odbc-dbi:fetch-row (odbc-result-set-query result-set) nil 'eof)))
160 (loop for elem in row
163 (setf (car rest) elem))
166 ;;; Sequence functions
168 (defun %sequence-name-to-table (sequence-name)
169 (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name)))
171 (defun %table-name-to-sequence-name (table-name)
172 (and (>= (length table-name) 11)
173 (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_")
174 (subseq table-name 11)))
176 (defmethod database-create-sequence (sequence-name
177 (database odbc-database))
178 (let ((table-name (%sequence-name-to-table sequence-name)))
179 (database-execute-command
180 (concatenate 'string "CREATE TABLE " table-name
181 " (last_value int NOT NULL PRIMARY KEY, increment_by int, min_value int, is_called char(1))")
183 (database-execute-command
184 (concatenate 'string "INSERT INTO " table-name
185 " VALUES (1,1,1,'f')")
188 (defmethod database-drop-sequence (sequence-name
189 (database odbc-database))
190 (database-execute-command
191 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
194 (defmethod database-list-sequences ((database odbc-database)
196 (declare (ignore owner))
197 ;; FIXME: Underlying database backend stuff should come from that backend
199 (case (database-odbc-db-type database)
201 (mapcan #'(lambda (s)
202 (let ((sn (%table-name-to-sequence-name (car s))))
204 (database-query "SHOW TABLES" database nil nil)))
205 ((:postgresql :postgresql-socket)
206 (mapcar #'(lambda (s) (%table-name-to-sequence-name (car s)))
207 (database-query "SELECT RELNAME FROM pg_class WHERE RELNAME LIKE '%clsql_seq%'"
208 database nil nil)))))
210 (defmethod database-list-tables ((database odbc-database)
212 (declare (ignore owner))
213 (multiple-value-bind (rows col-names)
214 (odbc-dbi:list-all-database-tables :db (database-odbc-conn database))
215 (declare (ignore col-names))
216 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
217 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
218 (loop for row in rows
219 when (and (not (string-equal "information_schema" (nth 1 row)))
220 (string-equal "TABLE" (nth 3 row)))
221 collect (nth 2 row))))
223 (defmethod database-list-views ((database odbc-database)
225 (declare (ignore owner))
226 (multiple-value-bind (rows col-names)
227 (odbc-dbi:list-all-database-tables :db (database-odbc-conn database))
228 (declare (ignore col-names))
229 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
230 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
231 (loop for row in rows
232 when (and (not (string-equal "information_schema" (nth 1 row)))
233 (string-equal "VIEW" (nth 3 row)))
234 collect (nth 2 row))))
236 (defmethod database-list-attributes ((table string) (database odbc-database)
238 (declare (ignore owner))
239 (multiple-value-bind (rows col-names)
240 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
241 (declare (ignore col-names))
242 ;; COLUMN_NAME is hard-coded by odbc spec as fourth position
243 (loop for row in rows
244 collect (fourth row))))
246 (defmethod database-attribute-type ((attribute string) (table string) (database odbc-database)
248 (declare (ignore owner))
249 (multiple-value-bind (rows col-names)
250 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
251 (declare (ignore col-names))
252 ;; COLUMN_NAME is hard-coded by odbc spec as fourth position
253 ;; TYPE_NAME is the sixth column
254 ;; PRECISION/COLUMN_SIZE is the seventh column
255 ;; SCALE/DECIMAL_DIGITS is the ninth column
256 ;; NULLABLE is the eleventh column
257 (loop for row in rows
258 when (string-equal attribute (fourth row))
260 (let ((size (seventh row))
261 (precision (ninth row))
262 (scale (nth 10 row)))
263 (return (values (ensure-keyword (sixth row))
264 (when size (parse-integer size))
265 (when precision (parse-integer precision))
266 (when scale (parse-integer scale))))))))
268 (defmethod database-set-sequence-position (sequence-name
270 (database odbc-database))
271 (database-execute-command
272 (format nil "UPDATE ~A SET last_value=~A,is_called='t'"
273 (%sequence-name-to-table sequence-name)
278 (defmethod database-sequence-next (sequence-name (database odbc-database))
280 (let* ((table-name (%sequence-name-to-table sequence-name))
283 (concatenate 'string "SELECT last_value,is_called FROM "
285 database :auto nil))))
287 ((char-equal (schar (second tuple) 0) #\f)
288 (database-execute-command
289 (format nil "UPDATE ~A SET is_called='t'" table-name)
293 (let ((new-pos (1+ (car tuple))))
294 (database-execute-command
295 (format nil "UPDATE ~A SET last_value=~D" table-name new-pos)
299 (defmethod database-sequence-last (sequence-name (database odbc-database))
301 (caar (database-query
302 (concatenate 'string "SELECT last_value FROM "
303 (%sequence-name-to-table sequence-name))
304 database :auto nil))))
306 (defmethod database-create (connection-spec (type (eql :odbc)))
307 (declare (ignore connection-spec))
308 (warn "Not implemented."))
310 (defmethod database-destroy (connection-spec (type (eql :odbc)))
311 (declare (ignore connection-spec))
312 (warn "Not implemented."))
314 (defmethod database-probe (connection-spec (type (eql :odbc)))
315 (when (find (car connection-spec) (database-list connection-spec type)
316 :test #'string-equal)
319 (defmethod database-list (connection-spec (type (eql :odbc)))
320 (declare (ignore connection-spec))
321 (odbc-dbi:list-all-data-sources))
323 (defmethod database-list-indexes ((database odbc-database)
326 (dolist (table (database-list-tables database :owner owner) result)
328 (append (database-list-table-indexes table database :owner owner)
331 (defmethod database-list-table-indexes (table (database odbc-database)
333 (declare (ignore owner))
334 (odbc-list-table-indexes table database))
336 (defun odbc-list-table-indexes (table database)
337 (multiple-value-bind (rows col-names)
338 (odbc-dbi:list-table-indexes
340 :db (database-odbc-conn database))
341 (declare (ignore col-names))
342 ;; INDEX_NAME is hard-coded in sixth position by ODBC driver
343 ;; FIXME: ??? is hard-coded in the fourth position
345 (loop-rows rows (cdr loop-rows)))
346 ((null loop-rows) (nreverse results))
347 (let* ((row (car loop-rows))
349 (unless (find col results :test #'string-equal)
350 (push col results))))))
352 ;;; Database capabilities
354 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :odbc)))
358 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
362 (when (clsql-base:database-type-library-loaded :odbc)
363 (clsql-base:initialize-database-type :database-type :odbc))