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