1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: aodbc-sql.cl
6 ;;;; Purpose: Low-level interface for CLSQL AODBC backend
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
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 (in-package #:clsql-aodbc)
21 ;; interface foreign library loading routines
22 (defmethod clsql-base:database-type-library-loaded ((database-type (eql :aodbc)))
23 "T if foreign library was able to be loaded successfully. "
24 (when (find-package :dbi) ;; finds Allegro's DBI (AODBC) package
27 (defmethod clsql-base:database-type-load-foreign ((databae-type (eql :aodbc)))
30 (when (find-package :dbi)
31 (clsql-base:database-type-load-foreign :aodbc))
33 (defmethod database-initialize-database-type ((database-type (eql :aodbc)))
39 (defclass aodbc-database (database)
40 ((aodbc-conn :accessor database-aodbc-conn :initarg :aodbc-conn)
41 (aodbc-db-type :accessor database-aodbc-db-type :initform :unknown)))
43 (defmethod database-name-from-spec (connection-spec
44 (database-type (eql :aodbc)))
45 (check-connection-spec connection-spec database-type (dsn user password))
46 (destructuring-bind (dsn user password) connection-spec
47 (declare (ignore password))
48 (concatenate 'string dsn "/" user)))
50 (defmethod database-connect (connection-spec (database-type (eql :aodbc)))
51 (check-connection-spec connection-spec database-type (dsn user password))
53 (destructuring-bind (dsn user password) connection-spec
55 (make-instance 'aodbc-database
56 :name (database-name-from-spec connection-spec :aodbc)
59 (dbi:connect :user user
61 :data-source-name dsn))
64 (error () ;; Init or Connect failed
65 (error 'clsql-connect-error
66 :database-type database-type
67 :connection-spec connection-spec
69 :error "Connection failed")))))
71 (defmethod database-disconnect ((database aodbc-database))
73 (dbi:disconnect (database-aodbc-conn database))
74 (setf (database-aodbc-conn database) nil)
77 (defmethod database-query (query-expression (database aodbc-database) result-types field-names)
80 (dbi:sql query-expression :db (database-aodbc-conn database)
82 :column-names field-names)
86 (error 'clsql-sql-error
88 :expression query-expression
90 :error "Query failed"))))
92 (defmethod database-execute-command (sql-expression
93 (database aodbc-database))
96 (dbi:sql sql-expression :db (database-aodbc-conn database))
100 (error 'clsql-sql-error
102 :expression sql-expression
104 :error "Execute command failed"))))
106 (defstruct aodbc-result-set
108 (types nil :type cons)
109 (full-set nil :type boolean))
111 (defmethod database-query-result-set ((query-expression string)
112 (database aodbc-database)
113 &key full-set result-types)
116 (multiple-value-bind (query column-names)
117 (dbi:sql query-expression
118 :db (database-aodbc-conn database)
125 (make-aodbc-result-set :query query :full-set full-set
127 (length column-names)
128 nil ;; not able to return number of rows with aodbc
133 (error 'clsql-sql-error
135 :expression query-expression
137 :error "Query result set failed"))))
139 (defmethod database-dump-result-set (result-set (database aodbc-database))
141 (dbi:close-query (aodbc-result-set-query result-set))
144 (defmethod database-store-next-row (result-set
145 (database aodbc-database)
148 (let ((row (dbi:fetch-row (aodbc-result-set-query result-set) nil 'eof)))
152 (loop for elem in row
155 (setf (car rest) elem))
158 ;;; Sequence functions
160 (defun %sequence-name-to-table (sequence-name)
161 (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name)))
163 (defun %table-name-to-sequence-name (table-name)
164 (and (>= (length table-name) 11)
165 (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_")
166 (subseq table-name 11)))
168 (defmethod database-create-sequence (sequence-name
169 (database aodbc-database))
170 (let ((table-name (%sequence-name-to-table sequence-name)))
171 (database-execute-command
172 (concatenate 'string "CREATE TABLE " table-name
173 " (last_value int NOT NULL PRIMARY KEY, increment_by int, min_value int, is_called char(1))")
175 (database-execute-command
176 (concatenate 'string "INSERT INTO " table-name
177 " VALUES (1,1,1,'f')")
180 (defmethod database-drop-sequence (sequence-name
181 (database aodbc-database))
182 (database-execute-command
183 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
186 (defmethod database-list-sequences ((database aodbc-database)
188 (declare (ignore owner))
189 (warn "database-list-sequences not implemented for AODBC.")
192 (defmethod database-list-tables ((database aodbc-database)
194 (declare (ignore owner))
196 (multiple-value-bind (rows col-names)
197 (dbi:list-all-database-tables :db (database-aodbc-conn database))
198 (declare (ignore col-names))
199 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
200 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
201 (loop for row in rows
202 when (and (not (string-equal "information_schema" (nth 1 row)))
203 (string-equal "TABLE" (nth 3 row)))
204 collect (nth 2 row))))
206 (defmethod database-list-views ((database aodbc-database)
208 (declare (ignore owner))
210 (multiple-value-bind (rows col-names)
211 (dbi:list-all-database-tables :db (database-aodbc-conn database))
212 (declare (ignore col-names))
213 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
214 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
215 (loop for row in rows
216 when (and (not (string-equal "information_schema" (nth 1 row)))
217 (string-equal "VIEW" (nth 3 row)))
218 collect (nth 2 row))))
220 (defmethod database-list-attributes ((table string) (database aodbc-database)
222 (declare (ignore owner))
224 (multiple-value-bind (rows col-names)
225 (dbi:list-all-table-columns table :db (database-aodbc-conn database))
226 (let ((pos (position "COLUMN_NAME" col-names :test #'string-equal)))
228 (loop for row in rows
229 collect (nth pos row))))))
231 (defmethod database-attribute-type ((attribute string) (table string) (database aodbc-database)
233 (declare (ignore owner))
235 (multiple-value-bind (rows col-names)
236 (dbi:list-all-table-columns table :db (database-aodbc-conn database))
237 (let ((pos (position "TYPE_NAME" col-names :test #'string-equal)))
239 (loop for row in rows
240 collect (nth pos row))))))
242 (defmethod database-list-indexes ((database aodbc-database)
244 (warn "database-list-indexes not implemented for AODBC.")
247 (defmethod database-set-sequence-position (sequence-name
249 (database aodbc-database))
250 (database-execute-command
251 (format nil "UPDATE ~A SET last_value=~A,is_called='t'"
252 (%sequence-name-to-table sequence-name)
257 (defmethod database-sequence-next (sequence-name (database aodbc-database))
259 (let* ((table-name (%sequence-name-to-table sequence-name))
262 (concatenate 'string "SELECT last_value,is_called FROM "
267 ((char-equal (schar (second tuple) 0) #\f)
268 (database-execute-command
269 (format nil "UPDATE ~A SET is_called='t'" table-name)
273 (let ((new-pos (1+ (car tuple))))
274 (database-execute-command
275 (format nil "UPDATE ~A SET last_value=~D" table-name new-pos)
279 (defmethod database-sequence-last (sequence-name (database aodbc-database))
281 (caar (database-query
282 (concatenate 'string "SELECT last_value FROM "
283 (%sequence-name-to-table sequence-name))
287 (defmethod database-create (connection-spec (type (eql :aodbc)))
288 (warn "Not implemented."))
290 (defmethod database-destroy (connection-spec (type (eql :aodbc)))
291 (warn "Not implemented."))
293 (defmethod database-probe (connection-spec (type (eql :aodbc)))
294 (warn "Not implemented."))
296 ;;; Backend capabilities
298 (defmethod database-underlying-type ((database aodbc-database))
299 (database-aodbc-db-type database))
301 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :aodbc)))
304 (defmethod database-initialize-database-type ((database-type (eql :aodbc)))
307 (when (clsql-base:database-type-library-loaded :aodbc)
308 (clsql-base:initialize-database-type :database-type :aodbc))