use md5sum-string instead of md5sum-sequence to adjust to upstream changes
[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
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)))
36
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
41     (handler-case
42         (let ((db (make-instance 'odbc-database
43                                  :name (database-name-from-spec connection-spec :odbc)
44                                  :database-type :odbc
45                                  :connection-spec connection-spec
46                                  :dbi-package (find-package '#:odbc-dbi)
47                                  :odbc-conn
48                                  (odbc-dbi:connect :user user
49                                                    :password password
50                                                    :data-source-name dsn
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)
58           db)
59       #+ignore
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")))))
65
66 (defmethod database-underlying-type ((database odbc-database))
67   (database-odbc-db-type database))
68
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))
73          (type
74           ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
75           (cond
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)))
80             :postgresql)
81            ((or (search "Microsoft SQL Server" server-name :test #'char-equal)
82                 (search "Microsoft SQL Server" dbms-name :test #'char-equal))
83             :mssql)
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)))
89             :mysql)
90            ((or (search "oracle" server-name :test #'char-equal)
91                 (search "oracle" dbms-name :test #'char-equal))
92             :oracle))))
93     (setf (database-odbc-db-type db) type)))
94
95
96
97 (defmethod database-create (connection-spec (type (eql :odbc)))
98   (declare (ignore connection-spec))
99   (warn "Not implemented."))
100
101 (defmethod database-destroy (connection-spec (type (eql :odbc)))
102   (declare (ignore connection-spec))
103   (warn "Not implemented."))
104
105 (defmethod database-probe (connection-spec (type (eql :odbc)))
106   (when (find (car connection-spec) (database-list connection-spec type)
107               :test #'string-equal)
108     t))
109
110 (defmethod database-list (connection-spec (type (eql :odbc)))
111   (declare (ignore connection-spec))
112   (odbc-dbi:list-all-data-sources))
113
114 (defmethod database-list-indexes ((database odbc-database)
115                                   &key (owner nil))
116   (let ((result '()))
117     (dolist (table (database-list-tables database :owner owner) result)
118       (setq result
119         (append (database-list-table-indexes table database :owner owner)
120                 result)))))
121
122 (defmethod database-list-table-indexes (table (database odbc-database)
123                                         &key (owner nil))
124   (declare (ignore owner))
125   (multiple-value-bind (rows col-names)
126       (odbc-dbi:list-table-indexes
127        table
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
132     (do ((results nil)
133          (loop-rows rows (cdr loop-rows)))
134         ((null loop-rows) (nreverse results))
135       (let* ((row (car loop-rows))
136              (col (nth 5 row)))
137         (unless (or (null col) (find col results :test #'string-equal))
138           (push col results))))))
139
140 ;;; Database capabilities
141
142 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :odbc)))
143   nil)
144
145
146 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
147   ;; nothing to do
148   t)
149
150 (when (clsql-sys:database-type-library-loaded :odbc)
151   (clsql-sys:initialize-database-type :database-type :odbc))