r9020: more odbc improvements on sbcl/cmu
[clsql.git] / db-odbc / odbc-dbi.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:    odbc-dbi.cl
6 ;;;; Purpose: Mid-level (DBI) interface for CLSQL ODBC backend
7 ;;;; Author:  Kevin M. Rosenberg
8 ;;;; Create:  April 2004
9 ;;;;
10 ;;;; $Id: odbc-sql.lisp 8983 2004-04-12 21:16:48Z kevin $
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 ;;;; *************************************************************************
18
19 (in-package #:cl-user)
20
21 (defpackage #:odbc-dbi
22   (:use #:cl #:odbc)
23   (:export
24    #:bind-parameter
25    #:close-query
26    #:connect
27    #:db-external-format
28    #:db-hstmt
29    #:db-width
30    #:disconnect
31    #:end-transaction
32    #:fetch-row
33    #:list-all-database-tables
34    #:list-all-table-columns
35    #:loop-over-results
36    #:prepare-sql
37    #:rr-sql
38    #:run-prepared-sql
39    #:set-autocommit
40    #:sql
41    
42    #:*auto-trim-strings*
43    #:*default-database*
44    #:*default-odbc-external-format*
45    #:*null-value*
46    )
47   (:documentation "This is the mid-level interface ODBC."))
48
49 (in-package #:odbc-dbi)
50
51 ;;; SQL Interface
52
53 (defclass odbc-db ()
54   (;; any reason to have more than one henv?
55    (width :initform +max-precision+ :accessor db-width)
56    (hstmt :initform nil :accessor db-hstmt)
57    (henv :initform nil :allocation :class :initarg :henv :accessor henv)
58    (hdbc :initform nil :initarg :hdbc :accessor hdbc)
59    ;; info returned from SQLGetInfo
60    (info :initform (make-hash-table) :reader db-info)
61    (type :initform nil :initarg :db-type :reader db-type)
62    (connected-p :initform nil :accessor db-connected-p)
63    ;; not used yet
64    (count :initform 0 :initarg :count :accessor db-count)
65    ;; not used yet
66    (total-count :initform 0 :allocation :class :accessor db-total-count)
67    ;; the use of this slot is deprecated; it will be removed when dtf works without it.
68    (query :initform nil :accessor db-query-object)
69    ;; resource of (active and inactive) query objects
70    (queries :initform () :accessor db-queries)))
71
72 (defclass odbc-query ()
73   ((hstmt :initform nil :initarg :hstmt :accessor hstmt) ; = cursor??
74    (width :initform +max-precision+ :accessor query-width)
75    (computed-result-types :initform nil :initarg :computed-result-types :accessor computed-result-types)
76    (column-count :initform nil :accessor column-count)
77    (column-names :initform (make-array 0 :element-type 'string :adjustable t :fill-pointer t)
78                  :accessor column-names)
79    (column-c-types :initform (make-array 0 :element-type 'fixnum :adjustable t :fill-pointer t)
80                    :accessor column-c-types)
81    (column-sql-types :initform (make-array 0 :element-type 'fixnum :adjustable t :fill-pointer t)
82                      :accessor column-sql-types)
83    (column-data-ptrs :initform (make-array 0 :adjustable t :fill-pointer t)
84                      :accessor data-ptrs)
85    (column-out-len-ptrs :initform (make-array 0 :adjustable t :fill-pointer t)
86                         :accessor column-out-len-ptrs)
87    (column-precisions :initform (make-array 0 :element-type 'fixnum :adjustable t :fill-pointer t)
88                       :accessor column-precisions)
89    (column-scales :initform (make-array 0 :element-type 'fixnum :adjustable t :fill-pointer t)
90                   :accessor column-scales)
91    (column-nullables-p :initform (make-array 0 :element-type 'fixnum :adjustable t :fill-pointer t)
92                        :accessor column-nullables-p)
93       ;;(parameter-count :initform 0 :accessor parameter-count)
94    (parameter-data-ptrs :initform (make-array 0 :adjustable t :fill-pointer t)
95                         :accessor parameter-ptrs)
96    ;; a query string or a query expression object
97    (sql-expression :initform nil :initarg :sql-expression :accessor sql-expression)
98    ;; database object the query is to be run against
99    (database :initarg :database :reader query-database)
100    (active-p :initform nil :initarg :active-p :accessor query-active-p))
101   (:documentation
102    "Stores query information, like SQL query string/expression and database to run
103 the query against." ))
104
105 ;;; AODBC Compatible interface
106
107 (defun connect (&key data-source-name user password (autocommit t))
108   (let ((db (make-instance 'odbc-db)))
109     (unless (henv db) ;; has class allocation!
110       (setf (henv db) (%new-environment-handle)))
111     (setf (hdbc db) (%new-db-connection-handle (henv db)))
112     (%sql-connect (hdbc db) data-source-name user password)
113     #+ignore (setf (db-hstmt db) (%new-statement-handle (hdbc db)))
114     (when (/= (get-odbc-info db odbc::$SQL_TXN_CAPABLE) odbc::$SQL_TC_NONE)
115       (if autocommit
116           (enable-autocommit (hdbc db))
117           (disable-autocommit (hdbc db))))
118     db))
119
120 (defun disconnect (database)
121   (with-slots (hdbc queries) database
122     (dolist (query queries)
123       (if (query-active-p query)
124           (with-slots (hstmt) query
125             (when hstmt 
126               (%free-statement hstmt :drop)
127               (setf hstmt nil)))))
128     (when (db-hstmt database)
129       (%free-statement (db-hstmt database) :drop))
130     (%disconnect hdbc)))
131
132
133 (defun sql (expr &key db result-types row-count (column-names t) query 
134                       hstmt width)
135   (declare (ignore hstmt))
136   (cond
137    (query
138     (let ((q (db-open-query db expr :result-types result-types :width width)))
139       (if column-names
140           (values q (column-names q))
141         q)))
142    (t
143     (multiple-value-bind (data col-names)
144         (db-query db expr :result-types result-types :width width)
145       (cond
146         (row-count
147          (if (consp data) (length data) data))
148         (column-names
149          (values data col-names))
150         (t
151          data))))))
152
153 (defun fetch-row (query &optional (eof-errorp t) eof-value)
154   (multiple-value-bind (row query count) (db-fetch-query-results query 1)
155     (cond
156      ((zerop count)
157       (close-query query)
158       (when eof-errorp
159         (error 'clsql-odbc-error :odbc-message "Ran out of data in fetch-row"))
160       eof-value)
161      (t
162       (car row)))))
163       
164
165 (defun close-query (query)
166   (db-close-query query))
167
168 (defun list-all-database-tables (&key db hstmt)
169   (declare (ignore hstmt))
170   (let ((query (get-free-query db)))
171     (unwind-protect
172         (progn
173           (with-slots (hstmt) query
174             (unless hstmt (setf hstmt (%new-statement-handle (hdbc db))))
175             (%list-tables hstmt)
176             (%initialize-query query nil nil)
177             (values
178              (db-fetch-query-results query)
179              (coerce (column-names query) 'list))))
180       (db-close-query query))))
181
182 (defun list-all-table-columns (table &key db hstmt)
183   (declare (ignore hstmt))
184   (db-describe-columns db "" "" table ""))
185
186 (defun rr-sql (hstmt sql-statement &key db)
187   (declare (ignore hstmt sql-statement db))
188   (warn "rr-sql not implemented."))
189
190 ;;; Mid-level interface
191
192 (defmethod db-commit ((database odbc-db))
193   (%commit (henv database) (hdbc database)))
194
195 (defmethod db-rollback ((database odbc-db))
196   (%rollback (henv database) (hdbc database)))
197
198 (defmethod db-cancel-query ((query odbc-query))
199   (with-slots (hstmt) query
200     (%sql-cancel hstmt)
201     (setf (query-active-p query) nil)))
202
203 #+simple-version
204 (defmacro with-transaction (&body body)
205   `(%with-transaction
206      (:henv (henv ,*default-database*) :hdbc (hdbc ,*default-database*))
207      ,@body))
208
209 (defmethod initialize-instance :after ((query odbc-query) 
210                                        &key sql henv hdbc &allow-other-keys)
211   (when sql
212     (let ((hstmt (%new-statement-handle hdbc)))
213       (%sql-exec-direct sql hstmt henv hdbc)
214       (with-slots (column-count 
215                    column-names column-c-types column-sql-types column-data-ptrs 
216                    column-out-len-ptrs column-precisions column-scales
217                    column-nullables-p active-p) query
218         (setf (hstmt query) hstmt)
219         (%initialize-query query nil nil)
220         (setf active-p t)))))
221
222 ;; one for odbc-db is missing
223 (defmethod terminate ((query odbc-query))
224   ;;(format tb::*local-output* "~%*** terminated: ~s" query)
225   (with-slots (hstmt) query
226     (when hstmt
227       ;(%free-statement hstmt :drop)
228       (uffi:free-foreign-object hstmt)) ;; ??
229     (%dispose-column-ptrs query)))
230
231 (defmethod %dispose-column-ptrs ((query odbc-query))
232   (with-slots (column-data-ptrs column-out-len-ptrs hstmt) query
233     (loop for data-ptr across column-data-ptrs
234           when data-ptr do (uffi:free-foreign-object data-ptr))
235     (loop for out-len-ptr across column-out-len-ptrs
236           when out-len-ptr do (uffi:free-foreign-object out-len-ptr))))
237
238 (defmethod db-open-query ((database odbc-db) query-expression
239                           &key arglen col-positions result-types width
240                           &allow-other-keys)
241   (db-open-query (get-free-query database) query-expression
242                  :arglen arglen :col-positions col-positions
243                  :result-types result-types
244                  :width (if width width (db-width database))))
245
246 (defmethod db-open-query ((query odbc-query) query-expression
247                           &key arglen col-positions result-types width
248                           &allow-other-keys)
249   (%db-execute query query-expression)
250   (%initialize-query query arglen col-positions :result-types result-types
251                      :width width))
252
253 (defmethod db-fetch-query-results ((database odbc-db) &optional count)
254   (db-fetch-query-results (db-query-object database) count))
255
256 (defmethod db-fetch-query-results ((query odbc-query) &optional count)
257   (when (query-active-p query)
258     (with-slots (column-count column-data-ptrs column-c-types column-sql-types 
259                  column-out-len-ptrs column-precisions hstmt computed-result-types)
260         query
261       (let* ((rows-fetched 0)
262              (rows
263               (loop for i from 0 
264                   until (or (and count (= i count))
265                             (= (%sql-fetch hstmt) odbc::$SQL_NO_DATA_FOUND))
266                   collect
267                     (loop for result-type across computed-result-types
268                         for data-ptr across column-data-ptrs
269                         for c-type across column-c-types
270                         for sql-type across column-sql-types
271                         for out-len-ptr across column-out-len-ptrs
272                         for precision across column-precisions
273                         for j from 0    ; column count is zero based in lisp
274                         collect 
275                           (progn
276                             (incf rows-fetched)
277                             (cond ((< 0 precision (query-width query))
278                                    (read-data data-ptr c-type sql-type out-len-ptr result-type))
279                                   ((zerop (get-cast-long out-len-ptr))
280                               nil)
281                                   (t
282                                    (read-data-in-chunks hstmt j data-ptr c-type sql-type
283                                                         out-len-ptr result-type))))))))
284         (values rows query rows-fetched)))))
285
286 (defmethod db-query ((database odbc-db) query-expression &key result-types width)
287   (let ((free-query (get-free-query database)))
288     (setf (sql-expression free-query) query-expression)
289     (unwind-protect
290       (progn
291         (%db-execute free-query query-expression)
292         (%initialize-query free-query nil nil :result-types result-types :width width)
293         (if (plusp (column-count free-query)) ;; KMR: Added check for commands that don't return columns
294             (values
295              (db-fetch-query-results free-query nil)
296              (column-names free-query))
297           (values
298            (result-rows-count (hstmt free-query))
299            nil)))
300       (db-close-query free-query)
301       )))
302
303 (defmethod %db-execute ((database odbc-db) sql-expression &key &allow-other-keys)
304   (%db-execute (get-free-query database) sql-expression))
305
306 (defmethod %db-execute ((query odbc-query) sql-expression &key &allow-other-keys)
307   (with-slots (henv hdbc) (odbc::query-database query)
308     (with-slots (hstmt) query
309       (unless hstmt (setf hstmt (%new-statement-handle hdbc))) 
310       (setf (sql-expression query) sql-expression)
311       (%sql-exec-direct sql-expression hstmt henv hdbc)
312       query)))
313
314 ;; reuse inactive queries
315 (defmethod get-free-query ((database odbc-db))
316   "get-free-query finds or makes a nonactive query object, and then sets it to active.
317 This makes the functions db-execute-command and db-query thread safe."
318   (with-slots (queries hdbc) database
319     (or (clsql-base-sys:without-interrupts
320          (let ((inactive-query (find-if (lambda (query)
321                                           (not (query-active-p query)))
322                                         queries)))
323            (when inactive-query 
324              (with-slots (column-count column-names column-c-types 
325                           width hstmt
326                           column-sql-types column-data-ptrs
327                           column-out-len-ptrs column-precisions
328                           column-scales column-nullables-p)
329                          inactive-query
330                ;;(print column-data-ptrs tb::*local-output*)
331                ;;(%dispose-column-ptrs inactive-query)
332                (setf column-count 0
333                      width +max-precision+
334                      ;; KMR hstmt (%new-statement-handle hdbc)
335                      (fill-pointer column-names) 0
336                      (fill-pointer column-c-types) 0
337                      (fill-pointer column-sql-types) 0
338                      (fill-pointer column-data-ptrs) 0
339                      (fill-pointer column-out-len-ptrs) 0
340                      (fill-pointer column-precisions) 0
341                      (fill-pointer column-scales) 0
342                      (fill-pointer column-nullables-p) 0))
343              (setf (query-active-p inactive-query) t))
344            inactive-query))
345         (let ((new-query (make-instance 'odbc-query
346                                         :database database
347                                         ;;(clone-database database)
348                                         :active-p t)))
349           (push new-query queries)
350           new-query))))
351
352 (defmethod db-execute-command ((database odbc-db) sql-string)
353   (db-execute-command (get-free-query database) sql-string))
354
355 (defmethod db-execute-command ((query odbc-query) sql-string)
356   (with-slots (hstmt database) query
357     (with-slots (henv hdbc) database
358       (unless hstmt (setf hstmt (%new-statement-handle hdbc))) 
359       (unwind-protect 
360           (%sql-exec-direct sql-string hstmt henv hdbc)
361         (db-close-query query)))))
362
363 (defmethod %initialize-query ((database odbc-db) arglen col-positions &key result-types width)
364   (%initialize-query (db-query-object database) arglen col-positions
365                      :result-types result-types 
366                      :width (if width width (db-width database))))
367
368 (defmethod %initialize-query ((query odbc-query) arglen col-positions &key result-types width)
369   (with-slots (hstmt computed-result-types
370                column-count column-names column-c-types column-sql-types
371                column-data-ptrs column-out-len-ptrs column-precisions
372                column-scales column-nullables-p) 
373       query 
374     (setf column-count (if arglen
375                          (min arglen (result-columns-count hstmt))
376                          (result-columns-count hstmt)))
377     (when width (setf (query-width query) width))
378     ;;(format tb::*local-output* "~%column-count: ~d, col-positions: ~d" column-count col-positions)
379     (labels ((initialize-column (col-nr)
380                 (multiple-value-bind (name sql-type precision scale nullable-p)
381                                      (%describe-column hstmt (1+ col-nr))
382                   ;; allocate space to bind result rows to
383                   (multiple-value-bind (c-type data-ptr out-len-ptr size long-p)
384                                        (%allocate-bindings sql-type precision)
385                     (unless long-p ;; if long-p we fetch in chunks with %sql-get-data
386                       (%bind-column hstmt col-nr c-type data-ptr (1+ size) out-len-ptr))
387                     (vector-push-extend name column-names) 
388                     (vector-push-extend sql-type column-sql-types)
389                     (vector-push-extend (sql-to-c-type sql-type) column-c-types)
390                     (vector-push-extend precision column-precisions)
391                     (vector-push-extend scale column-scales)
392                     (vector-push-extend nullable-p column-nullables-p)
393                     (vector-push-extend data-ptr column-data-ptrs)
394                     (vector-push-extend out-len-ptr column-out-len-ptrs)))))
395       (if col-positions
396         (dolist (col-nr col-positions)
397           (initialize-column col-nr))
398         (dotimes (col-nr column-count)
399           ;; get column information
400           (initialize-column col-nr))))
401     
402     (setf computed-result-types (make-array column-count))
403     (dotimes (i column-count)
404       (setf (aref computed-result-types i) 
405         (cond
406          ((consp result-types)
407           (nth i result-types))
408          ((eq result-types :auto)
409           (if (eq (aref column-sql-types i) odbc::$SQL_BIGINT)
410               :number
411             (case (aref column-c-types i)
412               (#.odbc::$SQL_C_SLONG :int)
413               (#.odbc::$SQL_C_DOUBLE :double)
414               (#.odbc::$SQL_C_FLOAT :float)
415               (#.odbc::$SQL_C_SSHORT :short)
416               (#.odbc::$SQL_BIGINT :short)
417               (t t))))
418           (t
419           t)))))
420   query)
421
422 (defmethod db-close-query ((query odbc-query) &key drop-p)
423   (with-slots (hstmt column-count column-names column-c-types column-sql-types
424                      column-data-ptrs column-out-len-ptrs column-precisions
425                      column-scales column-nullables-p) query
426     (let ((count (fill-pointer column-data-ptrs)))
427       (when (not (zerop count))
428         (dotimes (col-nr count)
429           (let ((data-ptr (aref column-data-ptrs col-nr))
430                 (out-len-ptr (aref column-out-len-ptrs col-nr)))
431             (when data-ptr (uffi:free-foreign-object data-ptr)) ; we *did* allocate them
432             (when out-len-ptr (uffi:free-foreign-object out-len-ptr)))))
433       (cond ((null hstmt)
434              nil)
435             (drop-p
436              (%free-statement hstmt :drop)
437              (setf hstmt nil))
438             (t
439              (%free-statement hstmt :unbind)
440              (%free-statement hstmt :reset)
441              (%free-statement hstmt :close)))
442       (setf (query-active-p query) nil)))
443   query)
444
445 (defmethod %read-query-data ((database odbc-db) ignore-columns)
446   (%read-query-data (db-query-object database) ignore-columns))
447
448 (defmethod %read-query-data ((query odbc-query) ignore-columns)
449   (with-slots (hstmt column-count column-c-types column-sql-types 
450                column-data-ptrs column-out-len-ptrs column-precisions
451                computed-result-types)
452       query
453     (unless (= (SQLFetch hstmt) odbc::$SQL_NO_DATA_FOUND)
454       (values
455        (loop for col-nr from 0 to (- column-count 
456                                      (if (eq ignore-columns :last) 2 1))
457            for result-type across computed-result-types
458            collect
459              (let ((precision (aref column-precisions col-nr))
460                    (sql-type (aref column-sql-types col-nr)))
461                (cond ((or (< 0 precision (query-width query))
462                           (and (zerop precision) (not (find sql-type '($SQL_C_CHAR)))))
463                       (read-data (aref column-data-ptrs col-nr) 
464                                  (aref column-c-types col-nr)
465                                  sql-type
466                                  (aref column-out-len-ptrs col-nr)
467                                  result-type))
468                      ((zerop (get-cast-long (aref column-out-len-ptrs col-nr)))
469                       *null*)
470                      (t
471                       (read-data-in-chunks hstmt col-nr
472                                            (aref column-data-ptrs col-nr) 
473                                            (aref column-c-types col-nr)
474                                            (aref column-sql-types col-nr)
475                                            (aref column-out-len-ptrs col-nr)
476                                            result-type)))))
477        t))))
478
479 (defmethod db-map-query ((database odbc-db) type function query-exp &key result-types)
480   (db-map-query (get-free-query database) type function query-exp :result-types result-types))
481
482 (defmethod db-map-query ((query odbc-query) type function query-exp &key result-types)
483   (declare (ignore type)) ; preliminary. Do a type coersion here
484   (%db-execute query (sql-expression query-exp))
485   (unwind-protect
486     (progn
487       (%initialize-query query nil nil :result-types result-types)
488       ;; the main loop
489       (loop for data = (%read-query-data query nil)
490             while data
491             do (apply function data)))
492     ;; dispose of memory and set query inactive or get rid of it
493     (db-close-query query)))
494
495 (defmethod db-map-bind-query ((query odbc-query) type function 
496                                  &rest parameters)
497   (declare (ignore type)) ; preliminary. Do a type coersion here
498   (unwind-protect
499     (progn
500       (apply #'%db-bind-execute query parameters)
501       ;; the main loop
502       (loop with data
503             while (setf data (%read-query-data query nil))
504             do (apply function data)))
505     ;; dispose of memory and set query inactive or get rid of it
506     (%db-reset-query query)))
507
508 ;; does not always return exactly a lisp type...
509 (defun sql-to-lisp-type (sql-type)
510   (ecase sql-type
511     ((#.odbc::$SQL_CHAR #.odbc::$SQL_VARCHAR #.odbc::$SQL_LONGVARCHAR) :string)
512     ((#.odbc::$SQL_NUMERIC #.odbc::$SQL_DECIMAL #.odbc::$SQL_BIGINT) :string) ; ??
513     (#.odbc::$SQL_INTEGER :long)
514     (#.odbc::$SQL_SMALLINT :short)
515     ((#.odbc::$SQL_FLOAT #.odbc::$SQL_DOUBLE) :long)
516     (#.odbc::$SQL_REAL :long)
517     (#.odbc::$SQL_DATE 'sql-c-date)
518     (#.odbc::$SQL_TIME 'sql-c-time)
519     (#.odbc::$SQL_TIMESTAMP 'sql-c-timestamp)
520     ;;((#.odbc::$SQL_BINARY #.odbc::$SQL_VARBINARY #.odbc::$SQL_LONGVARBINARY) odbc::$SQL_C_BINARY) ; ??
521     (#.odbc::$SQL_TINYINT :short)
522     ;;(#.odbc::$SQL_BIT odbc::$SQL_C_BIT) ; ??
523     ((#.odbc::$SQL_VARBINARY #.odbc::$SQL_LONGVARBINARY) :binary)
524     ))
525
526 ;; prepared queries
527
528 (defmethod db-prepare-statement ((database odbc-db) sql
529                                      &key parameter-table parameter-columns)
530   (with-slots (hdbc) database
531     (let ((query (get-free-query database))) 
532       (with-slots (hstmt) query
533         (unless hstmt (setf hstmt (%new-statement-handle hdbc))))
534       (db-prepare-statement query sql parameter-table parameter-columns))))
535
536 (defmethod db-prepare-statement ((query odbc-query) (sql string)
537                                      &key parameter-table parameter-columns)
538   ;; this is a workaround to get hold of the column types when the driver does not
539   ;; support SQLDescribeParam. To do: put code in here for drivers that do
540   ;; support it.
541   (unless (string-equal sql "insert" :end1 6)
542     (error "Only insert expressions are supported in literal ODBC: '~a'." sql))
543   (%db-execute query (format nil "select ~{~a~^,~} from ~a where 0 = 1"
544                              (or parameter-columns '("*")) parameter-table))
545   (%initialize-query query nil nil)
546   (with-slots (hstmt) query
547     (%free-statement hstmt :unbind)
548     (%free-statement hstmt :reset)
549     (setf (sql-expression query) sql)
550     (%sql-prepare hstmt sql))
551   query)
552
553
554 (defmethod %db-bind-execute ((query odbc-query) &rest parameters)
555   (with-slots (hstmt parameter-data-ptrs) query
556     (loop for parameter in parameters
557           with data-ptr and size and parameter-string
558           do
559           (setf parameter-string
560                 (if (stringp parameter)
561                   parameter
562                   (write-to-string parameter))
563            size (length parameter-string)
564                 data-ptr 
565                 (uffi:allocate-foreign-string (1+ size)))
566           (vector-push-extend data-ptr parameter-data-ptrs)
567           (%sql-bind-parameter 
568            hstmt (1- (fill-pointer parameter-data-ptrs)) odbc::$SQL_PARAM_INPUT
569            odbc::$SQL_C_CHAR ; (aref column-c-types parameter-count)
570            odbc::$SQL_CHAR ; sql-type
571            (query-width query)          ;precision ; this should be the actual precision!
572            ;; scale
573            0 ;; should be calculated for odbc::$SQL_DECIMAL,
574            ;;$SQL_NUMERIC and odbc::$SQL_TIMESTAMP
575            data-ptr ;; = rgbValue
576            0
577            ;; *pcbValue;
578            ;; change this for output and binary input! (see 3-32)
579            +null-ptr+)
580           (%put-str data-ptr parameter-string size))
581         (%sql-execute hstmt)))
582
583
584 (defmethod %db-reset-query ((query odbc-query))
585   (with-slots (hstmt parameter-data-ptrs) query
586     (prog1
587       (db-fetch-query-results query nil) 
588       (%free-statement hstmt :reset) ;; but _not_ :unbind !
589       (%free-statement hstmt :close)
590       (dotimes (param-nr (fill-pointer parameter-data-ptrs))
591         (let ((data-ptr (aref parameter-data-ptrs param-nr)))
592           (when data-ptr (uffi:free-foreign-object data-ptr))))
593       (setf (fill-pointer parameter-data-ptrs) 0))))
594
595 (defun data-parameter-ptr (hstmt)
596   (uffi:with-foreign-object (param-ptr :pointer-void)
597     (let ((return-code (%sql-param-data hstmt param-ptr)))
598       ;;(format t "~%return-code from %sql-param-data: ~a~%" return-code)
599       (when (= return-code odbc::$SQL_NEED_DATA)
600         ;;(ffc::%pointer-to-address (%get-ptr param-ptr))
601         (uffi:deref-pointer param-ptr :pointer-void)))))
602
603 ;; database inquiery functions
604
605 (defmethod db-describe-columns ((database odbc-db) 
606                                     table-qualifier table-owner table-name column-name)
607   (with-slots (hdbc) database
608     (%describe-columns hdbc table-qualifier table-owner table-name column-name)))
609
610 ;; should translate info-type integers to keywords in order to make this 
611 ;; more readable?
612 (defmethod get-odbc-info ((database odbc-db) info-type)
613   (with-slots (hdbc info) database
614     (or (gethash info-type info)
615         (setf (gethash info-type info)
616               (%sql-get-info hdbc info-type)))))
617
618 (defmethod get-odbc-info ((query odbc-query) info-type)
619   (get-odbc-info (odbc::query-database query) info-type))
620
621 ;; driver inquiery
622 (defmethod db-data-sources ((db-type (eql :odbc)))
623    "Returns a list of (data-source description) - pairs"
624    (let ((henv (%new-environment-handle)))
625     (unwind-protect
626           (loop with direction = :first
627                for data-source+description 
628                = (multiple-value-list (%sql-data-sources henv :direction direction))
629                while (car data-source+description)
630                collect data-source+description
631                do (setf direction :next))
632       (%sql-free-environment henv))))
633
634 ; EOF