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 :long)
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 :long))))))
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 :long)
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 (with-foreign-object (hstmt-ptr 'sql-handle)
238 (SQLAllocStmt hdbc hstmt-ptr)
239 (deref-pointer hstmt-ptr 'sql-handle))))
241 (defun %sql-get-info (hdbc info-type)
243 ;; those return string
244 ((#.$SQL_ACCESSIBLE_PROCEDURES
245 #.$SQL_ACCESSIBLE_TABLES
247 #.$SQL_DATA_SOURCE_NAME
248 #.$SQL_DATA_SOURCE_READ_ONLY
252 #.$SQL_DRIVER_ODBC_VER
254 #.$SQL_EXPRESSIONS_IN_ORDERBY
255 #.$SQL_IDENTIFIER_QUOTE_CHAR
257 #.$SQL_LIKE_ESCAPE_CLAUSE
258 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
259 #.$SQL_MULT_RESULT_SETS
260 #.$SQL_MULTIPLE_ACTIVE_TXN
261 #.$SQL_NEED_LONG_DATA_LEN
262 #.$SQL_ODBC_SQL_OPT_IEF
264 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
267 #.$SQL_PROCEDURE_TERM
269 #.$SQL_QUALIFIER_NAME_SEPARATOR
270 #.$SQL_QUALIFIER_TERM
272 #.$SQL_SEARCH_PATTERN_ESCAPE
274 #.$SQL_SPECIAL_CHARACTERS
277 (let ((info-ptr (allocate-foreign-string 1024)))
278 (with-foreign-object (info-length-ptr :short)
281 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
282 (let ((info (convert-from-foreign-string info-ptr)))
283 (free-foreign-object info-ptr)
285 ;; those returning a word
286 ((#.$SQL_ACTIVE_CONNECTIONS
287 #.$SQL_ACTIVE_STATEMENTS
288 #.$SQL_CONCAT_NULL_BEHAVIOR
289 #.$SQL_CORRELATION_NAME
290 #.$SQL_CURSOR_COMMIT_BEHAVIOR
291 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
292 #.$SQL_MAX_COLUMN_NAME_LEN
293 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
294 #.$SQL_MAX_COLUMNS_IN_INDEX
295 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
296 #.$SQL_MAX_COLUMNS_IN_SELECT
297 #.$SQL_MAX_COLUMNS_IN_TABLE
298 #.$SQL_MAX_CURSOR_NAME_LEN
299 #.$SQL_MAX_OWNER_NAME_LEN
300 #.$SQL_MAX_PROCEDURE_NAME_LEN
301 #.$SQL_MAX_QUALIFIER_NAME_LEN
302 #.$SQL_MAX_TABLE_NAME_LEN
303 #.$SQL_MAX_TABLES_IN_SELECT
304 #.$SQL_MAX_USER_NAME_LEN
305 #.$SQL_NON_NULLABLE_COLUMNS
306 #.$SQL_NULL_COLLATION
307 #.$SQL_ODBC_API_CONFORMANCE
308 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
309 #.$SQL_ODBC_SQL_CONFORMANCE
310 #.$SQL_QUALIFIER_LOCATION
311 #.$SQL_QUOTED_IDENTIFIER_CASE
313 (with-foreign-objects ((info-ptr :short)
314 (info-length-ptr :short))
322 (deref-pointer info-ptr :short)))
324 ;; those returning a long bitmask
326 #.$SQL_BOOKMARK_PERSISTENCE
327 #.$SQL_CONVERT_BIGINT
328 #.$SQL_CONVERT_BINARY
332 #.$SQL_CONVERT_DECIMAL
333 #.$SQL_CONVERT_DOUBLE
335 #.$SQL_CONVERT_INTEGER
336 #.$SQL_CONVERT_LONGVARCHAR
337 #.$SQL_CONVERT_NUMERIC
339 #.$SQL_CONVERT_SMALLINT
341 #.$SQL_CONVERT_TIMESTAMP
342 #.$SQL_CONVERT_TINYINT
343 #.$SQL_CONVERT_VARBINARY
344 #.$SQL_CONVERT_VARCHAR
345 #.$SQL_CONVERT_LONGVARBINARY
346 #.$SQL_CONVERT_FUNCTIONS
347 #.$SQL_FETCH_DIRECTION
349 #.$SQL_GETDATA_EXTENSIONS
351 #.$SQL_MAX_INDEX_SIZE
353 #.$SQL_MAX_STATEMENT_LEN
354 #.$SQL_NUMERIC_FUNCTIONS
356 #.$SQL_POS_OPERATIONS
357 #.$SQL_POSITIONED_STATEMENTS
358 #.$SQL_QUALIFIER_USAGE
359 #.$SQL_SCROLL_CONCURRENCY
360 #.$SQL_SCROLL_OPTIONS
361 #.$SQL_STATIC_SENSITIVITY
362 #.$SQL_STRING_FUNCTIONS
364 #.$SQL_SYSTEM_FUNCTIONS
365 #.$SQL_TIMEDATE_ADD_INTERVALS
366 #.$SQL_TIMEDATE_DIFF_INTERVALS
367 #.$SQL_TIMEDATE_FUNCTIONS
368 #.$SQL_TXN_ISOLATION_OPTION
370 (with-foreign-objects ((info-ptr :long)
371 (info-length-ptr :short))
379 (deref-pointer info-ptr :long)))
381 ;; those returning a long integer
382 ((#.$SQL_DEFAULT_TXN_ISOLATION
388 #.$SQL_IDENTIFIER_CASE
389 #.$SQL_MAX_BINARY_LITERAL_LEN
390 #.$SQL_MAX_CHAR_LITERAL_LEN
391 #.$SQL_ACTIVE_ENVIRONMENTS
393 (with-foreign-objects ((info-ptr :long)
394 (info-length-ptr :short))
397 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
398 (deref-pointer info-ptr :long))))))
400 (defun %sql-exec-direct (sql hstmt henv hdbc)
401 (with-cstring (sql-ptr sql)
403 (:hstmt hstmt :henv henv :hdbc hdbc)
404 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
406 (defun %sql-cancel (hstmt)
411 (defun %sql-execute (hstmt)
416 (defun result-columns-count (hstmt)
417 (with-foreign-objects ((columns-nr-ptr :short))
418 (with-error-handling (:hstmt hstmt)
419 (SQLNumResultCols hstmt columns-nr-ptr)
420 (deref-pointer columns-nr-ptr :short))))
422 (defun result-rows-count (hstmt)
423 (with-foreign-objects ((row-count-ptr :long))
424 (with-error-handling (:hstmt hstmt)
425 (SQLRowCount hstmt row-count-ptr)
426 (deref-pointer row-count-ptr :long))))
428 ;; column counting is 1-based
429 (defun %describe-column (hstmt column-nr)
430 (let ((column-name-ptr (allocate-foreign-string 256)))
431 (with-foreign-objects ((column-name-length-ptr :short)
432 (column-sql-type-ptr :short)
433 (column-precision-ptr :unsigned-long)
434 (column-scale-ptr :short)
435 (column-nullable-p-ptr :short))
436 (with-error-handling (:hstmt hstmt)
437 (SQLDescribeCol hstmt column-nr column-name-ptr 256
438 column-name-length-ptr
442 column-nullable-p-ptr)
443 (let ((column-name (convert-from-foreign-string column-name-ptr)))
444 (free-foreign-object column-name-ptr)
447 (deref-pointer column-sql-type-ptr :short)
448 (deref-pointer column-precision-ptr :unsigned-long)
449 (deref-pointer column-scale-ptr :short)
450 (deref-pointer column-nullable-p-ptr :short)))))))
452 ;; parameter counting is 1-based
453 (defun %describe-parameter (hstmt parameter-nr)
454 (with-foreign-objects ((column-sql-type-ptr :short)
455 (column-precision-ptr :unsigned-long)
456 (column-scale-ptr :short)
457 (column-nullable-p-ptr :short))
460 (SQLDescribeParam hstmt parameter-nr
464 column-nullable-p-ptr)
466 (deref-pointer column-sql-type-ptr :short)
467 (deref-pointer column-precision-ptr :unsigned-long)
468 (deref-pointer column-scale-ptr :short)
469 (deref-pointer column-nullable-p-ptr :short)))))
471 (defun %column-attributes (hstmt column-nr descriptor-type)
472 (let ((descriptor-info-ptr (allocate-foreign-string 256)))
473 (with-foreign-objects ((descriptor-length-ptr :short)
474 (numeric-descriptor-ptr :long))
477 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr
478 256 descriptor-length-ptr
479 numeric-descriptor-ptr)
480 (let ((desc (convert-from-foreign-string descriptor-info-ptr)))
481 (free-foreign-object descriptor-info-ptr)
484 (deref-pointer numeric-descriptor-ptr :long)))))))
486 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
487 table-name column-name)
488 (with-cstrings ((table-qualifier-ptr table-qualifier)
489 (table-owner-ptr table-owner)
490 (table-name-ptr table-name)
491 (column-name-ptr column-name))
495 table-qualifier-ptr (length table-qualifier)
496 table-owner-ptr (length table-owner)
497 table-name-ptr (length table-name)
498 column-name-ptr (length column-name)))))
500 (defun %describe-columns (hdbc table-qualifier table-owner
501 table-name column-name)
502 (with-statement-handle (hstmt hdbc)
503 (%prepare-describe-columns hstmt table-qualifier table-owner
504 table-name column-name)
505 (fetch-all-rows hstmt)))
507 (defun %sql-data-sources (henv &key (direction :first))
508 (let ((name-ptr (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
509 (description-ptr (allocate-foreign-string 1024)))
510 (with-foreign-objects ((name-length-ptr :short)
511 (description-length-ptr :short))
512 (let ((res (with-error-handling
516 (:first $SQL_FETCH_FIRST)
517 (:next $SQL_FETCH_NEXT))
519 (1+ $SQL_MAX_DSN_LENGTH)
523 description-length-ptr))))
525 ((= res $SQL_NO_DATA_FOUND)
526 (let ((name (convert-from-foreign-string name-ptr))
527 (desc (convert-from-foreign-string description-ptr)))
528 (free-foreign-object name-ptr)
529 (free-foreign-object description-ptr)
534 (free-foreign-object name-ptr)
535 (free-foreign-object description-ptr)
540 (defun sql-to-c-type (sql-type)
542 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
543 #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9) $SQL_C_CHAR)
544 (#.$SQL_INTEGER $SQL_C_SLONG)
545 (#.$SQL_SMALLINT $SQL_C_SSHORT)
546 (#.$SQL_DOUBLE $SQL_C_DOUBLE)
547 (#.$SQL_FLOAT $SQL_C_DOUBLE)
548 (#.$SQL_REAL $SQL_C_FLOAT)
549 (#.$SQL_DATE $SQL_C_DATE)
550 (#.$SQL_TIME $SQL_C_TIME)
551 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
552 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
553 (#.$SQL_TINYINT $SQL_C_STINYINT)
554 (#.$SQL_BIT $SQL_C_BIT)))
556 (def-type byte-pointer-type '(* :byte))
557 (def-type short-pointer-type '(* :short))
558 (def-type int-pointer-type '(* :int))
559 (def-type long-pointer-type '(* :long))
560 (def-type float-pointer-type '(* :float))
561 (def-type double-pointer-type '(* :double))
562 (def-type string-pointer-type '(* :unsigned-char))
564 (defun get-cast-byte (ptr)
565 (locally (declare (type byte-pointer-type ptr))
566 (deref-pointer ptr :byte)))
568 (defun get-cast-short (ptr)
569 (locally (declare (type short-pointer-type ptr))
570 (deref-pointer ptr :short)))
572 (defun get-cast-int (ptr)
573 (locally (declare (type int-pointer-type ptr))
574 (deref-pointer ptr :int)))
576 (defun get-cast-long (ptr)
577 (locally (declare (type long-pointer-type ptr))
578 (deref-pointer ptr :long)))
580 (defun get-cast-single-float (ptr)
581 (locally (declare (type float-pointer-type ptr))
582 (deref-pointer ptr :float)))
584 (defun get-cast-double-float (ptr)
585 (locally (declare (type double-pointer-type ptr))
586 (deref-pointer ptr :double)))
588 (defun get-cast-foreign-string (ptr)
589 (locally (declare (type string-pointer-type ptr))
590 (convert-from-foreign-string ptr)))
592 (defun get-cast-binary (ptr len format)
593 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
594 (with-cast-pointer (casted ptr :byte)
596 (:unsigned-byte-vector
597 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
599 (setf (aref vector i)
600 (deref-array casted '(:array :byte) i)))
603 (let ((vector (make-array (ash len 3) :element-type 'bit)))
605 (let ((byte (deref-array casted '(:array :byte) i)))
607 (setf (bit vector (+ (ash i 3) j))
608 (logand (ash byte (- j 7)) 1)))))
612 (defun read-data (data-ptr c-type sql-type out-len-ptr result-type)
613 (declare (type long-ptr-type out-len-ptr))
614 (let* ((out-len (deref-pointer out-len-ptr :long))
616 (cond ((= out-len $SQL_NULL_DATA)
620 ;; SQL extended datatypes
621 (#.$SQL_TINYINT (get-cast-byte data-ptr))
622 (#.$SQL_C_STINYINT (get-cast-byte data-ptr)) ;; ?
623 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
624 (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
625 (#.$SQL_INTEGER (get-cast-int data-ptr))
626 (#.$SQL_BIGINT (read-from-string
627 (get-cast-foreign-string data-ptr)))
628 (#.$SQL_TINYINT (get-cast-byte data-ptr))
630 (let ((*read-base* 10))
631 (read-from-string (get-cast-foreign-string data-ptr))))
635 (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
637 (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
638 (funcall *time-conversion-function* universal-time frac)))
640 (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
641 (funcall *time-conversion-function* universal-time frac)))
643 (get-cast-int data-ptr))
645 (get-cast-single-float data-ptr))
647 (get-cast-double-float data-ptr))
649 (get-cast-long data-ptr))
651 (#.$SQL_C_BIT ; encountered only in Access
652 (get-cast-byte data-ptr))
654 (get-cast-binary data-ptr out-len *binary-format*))
655 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
656 (get-cast-short data-ptr)) ; LMH
659 (code-char (get-cast-short data-ptr)))
661 (get-cast-foreign-string data-ptr)))))))))
663 ;; FIXME: this could be better optimized for types which use READ-FROM-STRING above
665 (if (and (or (eq result-type t) (eq result-type :string))
667 (not (stringp value)))
668 (write-to-string value)
671 ;; which value is appropriate?
672 (defparameter +max-precision+ 4001)
674 (defvar *break-on-unknown-data-type* t)
676 ;; C. Stacy's idea to factor this out
677 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
678 ;; so that I don't have to remember to make changes in more than one place.
679 ;; Just keep it in synch with READ-DATA."
680 (defun %allocate-bindings (sql-type precision)
681 (let* ((c-type (sql-to-c-type sql-type))
682 (size (if (zerop precision)
683 +max-precision+ ;; if the precision cannot be determined
684 (min precision +max-precision+)))
685 (long-p (= size +max-precision+))
687 (case c-type ;; add more?
688 (#.$SQL_C_SLONG (uffi:allocate-foreign-object :long))
689 (#.$SQL_C_DATE (allocate-foreign-object 'sql-c-date))
690 (#.$SQL_C_TIME (allocate-foreign-object 'sql-c-time))
691 (#.$SQL_C_TIMESTAMP (allocate-foreign-object 'sql-c-timestamp))
692 (#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
693 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
694 (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
695 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
696 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
697 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
698 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
700 ;; Maybe should signal a restartable condition for this?
701 (when *break-on-unknown-data-type*
702 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
703 sql-type precision size c-type))
704 (uffi:allocate-foreign-object :byte (1+ size)))))
705 (out-len-ptr (uffi:allocate-foreign-object :long)))
706 (values c-type data-ptr out-len-ptr size long-p)))
708 (defun fetch-all-rows (hstmt &key free-option flatp)
709 (let ((column-count (result-columns-count hstmt)))
710 (unless (zerop column-count)
711 (let ((names (make-array column-count))
712 (sql-types (make-array column-count :element-type 'fixnum))
713 (c-types (make-array column-count :element-type 'fixnum))
714 (precisions (make-array column-count :element-type 'fixnum))
715 (data-ptrs (make-array column-count :initial-element nil))
716 (out-len-ptrs (make-array column-count :initial-element nil))
717 (scales (make-array column-count :element-type 'fixnum))
718 (nullables-p (make-array column-count :element-type 'fixnum)))
722 (dotimes (col-nr column-count)
723 ;; get column information
724 (multiple-value-bind (name sql-type precision scale nullable-p)
725 (%describe-column hstmt (1+ col-nr))
726 ;; allocate space to bind result rows to
727 (multiple-value-bind (c-type data-ptr out-len-ptr)
728 (%allocate-bindings sql-type precision)
729 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
730 (setf (svref names col-nr) name
731 (aref sql-types col-nr) sql-type
732 (aref c-types col-nr) (sql-to-c-type sql-type)
733 (aref precisions col-nr) (if (zerop precision) 0 precision)
734 (aref scales col-nr) scale
735 (aref nullables-p col-nr) nullable-p
736 (aref data-ptrs col-nr) data-ptr
737 (aref out-len-ptrs col-nr) out-len-ptr))))
741 (when (> column-count 1)
742 (error "If more than one column is to be fetched, flatp has to be nil."))
743 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
745 (read-data (aref data-ptrs 0)
748 (aref out-len-ptrs 0)
751 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
753 (loop for col-nr from 0 to (1- column-count)
755 (read-data (aref data-ptrs col-nr)
756 (aref c-types col-nr)
757 (aref sql-types col-nr)
758 (aref out-len-ptrs col-nr)
761 ;; dispose of memory etc
762 (when free-option (%free-statement hstmt free-option))
763 (dotimes (col-nr column-count)
764 (let ((data-ptr (aref data-ptrs col-nr))
765 (out-len-ptr (aref out-len-ptrs col-nr)))
766 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
767 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
769 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
770 ;; of %do-fetch into sql package (has been done)
772 (defun %sql-prepare (hstmt sql)
773 (with-cstring (sql-ptr sql)
774 (with-error-handling (:hstmt hstmt)
775 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
777 ;; depending on option, we return a long int or a string; string not implemented
778 (defun get-connection-option (hdbc option)
779 (with-foreign-object (param-ptr :long)
780 (with-error-handling (:hdbc hdbc)
781 (SQLGetConnectOption hdbc option param-ptr)
782 (deref-pointer param-ptr :long))))
784 (defun set-connection-option (hdbc option param)
785 (with-error-handling (:hdbc hdbc)
786 (SQLSetConnectOption hdbc option param)))
788 (defun disable-autocommit (hdbc)
789 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
791 (defun enable-autocommit (hdbc)
792 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
794 (defun %sql-set-pos (hstmt row option lock)
797 (SQLSetPos hstmt row option lock)))
799 (defun %sql-extended-fetch (hstmt fetch-type row)
800 (with-foreign-objects ((row-count-ptr :unsigned-long)
801 (row-status-ptr :short))
802 (with-error-handling (:hstmt hstmt)
803 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
805 (values (deref-pointer row-count-ptr :unsigned-long)
806 (deref-pointer row-status-ptr :short)))))
808 ; column-nr is zero-based
809 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
811 (:hstmt hstmt :print-info nil)
812 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
813 c-type data-ptr precision out-len-ptr)))
815 (defun %sql-param-data (hstmt param-ptr)
816 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
817 (SQLParamData hstmt param-ptr)))
819 (defun %sql-put-data (hstmt data-ptr size)
821 (:hstmt hstmt :print-info t) ;; nil
822 (SQLPutData hstmt data-ptr size)))
824 (defconstant $sql-data-truncated (intern "01004" :keyword))
826 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
827 out-len-ptr result-type)
828 (declare (type long-ptr-type out-len-ptr))
829 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
830 +max-precision+ out-len-ptr))
831 (out-len (deref-pointer out-len-ptr :long))
835 (return-from read-data-in-chunks *null*))
836 (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
837 (let ((str (make-array 0 :element-type 'character :adjustable t)))
838 (loop do (if (= c-type #.$SQL_CHAR)
839 (let ((data-length (foreign-string-length data-ptr)))
840 (adjust-array str (+ offset data-length)
841 :initial-element #\?)
842 (setf offset (%cstring-into-vector
846 (error "wrong type. preliminary."))
847 while (and (= res $SQL_SUCCESS_WITH_INFO)
848 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
850 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
851 +max-precision+ out-len-ptr)))
852 (setf str (coerce str 'string))
853 (if (= sql-type $SQL_DECIMAL)
854 (let ((*read-base* 10))
855 (read-from-string str))
858 (let ((str (make-string out-len)))
859 (loop do (if (= c-type #.$SQL_CHAR)
860 (setf offset (%cstring-into-vector ;string
863 (min out-len (1- +max-precision+))))
864 (error "wrong type. preliminary."))
866 (and (= res $SQL_SUCCESS_WITH_INFO)
867 #+ingore(eq (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
869 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
871 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
872 +max-precision+ out-len-ptr)
873 out-len (deref-pointer out-len-ptr :long)))
874 (if (= sql-type $SQL_DECIMAL)
875 (let ((*read-base* 10))
876 (read-from-string str))
879 (def-type c-timestamp-ptr-type '(* (:struct sql-c-timestamp)))
880 (def-type c-time-ptr-type '(* (:struct sql-c-time)))
881 (def-type c-date-ptr-type '(* (:struct sql-c-date)))
883 (defun timestamp-to-universal-time (ptr)
884 (declare (type c-timestamp-ptr-type ptr))
886 (encode-universal-time
887 (get-slot-value ptr 'sql-c-timestamp 'second)
888 (get-slot-value ptr 'sql-c-timestamp 'minute)
889 (get-slot-value ptr 'sql-c-timestamp 'hour)
890 (get-slot-value ptr 'sql-c-timestamp 'day)
891 (get-slot-value ptr 'sql-c-timestamp 'month)
892 (get-slot-value ptr 'sql-c-timestamp 'year))
893 (get-slot-value ptr 'sql-c-timestamp 'fraction)))
895 (defun universal-time-to-timestamp (time &optional (fraction 0))
896 (multiple-value-bind (sec min hour day month year)
897 (decode-universal-time time)
898 (let ((ptr (allocate-foreign-object 'sql-c-timestamp)))
899 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
900 (get-slot-value ptr 'sql-c-timestamp 'minute) min
901 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
902 (get-slot-value ptr 'sql-c-timestamp 'day) day
903 (get-slot-value ptr 'sql-c-timestamp 'month) month
904 (get-slot-value ptr 'sql-c-timestamp 'year) year
905 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
908 (defun %put-timestamp (ptr time &optional (fraction 0))
909 (declare (type c-timestamp-ptr-type ptr))
910 (multiple-value-bind (sec min hour day month year)
911 (decode-universal-time time)
912 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
913 (get-slot-value ptr 'sql-c-timestamp 'minute) min
914 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
915 (get-slot-value ptr 'sql-c-timestamp 'day) day
916 (get-slot-value ptr 'sql-c-timestamp 'month) month
917 (get-slot-value ptr 'sql-c-timestamp 'year) year
918 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
921 (defun date-to-universal-time (ptr)
922 (declare (type c-date-ptr-type ptr))
923 (encode-universal-time
925 (get-slot-value ptr 'sql-c-timestamp 'day)
926 (get-slot-value ptr 'sql-c-timestamp 'month)
927 (get-slot-value ptr 'sql-c-timestamp 'year)))
929 (defun time-to-universal-time (ptr)
930 (declare (type c-time-ptr-type ptr))
931 (encode-universal-time
932 (get-slot-value ptr 'sql-c-timestamp 'second)
933 (get-slot-value ptr 'sql-c-timestamp 'minute)
934 (get-slot-value ptr 'sql-c-timestamp 'hour)
940 (defun %set-attr-odbc-version (henv version)
941 (with-error-handling (:henv henv)
942 (SQLSetEnvAttr henv $SQL_ATTR_ODBC_VERSION
943 (make-pointer version :void) 0)))
945 (defun %list-tables (hstmt)
946 (with-error-handling (:hstmt hstmt)
947 (SQLTables hstmt +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0)))
949 (defun %table-statistics (table hstmt &key unique (ensure t))
950 (with-cstrings ((table-cs table))
951 (with-error-handling (:hstmt hstmt)
957 (if unique $SQL_INDEX_UNIQUE $SQL_INDEX_ALL)
958 (if ensure $SQL_ENSURE $SQL_QUICK)))))
960 (defun %list-data-sources (henv)
961 (let ((dsn (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
962 (desc (allocate-foreign-string 256))
965 (with-foreign-objects ((dsn-len :short)
967 (let ((res (with-error-handling (:henv henv)
968 (SQLDataSources henv $SQL_FETCH_FIRST dsn
969 (1+ $SQL_MAX_DSN_LENGTH)
970 dsn-len desc 256 desc-len))))
971 (when (or (eql res $SQL_SUCCESS)
972 (eql res $SQL_SUCCESS_WITH_INFO))
973 (push (convert-from-foreign-string dsn) results))
975 (do ((res (with-error-handling (:henv henv)
976 (SQLDataSources henv $SQL_FETCH_NEXT dsn
977 (1+ $SQL_MAX_DSN_LENGTH)
978 dsn-len desc 256 desc-len))
979 (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 ((not (or (eql res $SQL_SUCCESS)
984 (eql res $SQL_SUCCESS_WITH_INFO))))
985 (push (convert-from-foreign-string dsn) results))))
987 (free-foreign-object dsn)
988 (free-foreign-object desc)))