1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
4 ;;;; Generic ODBC layer, used by db-odbc and db-aodbc backends
6 ;;;; This file is part of CLSQL.
8 ;;;; CLSQL users are granted the rights to distribute and use this software
9 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
10 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
11 ;;;; *************************************************************************
13 (in-package #:clsql-sys)
15 (defclass generic-odbc-database (database)
16 ((dbi-package :initarg :dbi-package :reader dbi-package)
17 (odbc-conn :initarg :odbc-conn :initform nil :accessor odbc-conn)
18 (disconnect-fn :reader disconnect-fn)
19 (sql-fn :reader sql-fn)
20 (close-query-fn :reader close-query-fn)
21 (fetch-row :reader fetch-row-fn)
22 (list-all-database-tables-fn :reader list-all-database-tables-fn)
23 (list-all-table-columns-fn :reader list-all-table-columns-fn)
24 (odbc-db-type :accessor database-odbc-db-type :initarg :odbc-db-type ))
25 (:documentation "Encapsulate same behavior across odbc and aodbc backends."))
27 (defmethod initialize-instance :after ((db generic-odbc-database)
29 (declare (ignore all-keys))
30 (unless (slot-boundp db 'dbi-package)
31 (error "dbi-package not specified."))
32 (let ((pkg (slot-value db 'dbi-package)))
34 (error "dbi-package is nil."))
35 (setf (slot-value db 'disconnect-fn)
36 (intern (symbol-name '#:disconnect) pkg)
37 (slot-value db 'sql-fn)
38 (intern (symbol-name '#:sql) pkg)
39 (slot-value db 'close-query-fn)
40 (intern (symbol-name '#:close-query) pkg)
41 (slot-value db 'fetch-row)
42 (intern (symbol-name '#:fetch-row) pkg)
43 (slot-value db 'list-all-database-tables-fn)
44 (intern (symbol-name '#:list-all-database-tables) pkg)
45 (slot-value db 'list-all-table-columns-fn)
46 (intern (symbol-name '#:list-all-table-columns) pkg))))
50 (defmethod database-get-type-specifier ((type symbol) args database
51 (db-type (eql :mssql)))
52 "Special database types for MSSQL backends"
53 (declare (ignore database db-type args))
55 (wall-time "DATETIME")
56 (date "SMALLDATETIME")
57 ((generalized-boolean boolean) "BIT")
58 ((longchar text) "ntext")
61 (format nil "NVARCHAR(~A)" (car args))
62 (format nil "NVARCHAR(~D)" *default-string-length*)))
63 (t (call-next-method))))
65 ;;; Generation of SQL strings from lisp expressions
67 (defmethod database-output-sql ((tee (eql t)) (database generic-odbc-database))
68 (case (database-underlying-type database)
72 ;;; Database backend capabilities
74 (defmethod db-type-use-fully-qualified-column-on-drop-index? ((db-type (eql :mssql)))
77 (defmethod db-type-has-boolean-where? ((db-type (eql :mssql)))
80 (defmethod db-type-has-intersect? ((db-type (eql :mssql)))
83 (defmethod db-type-has-except? ((db-type (eql :mssql)))
88 (defmethod database-disconnect ((database generic-odbc-database))
89 (funcall (disconnect-fn database) (odbc-conn database))
90 (setf (odbc-conn database) nil)
93 (defmethod database-query (query-expression (database generic-odbc-database)
94 result-types field-names)
96 (funcall (sql-fn database)
97 query-expression :db (odbc-conn database)
98 :result-types result-types
99 :column-names field-names)
102 (error 'sql-database-data-error
104 :expression query-expression
105 :message "Query failed"))))
108 (defmethod database-execute-command (sql-expression (database generic-odbc-database))
110 (funcall (sql-fn database)
111 sql-expression :db (odbc-conn database))
117 (error 'sql-database-data-error
119 :expression sql-expression
120 :message "Execute command failed"))))
123 (defstruct odbc-result-set
126 (full-set nil :type boolean))
131 (defmethod database-query-result-set ((query-expression string)
132 (database generic-odbc-database)
133 &key full-set result-types)
135 (multiple-value-bind (query column-names)
136 (funcall (sql-fn database)
138 :db (odbc-conn database)
142 :result-types result-types)
144 (make-odbc-result-set :query query :full-set full-set
146 (length column-names)
147 nil ;; not able to return number of rows with odbc
150 (error 'sql-database-data-error
152 :expression query-expression
153 :message "Query result set failed"))))
155 (defmethod database-dump-result-set (result-set (database generic-odbc-database))
156 (funcall (close-query-fn database) (odbc-result-set-query result-set))
159 (defmethod database-store-next-row (result-set
160 (database generic-odbc-database)
162 (let ((row (funcall (fetch-row-fn database)
163 (odbc-result-set-query result-set) nil 'eof)))
167 (loop for elem in row
170 (setf (car rest) elem))
174 (defun %database-list-* (database type owner)
175 "Internal function used by database-list-tables and
177 (multiple-value-bind (rows col-names)
178 (funcall (list-all-database-tables-fn database) :db (odbc-conn database))
179 (declare (ignore col-names))
180 ;; http://msdn.microsoft.com/en-us/library/ms711831%28VS.85%29.aspx
181 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
182 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
183 (loop for (category schema name ttype . rest) in rows
184 when (and (string-equal type ttype)
185 (or (null owner) (string-equal owner schema))
186 ;; unless requesting by name, skip system schema
187 (not (and (null owner)
188 (member schema '("information_schema" "sys")
189 :test #'string-equal)))
190 ;; skip system specific tables in mssql2000
191 (not (and (eql :mssql (database-underlying-type database))
192 (member name '("dtproperties" "sysconstraints"
194 :test #'string-equal))))
197 (defmethod database-list-tables ((database generic-odbc-database)
199 "Since ODBC doesn't expose the owner we use that parameter to filter
200 on schema since that's what tends to be exposed. Some DBs like mssql
201 2000 conflate the two so at least there it works nicely."
202 (%database-list-* database "TABLE" owner))
205 (defmethod database-list-views ((database generic-odbc-database)
207 "Since ODBC doesn't expose the owner we use that parameter to filter
208 on schema since that's what tends to be exposed. Some DBs like mssql
209 2000 conflate the two so at least there it works nicely."
210 (%database-list-* database "VIEW" owner))
213 (defmethod database-list-attributes ((table %database-identifier) (database generic-odbc-database)
215 &aux (table (unescaped-database-identifier table)))
216 (declare (ignore owner))
217 (multiple-value-bind (rows col-names)
218 (funcall (list-all-table-columns-fn database) table
219 :db (odbc-conn database))
220 (declare (ignore col-names))
221 ;; COLUMN_NAME is hard-coded by odbc spec as fourth position
222 (loop for row in rows
223 collect (fourth row))))
225 (defmethod database-attribute-type ((attribute %database-identifier) (table %database-identifier)
226 (database generic-odbc-database)
228 &aux (table (unescaped-database-identifier table))
229 (attribute (unescaped-database-identifier attribute)))
230 (declare (ignore owner))
231 (multiple-value-bind (rows col-names)
232 (funcall (list-all-table-columns-fn database) table
233 :db (odbc-conn database))
234 (declare (ignore col-names))
235 ;; COLUMN_NAME is hard-coded by odbc spec as fourth position
236 ;; TYPE_NAME is the sixth column
237 ;; PRECISION/COLUMN_SIZE is the seventh column
238 ;; SCALE/DECIMAL_DIGITS is the ninth column
239 ;; NULLABLE is the eleventh column
240 (loop for row in rows
241 when (string-equal attribute (fourth row))
243 (let ((size (seventh row))
244 (precision (ninth row))
245 (scale (nth 10 row)))
246 (return (values (ensure-keyword (sixth row))
247 (when size (parse-integer size))
248 (when precision (parse-integer precision))
249 (when scale (parse-integer scale))))))))
251 (defmethod database-last-auto-increment-id
252 ((database generic-odbc-database) table column)
253 (case (database-underlying-type database)
255 (first (clsql:query "SELECT SCOPE_IDENTITY()"
258 :result-types '(:int))))
259 (t (if (next-method-p)
260 (call-next-method)))))
262 (defmethod clsql-sys:db-type-has-auto-increment? ((db-underlying-type (eql :mssql)))