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 ;;;; $Id: odbc-package.lisp 7061 2003-09-07 06:34:45Z kevin $
11 ;;;; This file, part of CLSQL, is Copyright (c) 2004 by Kevin M. Rosenberg
12 ;;;; and Copyright (C) Paul Meurer 1999 - 2001. All rights reserved.
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
22 "Lisp representation of SQL Null value, default = nil.
23 May be locally bound to something else if a certain type is necessary.")
26 (defvar *binary-format* :unsigned-byte-vector)
27 (defvar *time-conversion-function*
28 (lambda (universal-time &optional fraction)
29 (declare (ignore fraction))
30 (clsql-sys:format-time
31 nil (clsql-sys:utime->time universal-time)
35 "Bound to a function that converts from a Lisp universal time fixnum (and a fractional
36 as possible second argument) to the desired representation of date/time/timestamp. By default, returns an iso-timestring.")
38 (defvar +null-ptr+ (make-null-pointer :byte))
39 (defparameter +null-handle-ptr+ (make-null-pointer :void))
40 (defvar *info-output* nil
41 "Stream to send SUCCESS_WITH_INFO messages.")
43 (defmacro %put-str (ptr string &optional max-length)
44 (let ((size (gensym)))
45 `(let ((,size (length ,string)))
46 (when (and ,max-length (> ,size ,max-length))
47 (error "string \"~a\" of length ~d is longer than max-length: ~d"
48 ,string ,size ,max-length))
49 (with-cast-pointer (char-ptr ,ptr :byte)
51 (setf (deref-array char-ptr '(:array :byte) i)
52 (char-code (char ,string i))))
53 (setf (deref-array char-ptr '(:array :byte) ,size) 0)))))
55 (defun %cstring-into-vector (ptr vector offset size-in-bytes)
56 (dotimes (i size-in-bytes)
57 (setf (schar vector offset)
58 (ensure-char-character
59 (deref-array ptr '(:array :unsigned-char) i)))
63 (defun handle-error (henv hdbc hstmt)
64 (let ((sql-state (allocate-foreign-string 256))
65 (error-message (allocate-foreign-string #.$SQL_MAX_MESSAGE_LENGTH)))
66 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
68 (SQLError henv hdbc hstmt sql-state
69 error-code error-message
70 #.$SQL_MAX_MESSAGE_LENGTH msg-length)
71 (let ((err (convert-from-foreign-string error-message))
72 (state (convert-from-foreign-string sql-state)))
73 (free-foreign-object error-message)
74 (free-foreign-object sql-state)
78 (deref-pointer msg-length :short)
79 (deref-pointer error-code #.$ODBC-LONG-TYPE))))))
81 (defun sql-state (henv hdbc hstmt)
82 (let ((sql-state (allocate-foreign-string 256))
83 (error-message (allocate-foreign-string #.$SQL_MAX_MESSAGE_LENGTH)))
84 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
86 (SQLError henv hdbc hstmt sql-state error-code
87 error-message #.$SQL_MAX_MESSAGE_LENGTH msg-length)
88 (let ((state (convert-from-foreign-string sql-state)))
89 (free-foreign-object error-message)
90 (free-foreign-object sql-state)
92 ;; test this: return a keyword for efficiency
93 ;;(%cstring-to-keyword state)
96 (defmacro with-error-handling ((&key henv hdbc hstmt (print-info t))
98 (let ((result-code (gensym "RC-")))
99 `(let ((,result-code ,odbc-call))
102 (progn ,result-code ,@body))
103 (#.$SQL_SUCCESS_WITH_INFO
105 (multiple-value-bind (error-message sql-state)
106 (handle-error (or ,henv +null-handle-ptr+)
107 (or ,hdbc +null-handle-ptr+)
108 (or ,hstmt +null-handle-ptr+))
110 (format *info-output* "[ODBC info ~A] ~A state: ~A"
111 ,result-code error-message
113 (progn ,result-code ,@body))
114 (#.$SQL_INVALID_HANDLE
116 'clsql-sys:sql-database-error
117 :message "ODBC: Invalid handle"))
118 (#.$SQL_STILL_EXECUTING
120 'clsql-sys:sql-temporary-error
121 :message "ODBC: Still executing"))
123 (multiple-value-bind (error-message sql-state)
124 (handle-error (or ,henv +null-handle-ptr+)
125 (or ,hdbc +null-handle-ptr+)
126 (or ,hstmt +null-handle-ptr+))
128 'clsql-sys:sql-database-error
129 :message error-message
130 :sql-state sql-state)))
131 (#.$SQL_NO_DATA_FOUND
132 (progn ,result-code ,@body))
133 ;; work-around for Allegro 7.0beta AMD64 which
134 ;; has for negative numbers
136 (multiple-value-bind (error-message sql-state)
137 (handle-error (or ,henv +null-handle-ptr+)
138 (or ,hdbc +null-handle-ptr+)
139 (or ,hstmt +null-handle-ptr+))
141 'clsql-sys:sql-database-error
142 :message error-message
143 :secondary-error-id sql-state))
145 (progn ,result-code ,@body))))))
147 (defun %new-environment-handle ()
149 (with-foreign-object (phenv 'sql-handle)
153 (deref-pointer phenv 'sql-handle)))))
154 (%set-attr-odbc-version henv $SQL_OV_ODBC2)
158 (defun %sql-free-environment (henv)
163 (defun %new-db-connection-handle (henv)
164 (with-foreign-object (phdbc 'sql-handle)
167 (SQLAllocConnect henv phdbc)
168 (deref-pointer phdbc 'sql-handle))))
170 (defun %free-statement (hstmt option)
178 (:unbind $SQL_UNBIND)
179 (:reset $SQL_RESET_PARAMS)))))
181 (defmacro with-statement-handle ((hstmt hdbc) &body body)
182 `(let ((,hstmt (%new-statement-handle ,hdbc)))
185 (%free-statement ,hstmt :drop))))
187 ;; functional interface
189 (defun %sql-connect (hdbc server uid pwd)
190 (with-cstrings ((server-ptr server)
195 (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr
196 $SQL_NTS pwd-ptr $SQL_NTS))))
199 (defun %disconnect (hdbc)
202 (SQLDisconnect hdbc)))
204 (defun %commit (henv hdbc)
206 (:henv henv :hdbc hdbc)
208 henv hdbc $SQL_COMMIT)))
210 (defun %rollback (henv hdbc)
212 (:henv henv :hdbc hdbc)
214 henv hdbc $SQL_ROLLBACK)))
216 ; col-nr is zero-based in Lisp
217 ; col-nr = :bookmark retrieves a bookmark.
218 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
222 (if (eq column-nr :bookmark) 0 (1+ column-nr))
223 c-type data-ptr precision out-len-ptr)))
225 ; parameter-nr is zero-based in Lisp
226 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
227 sql-type precision scale data-ptr
228 max-value out-len-ptr)
231 (SQLBindParameter hstmt (1+ parameter-nr)
232 parameter-type ;$SQL_PARAM_INPUT
234 sql-type ;$SQL_VARCHAR
235 precision ;(1- (length str))
239 out-len-ptr ;#.+null-ptr+
242 (defun %sql-fetch (hstmt)
247 (defun %new-statement-handle (hdbc)
248 (let ((statement-handle
249 (with-foreign-object (hstmt-ptr 'sql-handle)
252 (SQLAllocStmt hdbc hstmt-ptr)
253 (deref-pointer hstmt-ptr 'sql-handle)))))
254 (if (uffi:null-pointer-p statement-handle)
255 (error "Received null statement handle.")
258 (defun %sql-get-info (hdbc info-type)
260 ;; those return string
261 ((#.$SQL_ACCESSIBLE_PROCEDURES
262 #.$SQL_ACCESSIBLE_TABLES
264 #.$SQL_DATA_SOURCE_NAME
265 #.$SQL_DATA_SOURCE_READ_ONLY
269 #.$SQL_DRIVER_ODBC_VER
271 #.$SQL_EXPRESSIONS_IN_ORDERBY
272 #.$SQL_IDENTIFIER_QUOTE_CHAR
274 #.$SQL_LIKE_ESCAPE_CLAUSE
275 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
276 #.$SQL_MULT_RESULT_SETS
277 #.$SQL_MULTIPLE_ACTIVE_TXN
278 #.$SQL_NEED_LONG_DATA_LEN
279 #.$SQL_ODBC_SQL_OPT_IEF
281 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
284 #.$SQL_PROCEDURE_TERM
286 #.$SQL_QUALIFIER_NAME_SEPARATOR
287 #.$SQL_QUALIFIER_TERM
289 #.$SQL_SEARCH_PATTERN_ESCAPE
291 #.$SQL_SPECIAL_CHARACTERS
294 (let ((info-ptr (allocate-foreign-string 1024)))
295 (with-foreign-object (info-length-ptr :short)
298 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
299 (let ((info (convert-from-foreign-string info-ptr)))
300 (free-foreign-object info-ptr)
302 ;; those returning a word
303 ((#.$SQL_ACTIVE_CONNECTIONS
304 #.$SQL_ACTIVE_STATEMENTS
305 #.$SQL_CONCAT_NULL_BEHAVIOR
306 #.$SQL_CORRELATION_NAME
307 #.$SQL_CURSOR_COMMIT_BEHAVIOR
308 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
309 #.$SQL_MAX_COLUMN_NAME_LEN
310 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
311 #.$SQL_MAX_COLUMNS_IN_INDEX
312 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
313 #.$SQL_MAX_COLUMNS_IN_SELECT
314 #.$SQL_MAX_COLUMNS_IN_TABLE
315 #.$SQL_MAX_CURSOR_NAME_LEN
316 #.$SQL_MAX_OWNER_NAME_LEN
317 #.$SQL_MAX_PROCEDURE_NAME_LEN
318 #.$SQL_MAX_QUALIFIER_NAME_LEN
319 #.$SQL_MAX_TABLE_NAME_LEN
320 #.$SQL_MAX_TABLES_IN_SELECT
321 #.$SQL_MAX_USER_NAME_LEN
322 #.$SQL_NON_NULLABLE_COLUMNS
323 #.$SQL_NULL_COLLATION
324 #.$SQL_ODBC_API_CONFORMANCE
325 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
326 #.$SQL_ODBC_SQL_CONFORMANCE
327 #.$SQL_QUALIFIER_LOCATION
328 #.$SQL_QUOTED_IDENTIFIER_CASE
330 (with-foreign-objects ((info-ptr :short)
331 (info-length-ptr :short))
339 (deref-pointer info-ptr :short)))
341 ;; those returning a long bitmask
343 #.$SQL_BOOKMARK_PERSISTENCE
344 #.$SQL_CONVERT_BIGINT
345 #.$SQL_CONVERT_BINARY
349 #.$SQL_CONVERT_DECIMAL
350 #.$SQL_CONVERT_DOUBLE
352 #.$SQL_CONVERT_INTEGER
353 #.$SQL_CONVERT_LONGVARCHAR
354 #.$SQL_CONVERT_NUMERIC
356 #.$SQL_CONVERT_SMALLINT
358 #.$SQL_CONVERT_TIMESTAMP
359 #.$SQL_CONVERT_TINYINT
360 #.$SQL_CONVERT_VARBINARY
361 #.$SQL_CONVERT_VARCHAR
362 #.$SQL_CONVERT_LONGVARBINARY
363 #.$SQL_CONVERT_FUNCTIONS
364 #.$SQL_FETCH_DIRECTION
366 #.$SQL_GETDATA_EXTENSIONS
368 #.$SQL_MAX_INDEX_SIZE
370 #.$SQL_MAX_STATEMENT_LEN
371 #.$SQL_NUMERIC_FUNCTIONS
373 #.$SQL_POS_OPERATIONS
374 #.$SQL_POSITIONED_STATEMENTS
375 #.$SQL_QUALIFIER_USAGE
376 #.$SQL_SCROLL_CONCURRENCY
377 #.$SQL_SCROLL_OPTIONS
378 #.$SQL_STATIC_SENSITIVITY
379 #.$SQL_STRING_FUNCTIONS
381 #.$SQL_SYSTEM_FUNCTIONS
382 #.$SQL_TIMEDATE_ADD_INTERVALS
383 #.$SQL_TIMEDATE_DIFF_INTERVALS
384 #.$SQL_TIMEDATE_FUNCTIONS
385 #.$SQL_TXN_ISOLATION_OPTION
387 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
388 (info-length-ptr :short))
396 (deref-pointer info-ptr #.$ODBC-LONG-TYPE)))
398 ;; those returning a long integer
399 ((#.$SQL_DEFAULT_TXN_ISOLATION
405 #.$SQL_IDENTIFIER_CASE
406 #.$SQL_MAX_BINARY_LITERAL_LEN
407 #.$SQL_MAX_CHAR_LITERAL_LEN
408 #.$SQL_ACTIVE_ENVIRONMENTS
410 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
411 (info-length-ptr :short))
414 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
415 (deref-pointer info-ptr #.$ODBC-LONG-TYPE))))))
417 (defun %sql-exec-direct (sql hstmt henv hdbc)
418 (with-cstring (sql-ptr sql)
420 (:hstmt hstmt :henv henv :hdbc hdbc)
421 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
423 (defun %sql-cancel (hstmt)
428 (defun %sql-execute (hstmt)
433 (defun result-columns-count (hstmt)
434 (with-foreign-objects ((columns-nr-ptr :short))
435 (with-error-handling (:hstmt hstmt)
436 (SQLNumResultCols hstmt columns-nr-ptr)
437 (deref-pointer columns-nr-ptr :short))))
439 (defun result-rows-count (hstmt)
440 (with-foreign-objects ((row-count-ptr #.$ODBC-LONG-TYPE))
441 (with-error-handling (:hstmt hstmt)
442 (SQLRowCount hstmt row-count-ptr)
443 (deref-pointer row-count-ptr #.$ODBC-LONG-TYPE))))
445 ;; column counting is 1-based
446 (defun %describe-column (hstmt column-nr)
447 (let ((column-name-ptr (allocate-foreign-string 256)))
448 (with-foreign-objects ((column-name-length-ptr :short)
449 (column-sql-type-ptr :short)
450 (column-precision-ptr #.$ODBC-ULONG-TYPE)
451 (column-scale-ptr :short)
452 (column-nullable-p-ptr :short))
453 (with-error-handling (:hstmt hstmt)
454 (SQLDescribeCol hstmt column-nr column-name-ptr 256
455 column-name-length-ptr
459 column-nullable-p-ptr)
460 (let ((column-name (convert-from-foreign-string column-name-ptr)))
461 (free-foreign-object column-name-ptr)
464 (deref-pointer column-sql-type-ptr :short)
465 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
466 (deref-pointer column-scale-ptr :short)
467 (deref-pointer column-nullable-p-ptr :short)))))))
469 ;; parameter counting is 1-based
470 (defun %describe-parameter (hstmt parameter-nr)
471 (with-foreign-objects ((column-sql-type-ptr :short)
472 (column-precision-ptr #.$ODBC-ULONG-TYPE)
473 (column-scale-ptr :short)
474 (column-nullable-p-ptr :short))
477 (SQLDescribeParam hstmt parameter-nr
481 column-nullable-p-ptr)
483 (deref-pointer column-sql-type-ptr :short)
484 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
485 (deref-pointer column-scale-ptr :short)
486 (deref-pointer column-nullable-p-ptr :short)))))
488 (defun %column-attributes (hstmt column-nr descriptor-type)
489 (let ((descriptor-info-ptr (allocate-foreign-string 256)))
490 (with-foreign-objects ((descriptor-length-ptr :short)
491 (numeric-descriptor-ptr #.$ODBC-LONG-TYPE))
494 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr
495 256 descriptor-length-ptr
496 numeric-descriptor-ptr)
497 (let ((desc (convert-from-foreign-string descriptor-info-ptr)))
498 (free-foreign-object descriptor-info-ptr)
501 (deref-pointer numeric-descriptor-ptr #.$ODBC-LONG-TYPE)))))))
503 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
504 table-name column-name)
505 (with-cstrings ((table-qualifier-ptr table-qualifier)
506 (table-owner-ptr table-owner)
507 (table-name-ptr table-name)
508 (column-name-ptr column-name))
512 table-qualifier-ptr (length table-qualifier)
513 table-owner-ptr (length table-owner)
514 table-name-ptr (length table-name)
515 column-name-ptr (length column-name)))))
517 (defun %describe-columns (hdbc table-qualifier table-owner
518 table-name column-name)
519 (with-statement-handle (hstmt hdbc)
520 (%prepare-describe-columns hstmt table-qualifier table-owner
521 table-name column-name)
522 (fetch-all-rows hstmt)))
524 (defun %sql-data-sources (henv &key (direction :first))
525 (let ((name-ptr (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
526 (description-ptr (allocate-foreign-string 1024)))
527 (with-foreign-objects ((name-length-ptr :short)
528 (description-length-ptr :short))
529 (let ((res (with-error-handling
533 (:first $SQL_FETCH_FIRST)
534 (:next $SQL_FETCH_NEXT))
536 (1+ $SQL_MAX_DSN_LENGTH)
540 description-length-ptr))))
542 ((= res $SQL_NO_DATA_FOUND)
543 (let ((name (convert-from-foreign-string name-ptr))
544 (desc (convert-from-foreign-string description-ptr)))
545 (free-foreign-object name-ptr)
546 (free-foreign-object description-ptr)
551 (free-foreign-object name-ptr)
552 (free-foreign-object description-ptr)
557 (defun sql-to-c-type (sql-type)
559 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
560 #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9) $SQL_C_CHAR)
561 (#.$SQL_INTEGER $SQL_C_SLONG)
562 (#.$SQL_SMALLINT $SQL_C_SSHORT)
563 (#.$SQL_DOUBLE $SQL_C_DOUBLE)
564 (#.$SQL_FLOAT $SQL_C_DOUBLE)
565 (#.$SQL_REAL $SQL_C_FLOAT)
566 (#.$SQL_DATE $SQL_C_DATE)
567 (#.$SQL_TIME $SQL_C_TIME)
568 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
569 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
570 (#.$SQL_TINYINT $SQL_C_STINYINT)
571 (#.$SQL_BIT $SQL_C_BIT)))
573 (def-type byte-pointer-type '(* :byte))
574 (def-type short-pointer-type '(* :short))
575 (def-type int-pointer-type '(* :int))
576 (def-type long-pointer-type '(* #.$ODBC-LONG-TYPE))
577 (def-type float-pointer-type '(* :float))
578 (def-type double-pointer-type '(* :double))
579 (def-type string-pointer-type '(* :unsigned-char))
581 (defun get-cast-byte (ptr)
582 (locally (declare (type byte-pointer-type ptr))
583 (deref-pointer ptr :byte)))
585 (defun get-cast-short (ptr)
586 (locally (declare (type short-pointer-type ptr))
587 (deref-pointer ptr :short)))
589 (defun get-cast-int (ptr)
590 (locally (declare (type int-pointer-type ptr))
591 (deref-pointer ptr :int)))
593 (defun get-cast-long (ptr)
594 (locally (declare (type long-pointer-type ptr))
595 (deref-pointer ptr #.$ODBC-LONG-TYPE)))
597 (defun get-cast-single-float (ptr)
598 (locally (declare (type float-pointer-type ptr))
599 (deref-pointer ptr :float)))
601 (defun get-cast-double-float (ptr)
602 (locally (declare (type double-pointer-type ptr))
603 (deref-pointer ptr :double)))
605 (defun get-cast-foreign-string (ptr)
606 (locally (declare (type string-pointer-type ptr))
607 (convert-from-foreign-string ptr)))
609 (defun get-cast-binary (ptr len format)
610 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
611 (with-cast-pointer (casted ptr :byte)
613 (:unsigned-byte-vector
614 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
616 (setf (aref vector i)
617 (deref-array casted '(:array :byte) i)))
620 (let ((vector (make-array (ash len 3) :element-type 'bit)))
622 (let ((byte (deref-array casted '(:array :byte) i)))
624 (setf (bit vector (+ (ash i 3) j))
625 (logand (ash byte (- j 7)) 1)))))
629 (defun read-data (data-ptr c-type sql-type out-len-ptr result-type)
630 (declare (type long-ptr-type out-len-ptr))
631 (let* ((out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
633 (cond ((= out-len $SQL_NULL_DATA)
637 ;; SQL extended datatypes
638 (#.$SQL_TINYINT (get-cast-byte data-ptr))
639 (#.$SQL_C_STINYINT (get-cast-byte data-ptr)) ;; ?
640 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
641 (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
642 (#.$SQL_INTEGER (get-cast-int data-ptr))
643 (#.$SQL_BIGINT (read-from-string
644 (get-cast-foreign-string data-ptr)))
645 (#.$SQL_TINYINT (get-cast-byte data-ptr))
647 (let ((*read-base* 10))
648 (read-from-string (get-cast-foreign-string data-ptr))))
652 (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
654 (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
655 (funcall *time-conversion-function* universal-time frac)))
657 (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
658 (funcall *time-conversion-function* universal-time frac)))
660 (get-cast-int data-ptr))
662 (get-cast-single-float data-ptr))
664 (get-cast-double-float data-ptr))
666 (get-cast-long data-ptr))
668 (#.$SQL_C_BIT ; encountered only in Access
669 (get-cast-byte data-ptr))
671 (get-cast-binary data-ptr out-len *binary-format*))
672 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
673 (get-cast-short data-ptr)) ; LMH
676 (code-char (get-cast-short data-ptr)))
678 (get-cast-foreign-string data-ptr)))))))))
680 ;; FIXME: this could be better optimized for types which use READ-FROM-STRING above
682 (if (and (or (eq result-type t) (eq result-type :string))
684 (not (stringp value)))
685 (write-to-string value)
688 ;; which value is appropriate?
689 (defparameter +max-precision+ 4001)
691 (defvar *break-on-unknown-data-type* t)
693 ;; C. Stacy's idea to factor this out
694 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
695 ;; so that I don't have to remember to make changes in more than one place.
696 ;; Just keep it in synch with READ-DATA."
697 (defun %allocate-bindings (sql-type precision)
698 (let* ((c-type (sql-to-c-type sql-type))
699 (size (if (zerop precision)
700 +max-precision+ ;; if the precision cannot be determined
701 (min precision +max-precision+)))
702 (long-p (= size +max-precision+))
704 (case c-type ;; add more?
705 (#.$SQL_C_SLONG (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE))
706 (#.$SQL_C_DATE (allocate-foreign-object 'sql-c-date))
707 (#.$SQL_C_TIME (allocate-foreign-object 'sql-c-time))
708 (#.$SQL_C_TIMESTAMP (allocate-foreign-object 'sql-c-timestamp))
709 (#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
710 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
711 (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
712 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
713 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
714 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
715 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
717 ;; Maybe should signal a restartable condition for this?
718 (when *break-on-unknown-data-type*
719 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
720 sql-type precision size c-type))
721 (uffi:allocate-foreign-object :byte (1+ size)))))
722 (out-len-ptr (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE)))
723 (values c-type data-ptr out-len-ptr size long-p)))
725 (defun fetch-all-rows (hstmt &key free-option flatp)
726 (let ((column-count (result-columns-count hstmt)))
727 (unless (zerop column-count)
728 (let ((names (make-array column-count))
729 (sql-types (make-array column-count :element-type 'fixnum))
730 (c-types (make-array column-count :element-type 'fixnum))
731 (precisions (make-array column-count :element-type 'fixnum))
732 (data-ptrs (make-array column-count :initial-element nil))
733 (out-len-ptrs (make-array column-count :initial-element nil))
734 (scales (make-array column-count :element-type 'fixnum))
735 (nullables-p (make-array column-count :element-type 'fixnum)))
739 (dotimes (col-nr column-count)
740 ;; get column information
741 (multiple-value-bind (name sql-type precision scale nullable-p)
742 (%describe-column hstmt (1+ col-nr))
743 ;; allocate space to bind result rows to
744 (multiple-value-bind (c-type data-ptr out-len-ptr)
745 (%allocate-bindings sql-type precision)
746 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
747 (setf (svref names col-nr) name
748 (aref sql-types col-nr) sql-type
749 (aref c-types col-nr) (sql-to-c-type sql-type)
750 (aref precisions col-nr) (if (zerop precision) 0 precision)
751 (aref scales col-nr) scale
752 (aref nullables-p col-nr) nullable-p
753 (aref data-ptrs col-nr) data-ptr
754 (aref out-len-ptrs col-nr) out-len-ptr))))
758 (when (> column-count 1)
759 (error "If more than one column is to be fetched, flatp has to be nil."))
760 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
762 (read-data (aref data-ptrs 0)
765 (aref out-len-ptrs 0)
768 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
770 (loop for col-nr from 0 to (1- column-count)
772 (read-data (aref data-ptrs col-nr)
773 (aref c-types col-nr)
774 (aref sql-types col-nr)
775 (aref out-len-ptrs col-nr)
778 ;; dispose of memory etc
779 (when free-option (%free-statement hstmt free-option))
780 (dotimes (col-nr column-count)
781 (let ((data-ptr (aref data-ptrs col-nr))
782 (out-len-ptr (aref out-len-ptrs col-nr)))
783 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
784 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
786 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
787 ;; of %do-fetch into sql package (has been done)
789 (defun %sql-prepare (hstmt sql)
790 (with-cstring (sql-ptr sql)
791 (with-error-handling (:hstmt hstmt)
792 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
794 ;; depending on option, we return a long int or a string; string not implemented
795 (defun get-connection-option (hdbc option)
796 (with-foreign-object (param-ptr #.$ODBC-LONG-TYPE)
797 (with-error-handling (:hdbc hdbc)
798 (SQLGetConnectOption hdbc option param-ptr)
799 (deref-pointer param-ptr #.$ODBC-LONG-TYPE))))
801 (defun set-connection-option (hdbc option param)
802 (with-error-handling (:hdbc hdbc)
803 (SQLSetConnectOption hdbc option param)))
805 (defun disable-autocommit (hdbc)
806 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
808 (defun enable-autocommit (hdbc)
809 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
811 (defun %sql-set-pos (hstmt row option lock)
814 (SQLSetPos hstmt row option lock)))
816 (defun %sql-extended-fetch (hstmt fetch-type row)
817 (with-foreign-objects ((row-count-ptr #.$ODBC-ULONG-TYPE)
818 (row-status-ptr :short))
819 (with-error-handling (:hstmt hstmt)
820 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
822 (values (deref-pointer row-count-ptr #.$ODBC-ULONG-TYPE)
823 (deref-pointer row-status-ptr :short)))))
825 ; column-nr is zero-based
826 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
828 (:hstmt hstmt :print-info nil)
829 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
830 c-type data-ptr precision out-len-ptr)))
832 (defun %sql-param-data (hstmt param-ptr)
833 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
834 (SQLParamData hstmt param-ptr)))
836 (defun %sql-put-data (hstmt data-ptr size)
838 (:hstmt hstmt :print-info t) ;; nil
839 (SQLPutData hstmt data-ptr size)))
841 (defconstant $sql-data-truncated (intern "01004" :keyword))
843 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
844 out-len-ptr result-type)
845 (declare (type long-ptr-type out-len-ptr))
846 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
847 +max-precision+ out-len-ptr))
848 (out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
852 (return-from read-data-in-chunks *null*))
853 (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
854 (let ((str (make-array 0 :element-type 'character :adjustable t)))
855 (loop do (if (= c-type #.$SQL_CHAR)
856 (let ((data-length (foreign-string-length data-ptr)))
857 (adjust-array str (+ offset data-length)
858 :initial-element #\?)
859 (setf offset (%cstring-into-vector
863 (error "wrong type. preliminary."))
864 while (and (= res $SQL_SUCCESS_WITH_INFO)
865 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
867 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
868 +max-precision+ out-len-ptr)))
869 (setf str (coerce str 'string))
870 (if (= sql-type $SQL_DECIMAL)
871 (let ((*read-base* 10))
872 (read-from-string str))
875 (let ((str (make-string out-len)))
876 (loop do (if (= c-type #.$SQL_CHAR)
877 (setf offset (%cstring-into-vector ;string
880 (min out-len (1- +max-precision+))))
881 (error "wrong type. preliminary."))
883 (and (= res $SQL_SUCCESS_WITH_INFO)
884 #+ingore(eq (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
886 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
888 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
889 +max-precision+ out-len-ptr)
890 out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE)))
891 (if (= sql-type $SQL_DECIMAL)
892 (let ((*read-base* 10))
893 (read-from-string str))
896 (def-type c-timestamp-ptr-type '(* (:struct sql-c-timestamp)))
897 (def-type c-time-ptr-type '(* (:struct sql-c-time)))
898 (def-type c-date-ptr-type '(* (:struct sql-c-date)))
900 (defun timestamp-to-universal-time (ptr)
901 (declare (type c-timestamp-ptr-type ptr))
903 (encode-universal-time
904 (get-slot-value ptr 'sql-c-timestamp 'second)
905 (get-slot-value ptr 'sql-c-timestamp 'minute)
906 (get-slot-value ptr 'sql-c-timestamp 'hour)
907 (get-slot-value ptr 'sql-c-timestamp 'day)
908 (get-slot-value ptr 'sql-c-timestamp 'month)
909 (get-slot-value ptr 'sql-c-timestamp 'year))
910 (get-slot-value ptr 'sql-c-timestamp 'fraction)))
912 (defun universal-time-to-timestamp (time &optional (fraction 0))
913 (multiple-value-bind (sec min hour day month year)
914 (decode-universal-time time)
915 (let ((ptr (allocate-foreign-object 'sql-c-timestamp)))
916 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
917 (get-slot-value ptr 'sql-c-timestamp 'minute) min
918 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
919 (get-slot-value ptr 'sql-c-timestamp 'day) day
920 (get-slot-value ptr 'sql-c-timestamp 'month) month
921 (get-slot-value ptr 'sql-c-timestamp 'year) year
922 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
925 (defun %put-timestamp (ptr time &optional (fraction 0))
926 (declare (type c-timestamp-ptr-type ptr))
927 (multiple-value-bind (sec min hour day month year)
928 (decode-universal-time time)
929 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
930 (get-slot-value ptr 'sql-c-timestamp 'minute) min
931 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
932 (get-slot-value ptr 'sql-c-timestamp 'day) day
933 (get-slot-value ptr 'sql-c-timestamp 'month) month
934 (get-slot-value ptr 'sql-c-timestamp 'year) year
935 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
938 (defun date-to-universal-time (ptr)
939 (declare (type c-date-ptr-type ptr))
940 (encode-universal-time
942 (get-slot-value ptr 'sql-c-timestamp 'day)
943 (get-slot-value ptr 'sql-c-timestamp 'month)
944 (get-slot-value ptr 'sql-c-timestamp 'year)))
946 (defun time-to-universal-time (ptr)
947 (declare (type c-time-ptr-type ptr))
948 (encode-universal-time
949 (get-slot-value ptr 'sql-c-timestamp 'second)
950 (get-slot-value ptr 'sql-c-timestamp 'minute)
951 (get-slot-value ptr 'sql-c-timestamp 'hour)
957 (defun %set-attr-odbc-version (henv version)
958 (with-error-handling (:henv henv)
959 (SQLSetEnvAttr henv $SQL_ATTR_ODBC_VERSION
960 (make-pointer version :void) 0)))
962 (defun %list-tables (hstmt)
963 (with-error-handling (:hstmt hstmt)
964 (SQLTables hstmt +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0)))
966 (defun %table-statistics (table hstmt &key unique (ensure t))
967 (with-cstrings ((table-cs table))
968 (with-error-handling (:hstmt hstmt)
974 (if unique $SQL_INDEX_UNIQUE $SQL_INDEX_ALL)
975 (if ensure $SQL_ENSURE $SQL_QUICK)))))
977 (defun %list-data-sources (henv)
978 (let ((dsn (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
979 (desc (allocate-foreign-string 256))
982 (with-foreign-objects ((dsn-len :short)
984 (let ((res (with-error-handling (:henv henv)
985 (SQLDataSources henv $SQL_FETCH_FIRST dsn
986 (1+ $SQL_MAX_DSN_LENGTH)
987 dsn-len desc 256 desc-len))))
988 (when (or (eql res $SQL_SUCCESS)
989 (eql res $SQL_SUCCESS_WITH_INFO))
990 (push (convert-from-foreign-string dsn) results))
992 (do ((res (with-error-handling (:henv henv)
993 (SQLDataSources henv $SQL_FETCH_NEXT dsn
994 (1+ $SQL_MAX_DSN_LENGTH)
995 dsn-len desc 256 desc-len))
996 (with-error-handling (:henv henv)
997 (SQLDataSources henv $SQL_FETCH_NEXT dsn
998 (1+ $SQL_MAX_DSN_LENGTH)
999 dsn-len desc 256 desc-len))))
1000 ((not (or (eql res $SQL_SUCCESS)
1001 (eql res $SQL_SUCCESS_WITH_INFO))))
1002 (push (convert-from-foreign-string dsn) results))))
1004 (free-foreign-object dsn)
1005 (free-foreign-object desc)))
1006 (nreverse results)))