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