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