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 (let ((pos (position "TABLE_NAME" col-names :test #'string-equal)))
204 (loop for row in rows
205 collect (nth pos row))))))
207 (defmethod database-list-attributes ((table string) (database odbc-database)
209 (declare (ignore owner))
210 (multiple-value-bind (rows col-names)
211 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
212 (let ((pos (position "COLUMN_NAME" col-names :test #'string-equal)))
214 (loop for row in rows
215 collect (nth pos row))))))
217 (defmethod database-attribute-type ((attribute string) (table string) (database odbc-database)
219 (declare (ignore owner))
220 (multiple-value-bind (rows col-names)
221 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
222 (let ((pos (position "TYPE_NAME" col-names :test #'string-equal)))
224 (loop for row in rows
225 collect (nth pos row))))))
227 (defmethod database-set-sequence-position (sequence-name
229 (database odbc-database))
230 (database-execute-command
231 (format nil "UPDATE ~A SET last_value=~A,is_called='t'"
232 (%sequence-name-to-table sequence-name)
237 (defmethod database-sequence-next (sequence-name (database odbc-database))
239 (let* ((table-name (%sequence-name-to-table sequence-name))
242 (concatenate 'string "SELECT last_value,is_called FROM "
247 ((char-equal (schar (second tuple) 0) #\f)
248 (database-execute-command
249 (format nil "UPDATE ~A SET is_called='t'" table-name)
253 (let ((new-pos (1+ (car tuple))))
254 (database-execute-command
255 (format nil "UPDATE ~A SET last_value=~D" table-name new-pos)
259 (defmethod database-sequence-last (sequence-name (database odbc-database))
261 (caar (database-query
262 (concatenate 'string "SELECT last_value FROM "
263 (%sequence-name-to-table sequence-name))
267 (defmethod database-create (connection-spec (type (eql :odbc)))
268 (warn "Not implemented."))
270 (defmethod database-destroy (connection-spec (type (eql :odbc)))
271 (warn "Not implemented."))
273 (defmethod database-probe (connection-spec (type (eql :odbc)))
274 (when (find (car connection-spec) (database-list connection-spec type)
275 :test #'string-equal)
278 (defmethod database-list (connection-spec (type (eql :odbc)))
279 (declare (ignore connection-spec))
280 (odbc-dbi:list-all-data-sources))
283 (when (clsql-base-sys:database-type-library-loaded :odbc)
284 (clsql-base-sys:initialize-database-type :database-type :odbc))