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-base:format-time
31 nil (clsql-base: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-base:clsql-odbc-error
117 :odbc-message "Invalid handle"))
118 (#.$SQL_STILL_EXECUTING
120 'clsql-base:clsql-odbc-error
121 :odbc-message "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-base:clsql-odbc-error
129 :odbc-message error-message
130 :sql-state sql-state)))
132 (progn ,result-code ,@body))))))
134 (defun %new-environment-handle ()
136 (with-foreign-object (phenv 'sql-handle)
140 (deref-pointer phenv 'sql-handle)))))
141 (%set-attr-odbc-version henv $SQL_OV_ODBC2)
145 (defun %sql-free-environment (henv)
150 (defun %new-db-connection-handle (henv)
151 (with-foreign-object (phdbc 'sql-handle)
154 (SQLAllocConnect henv phdbc)
155 (deref-pointer phdbc 'sql-handle))))
157 (defun %free-statement (hstmt option)
165 (:unbind $SQL_UNBIND)
166 (:reset $SQL_RESET_PARAMS)))))
168 (defmacro with-statement-handle ((hstmt hdbc) &body body)
169 `(let ((,hstmt (%new-statement-handle ,hdbc)))
172 (%free-statement ,hstmt :drop))))
174 ;; functional interface
176 (defun %sql-connect (hdbc server uid pwd)
177 (with-cstrings ((server-ptr server)
182 (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr
183 $SQL_NTS pwd-ptr $SQL_NTS))))
186 (defun %disconnect (hdbc)
189 (SQLDisconnect hdbc)))
191 (defun %commit (henv hdbc)
193 (:henv henv :hdbc hdbc)
195 henv hdbc $SQL_COMMIT)))
197 (defun %rollback (henv hdbc)
199 (:henv henv :hdbc hdbc)
201 henv hdbc $SQL_ROLLBACK)))
203 ; col-nr is zero-based in Lisp
204 ; col-nr = :bookmark retrieves a bookmark.
205 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
209 (if (eq column-nr :bookmark) 0 (1+ column-nr))
210 c-type data-ptr precision out-len-ptr)))
212 ; parameter-nr is zero-based in Lisp
213 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
214 sql-type precision scale data-ptr
215 max-value out-len-ptr)
218 (SQLBindParameter hstmt (1+ parameter-nr)
219 parameter-type ;$SQL_PARAM_INPUT
221 sql-type ;$SQL_VARCHAR
222 precision ;(1- (length str))
226 out-len-ptr ;#.+null-ptr+
229 (defun %sql-fetch (hstmt)
234 (defun %new-statement-handle (hdbc)
235 (let ((statement-handle
236 (with-foreign-object (hstmt-ptr 'sql-handle)
239 (SQLAllocStmt hdbc hstmt-ptr)
240 (deref-pointer hstmt-ptr 'sql-handle)))))
241 (if (uffi:null-pointer-p statement-handle)
242 (error "Received null statement handle.")
245 (defun %sql-get-info (hdbc info-type)
247 ;; those return string
248 ((#.$SQL_ACCESSIBLE_PROCEDURES
249 #.$SQL_ACCESSIBLE_TABLES
251 #.$SQL_DATA_SOURCE_NAME
252 #.$SQL_DATA_SOURCE_READ_ONLY
256 #.$SQL_DRIVER_ODBC_VER
258 #.$SQL_EXPRESSIONS_IN_ORDERBY
259 #.$SQL_IDENTIFIER_QUOTE_CHAR
261 #.$SQL_LIKE_ESCAPE_CLAUSE
262 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
263 #.$SQL_MULT_RESULT_SETS
264 #.$SQL_MULTIPLE_ACTIVE_TXN
265 #.$SQL_NEED_LONG_DATA_LEN
266 #.$SQL_ODBC_SQL_OPT_IEF
268 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
271 #.$SQL_PROCEDURE_TERM
273 #.$SQL_QUALIFIER_NAME_SEPARATOR
274 #.$SQL_QUALIFIER_TERM
276 #.$SQL_SEARCH_PATTERN_ESCAPE
278 #.$SQL_SPECIAL_CHARACTERS
281 (let ((info-ptr (allocate-foreign-string 1024)))
282 (with-foreign-object (info-length-ptr :short)
285 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
286 (let ((info (convert-from-foreign-string info-ptr)))
287 (free-foreign-object info-ptr)
289 ;; those returning a word
290 ((#.$SQL_ACTIVE_CONNECTIONS
291 #.$SQL_ACTIVE_STATEMENTS
292 #.$SQL_CONCAT_NULL_BEHAVIOR
293 #.$SQL_CORRELATION_NAME
294 #.$SQL_CURSOR_COMMIT_BEHAVIOR
295 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
296 #.$SQL_MAX_COLUMN_NAME_LEN
297 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
298 #.$SQL_MAX_COLUMNS_IN_INDEX
299 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
300 #.$SQL_MAX_COLUMNS_IN_SELECT
301 #.$SQL_MAX_COLUMNS_IN_TABLE
302 #.$SQL_MAX_CURSOR_NAME_LEN
303 #.$SQL_MAX_OWNER_NAME_LEN
304 #.$SQL_MAX_PROCEDURE_NAME_LEN
305 #.$SQL_MAX_QUALIFIER_NAME_LEN
306 #.$SQL_MAX_TABLE_NAME_LEN
307 #.$SQL_MAX_TABLES_IN_SELECT
308 #.$SQL_MAX_USER_NAME_LEN
309 #.$SQL_NON_NULLABLE_COLUMNS
310 #.$SQL_NULL_COLLATION
311 #.$SQL_ODBC_API_CONFORMANCE
312 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
313 #.$SQL_ODBC_SQL_CONFORMANCE
314 #.$SQL_QUALIFIER_LOCATION
315 #.$SQL_QUOTED_IDENTIFIER_CASE
317 (with-foreign-objects ((info-ptr :short)
318 (info-length-ptr :short))
326 (deref-pointer info-ptr :short)))
328 ;; those returning a long bitmask
330 #.$SQL_BOOKMARK_PERSISTENCE
331 #.$SQL_CONVERT_BIGINT
332 #.$SQL_CONVERT_BINARY
336 #.$SQL_CONVERT_DECIMAL
337 #.$SQL_CONVERT_DOUBLE
339 #.$SQL_CONVERT_INTEGER
340 #.$SQL_CONVERT_LONGVARCHAR
341 #.$SQL_CONVERT_NUMERIC
343 #.$SQL_CONVERT_SMALLINT
345 #.$SQL_CONVERT_TIMESTAMP
346 #.$SQL_CONVERT_TINYINT
347 #.$SQL_CONVERT_VARBINARY
348 #.$SQL_CONVERT_VARCHAR
349 #.$SQL_CONVERT_LONGVARBINARY
350 #.$SQL_CONVERT_FUNCTIONS
351 #.$SQL_FETCH_DIRECTION
353 #.$SQL_GETDATA_EXTENSIONS
355 #.$SQL_MAX_INDEX_SIZE
357 #.$SQL_MAX_STATEMENT_LEN
358 #.$SQL_NUMERIC_FUNCTIONS
360 #.$SQL_POS_OPERATIONS
361 #.$SQL_POSITIONED_STATEMENTS
362 #.$SQL_QUALIFIER_USAGE
363 #.$SQL_SCROLL_CONCURRENCY
364 #.$SQL_SCROLL_OPTIONS
365 #.$SQL_STATIC_SENSITIVITY
366 #.$SQL_STRING_FUNCTIONS
368 #.$SQL_SYSTEM_FUNCTIONS
369 #.$SQL_TIMEDATE_ADD_INTERVALS
370 #.$SQL_TIMEDATE_DIFF_INTERVALS
371 #.$SQL_TIMEDATE_FUNCTIONS
372 #.$SQL_TXN_ISOLATION_OPTION
374 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
375 (info-length-ptr :short))
383 (deref-pointer info-ptr #.$ODBC-LONG-TYPE)))
385 ;; those returning a long integer
386 ((#.$SQL_DEFAULT_TXN_ISOLATION
392 #.$SQL_IDENTIFIER_CASE
393 #.$SQL_MAX_BINARY_LITERAL_LEN
394 #.$SQL_MAX_CHAR_LITERAL_LEN
395 #.$SQL_ACTIVE_ENVIRONMENTS
397 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
398 (info-length-ptr :short))
401 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
402 (deref-pointer info-ptr #.$ODBC-LONG-TYPE))))))
404 (defun %sql-exec-direct (sql hstmt henv hdbc)
405 (with-cstring (sql-ptr sql)
407 (:hstmt hstmt :henv henv :hdbc hdbc)
408 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
410 (defun %sql-cancel (hstmt)
415 (defun %sql-execute (hstmt)
420 (defun result-columns-count (hstmt)
421 (with-foreign-objects ((columns-nr-ptr :short))
422 (with-error-handling (:hstmt hstmt)
423 (SQLNumResultCols hstmt columns-nr-ptr)
424 (deref-pointer columns-nr-ptr :short))))
426 (defun result-rows-count (hstmt)
427 (with-foreign-objects ((row-count-ptr #.$ODBC-LONG-TYPE))
428 (with-error-handling (:hstmt hstmt)
429 (SQLRowCount hstmt row-count-ptr)
430 (deref-pointer row-count-ptr #.$ODBC-LONG-TYPE))))
432 ;; column counting is 1-based
433 (defun %describe-column (hstmt column-nr)
434 (let ((column-name-ptr (allocate-foreign-string 256)))
435 (with-foreign-objects ((column-name-length-ptr :short)
436 (column-sql-type-ptr :short)
437 (column-precision-ptr #.$ODBC-ULONG-TYPE)
438 (column-scale-ptr :short)
439 (column-nullable-p-ptr :short))
440 (with-error-handling (:hstmt hstmt)
441 (SQLDescribeCol hstmt column-nr column-name-ptr 256
442 column-name-length-ptr
446 column-nullable-p-ptr)
447 (let ((column-name (convert-from-foreign-string column-name-ptr)))
448 (free-foreign-object column-name-ptr)
451 (deref-pointer column-sql-type-ptr :short)
452 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
453 (deref-pointer column-scale-ptr :short)
454 (deref-pointer column-nullable-p-ptr :short)))))))
456 ;; parameter counting is 1-based
457 (defun %describe-parameter (hstmt parameter-nr)
458 (with-foreign-objects ((column-sql-type-ptr :short)
459 (column-precision-ptr #.$ODBC-ULONG-TYPE)
460 (column-scale-ptr :short)
461 (column-nullable-p-ptr :short))
464 (SQLDescribeParam hstmt parameter-nr
468 column-nullable-p-ptr)
470 (deref-pointer column-sql-type-ptr :short)
471 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
472 (deref-pointer column-scale-ptr :short)
473 (deref-pointer column-nullable-p-ptr :short)))))
475 (defun %column-attributes (hstmt column-nr descriptor-type)
476 (let ((descriptor-info-ptr (allocate-foreign-string 256)))
477 (with-foreign-objects ((descriptor-length-ptr :short)
478 (numeric-descriptor-ptr #.$ODBC-LONG-TYPE))
481 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr
482 256 descriptor-length-ptr
483 numeric-descriptor-ptr)
484 (let ((desc (convert-from-foreign-string descriptor-info-ptr)))
485 (free-foreign-object descriptor-info-ptr)
488 (deref-pointer numeric-descriptor-ptr #.$ODBC-LONG-TYPE)))))))
490 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
491 table-name column-name)
492 (with-cstrings ((table-qualifier-ptr table-qualifier)
493 (table-owner-ptr table-owner)
494 (table-name-ptr table-name)
495 (column-name-ptr column-name))
499 table-qualifier-ptr (length table-qualifier)
500 table-owner-ptr (length table-owner)
501 table-name-ptr (length table-name)
502 column-name-ptr (length column-name)))))
504 (defun %describe-columns (hdbc table-qualifier table-owner
505 table-name column-name)
506 (with-statement-handle (hstmt hdbc)
507 (%prepare-describe-columns hstmt table-qualifier table-owner
508 table-name column-name)
509 (fetch-all-rows hstmt)))
511 (defun %sql-data-sources (henv &key (direction :first))
512 (let ((name-ptr (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
513 (description-ptr (allocate-foreign-string 1024)))
514 (with-foreign-objects ((name-length-ptr :short)
515 (description-length-ptr :short))
516 (let ((res (with-error-handling
520 (:first $SQL_FETCH_FIRST)
521 (:next $SQL_FETCH_NEXT))
523 (1+ $SQL_MAX_DSN_LENGTH)
527 description-length-ptr))))
529 ((= res $SQL_NO_DATA_FOUND)
530 (let ((name (convert-from-foreign-string name-ptr))
531 (desc (convert-from-foreign-string description-ptr)))
532 (free-foreign-object name-ptr)
533 (free-foreign-object description-ptr)
538 (free-foreign-object name-ptr)
539 (free-foreign-object description-ptr)
544 (defun sql-to-c-type (sql-type)
546 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
547 #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9) $SQL_C_CHAR)
548 (#.$SQL_INTEGER $SQL_C_SLONG)
549 (#.$SQL_SMALLINT $SQL_C_SSHORT)
550 (#.$SQL_DOUBLE $SQL_C_DOUBLE)
551 (#.$SQL_FLOAT $SQL_C_DOUBLE)
552 (#.$SQL_REAL $SQL_C_FLOAT)
553 (#.$SQL_DATE $SQL_C_DATE)
554 (#.$SQL_TIME $SQL_C_TIME)
555 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
556 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
557 (#.$SQL_TINYINT $SQL_C_STINYINT)
558 (#.$SQL_BIT $SQL_C_BIT)))
560 (def-type byte-pointer-type '(* :byte))
561 (def-type short-pointer-type '(* :short))
562 (def-type int-pointer-type '(* :int))
563 (def-type long-pointer-type '(* #.$ODBC-LONG-TYPE))
564 (def-type float-pointer-type '(* :float))
565 (def-type double-pointer-type '(* :double))
566 (def-type string-pointer-type '(* :unsigned-char))
568 (defun get-cast-byte (ptr)
569 (locally (declare (type byte-pointer-type ptr))
570 (deref-pointer ptr :byte)))
572 (defun get-cast-short (ptr)
573 (locally (declare (type short-pointer-type ptr))
574 (deref-pointer ptr :short)))
576 (defun get-cast-int (ptr)
577 (locally (declare (type int-pointer-type ptr))
578 (deref-pointer ptr :int)))
580 (defun get-cast-long (ptr)
581 (locally (declare (type long-pointer-type ptr))
582 (deref-pointer ptr #.$ODBC-LONG-TYPE)))
584 (defun get-cast-single-float (ptr)
585 (locally (declare (type float-pointer-type ptr))
586 (deref-pointer ptr :float)))
588 (defun get-cast-double-float (ptr)
589 (locally (declare (type double-pointer-type ptr))
590 (deref-pointer ptr :double)))
592 (defun get-cast-foreign-string (ptr)
593 (locally (declare (type string-pointer-type ptr))
594 (convert-from-foreign-string ptr)))
596 (defun get-cast-binary (ptr len format)
597 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
598 (with-cast-pointer (casted ptr :byte)
600 (:unsigned-byte-vector
601 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
603 (setf (aref vector i)
604 (deref-array casted '(:array :byte) i)))
607 (let ((vector (make-array (ash len 3) :element-type 'bit)))
609 (let ((byte (deref-array casted '(:array :byte) i)))
611 (setf (bit vector (+ (ash i 3) j))
612 (logand (ash byte (- j 7)) 1)))))
616 (defun read-data (data-ptr c-type sql-type out-len-ptr result-type)
617 (declare (type long-ptr-type out-len-ptr))
618 (let* ((out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
620 (cond ((= out-len $SQL_NULL_DATA)
624 ;; SQL extended datatypes
625 (#.$SQL_TINYINT (get-cast-byte data-ptr))
626 (#.$SQL_C_STINYINT (get-cast-byte data-ptr)) ;; ?
627 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
628 (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
629 (#.$SQL_INTEGER (get-cast-int data-ptr))
630 (#.$SQL_BIGINT (read-from-string
631 (get-cast-foreign-string data-ptr)))
632 (#.$SQL_TINYINT (get-cast-byte data-ptr))
634 (let ((*read-base* 10))
635 (read-from-string (get-cast-foreign-string data-ptr))))
639 (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
641 (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
642 (funcall *time-conversion-function* universal-time frac)))
644 (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
645 (funcall *time-conversion-function* universal-time frac)))
647 (get-cast-int data-ptr))
649 (get-cast-single-float data-ptr))
651 (get-cast-double-float data-ptr))
653 (get-cast-long data-ptr))
655 (#.$SQL_C_BIT ; encountered only in Access
656 (get-cast-byte data-ptr))
658 (get-cast-binary data-ptr out-len *binary-format*))
659 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
660 (get-cast-short data-ptr)) ; LMH
663 (code-char (get-cast-short data-ptr)))
665 (get-cast-foreign-string data-ptr)))))))))
667 ;; FIXME: this could be better optimized for types which use READ-FROM-STRING above
669 (if (and (or (eq result-type t) (eq result-type :string))
671 (not (stringp value)))
672 (write-to-string value)
675 ;; which value is appropriate?
676 (defparameter +max-precision+ 4001)
678 (defvar *break-on-unknown-data-type* t)
680 ;; C. Stacy's idea to factor this out
681 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
682 ;; so that I don't have to remember to make changes in more than one place.
683 ;; Just keep it in synch with READ-DATA."
684 (defun %allocate-bindings (sql-type precision)
685 (let* ((c-type (sql-to-c-type sql-type))
686 (size (if (zerop precision)
687 +max-precision+ ;; if the precision cannot be determined
688 (min precision +max-precision+)))
689 (long-p (= size +max-precision+))
691 (case c-type ;; add more?
692 (#.$SQL_C_SLONG (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE))
693 (#.$SQL_C_DATE (allocate-foreign-object 'sql-c-date))
694 (#.$SQL_C_TIME (allocate-foreign-object 'sql-c-time))
695 (#.$SQL_C_TIMESTAMP (allocate-foreign-object 'sql-c-timestamp))
696 (#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
697 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
698 (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
699 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
700 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
701 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
702 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
704 ;; Maybe should signal a restartable condition for this?
705 (when *break-on-unknown-data-type*
706 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
707 sql-type precision size c-type))
708 (uffi:allocate-foreign-object :byte (1+ size)))))
709 (out-len-ptr (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE)))
710 (values c-type data-ptr out-len-ptr size long-p)))
712 (defun fetch-all-rows (hstmt &key free-option flatp)
713 (let ((column-count (result-columns-count hstmt)))
714 (unless (zerop column-count)
715 (let ((names (make-array column-count))
716 (sql-types (make-array column-count :element-type 'fixnum))
717 (c-types (make-array column-count :element-type 'fixnum))
718 (precisions (make-array column-count :element-type 'fixnum))
719 (data-ptrs (make-array column-count :initial-element nil))
720 (out-len-ptrs (make-array column-count :initial-element nil))
721 (scales (make-array column-count :element-type 'fixnum))
722 (nullables-p (make-array column-count :element-type 'fixnum)))
726 (dotimes (col-nr column-count)
727 ;; get column information
728 (multiple-value-bind (name sql-type precision scale nullable-p)
729 (%describe-column hstmt (1+ col-nr))
730 ;; allocate space to bind result rows to
731 (multiple-value-bind (c-type data-ptr out-len-ptr)
732 (%allocate-bindings sql-type precision)
733 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
734 (setf (svref names col-nr) name
735 (aref sql-types col-nr) sql-type
736 (aref c-types col-nr) (sql-to-c-type sql-type)
737 (aref precisions col-nr) (if (zerop precision) 0 precision)
738 (aref scales col-nr) scale
739 (aref nullables-p col-nr) nullable-p
740 (aref data-ptrs col-nr) data-ptr
741 (aref out-len-ptrs col-nr) out-len-ptr))))
745 (when (> column-count 1)
746 (error "If more than one column is to be fetched, flatp has to be nil."))
747 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
749 (read-data (aref data-ptrs 0)
752 (aref out-len-ptrs 0)
755 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
757 (loop for col-nr from 0 to (1- column-count)
759 (read-data (aref data-ptrs col-nr)
760 (aref c-types col-nr)
761 (aref sql-types col-nr)
762 (aref out-len-ptrs col-nr)
765 ;; dispose of memory etc
766 (when free-option (%free-statement hstmt free-option))
767 (dotimes (col-nr column-count)
768 (let ((data-ptr (aref data-ptrs col-nr))
769 (out-len-ptr (aref out-len-ptrs col-nr)))
770 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
771 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
773 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
774 ;; of %do-fetch into sql package (has been done)
776 (defun %sql-prepare (hstmt sql)
777 (with-cstring (sql-ptr sql)
778 (with-error-handling (:hstmt hstmt)
779 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
781 ;; depending on option, we return a long int or a string; string not implemented
782 (defun get-connection-option (hdbc option)
783 (with-foreign-object (param-ptr #.$ODBC-LONG-TYPE)
784 (with-error-handling (:hdbc hdbc)
785 (SQLGetConnectOption hdbc option param-ptr)
786 (deref-pointer param-ptr #.$ODBC-LONG-TYPE))))
788 (defun set-connection-option (hdbc option param)
789 (with-error-handling (:hdbc hdbc)
790 (SQLSetConnectOption hdbc option param)))
792 (defun disable-autocommit (hdbc)
793 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
795 (defun enable-autocommit (hdbc)
796 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
798 (defun %sql-set-pos (hstmt row option lock)
801 (SQLSetPos hstmt row option lock)))
803 (defun %sql-extended-fetch (hstmt fetch-type row)
804 (with-foreign-objects ((row-count-ptr #.$ODBC-ULONG-TYPE)
805 (row-status-ptr :short))
806 (with-error-handling (:hstmt hstmt)
807 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
809 (values (deref-pointer row-count-ptr #.$ODBC-ULONG-TYPE)
810 (deref-pointer row-status-ptr :short)))))
812 ; column-nr is zero-based
813 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
815 (:hstmt hstmt :print-info nil)
816 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
817 c-type data-ptr precision out-len-ptr)))
819 (defun %sql-param-data (hstmt param-ptr)
820 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
821 (SQLParamData hstmt param-ptr)))
823 (defun %sql-put-data (hstmt data-ptr size)
825 (:hstmt hstmt :print-info t) ;; nil
826 (SQLPutData hstmt data-ptr size)))
828 (defconstant $sql-data-truncated (intern "01004" :keyword))
830 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
831 out-len-ptr result-type)
832 (declare (type long-ptr-type out-len-ptr))
833 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
834 +max-precision+ out-len-ptr))
835 (out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
839 (return-from read-data-in-chunks *null*))
840 (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
841 (let ((str (make-array 0 :element-type 'character :adjustable t)))
842 (loop do (if (= c-type #.$SQL_CHAR)
843 (let ((data-length (foreign-string-length data-ptr)))
844 (adjust-array str (+ offset data-length)
845 :initial-element #\?)
846 (setf offset (%cstring-into-vector
850 (error "wrong type. preliminary."))
851 while (and (= res $SQL_SUCCESS_WITH_INFO)
852 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
854 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
855 +max-precision+ out-len-ptr)))
856 (setf str (coerce str 'string))
857 (if (= sql-type $SQL_DECIMAL)
858 (let ((*read-base* 10))
859 (read-from-string str))
862 (let ((str (make-string out-len)))
863 (loop do (if (= c-type #.$SQL_CHAR)
864 (setf offset (%cstring-into-vector ;string
867 (min out-len (1- +max-precision+))))
868 (error "wrong type. preliminary."))
870 (and (= res $SQL_SUCCESS_WITH_INFO)
871 #+ingore(eq (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
873 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
875 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
876 +max-precision+ out-len-ptr)
877 out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE)))
878 (if (= sql-type $SQL_DECIMAL)
879 (let ((*read-base* 10))
880 (read-from-string str))
883 (def-type c-timestamp-ptr-type '(* (:struct sql-c-timestamp)))
884 (def-type c-time-ptr-type '(* (:struct sql-c-time)))
885 (def-type c-date-ptr-type '(* (:struct sql-c-date)))
887 (defun timestamp-to-universal-time (ptr)
888 (declare (type c-timestamp-ptr-type ptr))
890 (encode-universal-time
891 (get-slot-value ptr 'sql-c-timestamp 'second)
892 (get-slot-value ptr 'sql-c-timestamp 'minute)
893 (get-slot-value ptr 'sql-c-timestamp 'hour)
894 (get-slot-value ptr 'sql-c-timestamp 'day)
895 (get-slot-value ptr 'sql-c-timestamp 'month)
896 (get-slot-value ptr 'sql-c-timestamp 'year))
897 (get-slot-value ptr 'sql-c-timestamp 'fraction)))
899 (defun universal-time-to-timestamp (time &optional (fraction 0))
900 (multiple-value-bind (sec min hour day month year)
901 (decode-universal-time time)
902 (let ((ptr (allocate-foreign-object 'sql-c-timestamp)))
903 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
904 (get-slot-value ptr 'sql-c-timestamp 'minute) min
905 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
906 (get-slot-value ptr 'sql-c-timestamp 'day) day
907 (get-slot-value ptr 'sql-c-timestamp 'month) month
908 (get-slot-value ptr 'sql-c-timestamp 'year) year
909 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
912 (defun %put-timestamp (ptr time &optional (fraction 0))
913 (declare (type c-timestamp-ptr-type ptr))
914 (multiple-value-bind (sec min hour day month year)
915 (decode-universal-time time)
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 date-to-universal-time (ptr)
926 (declare (type c-date-ptr-type ptr))
927 (encode-universal-time
929 (get-slot-value ptr 'sql-c-timestamp 'day)
930 (get-slot-value ptr 'sql-c-timestamp 'month)
931 (get-slot-value ptr 'sql-c-timestamp 'year)))
933 (defun time-to-universal-time (ptr)
934 (declare (type c-time-ptr-type ptr))
935 (encode-universal-time
936 (get-slot-value ptr 'sql-c-timestamp 'second)
937 (get-slot-value ptr 'sql-c-timestamp 'minute)
938 (get-slot-value ptr 'sql-c-timestamp 'hour)
944 (defun %set-attr-odbc-version (henv version)
945 (with-error-handling (:henv henv)
946 (SQLSetEnvAttr henv $SQL_ATTR_ODBC_VERSION
947 (make-pointer version :void) 0)))
949 (defun %list-tables (hstmt)
950 (with-error-handling (:hstmt hstmt)
951 (SQLTables hstmt +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0)))
953 (defun %table-statistics (table hstmt &key unique (ensure t))
954 (with-cstrings ((table-cs table))
955 (with-error-handling (:hstmt hstmt)
961 (if unique $SQL_INDEX_UNIQUE $SQL_INDEX_ALL)
962 (if ensure $SQL_ENSURE $SQL_QUICK)))))
964 (defun %list-data-sources (henv)
965 (let ((dsn (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
966 (desc (allocate-foreign-string 256))
969 (with-foreign-objects ((dsn-len :short)
971 (let ((res (with-error-handling (:henv henv)
972 (SQLDataSources henv $SQL_FETCH_FIRST dsn
973 (1+ $SQL_MAX_DSN_LENGTH)
974 dsn-len desc 256 desc-len))))
975 (when (or (eql res $SQL_SUCCESS)
976 (eql res $SQL_SUCCESS_WITH_INFO))
977 (push (convert-from-foreign-string dsn) results))
979 (do ((res (with-error-handling (:henv henv)
980 (SQLDataSources henv $SQL_FETCH_NEXT dsn
981 (1+ $SQL_MAX_DSN_LENGTH)
982 dsn-len desc 256 desc-len))
983 (with-error-handling (:henv henv)
984 (SQLDataSources henv $SQL_FETCH_NEXT dsn
985 (1+ $SQL_MAX_DSN_LENGTH)
986 dsn-len desc 256 desc-len))))
987 ((not (or (eql res $SQL_SUCCESS)
988 (eql res $SQL_SUCCESS_WITH_INFO))))
989 (push (convert-from-foreign-string dsn) results))))
991 (free-foreign-object dsn)
992 (free-foreign-object desc)))