1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Medium-level interface for CLSQL ODBC backend
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
10 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
17 (defpackage #:clsql-odbc
18 (:use #:common-lisp #:clsql-sys)
19 (:export #:odbc-database)
20 (:documentation "This is the CLSQL interface to ODBC."))
22 (in-package #:clsql-odbc)
26 (defclass odbc-database (generic-odbc-database)
27 ((odbc-db-type :accessor database-odbc-db-type)))
29 (defmethod database-name-from-spec (connection-spec
30 (database-type (eql :odbc)))
31 (check-connection-spec connection-spec database-type (dsn user password &key connection-string completion window-handle))
32 (destructuring-bind (dsn user password &key connection-string completion window-handle) connection-spec
33 (declare (ignore password connection-string completion window-handle))
34 (concatenate 'string dsn "/" user)))
36 (defmethod database-connect (connection-spec (database-type (eql :odbc)))
37 (check-connection-spec connection-spec database-type (dsn user password &key connection-string completion window-handle))
38 (destructuring-bind (dsn user password &key connection-string (completion :no-prompt) window-handle) connection-spec
40 (let ((db (make-instance 'odbc-database
41 :name (database-name-from-spec connection-spec :odbc)
43 :dbi-package (find-package '#:odbc-dbi)
45 (odbc-dbi:connect :user user
48 :connection-string connection-string
49 :completion completion
50 :window-handle window-handle))))
51 (store-type-of-connected-database db)
52 ;; Ensure this database type is initialized so can check capabilities of
53 ;; underlying database
54 (initialize-database-type :database-type database-type)
57 (error () ;; Init or Connect failed
58 (error 'sql-connection-error
59 :database-type database-type
60 :connection-spec connection-spec
61 :message "Connection failed")))))
63 (defmethod database-underlying-type ((database odbc-database))
64 (database-odbc-db-type database))
66 (defun store-type-of-connected-database (db)
67 (let* ((odbc-conn (clsql-sys::odbc-conn db))
68 (server-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_SERVER_NAME))
69 (dbms-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_DBMS_NAME))
71 ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
73 ((or (search "postgresql" server-name :test #'char-equal)
74 (search "postgresql" dbms-name :test #'char-equal))
75 (unless (find-package 'clsql-postgresql)
76 (ignore-errors (asdf:operate 'asdf:load-op 'clsql-postgresql-socket)))
78 ((or (search "Microsoft SQL Server" server-name :test #'char-equal)
79 (search "Microsoft SQL Server" dbms-name :test #'char-equal))
81 ((or (search "mysql" server-name :test #'char-equal)
82 (search "mysql" dbms-name :test #'char-equal))
83 (unless (find-package 'clsql-mysql)
84 ;; ignore errors on platforms where the shared libraries are not available
85 (ignore-errors (asdf:operate 'asdf:load-op 'clsql-mysql)))
87 ((or (search "oracle" server-name :test #'char-equal)
88 (search "oracle" dbms-name :test #'char-equal))
90 (setf (database-odbc-db-type db) type)))
94 (defmethod database-create (connection-spec (type (eql :odbc)))
95 (declare (ignore connection-spec))
96 (warn "Not implemented."))
98 (defmethod database-destroy (connection-spec (type (eql :odbc)))
99 (declare (ignore connection-spec))
100 (warn "Not implemented."))
102 (defmethod database-probe (connection-spec (type (eql :odbc)))
103 (when (find (car connection-spec) (database-list connection-spec type)
104 :test #'string-equal)
107 (defmethod database-list (connection-spec (type (eql :odbc)))
108 (declare (ignore connection-spec))
109 (odbc-dbi:list-all-data-sources))
111 (defmethod database-list-indexes ((database odbc-database)
114 (dolist (table (database-list-tables database :owner owner) result)
116 (append (database-list-table-indexes table database :owner owner)
119 (defmethod database-list-table-indexes (table (database odbc-database)
121 (declare (ignore owner))
122 (multiple-value-bind (rows col-names)
123 (odbc-dbi:list-table-indexes
125 :db (clsql-sys::odbc-conn database))
126 (declare (ignore col-names))
127 ;; INDEX_NAME is hard-coded in sixth position by ODBC driver
128 ;; FIXME: ??? is hard-coded in the fourth position
130 (loop-rows rows (cdr loop-rows)))
131 ((null loop-rows) (nreverse results))
132 (let* ((row (car loop-rows))
134 (unless (or (null col) (find col results :test #'string-equal))
135 (push col results))))))
137 ;;; Database capabilities
139 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :odbc)))
143 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
147 (when (clsql-sys:database-type-library-loaded :odbc)
148 (clsql-sys:initialize-database-type :database-type :odbc))