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