1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: odbc -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: odbc-api.lisp
6 ;;;; Purpose: Low-level ODBC API using UFFI
7 ;;;; Authors: Kevin M. Rosenberg and Paul Meurer
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.
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 ;;;; *************************************************************************
20 "Lisp representation of SQL Null value, default = nil.
21 May be locally bound to something else if a certain type is necessary.")
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+
30 (clsql-sys:make-duration :usec (/ fraction 1000))))
31 (clsql-sys:format-time nil time :format :iso))
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.")
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.")
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
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)
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)))))
56 (defun %cstring-into-vector (ptr vector offset size-in-bytes)
57 (dotimes (i size-in-bytes)
58 (setf (schar vector offset)
59 (ensure-char-character
60 (deref-array ptr '(:array :unsigned-char) i)))
64 (defun handle-error (henv hdbc hstmt)
65 (let ((sql-state (allocate-foreign-string 256))
66 (error-message (allocate-foreign-string #.$SQL_MAX_MESSAGE_LENGTH)))
67 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
69 (SQLError henv hdbc hstmt sql-state
70 error-code error-message
71 #.$SQL_MAX_MESSAGE_LENGTH msg-length)
72 (let ((err (convert-from-foreign-string error-message))
73 (state (convert-from-foreign-string sql-state)))
74 (free-foreign-object error-message)
75 (free-foreign-object sql-state)
79 (deref-pointer msg-length :short)
80 (deref-pointer error-code #.$ODBC-LONG-TYPE))))))
82 (defun sql-state (henv hdbc hstmt)
83 (let ((sql-state (allocate-foreign-string 256))
84 (error-message (allocate-foreign-string #.$SQL_MAX_MESSAGE_LENGTH)))
85 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
87 (SQLError henv hdbc hstmt sql-state error-code
88 error-message #.$SQL_MAX_MESSAGE_LENGTH msg-length)
89 (let ((state (convert-from-foreign-string sql-state)))
90 (free-foreign-object error-message)
91 (free-foreign-object sql-state)
93 ;; test this: return a keyword for efficiency
94 ;;(%cstring-to-keyword state)
97 (defmacro with-error-handling ((&key henv hdbc hstmt (print-info t))
99 (let ((result-code (gensym "RC-")))
100 `(let ((,result-code ,odbc-call))
102 ;; Check for allegro v7 & v8 bug with ODBC calls returning
103 ;; 32-bit unsigned ints, not 16-bit signed ints
104 #+(and allegro mswindows)
105 (when (> ,result-code #xFFFF)
106 (warn (format nil "16-bit return bug: result-code #x~X for expression ~S"
107 ,result-code (quote ,odbc-call)))
108 (setq ,result-code (logand ,result-code #xFFFF))
109 (when (> ,result-code #x7FFF)
110 (setq ,result-code (- ,result-code #x10000))))
114 (progn ,result-code ,@body))
115 (#.$SQL_SUCCESS_WITH_INFO
117 (multiple-value-bind (error-message sql-state)
118 (handle-error (or ,henv +null-handle-ptr+)
119 (or ,hdbc +null-handle-ptr+)
120 (or ,hstmt +null-handle-ptr+))
122 (format *info-output* "[ODBC info ~A] ~A state: ~A"
123 ,result-code error-message
125 (progn ,result-code ,@body))
126 (#.$SQL_INVALID_HANDLE
128 'clsql-sys:sql-database-error
129 :message "ODBC: Invalid handle"))
130 (#.$SQL_STILL_EXECUTING
132 'clsql-sys:sql-temporary-error
133 :message "ODBC: Still executing"))
135 (multiple-value-bind (error-message sql-state)
136 (handle-error (or ,henv +null-handle-ptr+)
137 (or ,hdbc +null-handle-ptr+)
138 (or ,hstmt +null-handle-ptr+))
140 'clsql-sys:sql-database-error
141 :message error-message
142 :secondary-error-id sql-state)))
143 (#.$SQL_NO_DATA_FOUND
144 (progn ,result-code ,@body))
145 ;; work-around for Allegro 7.0beta AMD64 which returns negative numbers
147 (multiple-value-bind (error-message sql-state)
148 (handle-error (or ,henv +null-handle-ptr+)
149 (or ,hdbc +null-handle-ptr+)
150 (or ,hstmt +null-handle-ptr+))
152 'clsql-sys:sql-database-error
153 :message error-message
154 :secondary-error-id sql-state))
156 (progn ,result-code ,@body))))))
158 (defun %new-environment-handle ()
160 (with-foreign-object (phenv 'sql-handle)
163 (SQLAllocHandle $SQL_HANDLE_ENV +null-handle-ptr+ phenv)
164 (deref-pointer phenv 'sql-handle)))))
165 (%set-attr-odbc-version henv $SQL_OV_ODBC3)
169 (defun %sql-free-environment (henv)
174 (defun %new-db-connection-handle (henv)
175 (with-foreign-object (phdbc 'sql-handle)
176 (setf (deref-pointer phdbc 'sql-handle) +null-handle-ptr+)
179 (SQLAllocHandle $SQL_HANDLE_DBC henv phdbc)
180 (deref-pointer phdbc 'sql-handle))))
182 (defun %free-statement (hstmt option)
190 (:unbind $SQL_UNBIND)
191 (:reset $SQL_RESET_PARAMS)))))
193 (defmacro with-statement-handle ((hstmt hdbc) &body body)
194 `(let ((,hstmt (%new-statement-handle ,hdbc)))
197 (%free-statement ,hstmt :drop))))
199 ;; functional interface
201 (defun %sql-connect (hdbc server uid pwd)
202 (with-cstrings ((server-ptr server)
207 (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr
208 $SQL_NTS pwd-ptr $SQL_NTS))))
210 (defun %sql-driver-connect (hdbc connection-string completion window-handle)
211 (with-cstring (connection-ptr connection-string)
212 (let ((completed-connection-string (allocate-foreign-string $SQL_MAX_CONN_OUT)))
214 (with-foreign-object (completed-connection-length :short)
217 (SQLDriverConnect hdbc
219 connection-ptr $SQL_NTS
220 completed-connection-string $SQL_MAX_CONN_OUT
221 completed-connection-length
223 (free-foreign-object completed-connection-string)))))
225 (defun %disconnect (hdbc)
228 (SQLDisconnect hdbc)))
230 (defun %commit (henv hdbc)
232 (:henv henv :hdbc hdbc)
234 henv hdbc $SQL_COMMIT)))
236 (defun %rollback (henv hdbc)
238 (:henv henv :hdbc hdbc)
240 henv hdbc $SQL_ROLLBACK)))
242 ; col-nr is zero-based in Lisp
243 ; col-nr = :bookmark retrieves a bookmark.
244 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
248 (if (eq column-nr :bookmark) 0 (1+ column-nr))
249 c-type data-ptr precision out-len-ptr)))
251 ; parameter-nr is zero-based in Lisp
252 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
253 sql-type precision scale data-ptr
254 max-value out-len-ptr)
257 (SQLBindParameter hstmt (1+ parameter-nr)
258 parameter-type ;$SQL_PARAM_INPUT
260 sql-type ;$SQL_VARCHAR
261 precision ;(1- (length str))
265 out-len-ptr ;#.+null-ptr+
268 (defun %sql-fetch (hstmt)
273 (defun %new-statement-handle (hdbc)
274 (let ((statement-handle
275 (with-foreign-object (phstmt 'sql-handle)
278 (SQLAllocHandle $SQL_HANDLE_STMT hdbc phstmt)
279 (deref-pointer phstmt 'sql-handle)))))
280 (if (uffi:null-pointer-p statement-handle)
281 (error 'clsql:sql-database-error :message "Received null statement handle.")
284 (defun %sql-get-info (hdbc info-type)
286 ;; those return string
287 ((#.$SQL_ACCESSIBLE_PROCEDURES
288 #.$SQL_ACCESSIBLE_TABLES
290 #.$SQL_DATA_SOURCE_NAME
291 #.$SQL_DATA_SOURCE_READ_ONLY
295 #.$SQL_DRIVER_ODBC_VER
297 #.$SQL_EXPRESSIONS_IN_ORDERBY
298 #.$SQL_IDENTIFIER_QUOTE_CHAR
300 #.$SQL_LIKE_ESCAPE_CLAUSE
301 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
302 #.$SQL_MULT_RESULT_SETS
303 #.$SQL_MULTIPLE_ACTIVE_TXN
304 #.$SQL_NEED_LONG_DATA_LEN
305 #.$SQL_ODBC_SQL_OPT_IEF
307 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
310 #.$SQL_PROCEDURE_TERM
312 #.$SQL_QUALIFIER_NAME_SEPARATOR
313 #.$SQL_QUALIFIER_TERM
315 #.$SQL_SEARCH_PATTERN_ESCAPE
317 #.$SQL_SPECIAL_CHARACTERS
320 (let ((info-ptr (allocate-foreign-string 1024)))
321 (with-foreign-object (info-length-ptr :short)
324 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
325 (let ((info (convert-from-foreign-string info-ptr)))
326 (free-foreign-object info-ptr)
328 ;; those returning a word
329 ((#.$SQL_ACTIVE_CONNECTIONS
330 #.$SQL_ACTIVE_STATEMENTS
331 #.$SQL_CONCAT_NULL_BEHAVIOR
332 #.$SQL_CORRELATION_NAME
333 #.$SQL_CURSOR_COMMIT_BEHAVIOR
334 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
335 #.$SQL_MAX_COLUMN_NAME_LEN
336 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
337 #.$SQL_MAX_COLUMNS_IN_INDEX
338 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
339 #.$SQL_MAX_COLUMNS_IN_SELECT
340 #.$SQL_MAX_COLUMNS_IN_TABLE
341 #.$SQL_MAX_CURSOR_NAME_LEN
342 #.$SQL_MAX_OWNER_NAME_LEN
343 #.$SQL_MAX_PROCEDURE_NAME_LEN
344 #.$SQL_MAX_QUALIFIER_NAME_LEN
345 #.$SQL_MAX_TABLE_NAME_LEN
346 #.$SQL_MAX_TABLES_IN_SELECT
347 #.$SQL_MAX_USER_NAME_LEN
348 #.$SQL_NON_NULLABLE_COLUMNS
349 #.$SQL_NULL_COLLATION
350 #.$SQL_ODBC_API_CONFORMANCE
351 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
352 #.$SQL_ODBC_SQL_CONFORMANCE
353 #.$SQL_QUALIFIER_LOCATION
354 #.$SQL_QUOTED_IDENTIFIER_CASE
356 (with-foreign-objects ((info-ptr :short)
357 (info-length-ptr :short))
365 (deref-pointer info-ptr :short)))
367 ;; those returning a long bitmask
369 #.$SQL_BOOKMARK_PERSISTENCE
370 #.$SQL_CONVERT_BIGINT
371 #.$SQL_CONVERT_BINARY
375 #.$SQL_CONVERT_DECIMAL
376 #.$SQL_CONVERT_DOUBLE
378 #.$SQL_CONVERT_INTEGER
379 #.$SQL_CONVERT_LONGVARCHAR
380 #.$SQL_CONVERT_NUMERIC
382 #.$SQL_CONVERT_SMALLINT
384 #.$SQL_CONVERT_TIMESTAMP
385 #.$SQL_CONVERT_TINYINT
386 #.$SQL_CONVERT_VARBINARY
387 #.$SQL_CONVERT_VARCHAR
388 #.$SQL_CONVERT_LONGVARBINARY
389 #.$SQL_CONVERT_FUNCTIONS
390 #.$SQL_FETCH_DIRECTION
392 #.$SQL_GETDATA_EXTENSIONS
394 #.$SQL_MAX_INDEX_SIZE
396 #.$SQL_MAX_STATEMENT_LEN
397 #.$SQL_NUMERIC_FUNCTIONS
399 #.$SQL_POS_OPERATIONS
400 #.$SQL_POSITIONED_STATEMENTS
401 #.$SQL_QUALIFIER_USAGE
402 #.$SQL_SCROLL_CONCURRENCY
403 #.$SQL_SCROLL_OPTIONS
404 #.$SQL_STATIC_SENSITIVITY
405 #.$SQL_STRING_FUNCTIONS
407 #.$SQL_SYSTEM_FUNCTIONS
408 #.$SQL_TIMEDATE_ADD_INTERVALS
409 #.$SQL_TIMEDATE_DIFF_INTERVALS
410 #.$SQL_TIMEDATE_FUNCTIONS
411 #.$SQL_TXN_ISOLATION_OPTION
413 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
414 (info-length-ptr :short))
422 (deref-pointer info-ptr #.$ODBC-LONG-TYPE)))
424 ;; those returning a long integer
425 ((#.$SQL_DEFAULT_TXN_ISOLATION
431 #.$SQL_IDENTIFIER_CASE
432 #.$SQL_MAX_BINARY_LITERAL_LEN
433 #.$SQL_MAX_CHAR_LITERAL_LEN
434 #.$SQL_ACTIVE_ENVIRONMENTS
436 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
437 (info-length-ptr :short))
440 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
441 (deref-pointer info-ptr #.$ODBC-LONG-TYPE))))))
443 (defun %sql-exec-direct (sql hstmt henv hdbc)
444 (with-cstring (sql-ptr sql)
446 (:hstmt hstmt :henv henv :hdbc hdbc)
447 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
449 (defun %sql-cancel (hstmt)
454 (defun %sql-execute (hstmt)
459 (defun result-columns-count (hstmt)
460 (with-foreign-objects ((columns-nr-ptr :short))
461 (with-error-handling (:hstmt hstmt)
462 (SQLNumResultCols hstmt columns-nr-ptr)
463 (deref-pointer columns-nr-ptr :short))))
465 (defun result-rows-count (hstmt)
466 (with-foreign-objects ((row-count-ptr #.$ODBC-LONG-TYPE))
467 (with-error-handling (:hstmt hstmt)
468 (SQLRowCount hstmt row-count-ptr)
469 (deref-pointer row-count-ptr #.$ODBC-LONG-TYPE))))
471 ;; column counting is 1-based
472 (defun %describe-column (hstmt column-nr)
473 (let ((column-name-ptr (allocate-foreign-string 256)))
474 (with-foreign-objects ((column-name-length-ptr :short)
475 (column-sql-type-ptr :short)
476 (column-precision-ptr #.$ODBC-ULONG-TYPE)
477 (column-scale-ptr :short)
478 (column-nullable-p-ptr :short))
479 (with-error-handling (:hstmt hstmt)
480 (SQLDescribeCol hstmt column-nr column-name-ptr 256
481 column-name-length-ptr
485 column-nullable-p-ptr)
486 (let ((column-name (convert-from-foreign-string column-name-ptr)))
487 (free-foreign-object column-name-ptr)
490 (deref-pointer column-sql-type-ptr :short)
491 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
492 (deref-pointer column-scale-ptr :short)
493 (deref-pointer column-nullable-p-ptr :short)))))))
495 ;; parameter counting is 1-based
496 (defun %describe-parameter (hstmt parameter-nr)
497 (with-foreign-objects ((column-sql-type-ptr :short)
498 (column-precision-ptr #.$ODBC-ULONG-TYPE)
499 (column-scale-ptr :short)
500 (column-nullable-p-ptr :short))
503 (SQLDescribeParam hstmt parameter-nr
507 column-nullable-p-ptr)
509 (deref-pointer column-sql-type-ptr :short)
510 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
511 (deref-pointer column-scale-ptr :short)
512 (deref-pointer column-nullable-p-ptr :short)))))
514 (defun %column-attributes (hstmt column-nr descriptor-type)
515 (let ((descriptor-info-ptr (allocate-foreign-string 256)))
516 (with-foreign-objects ((descriptor-length-ptr :short)
517 (numeric-descriptor-ptr #.$ODBC-LONG-TYPE))
520 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr
521 256 descriptor-length-ptr
522 numeric-descriptor-ptr)
523 (let ((desc (convert-from-foreign-string descriptor-info-ptr)))
524 (free-foreign-object descriptor-info-ptr)
527 (deref-pointer numeric-descriptor-ptr #.$ODBC-LONG-TYPE)))))))
529 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
530 table-name column-name)
531 (with-cstrings ((table-qualifier-ptr table-qualifier)
532 (table-owner-ptr table-owner)
533 (table-name-ptr table-name)
534 (column-name-ptr column-name))
538 table-qualifier-ptr (length table-qualifier)
539 table-owner-ptr (length table-owner)
540 table-name-ptr (length table-name)
541 column-name-ptr (length column-name)))))
543 (defun %describe-columns (hdbc table-qualifier table-owner
544 table-name column-name)
545 (with-statement-handle (hstmt hdbc)
546 (%prepare-describe-columns hstmt table-qualifier table-owner
547 table-name column-name)
548 (fetch-all-rows hstmt)))
550 (defun %sql-data-sources (henv &key (direction :first))
551 (let ((name-ptr (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
552 (description-ptr (allocate-foreign-string 1024)))
553 (with-foreign-objects ((name-length-ptr :short)
554 (description-length-ptr :short))
555 (let ((res (with-error-handling
559 (:first $SQL_FETCH_FIRST)
560 (:next $SQL_FETCH_NEXT))
562 (1+ $SQL_MAX_DSN_LENGTH)
566 description-length-ptr))))
568 ((= res $SQL_NO_DATA_FOUND)
569 (let ((name (convert-from-foreign-string name-ptr))
570 (desc (convert-from-foreign-string description-ptr)))
571 (free-foreign-object name-ptr)
572 (free-foreign-object description-ptr)
577 (free-foreign-object name-ptr)
578 (free-foreign-object description-ptr)
583 (defun sql-to-c-type (sql-type)
585 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
586 #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9 -10) $SQL_C_CHAR) ;; Added -10 for MSSQL ntext type
587 (#.$SQL_INTEGER $SQL_C_SLONG)
588 (#.$SQL_SMALLINT $SQL_C_SSHORT)
589 (#.$SQL_DOUBLE $SQL_C_DOUBLE)
590 (#.$SQL_FLOAT $SQL_C_DOUBLE)
591 (#.$SQL_REAL $SQL_C_FLOAT)
592 (#.$SQL_DATE $SQL_C_DATE)
593 (#.$SQL_TIME $SQL_C_TIME)
594 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
595 (#.$SQL_TYPE_DATE $SQL_C_TYPE_DATE)
596 (#.$SQL_TYPE_TIME $SQL_C_TYPE_TIME)
597 (#.$SQL_TYPE_TIMESTAMP $SQL_C_TYPE_TIMESTAMP)
598 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
599 (#.$SQL_TINYINT $SQL_C_STINYINT)
600 (#.$SQL_BIT $SQL_C_BIT)))
602 (def-type byte-pointer-type (* :byte))
603 (def-type short-pointer-type (* :short))
604 (def-type int-pointer-type (* :int))
605 (def-type long-pointer-type (* #.$ODBC-LONG-TYPE))
606 (def-type float-pointer-type (* :float))
607 (def-type double-pointer-type (* :double))
608 (def-type string-pointer-type (* :unsigned-char))
610 (defun get-cast-byte (ptr)
611 (locally (declare (type byte-pointer-type ptr))
612 (deref-pointer ptr :byte)))
614 (defun get-cast-short (ptr)
615 (locally (declare (type short-pointer-type ptr))
616 (deref-pointer ptr :short)))
618 (defun get-cast-int (ptr)
619 (locally (declare (type int-pointer-type ptr))
620 (deref-pointer ptr :int)))
622 (defun get-cast-long (ptr)
623 (locally (declare (type long-pointer-type ptr))
624 (deref-pointer ptr #.$ODBC-LONG-TYPE)))
626 (defun get-cast-single-float (ptr)
627 (locally (declare (type float-pointer-type ptr))
628 (deref-pointer ptr :float)))
630 (defun get-cast-double-float (ptr)
631 (locally (declare (type double-pointer-type ptr))
632 (deref-pointer ptr :double)))
634 (defun get-cast-foreign-string (ptr)
635 (locally (declare (type string-pointer-type ptr))
636 (convert-from-foreign-string ptr)))
638 (defun get-cast-binary (ptr len format)
639 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
640 (with-cast-pointer (casted ptr :unsigned-byte)
642 (:unsigned-byte-vector
643 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
645 (setf (aref vector i)
646 (deref-array casted '(:array :unsigned-byte) i)))
649 (let ((vector (make-array (ash len 3) :element-type 'bit)))
651 (let ((byte (deref-array casted '(:array :byte) i)))
653 (setf (bit vector (+ (ash i 3) j))
654 (logand (ash byte (- j 7)) 1)))))
658 (defun read-data (data-ptr c-type sql-type out-len-ptr result-type)
659 (declare (type long-ptr-type out-len-ptr))
660 (let* ((out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
662 (cond ((= out-len $SQL_NULL_DATA)
666 ;; SQL extended datatypes
667 (#.$SQL_TINYINT (get-cast-byte data-ptr))
668 (#.$SQL_C_STINYINT (get-cast-byte data-ptr)) ;; ?
669 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
670 (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
671 (#.$SQL_INTEGER (get-cast-int data-ptr))
672 (#.$SQL_BIGINT (read-from-string
673 (get-cast-foreign-string data-ptr)))
675 (let ((*read-base* 10))
676 (read-from-string (get-cast-foreign-string data-ptr))))
677 (#.$SQL_BIT (get-cast-byte data-ptr))
680 ((#.$SQL_C_DATE #.$SQL_C_TYPE_DATE)
681 (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
682 ((#.$SQL_C_TIME #.$SQL_C_TYPE_TIME)
683 (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
684 (funcall *time-conversion-function* universal-time frac)))
685 ((#.$SQL_C_TIMESTAMP #.$SQL_C_TYPE_TIMESTAMP)
686 (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
687 (funcall *time-conversion-function* universal-time frac)))
689 (get-cast-int data-ptr))
691 (get-cast-single-float data-ptr))
693 (get-cast-double-float data-ptr))
695 (get-cast-long data-ptr))
697 (#.$SQL_C_BIT ; encountered only in Access
698 (get-cast-byte data-ptr))
700 (get-cast-binary data-ptr out-len *binary-format*))
701 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
702 (get-cast-short data-ptr)) ; LMH
705 (code-char (get-cast-short data-ptr)))
707 (get-cast-foreign-string data-ptr)))))))))
709 ;; FIXME: this could be better optimized for types which use READ-FROM-STRING above
711 (if (and (or (eq result-type t) (eq result-type :string))
713 (not (stringp value)))
714 (write-to-string value)
717 ;; which value is appropriate?
718 (defparameter +max-precision+ 4001)
720 (defvar *break-on-unknown-data-type* t)
722 ;; C. Stacy's idea to factor this out
723 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
724 ;; so that I don't have to remember to make changes in more than one place.
725 ;; Just keep it in synch with READ-DATA."
726 (defun %allocate-bindings (sql-type precision)
727 (let* ((c-type (sql-to-c-type sql-type))
728 (size (if (zerop precision)
729 +max-precision+ ;; if the precision cannot be determined
730 (min precision +max-precision+)))
731 (long-p (= size +max-precision+))
733 (case c-type ;; add more?
734 (#.$SQL_C_SLONG (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE))
735 ((#.$SQL_C_DATE #.$SQL_C_TYPE_DATE) (allocate-foreign-object 'sql-c-date))
736 ((#.$SQL_C_TIME #.$SQL_C_TYPE_TIME) (allocate-foreign-object 'sql-c-time))
737 ((#.$SQL_C_TIMESTAMP #.$SQL_C_TYPE_TIMESTAMP) (allocate-foreign-object 'sql-c-timestamp))
738 (#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
739 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
740 (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
741 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
742 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
743 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
744 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
746 ;; Maybe should signal a restartable condition for this?
747 (when *break-on-unknown-data-type*
748 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
749 sql-type precision size c-type))
750 (uffi:allocate-foreign-object :byte (1+ size)))))
751 (out-len-ptr (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE)))
752 (values c-type data-ptr out-len-ptr size long-p)))
754 (defun fetch-all-rows (hstmt &key free-option flatp)
755 (let ((column-count (result-columns-count hstmt)))
756 (unless (zerop column-count)
757 (let ((names (make-array column-count))
758 (sql-types (make-array column-count :element-type 'fixnum))
759 (c-types (make-array column-count :element-type 'fixnum))
760 (precisions (make-array column-count :element-type 'fixnum))
761 (data-ptrs (make-array column-count :initial-element nil))
762 (out-len-ptrs (make-array column-count :initial-element nil))
763 (scales (make-array column-count :element-type 'fixnum))
764 (nullables-p (make-array column-count :element-type 'fixnum)))
768 (dotimes (col-nr column-count)
769 ;; get column information
770 (multiple-value-bind (name sql-type precision scale nullable-p)
771 (%describe-column hstmt (1+ col-nr))
772 ;; allocate space to bind result rows to
773 (multiple-value-bind (c-type data-ptr out-len-ptr)
774 (%allocate-bindings sql-type precision)
775 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
776 (setf (svref names col-nr) name
777 (aref sql-types col-nr) sql-type
778 (aref c-types col-nr) (sql-to-c-type sql-type)
779 (aref precisions col-nr) (if (zerop precision) 0 precision)
780 (aref scales col-nr) scale
781 (aref nullables-p col-nr) nullable-p
782 (aref data-ptrs col-nr) data-ptr
783 (aref out-len-ptrs col-nr) out-len-ptr))))
787 (when (> column-count 1)
788 (error 'clsql:sql-database-error
789 :message "If more than one column is to be fetched, flatp has to be nil."))
790 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
792 (read-data (aref data-ptrs 0)
795 (aref out-len-ptrs 0)
798 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
800 (loop for col-nr from 0 to (1- column-count)
802 (read-data (aref data-ptrs col-nr)
803 (aref c-types col-nr)
804 (aref sql-types col-nr)
805 (aref out-len-ptrs col-nr)
808 ;; dispose of memory etc
809 (when free-option (%free-statement hstmt free-option))
810 (dotimes (col-nr column-count)
811 (let ((data-ptr (aref data-ptrs col-nr))
812 (out-len-ptr (aref out-len-ptrs col-nr)))
813 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
814 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
816 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
817 ;; of %do-fetch into sql package (has been done)
819 (defun %sql-prepare (hstmt sql)
820 (with-cstring (sql-ptr sql)
821 (with-error-handling (:hstmt hstmt)
822 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
824 ;; depending on option, we return a long int or a string; string not implemented
825 (defun get-connection-option (hdbc option)
826 (with-foreign-object (param-ptr #.$ODBC-LONG-TYPE)
827 (with-error-handling (:hdbc hdbc)
828 (SQLGetConnectOption hdbc option param-ptr)
829 (deref-pointer param-ptr #.$ODBC-LONG-TYPE))))
831 (defun set-connection-option (hdbc option param)
832 (with-error-handling (:hdbc hdbc)
833 (SQLSetConnectOption hdbc option param)))
835 (defun disable-autocommit (hdbc)
836 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
838 (defun enable-autocommit (hdbc)
839 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
841 (defun %sql-set-pos (hstmt row option lock)
844 (SQLSetPos hstmt row option lock)))
846 (defun %sql-extended-fetch (hstmt fetch-type row)
847 (with-foreign-objects ((row-count-ptr #.$ODBC-ULONG-TYPE)
848 (row-status-ptr :short))
849 (with-error-handling (:hstmt hstmt)
850 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
852 (values (deref-pointer row-count-ptr #.$ODBC-ULONG-TYPE)
853 (deref-pointer row-status-ptr :short)))))
855 ; column-nr is zero-based
856 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
858 (:hstmt hstmt :print-info nil)
859 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
860 c-type data-ptr precision out-len-ptr)))
862 (defun %sql-param-data (hstmt param-ptr)
863 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
864 (SQLParamData hstmt param-ptr)))
866 (defun %sql-put-data (hstmt data-ptr size)
868 (:hstmt hstmt :print-info t) ;; nil
869 (SQLPutData hstmt data-ptr size)))
871 (defconstant $sql-data-truncated (intern "01004" :keyword))
873 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
874 out-len-ptr result-type)
875 (declare (type long-ptr-type out-len-ptr)
876 (ignore result-type))
877 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
878 +max-precision+ out-len-ptr))
879 (out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
881 (result (case out-len
883 (return-from read-data-in-chunks *null*))
884 (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
885 (let ((str (make-array 0 :element-type 'character :adjustable t)))
886 (loop do (if (= c-type #.$SQL_CHAR)
887 (let ((data-length (foreign-string-length data-ptr)))
888 (adjust-array str (+ offset data-length)
889 :initial-element #\?)
890 (setf offset (%cstring-into-vector
894 (error 'clsql:sql-database-error :message "wrong type. preliminary."))
895 while (and (= res $SQL_SUCCESS_WITH_INFO)
896 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
898 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
899 +max-precision+ out-len-ptr)))
900 (setf str (coerce str 'string))
901 (if (= sql-type $SQL_DECIMAL)
902 (let ((*read-base* 10))
903 (read-from-string str))
908 (octets (make-array out-len :element-type '(unsigned-byte 8) :initial-element 0)))
911 (loop for i from 0 to (1- (min out-len +max-precision+))
912 do (setf (aref octets (+ offset i)) (deref-array data-ptr '(:array :unsigned-byte) i))
913 finally (incf offset (1- i)))
915 (and (= res $SQL_SUCCESS_WITH_INFO)
916 #+ingore(eq (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
918 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt) "01004"))
920 (setf res (%sql-get-data hstmt column-nr c-type data-ptr +max-precision+ out-len-ptr)
921 out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE)))
922 (setf str (sb-ext:octets-to-string octets))
923 (if (= sql-type $SQL_DECIMAL)
924 (let ((*read-base* 10))
925 (read-from-string str))
927 (setf (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE) #.$SQL_NO_TOTAL) ;; reset the out length for the next row
930 (def-type c-timestamp-ptr-type (* (:struct sql-c-timestamp)))
931 (def-type c-time-ptr-type (* (:struct sql-c-time)))
932 (def-type c-date-ptr-type (* (:struct sql-c-date)))
934 (defun timestamp-to-universal-time (ptr)
935 (declare (type c-timestamp-ptr-type ptr))
937 (encode-universal-time
938 (get-slot-value ptr 'sql-c-timestamp 'second)
939 (get-slot-value ptr 'sql-c-timestamp 'minute)
940 (get-slot-value ptr 'sql-c-timestamp 'hour)
941 (get-slot-value ptr 'sql-c-timestamp 'day)
942 (get-slot-value ptr 'sql-c-timestamp 'month)
943 (get-slot-value ptr 'sql-c-timestamp 'year))
944 (get-slot-value ptr 'sql-c-timestamp 'fraction)))
946 (defun universal-time-to-timestamp (time &optional (fraction 0))
947 (multiple-value-bind (sec min hour day month year)
948 (decode-universal-time time)
949 (let ((ptr (allocate-foreign-object 'sql-c-timestamp)))
950 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
951 (get-slot-value ptr 'sql-c-timestamp 'minute) min
952 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
953 (get-slot-value ptr 'sql-c-timestamp 'day) day
954 (get-slot-value ptr 'sql-c-timestamp 'month) month
955 (get-slot-value ptr 'sql-c-timestamp 'year) year
956 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
959 (defun %put-timestamp (ptr time &optional (fraction 0))
960 (declare (type c-timestamp-ptr-type ptr))
961 (multiple-value-bind (sec min hour day month year)
962 (decode-universal-time time)
963 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
964 (get-slot-value ptr 'sql-c-timestamp 'minute) min
965 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
966 (get-slot-value ptr 'sql-c-timestamp 'day) day
967 (get-slot-value ptr 'sql-c-timestamp 'month) month
968 (get-slot-value ptr 'sql-c-timestamp 'year) year
969 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
972 (defun date-to-universal-time (ptr)
973 (declare (type c-date-ptr-type ptr))
974 (encode-universal-time
976 (get-slot-value ptr 'sql-c-timestamp 'day)
977 (get-slot-value ptr 'sql-c-timestamp 'month)
978 (get-slot-value ptr 'sql-c-timestamp 'year)))
980 (defun time-to-universal-time (ptr)
981 (declare (type c-time-ptr-type ptr))
982 (encode-universal-time
983 (get-slot-value ptr 'sql-c-timestamp 'second)
984 (get-slot-value ptr 'sql-c-timestamp 'minute)
985 (get-slot-value ptr 'sql-c-timestamp 'hour)
991 (defun %set-attr-odbc-version (henv version)
992 (with-error-handling (:henv henv)
993 (SQLSetEnvAttr henv $SQL_ATTR_ODBC_VERSION
994 (make-pointer version :void) 0)))
996 (defun %list-tables (hstmt)
997 (with-error-handling (:hstmt hstmt)
998 (SQLTables hstmt +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0)))
1000 (defun %table-statistics (table hstmt &key unique (ensure t))
1001 (with-cstrings ((table-cs table))
1002 (with-error-handling (:hstmt hstmt)
1008 (if unique $SQL_INDEX_UNIQUE $SQL_INDEX_ALL)
1009 (if ensure $SQL_ENSURE $SQL_QUICK)))))
1011 (defun %list-data-sources (henv)
1012 (let ((dsn (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
1013 (desc (allocate-foreign-string 256))
1016 (with-foreign-objects ((dsn-len :short)
1018 (let ((res (with-error-handling (:henv henv)
1019 (SQLDataSources henv $SQL_FETCH_FIRST dsn
1020 (1+ $SQL_MAX_DSN_LENGTH)
1021 dsn-len desc 256 desc-len))))
1022 (when (or (eql res $SQL_SUCCESS)
1023 (eql res $SQL_SUCCESS_WITH_INFO))
1024 (push (convert-from-foreign-string dsn) results))
1026 (do ((res (with-error-handling (:henv henv)
1027 (SQLDataSources henv $SQL_FETCH_NEXT dsn
1028 (1+ $SQL_MAX_DSN_LENGTH)
1029 dsn-len desc 256 desc-len))
1030 (with-error-handling (:henv henv)
1031 (SQLDataSources henv $SQL_FETCH_NEXT dsn
1032 (1+ $SQL_MAX_DSN_LENGTH)
1033 dsn-len desc 256 desc-len))))
1034 ((not (or (eql res $SQL_SUCCESS)
1035 (eql res $SQL_SUCCESS_WITH_INFO))))
1036 (push (convert-from-foreign-string dsn) results))))
1038 (free-foreign-object dsn)
1039 (free-foreign-object desc)))
1040 (nreverse results)))