Remove CVS $Id$ keyword
[clsql.git] / db-odbc / odbc-sql.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          odbc-sql.cl
6 ;;;; Purpose:       Medium-level interface for CLSQL ODBC backend
7 ;;;; Programmer:    Kevin M. Rosenberg
8 ;;;; Date Started:  Feb 2002
9 ;;;;
10 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
11 ;;;;
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 ;;;; *************************************************************************
16
17 (defpackage #:clsql-odbc
18     (:use #:common-lisp #:clsql-sys)
19     (:export #:odbc-database)
20     (:documentation "This is the CLSQL interface to ODBC."))
21
22 (in-package #:clsql-odbc)
23
24 ;; ODBC interface
25
26 (defclass odbc-database (generic-odbc-database)
27   ((odbc-db-type :accessor database-odbc-db-type)))
28
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)))
35
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
39     (handler-case
40         (let ((db (make-instance 'odbc-database
41                                  :name (database-name-from-spec connection-spec :odbc)
42                                  :database-type :odbc
43                                  :dbi-package (find-package '#:odbc-dbi)
44                                  :odbc-conn
45                                  (odbc-dbi:connect :user user
46                                                    :password password
47                                                    :data-source-name dsn
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)
55           db)
56       #+ignore
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")))))
62
63 (defmethod database-underlying-type ((database odbc-database))
64   (database-odbc-db-type database))
65
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))
70          (type
71           ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
72           (cond
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)))
77             :postgresql)
78            ((or (search "Microsoft SQL Server" server-name :test #'char-equal)
79                 (search "Microsoft SQL Server" dbms-name :test #'char-equal))
80             :mssql)
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)))
86             :mysql)
87            ((or (search "oracle" server-name :test #'char-equal)
88                 (search "oracle" dbms-name :test #'char-equal))
89             :oracle))))
90     (setf (database-odbc-db-type db) type)))
91
92
93
94 (defmethod database-create (connection-spec (type (eql :odbc)))
95   (declare (ignore connection-spec))
96   (warn "Not implemented."))
97
98 (defmethod database-destroy (connection-spec (type (eql :odbc)))
99   (declare (ignore connection-spec))
100   (warn "Not implemented."))
101
102 (defmethod database-probe (connection-spec (type (eql :odbc)))
103   (when (find (car connection-spec) (database-list connection-spec type)
104               :test #'string-equal)
105     t))
106
107 (defmethod database-list (connection-spec (type (eql :odbc)))
108   (declare (ignore connection-spec))
109   (odbc-dbi:list-all-data-sources))
110
111 (defmethod database-list-indexes ((database odbc-database)
112                                   &key (owner nil))
113   (let ((result '()))
114     (dolist (table (database-list-tables database :owner owner) result)
115       (setq result
116         (append (database-list-table-indexes table database :owner owner)
117                 result)))))
118
119 (defmethod database-list-table-indexes (table (database odbc-database)
120                                         &key (owner nil))
121   (declare (ignore owner))
122   (multiple-value-bind (rows col-names)
123       (odbc-dbi:list-table-indexes
124        table
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
129     (do ((results nil)
130          (loop-rows rows (cdr loop-rows)))
131         ((null loop-rows) (nreverse results))
132       (let* ((row (car loop-rows))
133              (col (nth 5 row)))
134         (unless (or (null col) (find col results :test #'string-equal))
135           (push col results))))))
136
137 ;;; Database capabilities
138
139 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :odbc)))
140   nil)
141
142
143 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
144   ;; nothing to do
145   t)
146
147 (when (clsql-sys:database-type-library-loaded :odbc)
148   (clsql-sys:initialize-database-type :database-type :odbc))