1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: odbc -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: odbc-ff-interface.lisp
6 ;;;; Purpose: Function definitions for UFFI interface to ODBC
7 ;;;; Author: Kevin M. Rosenberg, 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 ;;;; *************************************************************************
21 (defvar *null* (make-null-pointer :byte))
22 (defvar *binary-format* :unsigned-byte-vector)
23 (defvar *time-conversion-function* 'identity)
24 (defvar *trace-sql* nil)
27 (make-null-pointer :byte))
29 (defmacro %put-str (ptr string &optional max-length)
30 (let ((size (gensym)))
31 `(let ((,size (length ,string)))
32 (when (and ,max-length (> ,size ,max-length))
33 (error "string \"~a\" of length ~d is longer than max-length: ~d"
34 ,string ,size ,max-length))
36 (setf (deref-array ,ptr '(:array :unsigned-char) i) (char ,string i)))
37 (setf (deref-array ,ptr '(:array :unsigned-char) ,size) 0))))
39 (defun %cstring-into-vector (ptr vector offset size-in-bytes)
40 (dotimes (i size-in-bytes)
41 (setf (aref vector offset)
42 (deref-array ptr '(:array :unsigned-char) i))
46 (defun handle-error (henv hdbc hstmt)
47 (with-foreign-objects ((sql-state '(:array :unsigned-char 256))
48 (error-message '(:array :unsigned-char
49 #.$SQL_MAX_MESSAGE_LENGTH))
52 (SQLError henv hdbc hstmt sql-state
53 error-code error-message
54 $SQL_MAX_MESSAGE_LENGTH msg-length)
56 (convert-from-foreign-string error-message)
57 (convert-from-foreign-string sql-state)
58 (deref-pointer msg-length :short)
59 (deref-pointer error-code :long))))
61 ; test this: return a keyword for efficiency
62 (defun sql-state (henv hdbc hstmt)
63 (with-foreign-objects ((sql-state '(:array :unsigned-char 256))
64 (error-message '(:array :unsigned-char
65 #.$SQL_MAX_MESSAGE_LENGTH))
68 (SQLError henv hdbc hstmt sql-state error-code
69 error-message $SQL_MAX_MESSAGE_LENGTH msg-length)
70 (convert-from-foreign-string sql-state) ;(%cstring-to-keyword sql-state)
73 (defmacro with-error-handling ((&key henv hdbc hstmt (print-info t))
75 (let ((result-code (gensym "RC-")))
76 `(let ((,result-code ,odbc-call))
79 (progn ,result-code ,@body))
80 (#.$SQL_SUCCESS_WITH_INFO
82 (multiple-value-bind (error-message sql-state)
83 (handle-error (or ,henv (%null-ptr))
84 (or ,hdbc (%null-ptr))
85 (or ,hstmt (%null-ptr)))
86 (warn "[ODBC info] ~a state: ~a"
87 ,result-code error-message
89 (progn ,result-code ,@body))
90 (#.$SQL_INVALID_HANDLE
91 (error "[ODBC error] Invalid handle"))
92 (#.$SQL_STILL_EXECUTING
93 (error "[ODBC error] Still executing"))
95 (multiple-value-bind (error-message sql-state)
96 (handle-error (or ,henv (%null-ptr))
97 (or ,hdbc (%null-ptr))
98 (or ,hstmt (%null-ptr)))
99 (error "[ODBC error] ~a; state: ~a" error-message sql-state)))
101 (progn ,result-code ,@body))))))
103 (defun %new-environment-handle ()
104 (with-foreign-object (phenv 'sql-handle-ptr)
108 (deref-pointer phenv 'sql-handle-ptr))))
110 (defun %sql-free-environment (henv)
115 (defun %new-db-connection-handle (henv)
116 (with-foreign-object (phdbc 'sql-handle-ptr)
119 (SQLAllocConnect henv phdbc)
120 (deref-pointer phdbc 'sql-handle-ptr))))
122 (defun %free-statement (hstmt option)
130 (:unbind $SQL_UNBIND)
131 (:reset $SQL_RESET_PARAMS)))))
133 (defmacro with-statement-handle ((hstmt hdbc) &body body)
134 `(let ((,hstmt (%new-statement-handle ,hdbc)))
137 (%free-statement ,hstmt :drop))))
139 ;; functional interface
141 (defun %sql-connect (hdbc server uid pwd)
142 (with-cstrings ((server-ptr server)
147 (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr
148 $SQL_NTS pwd-ptr $SQL_NTS))))
151 (defun %disconnect (hdbc)
154 (SQLDisconnect hdbc)))
156 (defun %commit (henv hdbc)
158 (:henv henv :hdbc hdbc)
160 henv hdbc $SQL_COMMIT)))
162 (defun %rollback (henv hdbc)
164 (:henv henv :hdbc hdbc)
166 henv hdbc $SQL_ROLLBACK)))
168 ; col-nr is zero-based in Lisp
169 ; col-nr = :bookmark retrieves a bookmark.
170 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
174 (if (eq column-nr :bookmark) 0 (1+ column-nr))
175 c-type data-ptr precision out-len-ptr)))
177 ; parameter-nr is zero-based in Lisp
178 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
179 sql-type precision scale data-ptr
180 max-value out-len-ptr)
183 (SQLBindParameter hstmt (1+ parameter-nr)
184 parameter-type ;$SQL_PARAM_INPUT
186 sql-type ;$SQL_VARCHAR
187 precision ;(1- (length str))
191 out-len-ptr ;#.(%null-ptr)
194 (defun %sql-fetch (hstmt)
199 (defun %new-statement-handle (hdbc)
200 (with-foreign-object (hstmt-ptr 'sql-handle-ptr)
203 (SQLAllocStmt hdbc hstmt-ptr)
204 (deref-pointer hstmt-ptr 'sql-handle-ptr))))
206 (defun %sql-get-info (hdbc info-type)
208 ;; those return string
209 ((#.$SQL_ACCESSIBLE_PROCEDURES
210 #.$SQL_ACCESSIBLE_TABLES
212 #.$SQL_DATA_SOURCE_NAME
213 #.$SQL_DATA_SOURCE_READ_ONLY
217 #.$SQL_DRIVER_ODBC_VER
219 #.$SQL_EXPRESSIONS_IN_ORDERBY
220 #.$SQL_IDENTIFIER_QUOTE_CHAR
222 #.$SQL_LIKE_ESCAPE_CLAUSE
223 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
224 #.$SQL_MULT_RESULT_SETS
225 #.$SQL_MULTIPLE_ACTIVE_TXN
226 #.$SQL_NEED_LONG_DATA_LEN
227 #.$SQL_ODBC_SQL_OPT_IEF
229 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
232 #.$SQL_PROCEDURE_TERM
234 #.$SQL_QUALIFIER_NAME_SEPARATOR
235 #.$SQL_QUALIFIER_TERM
237 #.$SQL_SEARCH_PATTERN_ESCAPE
239 #.$SQL_SPECIAL_CHARACTERS
242 (with-foreign-objects ((info-ptr '(:array :unsigned-char 1024))
243 (info-length-ptr :short))
247 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
249 (SQLGetInfo-Str hdbc info-type info-ptr 1023 info-length-ptr)
250 (convert-from-foreign-string info-ptr))))
251 ;; those returning a word
252 ((#.$SQL_ACTIVE_CONNECTIONS
253 #.$SQL_ACTIVE_STATEMENTS
254 #.$SQL_CONCAT_NULL_BEHAVIOR
255 #.$SQL_CORRELATION_NAME
256 #.$SQL_CURSOR_COMMIT_BEHAVIOR
257 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
258 #.$SQL_MAX_COLUMN_NAME_LEN
259 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
260 #.$SQL_MAX_COLUMNS_IN_INDEX
261 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
262 #.$SQL_MAX_COLUMNS_IN_SELECT
263 #.$SQL_MAX_COLUMNS_IN_TABLE
264 #.$SQL_MAX_CURSOR_NAME_LEN
265 #.$SQL_MAX_OWNER_NAME_LEN
266 #.$SQL_MAX_PROCEDURE_NAME_LEN
267 #.$SQL_MAX_QUALIFIER_NAME_LEN
268 #.$SQL_MAX_TABLE_NAME_LEN
269 #.$SQL_MAX_TABLES_IN_SELECT
270 #.$SQL_MAX_USER_NAME_LEN
271 #.$SQL_NON_NULLABLE_COLUMNS
272 #.$SQL_NULL_COLLATION
273 #.$SQL_ODBC_API_CONFORMANCE
274 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
275 #.$SQL_ODBC_SQL_CONFORMANCE
276 #.$SQL_QUALIFIER_LOCATION
277 #.$SQL_QUOTED_IDENTIFIER_CASE
279 (with-foreign-objects ((info-ptr :short)
280 (info-length-ptr :short))
288 (deref-pointer info-ptr :short)))
290 ;; those returning a long bitmask
292 #.$SQL_BOOKMARK_PERSISTENCE
293 #.$SQL_CONVERT_BIGINT
294 #.$SQL_CONVERT_BINARY
298 #.$SQL_CONVERT_DECIMAL
299 #.$SQL_CONVERT_DOUBLE
301 #.$SQL_CONVERT_INTEGER
302 #.$SQL_CONVERT_LONGVARCHAR
303 #.$SQL_CONVERT_NUMERIC
305 #.$SQL_CONVERT_SMALLINT
307 #.$SQL_CONVERT_TIMESTAMP
308 #.$SQL_CONVERT_TINYINT
309 #.$SQL_CONVERT_VARBINARY
310 #.$SQL_CONVERT_VARCHAR
311 #.$SQL_CONVERT_LONGVARBINARY
312 #.$SQL_CONVERT_FUNCTIONS
313 #.$SQL_FETCH_DIRECTION
315 #.$SQL_GETDATA_EXTENSIONS
317 #.$SQL_MAX_INDEX_SIZE
319 #.$SQL_MAX_STATEMENT_LEN
320 #.$SQL_NUMERIC_FUNCTIONS
322 #.$SQL_POS_OPERATIONS
323 #.$SQL_POSITIONED_STATEMENTS
324 #.$SQL_QUALIFIER_USAGE
325 #.$SQL_SCROLL_CONCURRENCY
326 #.$SQL_SCROLL_OPTIONS
327 #.$SQL_STATIC_SENSITIVITY
328 #.$SQL_STRING_FUNCTIONS
330 #.$SQL_SYSTEM_FUNCTIONS
331 #.$SQL_TIMEDATE_ADD_INTERVALS
332 #.$SQL_TIMEDATE_DIFF_INTERVALS
333 #.$SQL_TIMEDATE_FUNCTIONS
334 #.$SQL_TXN_ISOLATION_OPTION
336 (with-foreign-objects ((info-ptr :long)
337 (info-length-ptr :short))
345 (deref-pointer info-ptr :long)))
347 ;; those returning a long integer
348 ((#.$SQL_DEFAULT_TXN_ISOLATION
354 #.$SQL_IDENTIFIER_CASE
355 #.$SQL_MAX_BINARY_LITERAL_LEN
356 #.$SQL_MAX_CHAR_LITERAL_LEN
357 #.$SQL_ACTIVE_ENVIRONMENTS
359 (with-foreign-objects ((info-ptr :long)
360 (info-length-ptr :short))
363 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
364 (deref-pointer info-ptr :long))))))
366 (defun %sql-exec-direct (sql hstmt henv hdbc)
367 (with-cstring (sql-ptr sql)
369 (:hstmt hstmt :henv henv :hdbc hdbc)
370 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
372 (defun %sql-cancel (hstmt)
377 (defun %sql-execute (hstmt)
382 (defun result-columns-count (hstmt)
383 (with-foreign-objects ((columns-nr-ptr :short))
384 (with-error-handling (:hstmt hstmt)
385 (SQLNumResultCols hstmt columns-nr-ptr)
386 (deref-pointer columns-nr-ptr :short))))
388 (defun result-rows-count (hstmt)
389 (with-foreign-objects ((row-count-ptr :long))
390 (with-error-handling (:hstmt hstmt)
391 (SQLRowCount hstmt row-count-ptr)
392 (deref-pointer row-count-ptr :long))))
394 ;; column counting is 1-based
395 (defun %describe-column (hstmt column-nr)
396 (with-foreign-objects ((column-name-ptr '(:array :unsigned-char 256))
397 (column-name-length-ptr :short)
398 (column-sql-type-ptr :short)
399 (column-precision-ptr :long)
400 (column-scale-ptr :short)
401 (column-nullable-p-ptr :short))
402 (with-error-handling (:hstmt hstmt)
403 (SQLDescribeCol hstmt column-nr column-name-ptr 256
404 column-name-length-ptr
408 column-nullable-p-ptr)
410 (convert-from-foreign-string column-name-ptr)
411 (deref-pointer column-sql-type-ptr :short)
412 (deref-pointer column-precision-ptr :long)
413 (deref-pointer column-scale-ptr :short)
414 (deref-pointer column-nullable-p-ptr :short)))))
416 ;; parameter counting is 1-based
417 (defun %describe-parameter (hstmt parameter-nr)
418 (with-foreign-objects ((column-sql-type-ptr :short)
419 (column-precision-ptr :long)
420 (column-scale-ptr :short)
421 (column-nullable-p-ptr :short))
424 (SQLDescribeParam hstmt parameter-nr
428 column-nullable-p-ptr)
430 (deref-pointer column-sql-type-ptr :short)
431 (deref-pointer column-precision-ptr :long)
432 (deref-pointer column-scale-ptr :short)
433 (deref-pointer column-nullable-p-ptr :short)))))
435 (defun %column-attributes (hstmt column-nr descriptor-type)
436 (with-foreign-objects ((descriptor-info-ptr '(:array :unsigned-char 256))
437 (descriptor-length-ptr :short)
438 (numeric-descriptor-ptr :long))
441 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr 256
442 descriptor-length-ptr
443 numeric-descriptor-ptr)
445 (convert-from-foreign-string descriptor-info-ptr)
446 (deref-pointer numeric-descriptor-ptr :long)))))
448 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
449 table-name column-name)
450 (with-cstrings ((table-qualifier-ptr table-qualifier)
451 (table-owner-ptr table-owner)
452 (table-name-ptr table-name)
453 (column-name-ptr column-name))
457 table-qualifier-ptr (length table-qualifier)
458 table-owner-ptr (length table-owner)
459 table-name-ptr (length table-name)
460 column-name-ptr (length column-name)))))
462 (defun %describe-columns (hdbc table-qualifier table-owner
463 table-name column-name)
464 (with-statement-handle (hstmt hdbc)
465 (%prepare-describe-columns hstmt table-qualifier table-owner
466 table-name column-name)
467 (fetch-all-rows hstmt)))
469 (defun %sql-data-sources (henv &key (direction :first))
470 (with-foreign-objects
471 ((name-ptr '(:array :unsigned-char #.(1+ $SQL_MAX_DSN_LENGTH)))
472 (name-length-ptr :short)
473 (description-ptr '(:array :unsigned-char 1024))
474 (description-length-ptr :short))
475 (let ((res (with-error-handling
479 (:first $SQL_FETCH_FIRST)
480 (:next $SQL_FETCH_NEXT))
482 (1+ $SQL_MAX_DSN_LENGTH)
486 description-length-ptr))))
487 (unless (= res $SQL_NO_DATA_FOUND)
488 (values (convert-from-foreign-string name-ptr)
489 (convert-from-foreign-string description-ptr))))))
491 (defun sql-to-c-type (sql-type)
493 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
494 #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9) $SQL_C_CHAR)
495 (#.$SQL_INTEGER $SQL_C_SLONG)
496 (#.$SQL_SMALLINT $SQL_C_SSHORT)
497 ((#.$SQL_FLOAT #.$SQL_DOUBLE) $SQL_C_DOUBLE)
498 (#.$SQL_REAL $SQL_C_FLOAT)
499 (#.$SQL_DATE $SQL_C_DATE)
500 (#.$SQL_TIME $SQL_C_TIME)
501 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
502 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
503 (#.$SQL_TINYINT $SQL_C_STINYINT)
504 (#.$SQL_BIT $SQL_C_BIT)))
506 (defun get-cast-byte (ptr)
507 (declare (type long-ptr-type out-len-ptr))
508 (with-cast-pointer (casted ptr '(* :byte))
509 (deref-pointer casted :byte)))
511 (defun get-cast-short (ptr)
512 (declare (type long-ptr-type out-len-ptr))
513 (with-cast-pointer (casted ptr '(* :short))
514 (deref-pointer casted :short)))
516 (defun get-cast-int (ptr)
517 (declare (type long-ptr-type out-len-ptr))
518 (with-cast-pointer (casted ptr '(* :int))
519 (deref-pointer casted :int)))
521 (defun get-cast-long (ptr)
522 (declare (type long-ptr-type out-len-ptr))
523 (with-cast-pointer (casted ptr '(* :long))
524 (deref-pointer casted :long)))
526 (defun get-cast-single-float (ptr)
527 (declare (type long-ptr-type out-len-ptr))
528 (with-cast-pointer (casted ptr '(* :float))
529 (deref-pointer casted :float)))
531 (defun get-cast-double-float (ptr)
532 (declare (type long-ptr-type out-len-ptr))
533 (with-cast-pointer (casted ptr '(* :double))
534 (deref-pointer casted :double)))
536 (defun get-cast-foreign-string (ptr)
537 (declare (type long-ptr-type out-len-ptr))
538 (with-cast-pointer (casted ptr '(* :unsigned-char))
539 (convert-from-foreign-string casted)))
541 (defun get-cast-binary (ptr len format)
542 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
543 (with-cast-pointer (casted ptr '(* :byte))
545 (:unsigned-byte-vector
546 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
548 (setf (aref vector i)
549 (deref-array casted '(:array :byte) i)))
552 (let ((vector (make-array (ash len 3) :element-type 'bit)))
554 (let ((byte (deref-array casted '(:array :byte) i)))
556 (setf (bit vector (+ (ash i 3) j)) (logand (ash byte (- j 7)) 1)))))
560 (defun read-data (data-ptr c-type sql-type out-len-ptr convert-to-string-p)
561 (declare (type long-ptr-type out-len-ptr))
562 (let ((out-len (deref-pointer out-len-ptr :long)))
563 (cond ((= out-len $SQL_NULL_DATA)
567 (convert-from-foreign-string data-ptr))
570 ;; SQL extended datatypes
571 (#.$SQL_TINYINT (get-cast-short data-ptr))
572 (#.$SQL_C_STINYINT (get-cast-short data-ptr)) ;; ?
573 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
574 (#.$SQL_SMALLINT (deref-pointer data-ptr :short)) ; ??
575 (#.$SQL_INTEGER (deref-pointer data-ptr :long))
577 (let ((*read-base* 10))
578 (read-from-string (get-cast-foreign-string data-ptr))))
582 (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
584 (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
585 (funcall *time-conversion-function* universal-time frac)))
587 (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
588 (funcall *time-conversion-function* universal-time frac)))
590 (get-cast-int data-ptr))
592 (get-cast-single-float data-ptr))
594 (get-cast-double-float data-ptr))
596 (get-cast-long data-ptr))
598 (#.$SQL_C_BIT ; encountered only in Access
599 (get-cast-byte data-ptr))
601 (get-cast-binary data-ptr out-len *binary-format*))
602 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
603 (get-cast-short data-ptr)) ; LMH
606 (code-char (get-cast-short data-ptr)))
608 (convert-from-foreign-string data-ptr)))))))))
610 ;; which value is appropriate?
611 (defparameter +max-precision+
615 (defvar *break-on-unknown-data-type* t)
617 ;; C. Stacy's idea to factor this out
618 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
619 ;; so that I don't have to remember to make changes in more than one place.
620 ;; Just keep it in synch with READ-DATA."
621 (defun %allocate-bindings (sql-type precision)
622 (let* ((c-type (sql-to-c-type sql-type))
623 (size (if (zerop precision)
624 +max-precision+ ;; if the precision cannot be determined
625 (min precision +max-precision+)))
626 (long-p (= size +max-precision+))
628 (case c-type ;; add more?
629 (#.$SQL_C_SLONG (uffi:allocate-foreign-object :long))
630 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
631 (#.$SQL_C_DATE (allocate-foreign-object 'sql-c-date))
632 (#.$SQL_C_TIME (allocate-foreign-object 'sql-c-time))
633 (#.$SQL_C_TIMESTAMP (allocate-foreign-object 'sql-c-timestamp))
634 #+lispworks(#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
635 (#.$SQL_C_BIT (uffi:allocate-foreign-object :boolean))
636 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
637 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
638 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
639 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
641 ;; Maybe should signal a restartable condition for this?
642 (when *break-on-unknown-data-type*
643 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
644 sql-type precision size c-type))
645 (uffi:allocate-foreign-object :ptr (1+ size)))))
646 (out-len-ptr (uffi:allocate-foreign-object :long)))
647 (values c-type data-ptr out-len-ptr size long-p)))
649 (defun fetch-all-rows (hstmt &key free-option flatp)
650 (let ((column-count (result-columns-count hstmt)))
651 (unless (zerop column-count)
652 (let ((names (make-array column-count :element-type 'string))
653 (sql-types (make-array column-count :element-type 'fixnum))
654 (c-types (make-array column-count :element-type 'fixnum))
655 (precisions (make-array column-count :element-type 'fixnum))
656 (data-ptrs (make-array column-count :initial-element nil))
657 (out-len-ptrs (make-array column-count :initial-element nil))
658 (scales (make-array column-count :element-type 'fixnum))
659 (nullables-p (make-array column-count :element-type 'fixnum)))
663 (dotimes (col-nr column-count)
664 ;; get column information
665 (multiple-value-bind (name sql-type precision scale nullable-p)
666 (%describe-column hstmt (1+ col-nr))
667 ;; allocate space to bind result rows to
668 (multiple-value-bind (c-type data-ptr out-len-ptr)
669 (%allocate-bindings sql-type precision)
670 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
671 (setf (svref names col-nr) name
672 (aref sql-types col-nr) sql-type
673 (aref c-types col-nr) (sql-to-c-type sql-type)
674 (aref precisions col-nr) (if (zerop precision) nil precision)
675 (aref scales col-nr) scale
676 (aref nullables-p col-nr) nullable-p
677 (aref data-ptrs col-nr) data-ptr
678 (aref out-len-ptrs col-nr) out-len-ptr))))
682 (when (> column-count 1)
683 (error "If more than one column is to be fetched, flatp has to be nil."))
684 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
686 (read-data (aref data-ptrs 0)
689 (aref out-len-ptrs 0)
692 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
694 (loop for col-nr from 0 to (1- column-count)
696 (read-data (aref data-ptrs col-nr)
697 (aref c-types col-nr)
698 (aref sql-types col-nr)
699 (aref out-len-ptrs col-nr)
702 ;; dispose of memory etc
703 (when free-option (%free-statement hstmt free-option))
704 (dotimes (col-nr column-count)
705 (let ((data-ptr (aref data-ptrs col-nr))
706 (out-len-ptr (aref out-len-ptrs col-nr)))
707 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
708 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
710 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
711 ;; of %do-fetch into sql package (has been done)
713 (defun %sql-prepare (hstmt sql)
714 (with-cstring (sql-ptr sql)
715 (with-error-handling (:hstmt hstmt)
716 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
718 ;; depending on option, we return a long int or a string; string not implemented
719 (defun get-connection-option (hdbc option)
720 (with-foreign-objects ((param-ptr :long #+ignore #.(1+ $SQL_MAX_OPTION_STRING_LENGTH)))
721 (with-error-handling (:hdbc hdbc)
722 (SQLGetConnectOption hdbc option param-ptr)
723 (deref-pointer param-ptr :long))))
725 (defun set-connection-option (hdbc option param)
726 (with-error-handling (:hdbc hdbc)
727 (SQLSetConnectOption hdbc option param)))
729 (defun disable-autocommit (hdbc)
730 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
732 (defun enable-autocommit (hdbc)
733 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
735 (defun %sql-set-pos (hstmt row option lock)
738 (SQLSetPos hstmt row option lock)))
740 (defun %sql-extended-fetch (hstmt fetch-type row)
741 (with-foreign-objects ((row-count-ptr :unsigned-long)
742 (row-status-ptr :short))
743 (with-error-handling (:hstmt hstmt)
744 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
746 (values (deref-pointer row-count-ptr :unsigned-long)
747 (deref-pointer row-status-ptr :short)))))
749 ; column-nr is zero-based
750 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
752 (:hstmt hstmt :print-info nil)
753 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
754 c-type data-ptr precision out-len-ptr)))
756 (defun %sql-param-data (hstmt param-ptr)
757 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
758 (SQLParamData hstmt param-ptr)))
760 (defun %sql-put-data (hstmt data-ptr size)
762 (:hstmt hstmt :print-info t) ;; nil
763 (SQLPutData hstmt data-ptr size)))
765 (defconstant $sql-data-truncated (intern "01004" :keyword))
767 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
768 out-len-ptr convert-to-string-p)
769 (declare (ignore convert-to-string-p) ; prelimianary
770 (type long-ptr-type out-len-ptr))
771 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
772 +max-precision+ out-len-ptr))
773 (out-len (deref-pointer out-len-ptr :long))
777 (return-from read-data-in-chunks *null*))
778 (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
779 (let ((str (make-array 0 :element-type 'character :adjustable t)))
780 (loop do (if (= c-type #.$SQL_CHAR)
781 (let ((data-length (foreign-string-length data-ptr)))
782 (adjust-array str (+ offset data-length)
783 :initial-element #\?)
784 (setf offset (%cstring-into-vector
788 (error "wrong type. preliminary."))
789 while (and (= res $SQL_SUCCESS_WITH_INFO)
790 (equal (sql-state (%null-ptr) (%null-ptr) hstmt)
792 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
793 +max-precision+ out-len-ptr)))
794 (setf str (coerce str 'string))
795 (if (= sql-type $SQL_DECIMAL)
796 (let ((*read-base* 10))
797 (read-from-string str))
800 (let ((str (make-string out-len)))
801 (loop do (if (= c-type #.$SQL_CHAR)
802 (setf offset (%cstring-into-vector ;string
805 (min out-len (1- +max-precision+))))
806 (error "wrong type. preliminary."))
808 (and (= res $SQL_SUCCESS_WITH_INFO)
809 #+ingore(eq (sql-state (%null-ptr) (%null-ptr) hstmt)
811 (equal (sql-state (%null-ptr) (%null-ptr) hstmt)
813 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
814 +max-precision+ out-len-ptr)
815 out-len (deref-pointer out-len-ptr :long)))
816 (if (= sql-type $SQL_DECIMAL)
817 (let ((*read-base* 10))
818 (read-from-string str))
821 (defun timestamp-to-universal-time (ptr)
823 (encode-universal-time
824 (get-slot-value ptr 'sql-c-timestamp 'second)
825 (get-slot-value ptr 'sql-c-timestamp 'minute)
826 (get-slot-value ptr 'sql-c-timestamp 'hour)
827 (get-slot-value ptr 'sql-c-timestamp 'day)
828 (get-slot-value ptr 'sql-c-timestamp 'month)
829 (get-slot-value ptr 'sql-c-timestamp 'year))
830 (get-slot-value ptr 'sql-c-timestamp 'fraction)))
832 (defun universal-time-to-timestamp (time &optional (fraction 0))
833 (multiple-value-bind (sec min hour day month year)
834 (decode-universal-time time)
835 (with-foreign-object (ptr 'sql-c-timestamp)
836 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
837 (get-slot-value ptr 'sql-c-timestamp 'minute) min
838 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
839 (get-slot-value ptr 'sql-c-timestamp 'day) day
840 (get-slot-value ptr 'sql-c-timestamp 'month) month
841 (get-slot-value ptr 'sql-c-timestamp 'year) year
842 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
845 (defun %put-timestamp (ptr time &optional (fraction 0))
846 (multiple-value-bind (sec min hour day month year)
847 (decode-universal-time time)
848 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
849 (get-slot-value ptr 'sql-c-timestamp 'minute) min
850 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
851 (get-slot-value ptr 'sql-c-timestamp 'day) day
852 (get-slot-value ptr 'sql-c-timestamp 'month) month
853 (get-slot-value ptr 'sql-c-timestamp 'year) year
854 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
857 (defun date-to-universal-time (ptr)
858 (encode-universal-time
860 (get-slot-value ptr 'sql-c-timestamp 'day)
861 (get-slot-value ptr 'sql-c-timestamp 'month)
862 (get-slot-value ptr 'sql-c-timestamp 'year)))
864 (defun time-to-universal-time (ptr)
865 (encode-universal-time
866 (get-slot-value ptr 'sql-c-timestamp 'second)
867 (get-slot-value ptr 'sql-c-timestamp 'minute)
868 (get-slot-value ptr 'sql-c-timestamp 'hour)