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)
55 (error () ;; Init or Connect failed
56 (error 'clsql-connect-error
57 :database-type database-type
58 :connection-spec connection-spec
60 :error "Connection failed")))))
62 (defun store-type-of-connected-database (db)
63 (let* ((odbc-conn (database-odbc-conn db))
64 (server-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_SERVER_NAME))
65 (dbms-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_DBMS_NAME))
67 ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
69 ((or (search "postgresql" server-name :test #'char-equal)
70 (search "postgresql" dbms-name :test #'char-equal))
72 ((or (search "mysql" server-name :test #'char-equal)
73 (search "mysql" dbms-name :test #'char-equal))
75 ((or (search "oracle" server-name :test #'char-equal)
76 (search "oracle" dbms-name :test #'char-equal))
78 (setf (database-odbc-db-type db) type)))
80 (defmethod database-disconnect ((database odbc-database))
81 (odbc-dbi:disconnect (database-odbc-conn database))
82 (setf (database-odbc-conn database) nil)
85 (defmethod database-query (query-expression (database odbc-database)
88 (odbc-dbi:sql query-expression :db (database-odbc-conn database)
89 :result-types result-types)
94 (error 'clsql-sql-error
96 :expression query-expression
98 :error "Query failed"))))
100 (defmethod database-execute-command (sql-expression
101 (database odbc-database))
103 (odbc-dbi:sql sql-expression :db (database-odbc-conn database))
108 (error 'clsql-sql-error
110 :expression sql-expression
112 :error "Execute command failed"))))
114 (defstruct odbc-result-set
117 (full-set nil :type boolean))
119 (defmethod database-query-result-set ((query-expression string)
120 (database odbc-database)
121 &key full-set result-types)
123 (multiple-value-bind (query column-names)
124 (odbc-dbi:sql query-expression
125 :db (database-odbc-conn database)
129 :result-types result-types)
131 (make-odbc-result-set :query query :full-set full-set
133 (length column-names)
134 nil ;; not able to return number of rows with odbc
137 (error 'clsql-sql-error
139 :expression query-expression
141 :error "Query result set failed"))))
143 (defmethod database-dump-result-set (result-set (database odbc-database))
144 (odbc-dbi:close-query (odbc-result-set-query result-set))
147 (defmethod database-store-next-row (result-set
148 (database odbc-database)
150 (let ((row (odbc-dbi:fetch-row (odbc-result-set-query result-set) nil 'eof)))
154 (loop for elem in row
157 (setf (car rest) elem))
160 ;;; Sequence functions
162 (defun %sequence-name-to-table (sequence-name)
163 (concatenate 'string "_clsql_seq_" (sql-escape sequence-name)))
165 (defun %table-name-to-sequence-name (table-name)
166 (and (>= (length table-name) 11)
167 (string= (subseq table-name 0 11) "_clsql_seq_")
168 (subseq table-name 11)))
170 (defmethod database-create-sequence (sequence-name
171 (database odbc-database))
172 (let ((table-name (%sequence-name-to-table sequence-name)))
173 (database-execute-command
174 (concatenate 'string "CREATE TABLE " table-name
175 " (last_value int NOT NULL PRIMARY KEY, increment_by int, min_value int, is_called char(1))")
177 (database-execute-command
178 (concatenate 'string "INSERT INTO " table-name
179 " VALUES (1,1,1,'f')")
182 (defmethod database-drop-sequence (sequence-name
183 (database odbc-database))
184 (database-execute-command
185 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
188 (defmethod database-list-sequences ((database odbc-database)
190 (declare (ignore owner))
191 (mapcar #'(lambda (s) (%table-name-to-sequence-name (car s)))
192 (database-query "SHOW TABLES LIKE '%clsql_seq%'"
195 (defmethod database-list-tables ((database odbc-database)
197 (declare (ignore owner))
198 (multiple-value-bind (rows col-names)
199 (odbc-dbi:list-all-database-tables :db (database-odbc-conn database))
200 (let ((pos (position "TABLE_NAME" col-names :test #'string-equal)))
202 (loop for row in rows
203 collect (nth pos row))))))
205 (defmethod database-list-attributes ((table string) (database odbc-database)
207 (declare (ignore owner))
208 (multiple-value-bind (rows col-names)
209 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
210 (let ((pos (position "COLUMN_NAME" col-names :test #'string-equal)))
212 (loop for row in rows
213 collect (nth pos row))))))
215 (defmethod database-attribute-type ((attribute string) (table string) (database odbc-database)
217 (declare (ignore owner))
218 (multiple-value-bind (rows col-names)
219 (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database))
220 (let ((pos (position "TYPE_NAME" col-names :test #'string-equal)))
222 (loop for row in rows
223 collect (nth pos row))))))
225 (defmethod database-set-sequence-position (sequence-name
227 (database odbc-database))
228 (database-execute-command
229 (format nil "UPDATE ~A SET last_value=~A,is_called='t'"
230 (%sequence-name-to-table sequence-name)
235 (defmethod database-sequence-next (sequence-name (database odbc-database))
237 (let* ((table-name (%sequence-name-to-table sequence-name))
240 (concatenate 'string "SELECT last_value,is_called FROM "
245 ((char-equal (schar (second tuple) 0) #\f)
246 (database-execute-command
247 (format nil "UPDATE ~A SET is_called='t'" table-name)
251 (let ((new-pos (1+ (car tuple))))
252 (database-execute-command
253 (format nil "UPDATE ~A SET last_value=~D" table-name new-pos)
257 (defmethod database-sequence-last (sequence-name (database odbc-database))
259 (caar (database-query
260 (concatenate 'string "SELECT last_value FROM "
261 (%sequence-name-to-table sequence-name))
265 (defmethod database-create (connection-spec (type (eql :odbc)))
266 (warn "Not implemented."))
268 (defmethod database-destroy (connection-spec (type (eql :odbc)))
269 (warn "Not implemented."))
271 (defmethod database-probe (connection-spec (type (eql :odbc)))
272 (warn "Not implemented."))
275 (when (clsql-base-sys:database-type-library-loaded :odbc)
276 (clsql-base-sys:initialize-database-type :database-type :odbc))