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
32 (dsn user password &key connection-string completion window-handle))
33 (destructuring-bind (dsn user password &key connection-string completion window-handle) connection-spec
34 (declare (ignore password connection-string completion window-handle))
35 (concatenate 'string dsn "/" user)))
37 (defmethod database-connect (connection-spec (database-type (eql :odbc)))
38 (check-connection-spec connection-spec database-type
39 (dsn user password &key connection-string completion window-handle))
40 (destructuring-bind (dsn user password &key connection-string (completion :no-prompt) window-handle) connection-spec
42 (let ((db (make-instance 'odbc-database
43 :name (database-name-from-spec connection-spec :odbc)
45 :connection-spec connection-spec
46 :dbi-package (find-package '#:odbc-dbi)
48 (odbc-dbi:connect :user user
51 :connection-string connection-string
52 :completion completion
53 :window-handle window-handle))))
54 (store-type-of-connected-database db)
55 ;; Ensure this database type is initialized so can check capabilities of
56 ;; underlying database
57 (initialize-database-type :database-type database-type)
60 (error () ;; Init or Connect failed
61 (error 'sql-connection-error
62 :database-type database-type
63 :connection-spec connection-spec
64 :message "Connection failed")))))
66 (defmethod database-underlying-type ((database odbc-database))
67 (database-odbc-db-type database))
69 (defun store-type-of-connected-database (db)
70 (let* ((odbc-conn (clsql-sys::odbc-conn db))
71 (server-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_SERVER_NAME))
72 (dbms-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_DBMS_NAME))
74 ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
76 ((or (search "postgresql" server-name :test #'char-equal)
77 (search "postgresql" dbms-name :test #'char-equal))
78 (unless (find-package 'clsql-postgresql)
79 (ignore-errors (asdf:operate 'asdf:load-op 'clsql-postgresql-socket)))
81 ((or (search "Microsoft SQL Server" server-name :test #'char-equal)
82 (search "Microsoft SQL Server" dbms-name :test #'char-equal))
84 ((or (search "mysql" server-name :test #'char-equal)
85 (search "mysql" dbms-name :test #'char-equal))
86 (unless (find-package 'clsql-mysql)
87 ;; ignore errors on platforms where the shared libraries are not available
88 (ignore-errors (asdf:operate 'asdf:load-op 'clsql-mysql)))
90 ((or (search "oracle" server-name :test #'char-equal)
91 (search "oracle" dbms-name :test #'char-equal))
93 (setf (database-odbc-db-type db) type)))
97 (defmethod database-create (connection-spec (type (eql :odbc)))
98 (declare (ignore connection-spec))
99 (warn "Not implemented."))
101 (defmethod database-destroy (connection-spec (type (eql :odbc)))
102 (declare (ignore connection-spec))
103 (warn "Not implemented."))
105 (defmethod database-probe (connection-spec (type (eql :odbc)))
106 (when (find (car connection-spec) (database-list connection-spec type)
107 :test #'string-equal)
110 (defmethod database-list (connection-spec (type (eql :odbc)))
111 (declare (ignore connection-spec))
112 (odbc-dbi:list-all-data-sources))
114 (defmethod database-list-indexes ((database odbc-database)
117 (dolist (table (database-list-tables database :owner owner) result)
119 (append (database-list-table-indexes table database :owner owner)
122 (defmethod database-list-table-indexes (table (database odbc-database)
124 (declare (ignore owner))
125 (multiple-value-bind (rows col-names)
126 (odbc-dbi:list-table-indexes
128 :db (clsql-sys::odbc-conn database))
129 (declare (ignore col-names))
130 ;; INDEX_NAME is hard-coded in sixth position by ODBC driver
131 ;; FIXME: ??? is hard-coded in the fourth position
133 (loop-rows rows (cdr loop-rows)))
134 ((null loop-rows) (nreverse results))
135 (let* ((row (car loop-rows))
137 (unless (or (null col) (find col results :test #'string-equal))
138 (push col results))))))
140 ;;; Database capabilities
142 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :odbc)))
146 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
150 (when (clsql-sys:database-type-library-loaded :odbc)
151 (clsql-sys:initialize-database-type :database-type :odbc))