r9003: odbc updates
[clsql.git] / db-odbc / odbc-api.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: odbc -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:     odbc-ff-interface.lisp
6 ;;;; Purpose:  Function definitions for UFFI interface to ODBC
7 ;;;; Author:   Kevin M. Rosenberg, Paul Meurer
8 ;;;;
9 ;;;; $Id: odbc-package.lisp 7061 2003-09-07 06:34:45Z kevin $
10 ;;;;
11 ;;;; This file, part of CLSQL, is Copyright (c) 2004 by Kevin M. Rosenberg
12 ;;;; and Copyright (C) Paul Meurer 1999 - 2001. All rights reserved.
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 #:odbc)
20
21 (defvar *null* (make-null-pointer :byte))
22 (defvar *binary-format* :unsigned-byte-vector)
23 (defvar *time-conversion-function* 'identity)
24 (defvar *trace-sql* nil)
25
26 (defun %null-ptr ()
27   (make-null-pointer :byte))
28
29 (defmacro %put-str (ptr string &optional max-length)
30   (let ((size (gensym)))
31     `(let ((,size (length ,string)))
32        (when (and ,max-length (> ,size ,max-length))
33          (error "string \"~a\" of length ~d is longer than max-length: ~d"
34                 ,string ,size ,max-length))
35        (dotimes (i ,size)
36          (setf (deref-array ,ptr '(:array :unsigned-char) i) (char ,string i)))
37        (setf (deref-array ,ptr '(:array :unsigned-char) ,size) 0))))
38
39 (defun %cstring-into-vector (ptr vector offset size-in-bytes)
40     (dotimes (i size-in-bytes)
41       (setf (aref vector offset)
42             (deref-array ptr '(:array :unsigned-char) i))
43       (incf offset))
44     offset)
45   
46 (defun handle-error (henv hdbc hstmt)
47   (with-foreign-objects ((sql-state '(:array :unsigned-char 256))
48                          (error-message '(:array :unsigned-char
49                                           #.$SQL_MAX_MESSAGE_LENGTH))
50                          (error-code :long)
51                          (msg-length :short))
52     (SQLError henv hdbc hstmt sql-state
53               error-code error-message
54               $SQL_MAX_MESSAGE_LENGTH msg-length)
55     (values
56      (convert-from-foreign-string error-message)
57      (convert-from-foreign-string sql-state)
58      (deref-pointer msg-length :short) 
59      (deref-pointer error-code :long))))
60
61 ; test this: return a keyword for efficiency
62 (defun sql-state (henv hdbc hstmt)
63   (with-foreign-objects ((sql-state '(:array :unsigned-char 256))
64                          (error-message '(:array :unsigned-char
65                                           #.$SQL_MAX_MESSAGE_LENGTH))
66                          (error-code :long)
67                          (msg-length :short))
68     (SQLError henv hdbc hstmt sql-state error-code
69               error-message $SQL_MAX_MESSAGE_LENGTH msg-length)
70     (convert-from-foreign-string sql-state) ;(%cstring-to-keyword sql-state)
71     ))
72
73 (defmacro with-error-handling ((&key henv hdbc hstmt (print-info t))
74                                    odbc-call &body body)
75   (let ((result-code (gensym "RC-")))
76     `(let ((,result-code ,odbc-call))
77        (case ,result-code
78          (#.$SQL_SUCCESS
79           (progn ,result-code ,@body))
80          (#.$SQL_SUCCESS_WITH_INFO
81           (when ,print-info
82             (multiple-value-bind (error-message sql-state)
83                                  (handle-error (or ,henv (%null-ptr))
84                                                (or ,hdbc (%null-ptr))
85                                                (or ,hstmt (%null-ptr)))
86               (warn "[ODBC info] ~a state: ~a"
87                     ,result-code error-message
88                     sql-state)))
89           (progn ,result-code ,@body))
90          (#.$SQL_INVALID_HANDLE
91           (error "[ODBC error] Invalid handle"))
92          (#.$SQL_STILL_EXECUTING
93           (error "[ODBC error] Still executing"))
94          (#.$SQL_ERROR
95           (multiple-value-bind (error-message sql-state)
96                                (handle-error (or ,henv (%null-ptr))
97                                              (or ,hdbc (%null-ptr))
98                                              (or ,hstmt (%null-ptr)))
99             (error "[ODBC error] ~a; state: ~a" error-message sql-state)))
100          (otherwise
101           (progn ,result-code ,@body))))))
102
103 (defun %new-environment-handle ()
104   (with-foreign-object (phenv 'sql-handle-ptr)
105     (with-error-handling
106         ()
107       (SQLAllocEnv phenv)
108       (deref-pointer phenv 'sql-handle-ptr))))
109
110 (defun %sql-free-environment (henv)
111   (with-error-handling 
112     (:henv henv)
113     (SQLFreeEnv henv)))
114
115 (defun %new-db-connection-handle (henv)
116   (with-foreign-object (phdbc 'sql-handle-ptr)
117     (with-error-handling
118       (:henv henv)
119       (SQLAllocConnect henv phdbc)
120       (deref-pointer phdbc 'sql-handle-ptr))))
121
122 (defun %free-statement (hstmt option)
123   (with-error-handling 
124       (:hstmt hstmt)
125       (SQLFreeStmt 
126        hstmt 
127        (ecase option
128          (:drop $SQL_DROP)
129          (:close $SQL_CLOSE)
130          (:unbind $SQL_UNBIND)
131          (:reset $SQL_RESET_PARAMS)))))
132
133 (defmacro with-statement-handle ((hstmt hdbc) &body body)
134   `(let ((,hstmt (%new-statement-handle ,hdbc)))
135      (unwind-protect
136        (progn ,@body)
137        (%free-statement ,hstmt :drop))))
138
139 ;; functional interface
140
141 (defun %sql-connect (hdbc server uid pwd)
142   (with-cstrings ((server-ptr server)
143                   (uid-ptr uid)
144                   (pwd-ptr pwd))
145     (with-error-handling 
146         (:hdbc hdbc)
147       (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr 
148                   $SQL_NTS pwd-ptr $SQL_NTS))))
149
150
151 (defun %disconnect (hdbc)
152   (with-error-handling 
153     (:hdbc hdbc)
154     (SQLDisconnect hdbc)))
155
156 (defun %commit (henv hdbc)
157   (with-error-handling 
158     (:henv henv :hdbc hdbc)
159     (SQLTransact 
160      henv hdbc $SQL_COMMIT)))
161
162 (defun %rollback (henv hdbc)
163   (with-error-handling 
164     (:henv henv :hdbc hdbc)
165     (SQLTransact 
166      henv hdbc $SQL_ROLLBACK)))
167
168 ; col-nr is zero-based in Lisp
169 ; col-nr = :bookmark retrieves a bookmark.
170 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
171   (with-error-handling
172     (:hstmt hstmt)
173     (SQLBindCol hstmt
174                 (if (eq column-nr :bookmark) 0 (1+ column-nr))
175                 c-type data-ptr precision out-len-ptr)))
176
177 ; parameter-nr is zero-based in Lisp
178 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
179                                       sql-type precision scale data-ptr
180                                       max-value out-len-ptr)
181   (with-error-handling
182     (:hstmt hstmt)
183     (SQLBindParameter hstmt (1+ parameter-nr)
184                       parameter-type ;$SQL_PARAM_INPUT 
185                       c-type ;$SQL_C_CHAR
186                       sql-type ;$SQL_VARCHAR
187                       precision ;(1- (length str))
188                       scale ;0
189                       data-ptr
190                       max-value
191                       out-len-ptr ;#.(%null-ptr)
192                       )))
193
194 (defun %sql-fetch (hstmt)
195   (with-error-handling 
196       (:hstmt hstmt)
197       (SQLFetch hstmt)))
198
199 (defun %new-statement-handle (hdbc)
200   (with-foreign-object (hstmt-ptr 'sql-handle-ptr)
201     (with-error-handling 
202       (:hdbc hdbc)
203       (SQLAllocStmt hdbc hstmt-ptr) 
204       (deref-pointer hstmt-ptr 'sql-handle-ptr))))
205
206 (defun %sql-get-info (hdbc info-type)
207   (ecase info-type
208     ;; those return string
209     ((#.$SQL_ACCESSIBLE_PROCEDURES
210       #.$SQL_ACCESSIBLE_TABLES
211       #.$SQL_COLUMN_ALIAS
212       #.$SQL_DATA_SOURCE_NAME
213       #.$SQL_DATA_SOURCE_READ_ONLY
214       #.$SQL_DBMS_NAME
215       #.$SQL_DBMS_VER
216       #.$SQL_DRIVER_NAME
217       #.$SQL_DRIVER_ODBC_VER
218       #.$SQL_DRIVER_VER
219       #.$SQL_EXPRESSIONS_IN_ORDERBY
220       #.$SQL_IDENTIFIER_QUOTE_CHAR
221       #.$SQL_KEYWORDS
222       #.$SQL_LIKE_ESCAPE_CLAUSE
223       #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
224       #.$SQL_MULT_RESULT_SETS
225       #.$SQL_MULTIPLE_ACTIVE_TXN
226       #.$SQL_NEED_LONG_DATA_LEN
227       #.$SQL_ODBC_SQL_OPT_IEF
228       #.$SQL_ODBC_VER
229       #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
230       #.$SQL_OUTER_JOINS
231       #.$SQL_OWNER_TERM
232       #.$SQL_PROCEDURE_TERM
233       #.$SQL_PROCEDURES
234       #.$SQL_QUALIFIER_NAME_SEPARATOR
235       #.$SQL_QUALIFIER_TERM
236       #.$SQL_ROW_UPDATES
237       #.$SQL_SEARCH_PATTERN_ESCAPE
238       #.$SQL_SERVER_NAME
239       #.$SQL_SPECIAL_CHARACTERS
240       #.$SQL_TABLE_TERM
241       #.$SQL_USER_NAME)
242      (with-foreign-objects ((info-ptr '(:array :unsigned-char 1024))
243                             (info-length-ptr :short))
244        (with-error-handling 
245          (:hdbc hdbc)
246          #-pcl
247          (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
248          #+pcl
249          (SQLGetInfo-Str hdbc info-type info-ptr 1023 info-length-ptr)
250          (convert-from-foreign-string info-ptr))))
251     ;; those returning a word
252     ((#.$SQL_ACTIVE_CONNECTIONS
253       #.$SQL_ACTIVE_STATEMENTS
254       #.$SQL_CONCAT_NULL_BEHAVIOR
255       #.$SQL_CORRELATION_NAME
256       #.$SQL_CURSOR_COMMIT_BEHAVIOR
257       #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
258       #.$SQL_MAX_COLUMN_NAME_LEN
259       #.$SQL_MAX_COLUMNS_IN_GROUP_BY
260       #.$SQL_MAX_COLUMNS_IN_INDEX
261       #.$SQL_MAX_COLUMNS_IN_ORDER_BY
262       #.$SQL_MAX_COLUMNS_IN_SELECT
263       #.$SQL_MAX_COLUMNS_IN_TABLE
264       #.$SQL_MAX_CURSOR_NAME_LEN
265       #.$SQL_MAX_OWNER_NAME_LEN
266       #.$SQL_MAX_PROCEDURE_NAME_LEN
267       #.$SQL_MAX_QUALIFIER_NAME_LEN
268       #.$SQL_MAX_TABLE_NAME_LEN
269       #.$SQL_MAX_TABLES_IN_SELECT
270       #.$SQL_MAX_USER_NAME_LEN
271       #.$SQL_NON_NULLABLE_COLUMNS
272       #.$SQL_NULL_COLLATION
273       #.$SQL_ODBC_API_CONFORMANCE
274       #.$SQL_ODBC_SAG_CLI_CONFORMANCE
275       #.$SQL_ODBC_SQL_CONFORMANCE
276       #.$SQL_QUALIFIER_LOCATION
277       #.$SQL_QUOTED_IDENTIFIER_CASE
278       #.$SQL_TXN_CAPABLE)
279      (with-foreign-objects ((info-ptr :short)
280                             (info-length-ptr :short))
281        (with-error-handling 
282         (:hdbc hdbc)
283          (SQLGetInfo hdbc
284                      info-type
285                      info-ptr
286                      255
287                      info-length-ptr)
288          (deref-pointer info-ptr :short)))
289      )
290     ;; those returning a long bitmask
291     ((#.$SQL_ALTER_TABLE 
292       #.$SQL_BOOKMARK_PERSISTENCE
293       #.$SQL_CONVERT_BIGINT
294       #.$SQL_CONVERT_BINARY
295       #.$SQL_CONVERT_BIT
296       #.$SQL_CONVERT_CHAR
297       #.$SQL_CONVERT_DATE
298       #.$SQL_CONVERT_DECIMAL
299       #.$SQL_CONVERT_DOUBLE
300       #.$SQL_CONVERT_FLOAT
301       #.$SQL_CONVERT_INTEGER
302       #.$SQL_CONVERT_LONGVARCHAR
303       #.$SQL_CONVERT_NUMERIC
304       #.$SQL_CONVERT_REAL
305       #.$SQL_CONVERT_SMALLINT
306       #.$SQL_CONVERT_TIME
307       #.$SQL_CONVERT_TIMESTAMP
308       #.$SQL_CONVERT_TINYINT
309       #.$SQL_CONVERT_VARBINARY
310       #.$SQL_CONVERT_VARCHAR
311       #.$SQL_CONVERT_LONGVARBINARY
312       #.$SQL_CONVERT_FUNCTIONS
313       #.$SQL_FETCH_DIRECTION
314       #.$SQL_FILE_USAGE
315       #.$SQL_GETDATA_EXTENSIONS
316       #.$SQL_LOCK_TYPES
317       #.$SQL_MAX_INDEX_SIZE
318       #.$SQL_MAX_ROW_SIZE
319       #.$SQL_MAX_STATEMENT_LEN
320       #.$SQL_NUMERIC_FUNCTIONS
321       #.$SQL_OWNER_USAGE
322       #.$SQL_POS_OPERATIONS
323       #.$SQL_POSITIONED_STATEMENTS
324       #.$SQL_QUALIFIER_USAGE
325       #.$SQL_SCROLL_CONCURRENCY
326       #.$SQL_SCROLL_OPTIONS
327       #.$SQL_STATIC_SENSITIVITY
328       #.$SQL_STRING_FUNCTIONS
329       #.$SQL_SUBQUERIES
330       #.$SQL_SYSTEM_FUNCTIONS
331       #.$SQL_TIMEDATE_ADD_INTERVALS
332       #.$SQL_TIMEDATE_DIFF_INTERVALS
333       #.$SQL_TIMEDATE_FUNCTIONS
334       #.$SQL_TXN_ISOLATION_OPTION
335       #.$SQL_UNION)
336      (with-foreign-objects ((info-ptr :long)
337                             (info-length-ptr :short))
338        (with-error-handling 
339          (:hdbc hdbc)
340          (SQLGetInfo hdbc
341                      info-type
342                      info-ptr
343                      255
344                      info-length-ptr)
345          (deref-pointer info-ptr :long)))
346      )
347     ;; those returning a long integer
348     ((#.$SQL_DEFAULT_TXN_ISOLATION
349       #.$SQL_DRIVER_HDBC
350       #.$SQL_DRIVER_HENV
351       #.$SQL_DRIVER_HLIB
352       #.$SQL_DRIVER_HSTMT
353       #.$SQL_GROUP_BY
354       #.$SQL_IDENTIFIER_CASE
355       #.$SQL_MAX_BINARY_LITERAL_LEN
356       #.$SQL_MAX_CHAR_LITERAL_LEN
357       #.$SQL_ACTIVE_ENVIRONMENTS
358       )
359      (with-foreign-objects ((info-ptr :long)
360                             (info-length-ptr :short))
361        (with-error-handling 
362          (:hdbc hdbc)
363          (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
364          (deref-pointer info-ptr :long))))))
365      
366 (defun %sql-exec-direct (sql hstmt henv hdbc)
367   (with-cstring (sql-ptr sql)
368     (with-error-handling
369       (:hstmt hstmt :henv henv :hdbc hdbc)
370       (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
371
372 (defun %sql-cancel (hstmt)
373   (with-error-handling
374     (:hstmt hstmt)
375     (SQLCancel hstmt)))
376
377 (defun %sql-execute (hstmt)
378   (with-error-handling
379     (:hstmt hstmt)
380     (SQLExecute hstmt)))
381
382 (defun result-columns-count (hstmt)
383   (with-foreign-objects ((columns-nr-ptr :short))
384     (with-error-handling (:hstmt hstmt)
385                          (SQLNumResultCols hstmt columns-nr-ptr)
386       (deref-pointer columns-nr-ptr :short))))
387
388 (defun result-rows-count (hstmt)
389   (with-foreign-objects ((row-count-ptr :long))
390     (with-error-handling (:hstmt hstmt)
391                          (SQLRowCount hstmt row-count-ptr)
392       (deref-pointer row-count-ptr :long))))
393
394 ;; column counting is 1-based
395 (defun %describe-column (hstmt column-nr)
396   (with-foreign-objects ((column-name-ptr '(:array :unsigned-char 256))
397                          (column-name-length-ptr :short)
398                          (column-sql-type-ptr :short)
399                          (column-precision-ptr :long)
400                          (column-scale-ptr :short)
401                          (column-nullable-p-ptr :short))
402     (with-error-handling (:hstmt hstmt)
403                          (SQLDescribeCol hstmt column-nr column-name-ptr 256
404                                          column-name-length-ptr
405                                          column-sql-type-ptr
406                                          column-precision-ptr
407                                          column-scale-ptr
408                                          column-nullable-p-ptr)
409       (values
410        (convert-from-foreign-string column-name-ptr)
411        (deref-pointer column-sql-type-ptr :short)
412        (deref-pointer column-precision-ptr :long)
413        (deref-pointer column-scale-ptr :short)
414        (deref-pointer column-nullable-p-ptr :short)))))
415
416 ;; parameter counting is 1-based
417 (defun %describe-parameter (hstmt parameter-nr)
418   (with-foreign-objects ((column-sql-type-ptr :short)
419                          (column-precision-ptr :long)
420                          (column-scale-ptr :short)
421                          (column-nullable-p-ptr :short))
422     (with-error-handling 
423       (:hstmt hstmt)
424       (SQLDescribeParam hstmt parameter-nr
425                         column-sql-type-ptr
426                         column-precision-ptr
427                         column-scale-ptr
428                         column-nullable-p-ptr)
429       (values
430        (deref-pointer column-sql-type-ptr :short)
431        (deref-pointer column-precision-ptr :long)
432        (deref-pointer column-scale-ptr :short)
433        (deref-pointer column-nullable-p-ptr :short)))))
434
435 (defun %column-attributes (hstmt column-nr descriptor-type)
436   (with-foreign-objects ((descriptor-info-ptr '(:array :unsigned-char 256))
437                          (descriptor-length-ptr :short)
438                          (numeric-descriptor-ptr :long))
439     (with-error-handling
440       (:hstmt hstmt) 
441       (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr 256
442                         descriptor-length-ptr
443                         numeric-descriptor-ptr)
444       (values
445        (convert-from-foreign-string descriptor-info-ptr)
446        (deref-pointer numeric-descriptor-ptr :long)))))
447
448 (defun %prepare-describe-columns (hstmt table-qualifier table-owner 
449                                    table-name column-name)
450   (with-cstrings ((table-qualifier-ptr table-qualifier)
451                   (table-owner-ptr table-owner) 
452                   (table-name-ptr table-name)
453                   (column-name-ptr column-name))
454     (with-error-handling
455         (:hstmt hstmt) 
456       (SQLColumns hstmt
457                   table-qualifier-ptr (length table-qualifier)
458                   table-owner-ptr (length table-owner)
459                   table-name-ptr (length table-name)
460                   column-name-ptr (length column-name)))))
461
462 (defun %describe-columns (hdbc table-qualifier table-owner 
463                           table-name column-name)
464   (with-statement-handle (hstmt hdbc)
465     (%prepare-describe-columns hstmt table-qualifier table-owner 
466                                table-name column-name)
467     (fetch-all-rows hstmt)))
468
469 (defun %sql-data-sources (henv &key (direction :first))
470   (with-foreign-objects 
471    ((name-ptr '(:array :unsigned-char #.(1+ $SQL_MAX_DSN_LENGTH)))
472     (name-length-ptr :short)
473     (description-ptr '(:array :unsigned-char 1024))
474     (description-length-ptr :short))
475     (let ((res (with-error-handling
476                  (:henv henv)
477                  (SQLDataSources henv
478                                  (ecase direction
479                                    (:first $SQL_FETCH_FIRST)
480                                    (:next $SQL_FETCH_NEXT))
481                                  name-ptr
482                                  (1+ $SQL_MAX_DSN_LENGTH)
483                                  name-length-ptr
484                                  description-ptr
485                                  1024
486                                  description-length-ptr))))
487       (unless (= res $SQL_NO_DATA_FOUND)
488         (values (convert-from-foreign-string name-ptr)
489                 (convert-from-foreign-string description-ptr))))))
490
491 (defun sql-to-c-type (sql-type)
492   (ecase sql-type
493     ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR 
494       #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9) $SQL_C_CHAR)
495     (#.$SQL_INTEGER $SQL_C_SLONG)
496     (#.$SQL_SMALLINT $SQL_C_SSHORT)
497     ((#.$SQL_FLOAT #.$SQL_DOUBLE) $SQL_C_DOUBLE)
498     (#.$SQL_REAL $SQL_C_FLOAT)
499     (#.$SQL_DATE $SQL_C_DATE)
500     (#.$SQL_TIME $SQL_C_TIME)
501     (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
502     ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
503     (#.$SQL_TINYINT $SQL_C_STINYINT)
504     (#.$SQL_BIT $SQL_C_BIT)))
505
506 (defun get-cast-byte (ptr)
507   (declare (type long-ptr-type out-len-ptr))
508   (with-cast-pointer (casted ptr '(* :byte))
509     (deref-pointer casted :byte)))
510
511 (defun get-cast-short (ptr)
512   (declare (type long-ptr-type out-len-ptr))
513   (with-cast-pointer (casted ptr '(* :short))
514     (deref-pointer casted :short)))
515
516 (defun get-cast-int (ptr)
517   (declare (type long-ptr-type out-len-ptr))
518   (with-cast-pointer (casted ptr '(* :int))
519     (deref-pointer casted :int)))
520
521 (defun get-cast-long (ptr)
522   (declare (type long-ptr-type out-len-ptr))
523   (with-cast-pointer (casted ptr '(* :long))
524     (deref-pointer casted :long)))
525
526 (defun get-cast-single-float (ptr)
527   (declare (type long-ptr-type out-len-ptr))
528   (with-cast-pointer (casted ptr '(* :float))
529     (deref-pointer casted :float)))
530
531 (defun get-cast-double-float (ptr)
532   (declare (type long-ptr-type out-len-ptr))
533   (with-cast-pointer (casted ptr '(* :double))
534     (deref-pointer casted :double)))
535
536 (defun get-cast-foreign-string (ptr)
537   (declare (type long-ptr-type out-len-ptr))
538   (with-cast-pointer (casted ptr '(* :unsigned-char))
539     (convert-from-foreign-string casted)))
540
541 (defun get-cast-binary (ptr len format)
542   "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
543   (with-cast-pointer (casted ptr '(* :byte))
544     (ecase format
545       (:unsigned-byte-vector
546        (let ((vector (make-array len :element-type '(unsigned-byte 8))))
547          (dotimes (i len)
548            (setf (aref vector i)
549                  (deref-array casted '(:array :byte) i)))
550          vector))
551       (:bit-vector
552        (let ((vector (make-array (ash len 3) :element-type 'bit)))
553          (dotimes (i len)
554            (let ((byte (deref-array casted '(:array :byte) i)))
555              (dotimes (j 8)
556                (setf (bit vector (+ (ash i 3) j)) (logand (ash byte (- j 7)) 1)))))
557          vector)))))
558
559
560 (defun read-data (data-ptr c-type sql-type out-len-ptr convert-to-string-p)
561   (declare (type long-ptr-type out-len-ptr))
562   (let ((out-len (deref-pointer out-len-ptr :long)))
563     (cond ((= out-len $SQL_NULL_DATA)
564            *null*)
565           ;; obsolete?
566           (convert-to-string-p
567            (convert-from-foreign-string data-ptr))
568           (t
569            (case sql-type
570              ;; SQL extended datatypes
571              (#.$SQL_TINYINT  (get-cast-short data-ptr))
572              (#.$SQL_C_STINYINT (get-cast-short data-ptr)) ;; ?
573              (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
574              (#.$SQL_SMALLINT (deref-pointer data-ptr :short)) ; ??
575              (#.$SQL_INTEGER (deref-pointer data-ptr :long))
576              (#.$SQL_DECIMAL 
577               (let ((*read-base* 10))
578                 (read-from-string (get-cast-foreign-string data-ptr))))
579              (t 
580               (case c-type
581                 (#.$SQL_C_DATE
582                  (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
583                 (#.$SQL_C_TIME
584                  (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
585                    (funcall *time-conversion-function* universal-time frac)))
586                 (#.$SQL_C_TIMESTAMP
587                  (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
588                    (funcall *time-conversion-function* universal-time frac)))
589                 (#.$SQL_INTEGER
590                  (get-cast-int data-ptr))
591                 (#.$SQL_C_FLOAT
592                  (get-cast-single-float data-ptr))
593                 (#.$SQL_C_DOUBLE
594                  (get-cast-double-float data-ptr))
595                 (#.$SQL_C_SLONG
596                  (get-cast-long data-ptr))
597                 #+lispworks
598                 (#.$SQL_C_BIT ; encountered only in Access
599                  (get-cast-byte data-ptr))
600                 (#.$SQL_C_BINARY
601                  (get-cast-binary data-ptr out-len *binary-format*))
602                 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
603                  (get-cast-short data-ptr))     ; LMH
604                 #+ignore
605                 (#.$SQL_C_CHAR
606                  (code-char (get-cast-short data-ptr)))
607                 (t
608                  (convert-from-foreign-string data-ptr)))))))))
609
610 ;; which value is appropriate?
611 (defparameter +max-precision+ 
612   #+mcl 512
613   #-mcl 4001)
614
615 (defvar *break-on-unknown-data-type* t)
616
617 ;; C. Stacy's idea to factor this out
618 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
619 ;; so that I don't have to remember to make changes in more than one place.
620 ;; Just keep it in synch with READ-DATA."
621 (defun %allocate-bindings (sql-type precision)
622   (let* ((c-type (sql-to-c-type sql-type))
623          (size (if (zerop precision)
624                    +max-precision+ ;; if the precision cannot be determined
625                  (min precision +max-precision+)))
626          (long-p (= size +max-precision+))
627          (data-ptr
628           (case c-type ;; add more?
629             (#.$SQL_C_SLONG (uffi:allocate-foreign-object :long))
630             (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
631             (#.$SQL_C_DATE (allocate-foreign-object 'sql-c-date))
632             (#.$SQL_C_TIME (allocate-foreign-object 'sql-c-time))
633             (#.$SQL_C_TIMESTAMP (allocate-foreign-object 'sql-c-timestamp))
634             #+lispworks(#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
635             (#.$SQL_C_BIT (uffi:allocate-foreign-object :boolean))
636             (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
637             (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
638             (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
639             (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
640             (t 
641                 ;; Maybe should signal a restartable condition for this?
642                 (when *break-on-unknown-data-type*
643                   (break "SQL type is ~A, precision ~D, size ~D, C type is ~A" 
644                          sql-type precision size c-type))
645                 (uffi:allocate-foreign-object :ptr (1+ size)))))
646          (out-len-ptr (uffi:allocate-foreign-object :long)))
647     (values c-type data-ptr out-len-ptr size long-p)))
648
649 (defun fetch-all-rows (hstmt &key free-option flatp)
650   (let ((column-count (result-columns-count hstmt)))
651     (unless (zerop column-count)
652       (let ((names (make-array column-count :element-type 'string))
653             (sql-types (make-array column-count :element-type 'fixnum))
654             (c-types (make-array column-count :element-type 'fixnum))
655             (precisions (make-array column-count :element-type 'fixnum))
656             (data-ptrs (make-array column-count :initial-element nil))
657             (out-len-ptrs (make-array column-count :initial-element nil))
658             (scales (make-array column-count :element-type 'fixnum))
659             (nullables-p (make-array column-count :element-type 'fixnum)))
660         (unwind-protect
661           (values
662            (progn
663              (dotimes (col-nr column-count)
664                ;; get column information
665                (multiple-value-bind (name sql-type precision scale nullable-p)
666                                     (%describe-column hstmt (1+ col-nr))
667                  ;; allocate space to bind result rows to
668                  (multiple-value-bind (c-type data-ptr out-len-ptr)
669                      (%allocate-bindings sql-type precision)
670                    (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
671                    (setf (svref names col-nr) name
672                          (aref sql-types col-nr) sql-type
673                          (aref c-types col-nr) (sql-to-c-type sql-type)
674                          (aref precisions col-nr) (if (zerop precision) nil precision)
675                          (aref scales col-nr) scale
676                          (aref nullables-p col-nr) nullable-p
677                          (aref data-ptrs col-nr) data-ptr
678                          (aref out-len-ptrs col-nr) out-len-ptr))))
679              ;; the main loop
680              (prog1
681                (cond (flatp 
682                       (when (> column-count 1)
683                         (error "If more than one column is to be fetched, flatp has to be nil."))
684                       (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
685                             collect
686                             (read-data (aref data-ptrs 0) 
687                                        (aref c-types 0)
688                                        (aref sql-types 0)
689                                        (aref out-len-ptrs 0)
690                                        nil)))
691                      (t
692                       (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
693                             collect
694                             (loop for col-nr from 0 to (1- column-count)
695                                   collect
696                                   (read-data (aref data-ptrs col-nr) 
697                                              (aref c-types col-nr)
698                                              (aref sql-types col-nr)
699                                              (aref out-len-ptrs col-nr)
700                                              nil)))))))
701            names)
702           ;; dispose of memory etc
703           (when free-option (%free-statement hstmt free-option))
704           (dotimes (col-nr column-count)
705             (let ((data-ptr (aref data-ptrs col-nr))
706                   (out-len-ptr (aref out-len-ptrs col-nr)))
707               (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
708               (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
709
710 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
711 ;; of %do-fetch into sql package (has been done)
712
713 (defun %sql-prepare (hstmt sql)
714   (with-cstring (sql-ptr sql)
715     (with-error-handling (:hstmt hstmt)
716       (SQLPrepare hstmt sql-ptr $SQL_NTS))))
717
718 ;; depending on option, we return a long int or a string; string not implemented
719 (defun get-connection-option (hdbc option)
720   (with-foreign-objects ((param-ptr :long #+ignore #.(1+ $SQL_MAX_OPTION_STRING_LENGTH)))
721     (with-error-handling (:hdbc hdbc)
722                          (SQLGetConnectOption hdbc option param-ptr)
723       (deref-pointer param-ptr :long))))
724
725 (defun set-connection-option (hdbc option param)
726   (with-error-handling (:hdbc hdbc)
727     (SQLSetConnectOption hdbc option param)))
728
729 (defun disable-autocommit (hdbc)
730   (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
731
732 (defun enable-autocommit (hdbc)
733   (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
734
735 (defun %sql-set-pos (hstmt row option lock)
736   (with-error-handling 
737     (:hstmt hstmt)
738     (SQLSetPos hstmt row option lock)))
739
740 (defun %sql-extended-fetch (hstmt fetch-type row)
741   (with-foreign-objects ((row-count-ptr :unsigned-long)
742                          (row-status-ptr :short))
743     (with-error-handling (:hstmt hstmt)
744       (SQLExtendedFetch hstmt fetch-type row row-count-ptr
745                         row-status-ptr)
746       (values (deref-pointer row-count-ptr :unsigned-long)
747               (deref-pointer row-status-ptr :short)))))
748
749 ; column-nr is zero-based
750 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
751   (with-error-handling
752     (:hstmt hstmt :print-info nil)
753     (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
754                 c-type data-ptr precision out-len-ptr)))
755
756 (defun %sql-param-data (hstmt param-ptr)
757   (with-error-handling (:hstmt hstmt :print-info t) ;; nil
758       (SQLParamData hstmt param-ptr)))
759
760 (defun %sql-put-data (hstmt data-ptr size)
761   (with-error-handling
762     (:hstmt hstmt :print-info t) ;; nil
763     (SQLPutData hstmt data-ptr size)))
764
765 (defconstant $sql-data-truncated (intern "01004" :keyword))
766
767 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type 
768                                       out-len-ptr convert-to-string-p)
769   (declare (ignore convert-to-string-p) ; prelimianary
770            (type long-ptr-type out-len-ptr))
771   (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr 
772                              +max-precision+ out-len-ptr))
773          (out-len (deref-pointer out-len-ptr :long))
774          (offset 0))
775     (case out-len
776       (#.$SQL_NULL_DATA
777        (return-from read-data-in-chunks *null*))
778       (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
779        (let ((str (make-array 0 :element-type 'character :adjustable t)))
780          (loop do (if (= c-type #.$SQL_CHAR)
781                       (let ((data-length (foreign-string-length data-ptr)))
782                         (adjust-array str (+ offset data-length)
783                                       :initial-element #\?)
784                         (setf offset (%cstring-into-vector
785                                       data-ptr str 
786                                       offset 
787                                       data-length)))
788                     (error "wrong type. preliminary."))
789                while (and (= res $SQL_SUCCESS_WITH_INFO)
790                           (equal (sql-state (%null-ptr) (%null-ptr) hstmt)
791                                  "01004"))
792                do (setf res (%sql-get-data hstmt column-nr c-type data-ptr 
793                                            +max-precision+ out-len-ptr)))
794          (setf str (coerce str 'string))
795          (if (= sql-type $SQL_DECIMAL)
796              (let ((*read-base* 10))
797                (read-from-string str))
798            str)))
799       (otherwise
800        (let ((str (make-string out-len)))
801          (loop do (if (= c-type #.$SQL_CHAR)
802                       (setf offset (%cstring-into-vector ;string
803                                     data-ptr str 
804                                     offset 
805                                     (min out-len (1- +max-precision+))))
806                     (error "wrong type. preliminary."))
807                while 
808                (and (= res $SQL_SUCCESS_WITH_INFO)
809                     #+ingore(eq (sql-state (%null-ptr) (%null-ptr) hstmt)
810                                 $sql-data-truncated)
811                     (equal (sql-state (%null-ptr) (%null-ptr) hstmt)
812                            "01004"))
813                do (setf res (%sql-get-data hstmt column-nr c-type data-ptr 
814                                            +max-precision+ out-len-ptr)
815                         out-len (deref-pointer out-len-ptr :long)))
816          (if (= sql-type $SQL_DECIMAL)
817              (let ((*read-base* 10))
818                (read-from-string str))
819            str))))))
820
821 (defun timestamp-to-universal-time (ptr)
822   (values
823    (encode-universal-time 
824     (get-slot-value ptr 'sql-c-timestamp 'second)
825     (get-slot-value ptr 'sql-c-timestamp 'minute)
826     (get-slot-value ptr 'sql-c-timestamp 'hour)
827     (get-slot-value ptr 'sql-c-timestamp 'day)
828     (get-slot-value ptr 'sql-c-timestamp 'month)
829     (get-slot-value ptr 'sql-c-timestamp 'year))
830    (get-slot-value ptr 'sql-c-timestamp 'fraction)))
831
832 (defun universal-time-to-timestamp (time &optional (fraction 0))
833   (multiple-value-bind (sec min hour day month year)
834       (decode-universal-time time)
835     (with-foreign-object (ptr 'sql-c-timestamp)
836       (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
837             (get-slot-value ptr 'sql-c-timestamp 'minute) min
838             (get-slot-value ptr 'sql-c-timestamp 'hour) hour
839             (get-slot-value ptr 'sql-c-timestamp 'day) day
840             (get-slot-value ptr 'sql-c-timestamp 'month) month
841             (get-slot-value ptr 'sql-c-timestamp 'year) year
842             (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
843       ptr)))
844
845 (defun %put-timestamp (ptr time &optional (fraction 0))
846   (multiple-value-bind (sec min hour day month year)
847       (decode-universal-time time)
848     (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
849           (get-slot-value ptr 'sql-c-timestamp 'minute) min
850           (get-slot-value ptr 'sql-c-timestamp 'hour) hour
851           (get-slot-value ptr 'sql-c-timestamp 'day) day
852           (get-slot-value ptr 'sql-c-timestamp 'month) month
853           (get-slot-value ptr 'sql-c-timestamp 'year) year
854           (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
855       ptr))
856
857 (defun date-to-universal-time (ptr)
858   (encode-universal-time
859    0 0 0
860    (get-slot-value ptr 'sql-c-timestamp 'day)
861    (get-slot-value ptr 'sql-c-timestamp 'month)
862    (get-slot-value ptr 'sql-c-timestamp 'year)))
863
864 (defun time-to-universal-time (ptr)
865   (encode-universal-time 
866    (get-slot-value ptr 'sql-c-timestamp 'second)
867    (get-slot-value ptr 'sql-c-timestamp 'minute)
868    (get-slot-value ptr 'sql-c-timestamp 'hour)
869    0 0 0))
870