1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Low-level interface for CLSQL ODBC backend
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
19 (defpackage #:clsql-odbc
20 (:use #:common-lisp #:clsql-sys)
21 (:export #:odbc-database)
22 (:documentation "This is the CLSQL interface to ODBC."))
24 (in-package #:clsql-odbc)
28 (defclass odbc-database (generic-odbc-database)
29 ((odbc-db-type :accessor database-odbc-db-type)))
31 (defmethod database-name-from-spec (connection-spec
32 (database-type (eql :odbc)))
33 (check-connection-spec connection-spec database-type (dsn user password))
34 (destructuring-bind (dsn user password) connection-spec
35 (declare (ignore password))
36 (concatenate 'string dsn "/" user)))
38 (defmethod database-connect (connection-spec (database-type (eql :odbc)))
39 (check-connection-spec connection-spec database-type (dsn user password))
40 (destructuring-bind (dsn user password) connection-spec
42 (let ((db (make-instance 'odbc-database
43 :name (database-name-from-spec connection-spec :odbc)
45 :dbi-package (find-package '#:odbc-dbi)
47 (odbc-dbi:connect :user user
49 :data-source-name dsn))))
50 (store-type-of-connected-database db)
51 ;; Ensure this database type is initialized so can check capabilities of
52 ;; underlying database
53 (initialize-database-type :database-type database-type)
55 (error () ;; Init or Connect failed
56 (error 'sql-connection-error
57 :database-type database-type
58 :connection-spec connection-spec
59 :message "Connection failed")))))
61 (defmethod database-underlying-type ((database odbc-database))
62 (database-odbc-db-type database))
64 (defun store-type-of-connected-database (db)
65 (let* ((odbc-conn (clsql-sys::odbc-conn db))
66 (server-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_SERVER_NAME))
67 (dbms-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_DBMS_NAME))
69 ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
71 ((or (search "postgresql" server-name :test #'char-equal)
72 (search "postgresql" dbms-name :test #'char-equal))
74 ((or (search "mysql" server-name :test #'char-equal)
75 (search "mysql" dbms-name :test #'char-equal))
77 ((or (search "oracle" server-name :test #'char-equal)
78 (search "oracle" dbms-name :test #'char-equal))
80 (setf (database-odbc-db-type db) type)))
84 (defmethod database-create (connection-spec (type (eql :odbc)))
85 (declare (ignore connection-spec))
86 (warn "Not implemented."))
88 (defmethod database-destroy (connection-spec (type (eql :odbc)))
89 (declare (ignore connection-spec))
90 (warn "Not implemented."))
92 (defmethod database-probe (connection-spec (type (eql :odbc)))
93 (when (find (car connection-spec) (database-list connection-spec type)
97 (defmethod database-list (connection-spec (type (eql :odbc)))
98 (declare (ignore connection-spec))
99 (odbc-dbi:list-all-data-sources))
101 (defmethod database-list-indexes ((database odbc-database)
104 (dolist (table (database-list-tables database :owner owner) result)
106 (append (database-list-table-indexes table database :owner owner)
109 (defmethod database-list-table-indexes (table (database odbc-database)
111 (declare (ignore owner))
112 (multiple-value-bind (rows col-names)
113 (odbc-dbi:list-table-indexes
115 :db (clsql-sys::odbc-conn database))
116 (declare (ignore col-names))
117 ;; INDEX_NAME is hard-coded in sixth position by ODBC driver
118 ;; FIXME: ??? is hard-coded in the fourth position
120 (loop-rows rows (cdr loop-rows)))
121 ((null loop-rows) (nreverse results))
122 (let* ((row (car loop-rows))
124 (unless (find col results :test #'string-equal)
125 (push col results))))))
127 ;;; Database capabilities
129 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :odbc)))
133 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
137 (when (clsql-sys:database-type-library-loaded :odbc)
138 (clsql-sys:initialize-database-type :database-type :odbc))