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