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-sys: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-sys:database-type-load-foreign ((databae-type (eql :aodbc)))
30 (when (find-package :dbi)
31 (clsql-base-sys: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)
80 (dbi:sql query-expression :db (database-aodbc-conn database)
85 (error 'clsql-sql-error
87 :expression query-expression
89 :error "Query failed"))))
91 (defmethod database-execute-command (sql-expression
92 (database aodbc-database))
95 (dbi:sql sql-expression :db (database-aodbc-conn database))
99 (error 'clsql-sql-error
101 :expression sql-expression
103 :error "Execute command failed"))))
105 (defstruct aodbc-result-set
107 (types nil :type cons)
108 (full-set nil :type boolean))
110 (defmethod database-query-result-set ((query-expression string)
111 (database aodbc-database)
112 &key full-set result-types)
115 (multiple-value-bind (query column-names)
116 (dbi:sql query-expression
117 :db (database-aodbc-conn database)
124 (make-aodbc-result-set :query query :full-set full-set
126 (length column-names)
127 nil ;; not able to return number of rows with aodbc
132 (error 'clsql-sql-error
134 :expression query-expression
136 :error "Query result set failed"))))
138 (defmethod database-dump-result-set (result-set (database aodbc-database))
140 (dbi:close-query (aodbc-result-set-query result-set))
143 (defmethod database-store-next-row (result-set
144 (database aodbc-database)
147 (let ((row (dbi:fetch-row (aodbc-result-set-query result-set) nil 'eof)))
151 (loop for elem in row
154 (setf (car rest) elem))
157 ;;; Sequence functions
159 (defun %sequence-name-to-table (sequence-name)
160 (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name)))
162 (defun %table-name-to-sequence-name (table-name)
163 (and (>= (length table-name) 11)
164 (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_")
165 (subseq table-name 11)))
167 (defmethod database-create-sequence (sequence-name
168 (database aodbc-database))
169 (let ((table-name (%sequence-name-to-table sequence-name)))
170 (database-execute-command
171 (concatenate 'string "CREATE TABLE " table-name
172 " (last_value int NOT NULL PRIMARY KEY, increment_by int, min_value int, is_called char(1))")
174 (database-execute-command
175 (concatenate 'string "INSERT INTO " table-name
176 " VALUES (1,1,1,'f')")
179 (defmethod database-drop-sequence (sequence-name
180 (database aodbc-database))
181 (database-execute-command
182 (concatenate 'string "DROP TABLE " (%sequence-name-to-table sequence-name))
185 (defmethod database-list-sequences ((database aodbc-database)
187 (declare (ignore owner))
188 (warn "database-list-sequences not implemented for AODBC.")
191 (defmethod database-list-tables ((database aodbc-database)
193 (declare (ignore owner))
195 (multiple-value-bind (rows col-names)
196 (dbi:list-all-database-tables :db (database-aodbc-conn database))
197 (declare (ignore col-names))
198 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
199 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
200 (loop for row in rows
201 when (and (not (string-equal "information_schema" (nth 1 row)))
202 (string-equal "TABLE" (nth 3 row)))
203 collect (nth 2 row))))
205 (defmethod database-list-views ((database aodbc-database)
207 (declare (ignore owner))
209 (multiple-value-bind (rows col-names)
210 (dbi:list-all-database-tables :db (database-aodbc-conn database))
211 (declare (ignore col-names))
212 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
213 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
214 (loop for row in rows
215 when (and (not (string-equal "information_schema" (nth 1 row)))
216 (string-equal "VIEW" (nth 3 row)))
217 collect (nth 2 row))))
219 (defmethod database-list-attributes ((table string) (database aodbc-database)
221 (declare (ignore owner))
223 (multiple-value-bind (rows col-names)
224 (dbi:list-all-table-columns table :db (database-aodbc-conn database))
225 (let ((pos (position "COLUMN_NAME" col-names :test #'string-equal)))
227 (loop for row in rows
228 collect (nth pos row))))))
230 (defmethod database-attribute-type ((attribute string) (table string) (database aodbc-database)
232 (declare (ignore owner))
234 (multiple-value-bind (rows col-names)
235 (dbi:list-all-table-columns table :db (database-aodbc-conn database))
236 (let ((pos (position "TYPE_NAME" col-names :test #'string-equal)))
238 (loop for row in rows
239 collect (nth pos row))))))
241 (defmethod database-list-indexes ((database aodbc-database)
243 (warn "database-list-indexes not implemented for AODBC.")
246 (defmethod database-set-sequence-position (sequence-name
248 (database aodbc-database))
249 (database-execute-command
250 (format nil "UPDATE ~A SET last_value=~A,is_called='t'"
251 (%sequence-name-to-table sequence-name)
256 (defmethod database-sequence-next (sequence-name (database aodbc-database))
258 (let* ((table-name (%sequence-name-to-table sequence-name))
261 (concatenate 'string "SELECT last_value,is_called FROM "
266 ((char-equal (schar (second tuple) 0) #\f)
267 (database-execute-command
268 (format nil "UPDATE ~A SET is_called='t'" table-name)
272 (let ((new-pos (1+ (car tuple))))
273 (database-execute-command
274 (format nil "UPDATE ~A SET last_value=~D" table-name new-pos)
278 (defmethod database-sequence-last (sequence-name (database aodbc-database))
280 (caar (database-query
281 (concatenate 'string "SELECT last_value FROM "
282 (%sequence-name-to-table sequence-name))
286 (defmethod database-create (connection-spec (type (eql :aodbc)))
287 (warn "Not implemented."))
289 (defmethod database-destroy (connection-spec (type (eql :aodbc)))
290 (warn "Not implemented."))
292 (defmethod database-probe (connection-spec (type (eql :aodbc)))
293 (warn "Not implemented."))
295 ;;; Backend capabilities
297 (defmethod database-underlying-type ((database aodbc-database))
298 (database-aodbc-db-type database))
300 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :aodbc)))
304 (when (clsql-base-sys:database-type-library-loaded :aodbc)
305 (clsql-base-sys:initialize-database-type :database-type :aodbc))