X-Git-Url: http://git.kpe.io/?a=blobdiff_plain;f=db-odbc%2Fodbc-sql.lisp;h=862c99188490b1f597f5fd1c31a23783c6e6165c;hb=bb2818fb8a6714a55b360dec0bb043c5edccb3f4;hp=2850454bd5072421680de040bf07ade105bbbcfc;hpb=cc92d162f24648d65ad872098353305a5baf91d7;p=clsql.git diff --git a/db-odbc/odbc-sql.lisp b/db-odbc/odbc-sql.lisp index 2850454..862c991 100644 --- a/db-odbc/odbc-sql.lisp +++ b/db-odbc/odbc-sql.lisp @@ -17,7 +17,7 @@ ;;;; ************************************************************************* (defpackage #:clsql-odbc - (:use #:common-lisp #:clsql-base-sys) + (:use #:common-lisp #:clsql-sys) (:export #:odbc-database) (:documentation "This is the CLSQL interface to ODBC.")) @@ -25,9 +25,8 @@ ;; ODBC interface -(defclass odbc-database (database) - ((odbc-conn :accessor database-odbc-conn :initarg :odbc-conn) - (odbc-db-type :accessor database-odbc-db-type))) +(defclass odbc-database (generic-odbc-database) + ((odbc-db-type :accessor database-odbc-db-type))) (defmethod database-name-from-spec (connection-spec (database-type (eql :odbc))) @@ -40,31 +39,27 @@ (check-connection-spec connection-spec database-type (dsn user password)) (destructuring-bind (dsn user password) connection-spec (handler-case - (let ((db - (make-instance 'odbc-database - :name (database-name-from-spec connection-spec :odbc) - :database-type :odbc - :odbc-conn - (odbc-dbi:connect :user user - :password password - :data-source-name dsn)))) + (let ((db (make-instance 'odbc-database + :name (database-name-from-spec connection-spec :odbc) + :database-type :odbc + :dbi-package (find-package '#:odbc-dbi) + :odbc-conn + (odbc-dbi:connect :user user + :password password + :data-source-name dsn)))) (store-type-of-connected-database db) db) - (clsql-error (e) - (error e)) - #+ignore - (error () ;; Init or Connect failed - (error 'clsql-connect-error - :database-type database-type - :connection-spec connection-spec - :errno nil - :error "Connection failed"))))) + (error () ;; Init or Connect failed + (error 'sql-connection-error + :database-type database-type + :connection-spec connection-spec + :message "Connection failed"))))) (defmethod database-underlying-type ((database odbc-database)) (database-odbc-db-type database)) (defun store-type-of-connected-database (db) - (let* ((odbc-conn (database-odbc-conn db)) + (let* ((odbc-conn (clsql-sys::odbc-conn db)) (server-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_SERVER_NAME)) (dbms-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_DBMS_NAME)) (type @@ -81,95 +76,17 @@ :oracle)))) (setf (database-odbc-db-type db) type))) -(defmethod database-disconnect ((database odbc-database)) - (odbc-dbi:disconnect (database-odbc-conn database)) - (setf (database-odbc-conn database) nil) - t) - -(defmethod database-query (query-expression (database odbc-database) - result-types) - (handler-case - (odbc-dbi:sql query-expression :db (database-odbc-conn database) - :result-types result-types) - (clsql-error (e) - (error e)) - #+ignore - (error () - (error 'clsql-sql-error - :database database - :expression query-expression - :errno nil - :error "Query failed")))) - -(defmethod database-execute-command (sql-expression - (database odbc-database)) - (handler-case - (odbc-dbi:sql sql-expression :db (database-odbc-conn database)) - (clsql-error (e) - (error e)) - #+ignore - (error () - (error 'clsql-sql-error - :database database - :expression sql-expression - :errno nil - :error "Execute command failed")))) - -(defstruct odbc-result-set - (query nil) - (types nil) - (full-set nil :type boolean)) -(defmethod database-query-result-set ((query-expression string) - (database odbc-database) - &key full-set result-types) - (handler-case - (multiple-value-bind (query column-names) - (odbc-dbi:sql query-expression - :db (database-odbc-conn database) - :row-count nil - :column-names t - :query t - :result-types result-types) - (values - (make-odbc-result-set :query query :full-set full-set - :types result-types) - (length column-names) - nil ;; not able to return number of rows with odbc - )) - #+ignore - (error () - (error 'clsql-sql-error - :database database - :expression query-expression - :errno nil - :error "Query result set failed")))) -(defmethod database-dump-result-set (result-set (database odbc-database)) - (odbc-dbi:close-query (odbc-result-set-query result-set)) - t) - -(defmethod database-store-next-row (result-set - (database odbc-database) - list) - (let ((row (odbc-dbi:fetch-row (odbc-result-set-query result-set) nil 'eof))) - (if (eq row 'eof) - nil - (progn - (loop for elem in row - for rest on list - do - (setf (car rest) elem)) - list)))) ;;; Sequence functions (defun %sequence-name-to-table (sequence-name) - (concatenate 'string "_CLSQL_SQL__" (sql-escape sequence-name))) + (concatenate 'string "_CLSQL_SEQ_" (sql-escape sequence-name))) (defun %table-name-to-sequence-name (table-name) (and (>= (length table-name) 11) - (string= (subseq table-name 0 11) "_CLSQL_SEQ_") + (string-equal (subseq table-name 0 11) "_CLSQL_SEQ_") (subseq table-name 11))) (defmethod database-create-sequence (sequence-name @@ -194,63 +111,18 @@ &key (owner nil)) (declare (ignore owner)) ;; FIXME: Underlying database backend stuff should come from that backend - ;; Would need to use ASDF to ensure underlying backend was loaded (case (database-odbc-db-type database) (:mysql - (mapcar #'(lambda (s) (%table-name-to-sequence-name (car s))) - (database-query "SHOW TABLES LIKE '%clsql_seq%'" - database nil))) + (mapcan #'(lambda (s) + (let ((sn (%table-name-to-sequence-name (car s)))) + (and sn (list sn)))) + (database-query "SHOW TABLES" database nil nil))) ((:postgresql :postgresql-socket) (mapcar #'(lambda (s) (%table-name-to-sequence-name (car s))) - (database-query "SELECT RELNAME FROM pg_class WHERE RELNAME LIKE '%clsql_seq%'" - database nil))))) + (database-query "SELECT RELNAME FROM pg_class WHERE RELNAME LIKE '%clsql_seq%'" + database nil nil))))) -(defmethod database-list-tables ((database odbc-database) - &key (owner nil)) - (declare (ignore owner)) - (multiple-value-bind (rows col-names) - (odbc-dbi:list-all-database-tables :db (database-odbc-conn database)) - (declare (ignore col-names)) - ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager - ;; TABLE_NAME in third column, TABLE_TYPE in fourth column - (loop for row in rows - when (and (not (string-equal "information_schema" (nth 1 row))) - (string-equal "TABLE" (nth 3 row))) - collect (nth 2 row)))) - -(defmethod database-list-views ((database odbc-database) - &key (owner nil)) - (declare (ignore owner)) - (multiple-value-bind (rows col-names) - (odbc-dbi:list-all-database-tables :db (database-odbc-conn database)) - (declare (ignore col-names)) - ;; TABLE_SCHEM is hard-coded in second column by ODBC Driver Manager - ;; TABLE_NAME in third column, TABLE_TYPE in fourth column - (loop for row in rows - when (and (not (string-equal "information_schema" (nth 1 row))) - (string-equal "VIEW" (nth 3 row))) - collect (nth 2 row)))) - -(defmethod database-list-attributes ((table string) (database odbc-database) - &key (owner nil)) - (declare (ignore owner)) - (multiple-value-bind (rows col-names) - (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database)) - (let ((pos (position "COLUMN_NAME" col-names :test #'string-equal))) - (when pos - (loop for row in rows - collect (nth pos row)))))) - -(defmethod database-attribute-type ((attribute string) (table string) (database odbc-database) - &key (owner nil)) - (declare (ignore owner)) - (multiple-value-bind (rows col-names) - (odbc-dbi:list-all-table-columns table :db (database-odbc-conn database)) - (let ((pos (position "TYPE_NAME" col-names :test #'string-equal))) - (when pos - (loop for row in rows - collect (nth pos row)))))) (defmethod database-set-sequence-position (sequence-name (position integer) @@ -269,8 +141,7 @@ (car (database-query (concatenate 'string "SELECT last_value,is_called FROM " table-name) - database - :auto)))) + database :auto nil)))) (cond ((char-equal (schar (second tuple) 0) #\f) (database-execute-command @@ -289,13 +160,14 @@ (caar (database-query (concatenate 'string "SELECT last_value FROM " (%sequence-name-to-table sequence-name)) - database - :auto)))) + database :auto nil)))) (defmethod database-create (connection-spec (type (eql :odbc))) + (declare (ignore connection-spec)) (warn "Not implemented.")) (defmethod database-destroy (connection-spec (type (eql :odbc))) + (declare (ignore connection-spec)) (warn "Not implemented.")) (defmethod database-probe (connection-spec (type (eql :odbc))) @@ -318,27 +190,10 @@ (defmethod database-list-table-indexes (table (database odbc-database) &key (owner nil)) (declare (ignore owner)) - (if (eq :mysql (database-odbc-db-type database)) - (mysql-workaround-bug-list-table-indexes table database) - (odbc-list-table-indexes table database))) - -(defun mysql-workaround-bug-list-table-indexes (table database) - ;; MyODBC 3.52 does not properly return results from SQLStatistics - (do ((results nil) - (rows (database-query - (format nil "SHOW INDEX FROM ~A" (string-upcase table)) - database nil) - (cdr rows))) - ((null rows) (nreverse results)) - (let ((col (nth 2 (car rows)))) - (unless (find col results :test #'string-equal) - (push col results))))) - -(defun odbc-list-table-indexes (table database) (multiple-value-bind (rows col-names) (odbc-dbi:list-table-indexes table - :db (database-odbc-conn database)) + :db (clsql-sys::odbc-conn database)) (declare (ignore col-names)) ;; INDEX_NAME is hard-coded in sixth position by ODBC driver ;; FIXME: ??? is hard-coded in the fourth position @@ -360,5 +215,5 @@ ;; nothing to do t) -(when (clsql-base-sys:database-type-library-loaded :odbc) - (clsql-base-sys:initialize-database-type :database-type :odbc)) +(when (clsql-sys:database-type-library-loaded :odbc) + (clsql-sys:initialize-database-type :database-type :odbc))