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