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 (defun store-type-of-connected-database (db)
64 (let* ((odbc-conn (database-odbc-conn db))
65 (server-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_SERVER_NAME))
66 (dbms-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_DBMS_NAME))
68 ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
70 ((or (search "postgresql" server-name :test #'char-equal)
71 (search "postgresql" dbms-name :test #'char-equal))
73 ((or (search "mysql" server-name :test #'char-equal)
74 (search "mysql" dbms-name :test #'char-equal))
76 ((or (search "oracle" server-name :test #'char-equal)
77 (search "oracle" dbms-name :test #'char-equal))
79 (setf (database-odbc-db-type db) type)))
81 (defmethod database-disconnect ((database odbc-database))
82 (odbc-dbi:disconnect (database-odbc-conn database))
83 (setf (database-odbc-conn database) nil)
86 (defmethod database-query (query-expression (database odbc-database)
89 (odbc-dbi:sql query-expression :db (database-odbc-conn database)
90 :result-types result-types)
95 (error 'clsql-sql-error
97 :expression query-expression
99 :error "Query failed"))))
101 (defmethod database-execute-command (sql-expression
102 (database odbc-database))
104 (odbc-dbi:sql sql-expression :db (database-odbc-conn database))
109 (error 'clsql-sql-error
111 :expression sql-expression
113 :error "Execute command failed"))))
115 (defstruct odbc-result-set
118 (full-set nil :type boolean))
120 (defmethod database-query-result-set ((query-expression string)
121 (database odbc-database)
122 &key full-set result-types)
124 (multiple-value-bind (query column-names)
125 (odbc-dbi:sql query-expression
126 :db (database-odbc-conn database)
130 :result-types result-types)
132 (make-odbc-result-set :query query :full-set full-set
134 (length column-names)
135 nil ;; not able to return number of rows with odbc
139 (error 'clsql-sql-error
141 :expression query-expression
143 :error "Query result set failed"))))
145 (defmethod database-dump-result-set (result-set (database odbc-database))
146 (odbc-dbi:close-query (odbc-result-set-query result-set))
149 (defmethod database-store-next-row (result-set
150 (database odbc-database)
152 (let ((row (odbc-dbi:fetch-row (odbc-result-set-query result-set) nil 'eof)))
156 (loop for elem in row
159 (setf (car rest) elem))
162 ;;; Sequence functions
164 (defun %sequence-name-to-table (sequence-name)
165 (concatenate 'string "_clsql_seq_" (sql-escape sequence-name)))
167 (defun %table-name-to-sequence-name (table-name)
168 (and (>= (length table-name) 11)
169 (string= (subseq table-name 0 11) "_clsql_seq_")
170 (subseq table-name 11)))
172 (defmethod database-create-sequence (sequence-name
173 (database odbc-database))
174 (let ((table-name (%sequence-name-to-table sequence-name)))
175 (database-execute-command
176 (concatenate 'string "CREATE TABLE " table-name
177 " (last_value int NOT NULL PRIMARY KEY, increment_by int, min_value int, is_called char(1))")
179 (database-execute-command
180 (concatenate 'string "INSERT INTO " table-name
181 " VALUES (1,1,1,'f')")
184 (defmethod database-drop-sequence (sequence-name
185 (database odbc-database))
186 (database-execute-command
187 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
190 (defmethod database-list-sequences ((database odbc-database)
192 (declare (ignore owner))
193 (mapcar #'(lambda (s) (%table-name-to-sequence-name (car s)))
194 (database-query "SHOW TABLES LIKE '%clsql_seq%'"
197 (defmethod database-list-tables ((database odbc-database)
199 (declare (ignore owner))
200 (multiple-value-bind (rows col-names)
201 (odbc-dbi:list-all-database-tables :db (database-odbc-conn database))
202 (declare (ignore col-names))
203 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
204 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
205 (loop for row in rows
206 when (and (not (string-equal "information_schema" (nth 1 row)))
207 (string-equal "TABLE" (nth 3 row)))
208 collect (nth 2 row))))
210 (defmethod database-list-attributes ((table string) (database odbc-database)
212 (declare (ignore owner))
213 (multiple-value-bind (rows col-names)
214 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
215 (let ((pos (position "COLUMN_NAME" col-names :test #'string-equal)))
217 (loop for row in rows
218 collect (nth pos row))))))
220 (defmethod database-attribute-type ((attribute string) (table string) (database odbc-database)
222 (declare (ignore owner))
223 (multiple-value-bind (rows col-names)
224 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
225 (let ((pos (position "TYPE_NAME" col-names :test #'string-equal)))
227 (loop for row in rows
228 collect (nth pos row))))))
230 (defmethod database-set-sequence-position (sequence-name
232 (database odbc-database))
233 (database-execute-command
234 (format nil "UPDATE ~A SET last_value=~A,is_called='t'"
235 (%sequence-name-to-table sequence-name)
240 (defmethod database-sequence-next (sequence-name (database odbc-database))
242 (let* ((table-name (%sequence-name-to-table sequence-name))
245 (concatenate 'string "SELECT last_value,is_called FROM "
250 ((char-equal (schar (second tuple) 0) #\f)
251 (database-execute-command
252 (format nil "UPDATE ~A SET is_called='t'" table-name)
256 (let ((new-pos (1+ (car tuple))))
257 (database-execute-command
258 (format nil "UPDATE ~A SET last_value=~D" table-name new-pos)
262 (defmethod database-sequence-last (sequence-name (database odbc-database))
264 (caar (database-query
265 (concatenate 'string "SELECT last_value FROM "
266 (%sequence-name-to-table sequence-name))
270 (defmethod database-create (connection-spec (type (eql :odbc)))
271 (warn "Not implemented."))
273 (defmethod database-destroy (connection-spec (type (eql :odbc)))
274 (warn "Not implemented."))
276 (defmethod database-probe (connection-spec (type (eql :odbc)))
277 (when (find (car connection-spec) (database-list connection-spec type)
278 :test #'string-equal)
281 (defmethod database-list (connection-spec (type (eql :odbc)))
282 (declare (ignore connection-spec))
283 (odbc-dbi:list-all-data-sources))
285 (defmethod database-list-indexes ((database odbc-database)
288 (dolist (table (database-list-tables database :owner owner) result)
290 (append (database-list-table-indexes table database :owner owner)
293 (defmethod database-list-table-indexes (table (database odbc-database)
295 (declare (ignore owner))
296 (multiple-value-bind (rows col-names)
297 (odbc-dbi:list-table-indexes
298 (string-downcase table)
299 :db (database-odbc-conn database))
300 (declare (ignore col-names))
301 ;; INDEX_NAME is hard-coded in sixth position by ODBC driver
302 ;; FIXME: ??? is hard-coded in the fourth position
304 (loop-rows rows (cdr loop-rows)))
305 ((null loop-rows) (nreverse results))
306 (let* ((row (car loop-rows))
309 (unless (or (find col results :test #'string-equal)
310 #+ignore (equal "0" type))
311 (push col results))))))
313 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
317 (when (clsql-base-sys:database-type-library-loaded :odbc)
318 (clsql-base-sys:initialize-database-type :database-type :odbc))