1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
6 ;;;; Generic ODBC layer, used by db-odbc and db-aodbc backends
8 ;;;; This file is part of CLSQL.
10 ;;;; CLSQL users are granted the rights to distribute and use this software
11 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
12 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
13 ;;;; *************************************************************************
15 (in-package #:clsql-sys)
17 (defclass generic-odbc-database (database)
18 ((dbi-package :initarg :dbi-package :reader dbi-package)
19 (odbc-conn :initarg :odbc-conn :initform nil :accessor odbc-conn)
20 (disconnect-fn :reader disconnect-fn)
21 (sql-fn :reader sql-fn)
22 (close-query-fn :reader close-query-fn)
23 (fetch-row :reader fetch-row-fn)
24 (list-all-database-tables-fn :reader list-all-database-tables-fn)
25 (list-all-table-columns-fn :reader list-all-table-columns-fn))
26 (:documentation "Encapsulate same behavior across odbc and aodbc backends."))
28 (defmethod initialize-instance :after ((db generic-odbc-database)
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 read-sql-value (val (type (eql 'boolean))
51 (database generic-odbc-database)
52 (db-type (eql :postgresql)))
53 (if (string= "0" val) nil t))
58 (defmethod database-disconnect ((database generic-odbc-database))
59 (funcall (disconnect-fn database) (odbc-conn database))
60 (setf (odbc-conn database) nil)
63 (defmethod database-query (query-expression (database generic-odbc-database)
64 result-types field-names)
66 (funcall (sql-fn database)
67 query-expression :db (odbc-conn database)
68 :result-types result-types
69 :column-names field-names)
71 (error 'sql-database-data-error
73 :expression query-expression
74 :message "Query failed"))))
77 (defmethod database-execute-command (sql-expression (database generic-odbc-database))
79 (funcall (sql-fn database)
80 sql-expression :db (odbc-conn database))
86 (error 'sql-database-data-error
88 :expression sql-expression
89 :message "Execute command failed"))))
92 (defstruct odbc-result-set
95 (full-set nil :type boolean))
100 (defmethod database-query-result-set ((query-expression string)
101 (database generic-odbc-database)
102 &key full-set result-types)
104 (multiple-value-bind (query column-names)
105 (funcall (sql-fn database)
107 :db (odbc-conn database)
111 :result-types result-types)
113 (make-odbc-result-set :query query :full-set full-set
115 (length column-names)
116 nil ;; not able to return number of rows with odbc
119 (error 'sql-database-data-error
121 :expression query-expression
122 :message "Query result set failed"))))
124 (defmethod database-dump-result-set (result-set (database generic-odbc-database))
125 (funcall (close-query-fn database) (odbc-result-set-query result-set))
128 (defmethod database-store-next-row (result-set
129 (database generic-odbc-database)
131 (let ((row (funcall (fetch-row-fn database)
132 (odbc-result-set-query result-set) nil 'eof)))
136 (loop for elem in row
139 (setf (car rest) elem))
142 (defmethod database-list-tables ((database generic-odbc-database)
144 (declare (ignore owner))
145 (multiple-value-bind (rows col-names)
146 (funcall (list-all-database-tables-fn database) :db (odbc-conn database))
147 (declare (ignore col-names))
148 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
149 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
150 (loop for row in rows
151 when (and (not (string-equal "information_schema" (nth 1 row)))
152 (string-equal "TABLE" (nth 3 row)))
153 collect (nth 2 row))))
156 (defmethod database-list-views ((database generic-odbc-database)
158 (declare (ignore owner))
159 (multiple-value-bind (rows col-names)
160 (funcall (list-all-database-tables-fn database) :db (odbc-conn database))
161 (declare (ignore col-names))
162 ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager
163 ;; TABLE_NAME in third column, TABLE_TYPE in fourth column
164 (loop for row in rows
165 when (and (not (string-equal "information_schema" (nth 1 row)))
166 (string-equal "VIEW" (nth 3 row)))
167 collect (nth 2 row))))
170 (defmethod database-list-attributes ((table string) (database generic-odbc-database)
172 (declare (ignore owner))
173 (multiple-value-bind (rows col-names)
174 (funcall (list-all-table-columns-fn database) table
175 :db (odbc-conn database))
176 (declare (ignore col-names))
177 ;; COLUMN_NAME is hard-coded by odbc spec as fourth position
178 (loop for row in rows
179 collect (fourth row))))
183 (defmethod database-attribute-type ((attribute string) (table string) (database generic-odbc-database)
185 (declare (ignore owner))
186 (multiple-value-bind (rows col-names)
187 (funcall (list-all-table-columns-fn database) table
188 :db (odbc-conn database))
189 (declare (ignore col-names))
190 ;; COLUMN_NAME is hard-coded by odbc spec as fourth position
191 ;; TYPE_NAME is the sixth column
192 ;; PRECISION/COLUMN_SIZE is the seventh column
193 ;; SCALE/DECIMAL_DIGITS is the ninth column
194 ;; NULLABLE is the eleventh column
195 (loop for row in rows
196 when (string-equal attribute (fourth row))
198 (let ((size (seventh row))
199 (precision (ninth row))
200 (scale (nth 10 row)))
201 (return (values (ensure-keyword (sixth row))
202 (when size (parse-integer size))
203 (when precision (parse-integer precision))
204 (when scale (parse-integer scale))))))))