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