r9514: Automated commit for Debian build of clsql upstream-version-2.11.3
[clsql/s11.git] / db-odbc / odbc-sql.lisp
blobe1ad0c234d03333177ae3c4798777880076ef1ef
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name: odbc-sql.cl
6 ;;;; Purpose: Low-level interface for CLSQL ODBC backend
7 ;;;; Programmer: Kevin M. Rosenberg
8 ;;;; Date Started: Feb 2002
9 ;;;;
10 ;;;; $Id$
11 ;;;;
12 ;;;; This file, part of CLSQL, is Copyright (c) 2002 by Kevin M. Rosenberg
13 ;;;;
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)
26 ;; ODBC interface
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
41 (handler-case
42 (let ((db (make-instance 'odbc-database
43 :name (database-name-from-spec connection-spec :odbc)
44 :database-type :odbc
45 :dbi-package (find-package '#:odbc-dbi)
46 :odbc-conn
47 (odbc-dbi:connect :user user
48 :password password
49 :data-source-name dsn))))
50 (store-type-of-connected-database db)
51 db)
52 (error () ;; Init or Connect failed
53 (error 'sql-connection-error
54 :database-type database-type
55 :connection-spec connection-spec
56 :message "Connection failed")))))
58 (defmethod database-underlying-type ((database odbc-database))
59 (database-odbc-db-type database))
61 (defun store-type-of-connected-database (db)
62 (let* ((odbc-conn (clsql-sys::odbc-conn db))
63 (server-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_SERVER_NAME))
64 (dbms-name (odbc-dbi::get-odbc-info odbc-conn odbc::$SQL_DBMS_NAME))
65 (type
66 ;; need SERVER-NAME and DBMS-NAME because many drivers mix this up
67 (cond
68 ((or (search "postgresql" server-name :test #'char-equal)
69 (search "postgresql" dbms-name :test #'char-equal))
70 :postgresql)
71 ((or (search "mysql" server-name :test #'char-equal)
72 (search "mysql" dbms-name :test #'char-equal))
73 :mysql)
74 ((or (search "oracle" server-name :test #'char-equal)
75 (search "oracle" dbms-name :test #'char-equal))
76 :oracle))))
77 (setf (database-odbc-db-type db) type)))
81 (defmethod database-create (connection-spec (type (eql :odbc)))
82 (declare (ignore connection-spec))
83 (warn "Not implemented."))
85 (defmethod database-destroy (connection-spec (type (eql :odbc)))
86 (declare (ignore connection-spec))
87 (warn "Not implemented."))
89 (defmethod database-probe (connection-spec (type (eql :odbc)))
90 (when (find (car connection-spec) (database-list connection-spec type)
91 :test #'string-equal)
92 t))
94 (defmethod database-list (connection-spec (type (eql :odbc)))
95 (declare (ignore connection-spec))
96 (odbc-dbi:list-all-data-sources))
98 (defmethod database-list-indexes ((database odbc-database)
99 &key (owner nil))
100 (let ((result '()))
101 (dolist (table (database-list-tables database :owner owner) result)
102 (setq result
103 (append (database-list-table-indexes table database :owner owner)
104 result)))))
106 (defmethod database-list-table-indexes (table (database odbc-database)
107 &key (owner nil))
108 (declare (ignore owner))
109 (multiple-value-bind (rows col-names)
110 (odbc-dbi:list-table-indexes
111 table
112 :db (clsql-sys::odbc-conn database))
113 (declare (ignore col-names))
114 ;; INDEX_NAME is hard-coded in sixth position by ODBC driver
115 ;; FIXME: ??? is hard-coded in the fourth position
116 (do ((results nil)
117 (loop-rows rows (cdr loop-rows)))
118 ((null loop-rows) (nreverse results))
119 (let* ((row (car loop-rows))
120 (col (nth 5 row)))
121 (unless (find col results :test #'string-equal)
122 (push col results))))))
124 ;;; Database capabilities
126 (defmethod db-backend-has-create/destroy-db? ((db-type (eql :odbc)))
127 nil)
130 (defmethod database-initialize-database-type ((database-type (eql :odbc)))
131 ;; nothing to do
134 (when (clsql-sys:database-type-library-loaded :odbc)
135 (clsql-sys:initialize-database-type :database-type :odbc))