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* (lambda (universal-time &optional fraction)
28 (declare (ignore fraction))
30 "Bound to a function that converts from a Lisp universal time fixnum (and a fractional
31 as possible second argument) to the desired representation of date/time/timestamp.")
33 (defvar +null-ptr+ (make-null-pointer :byte))
34 (defparameter +null-handle-ptr+ (make-null-pointer :void))
35 (defvar *info-output* nil
36 "Stream to send SUCCESS_WITH_INFO messages.")
38 (defmacro %put-str (ptr string &optional max-length)
39 (let ((size (gensym)))
40 `(let ((,size (length ,string)))
41 (when (and ,max-length (> ,size ,max-length))
42 (error "string \"~a\" of length ~d is longer than max-length: ~d"
43 ,string ,size ,max-length))
44 (with-cast-pointer (char-ptr ,ptr :byte)
46 (setf (deref-array char-ptr '(:array :byte) i)
47 (char-code (char ,string i))))
48 (setf (deref-array char-ptr '(:array :byte) ,size) 0)))))
50 (defun %cstring-into-vector (ptr vector offset size-in-bytes)
51 (dotimes (i size-in-bytes)
52 (setf (schar vector offset)
53 (ensure-char-character
54 (deref-array ptr '(:array :unsigned-char) i)))
58 (defun handle-error (henv hdbc hstmt)
59 (let ((sql-state (allocate-foreign-string 256))
60 (error-message (allocate-foreign-string #.$SQL_MAX_MESSAGE_LENGTH)))
61 (with-foreign-objects ((error-code :long)
63 (SQLError henv hdbc hstmt sql-state
64 error-code error-message
65 #.$SQL_MAX_MESSAGE_LENGTH msg-length)
66 (let ((err (convert-from-foreign-string error-message))
67 (state (convert-from-foreign-string sql-state)))
68 (free-foreign-object error-message)
69 (free-foreign-object sql-state)
73 (deref-pointer msg-length :short)
74 (deref-pointer error-code :long))))))
76 (defun sql-state (henv hdbc hstmt)
77 (let ((sql-state (allocate-foreign-string 256))
78 (error-message (allocate-foreign-string #.$SQL_MAX_MESSAGE_LENGTH)))
79 (with-foreign-objects ((error-code :long)
81 (SQLError henv hdbc hstmt sql-state error-code
82 error-message #.$SQL_MAX_MESSAGE_LENGTH msg-length)
83 (let ((state (convert-from-foreign-string sql-state)))
84 (free-foreign-object error-message)
85 (free-foreign-object sql-state)
87 ;; test this: return a keyword for efficiency
88 ;;(%cstring-to-keyword state)
91 (defmacro with-error-handling ((&key henv hdbc hstmt (print-info t))
93 (let ((result-code (gensym "RC-")))
94 `(let ((,result-code ,odbc-call))
97 (progn ,result-code ,@body))
98 (#.$SQL_SUCCESS_WITH_INFO
100 (multiple-value-bind (error-message sql-state)
101 (handle-error (or ,henv +null-handle-ptr+)
102 (or ,hdbc +null-handle-ptr+)
103 (or ,hstmt +null-handle-ptr+))
105 (format *info-output* "[ODBC info ~A] ~A state: ~A"
106 ,result-code error-message
108 (progn ,result-code ,@body))
109 (#.$SQL_INVALID_HANDLE
111 'clsql-base-sys:clsql-odbc-error
112 :odbc-message "Invalid handle"))
113 (#.$SQL_STILL_EXECUTING
115 'clsql-base-sys:clsql-odbc-error
116 :odbc-message "Still executing"))
118 (multiple-value-bind (error-message sql-state)
119 (handle-error (or ,henv +null-handle-ptr+)
120 (or ,hdbc +null-handle-ptr+)
121 (or ,hstmt +null-handle-ptr+))
123 'clsql-base-sys:clsql-odbc-error
124 :odbc-message error-message
125 :sql-state sql-state)))
127 (progn ,result-code ,@body))))))
129 (defun %new-environment-handle ()
131 (with-foreign-object (phenv 'sql-handle)
135 (deref-pointer phenv 'sql-handle)))))
136 (%set-attr-odbc-version henv $SQL_OV_ODBC2)
140 (defun %sql-free-environment (henv)
145 (defun %new-db-connection-handle (henv)
146 (with-foreign-object (phdbc 'sql-handle)
149 (SQLAllocConnect henv phdbc)
150 (deref-pointer phdbc 'sql-handle))))
152 (defun %free-statement (hstmt option)
160 (:unbind $SQL_UNBIND)
161 (:reset $SQL_RESET_PARAMS)))))
163 (defmacro with-statement-handle ((hstmt hdbc) &body body)
164 `(let ((,hstmt (%new-statement-handle ,hdbc)))
167 (%free-statement ,hstmt :drop))))
169 ;; functional interface
171 (defun %sql-connect (hdbc server uid pwd)
172 (with-cstrings ((server-ptr server)
177 (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr
178 $SQL_NTS pwd-ptr $SQL_NTS))))
181 (defun %disconnect (hdbc)
184 (SQLDisconnect hdbc)))
186 (defun %commit (henv hdbc)
188 (:henv henv :hdbc hdbc)
190 henv hdbc $SQL_COMMIT)))
192 (defun %rollback (henv hdbc)
194 (:henv henv :hdbc hdbc)
196 henv hdbc $SQL_ROLLBACK)))
198 ; col-nr is zero-based in Lisp
199 ; col-nr = :bookmark retrieves a bookmark.
200 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
204 (if (eq column-nr :bookmark) 0 (1+ column-nr))
205 c-type data-ptr precision out-len-ptr)))
207 ; parameter-nr is zero-based in Lisp
208 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
209 sql-type precision scale data-ptr
210 max-value out-len-ptr)
213 (SQLBindParameter hstmt (1+ parameter-nr)
214 parameter-type ;$SQL_PARAM_INPUT
216 sql-type ;$SQL_VARCHAR
217 precision ;(1- (length str))
221 out-len-ptr ;#.+null-ptr+
224 (defun %sql-fetch (hstmt)
229 (defun %new-statement-handle (hdbc)
230 (with-foreign-object (hstmt-ptr 'sql-handle)
233 (SQLAllocStmt hdbc hstmt-ptr)
234 (deref-pointer hstmt-ptr 'sql-handle))))
236 (defun %sql-get-info (hdbc info-type)
238 ;; those return string
239 ((#.$SQL_ACCESSIBLE_PROCEDURES
240 #.$SQL_ACCESSIBLE_TABLES
242 #.$SQL_DATA_SOURCE_NAME
243 #.$SQL_DATA_SOURCE_READ_ONLY
247 #.$SQL_DRIVER_ODBC_VER
249 #.$SQL_EXPRESSIONS_IN_ORDERBY
250 #.$SQL_IDENTIFIER_QUOTE_CHAR
252 #.$SQL_LIKE_ESCAPE_CLAUSE
253 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
254 #.$SQL_MULT_RESULT_SETS
255 #.$SQL_MULTIPLE_ACTIVE_TXN
256 #.$SQL_NEED_LONG_DATA_LEN
257 #.$SQL_ODBC_SQL_OPT_IEF
259 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
262 #.$SQL_PROCEDURE_TERM
264 #.$SQL_QUALIFIER_NAME_SEPARATOR
265 #.$SQL_QUALIFIER_TERM
267 #.$SQL_SEARCH_PATTERN_ESCAPE
269 #.$SQL_SPECIAL_CHARACTERS
272 (let ((info-ptr (allocate-foreign-string 1024)))
273 (with-foreign-object (info-length-ptr :short)
276 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
277 (let ((info (convert-from-foreign-string info-ptr)))
278 (free-foreign-object info-ptr)
280 ;; those returning a word
281 ((#.$SQL_ACTIVE_CONNECTIONS
282 #.$SQL_ACTIVE_STATEMENTS
283 #.$SQL_CONCAT_NULL_BEHAVIOR
284 #.$SQL_CORRELATION_NAME
285 #.$SQL_CURSOR_COMMIT_BEHAVIOR
286 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
287 #.$SQL_MAX_COLUMN_NAME_LEN
288 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
289 #.$SQL_MAX_COLUMNS_IN_INDEX
290 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
291 #.$SQL_MAX_COLUMNS_IN_SELECT
292 #.$SQL_MAX_COLUMNS_IN_TABLE
293 #.$SQL_MAX_CURSOR_NAME_LEN
294 #.$SQL_MAX_OWNER_NAME_LEN
295 #.$SQL_MAX_PROCEDURE_NAME_LEN
296 #.$SQL_MAX_QUALIFIER_NAME_LEN
297 #.$SQL_MAX_TABLE_NAME_LEN
298 #.$SQL_MAX_TABLES_IN_SELECT
299 #.$SQL_MAX_USER_NAME_LEN
300 #.$SQL_NON_NULLABLE_COLUMNS
301 #.$SQL_NULL_COLLATION
302 #.$SQL_ODBC_API_CONFORMANCE
303 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
304 #.$SQL_ODBC_SQL_CONFORMANCE
305 #.$SQL_QUALIFIER_LOCATION
306 #.$SQL_QUOTED_IDENTIFIER_CASE
308 (with-foreign-objects ((info-ptr :short)
309 (info-length-ptr :short))
317 (deref-pointer info-ptr :short)))
319 ;; those returning a long bitmask
321 #.$SQL_BOOKMARK_PERSISTENCE
322 #.$SQL_CONVERT_BIGINT
323 #.$SQL_CONVERT_BINARY
327 #.$SQL_CONVERT_DECIMAL
328 #.$SQL_CONVERT_DOUBLE
330 #.$SQL_CONVERT_INTEGER
331 #.$SQL_CONVERT_LONGVARCHAR
332 #.$SQL_CONVERT_NUMERIC
334 #.$SQL_CONVERT_SMALLINT
336 #.$SQL_CONVERT_TIMESTAMP
337 #.$SQL_CONVERT_TINYINT
338 #.$SQL_CONVERT_VARBINARY
339 #.$SQL_CONVERT_VARCHAR
340 #.$SQL_CONVERT_LONGVARBINARY
341 #.$SQL_CONVERT_FUNCTIONS
342 #.$SQL_FETCH_DIRECTION
344 #.$SQL_GETDATA_EXTENSIONS
346 #.$SQL_MAX_INDEX_SIZE
348 #.$SQL_MAX_STATEMENT_LEN
349 #.$SQL_NUMERIC_FUNCTIONS
351 #.$SQL_POS_OPERATIONS
352 #.$SQL_POSITIONED_STATEMENTS
353 #.$SQL_QUALIFIER_USAGE
354 #.$SQL_SCROLL_CONCURRENCY
355 #.$SQL_SCROLL_OPTIONS
356 #.$SQL_STATIC_SENSITIVITY
357 #.$SQL_STRING_FUNCTIONS
359 #.$SQL_SYSTEM_FUNCTIONS
360 #.$SQL_TIMEDATE_ADD_INTERVALS
361 #.$SQL_TIMEDATE_DIFF_INTERVALS
362 #.$SQL_TIMEDATE_FUNCTIONS
363 #.$SQL_TXN_ISOLATION_OPTION
365 (with-foreign-objects ((info-ptr :long)
366 (info-length-ptr :short))
374 (deref-pointer info-ptr :long)))
376 ;; those returning a long integer
377 ((#.$SQL_DEFAULT_TXN_ISOLATION
383 #.$SQL_IDENTIFIER_CASE
384 #.$SQL_MAX_BINARY_LITERAL_LEN
385 #.$SQL_MAX_CHAR_LITERAL_LEN
386 #.$SQL_ACTIVE_ENVIRONMENTS
388 (with-foreign-objects ((info-ptr :long)
389 (info-length-ptr :short))
392 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
393 (deref-pointer info-ptr :long))))))
395 (defun %sql-exec-direct (sql hstmt henv hdbc)
396 (with-cstring (sql-ptr sql)
398 (:hstmt hstmt :henv henv :hdbc hdbc)
399 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
401 (defun %sql-cancel (hstmt)
406 (defun %sql-execute (hstmt)
411 (defun result-columns-count (hstmt)
412 (with-foreign-objects ((columns-nr-ptr :short))
413 (with-error-handling (:hstmt hstmt)
414 (SQLNumResultCols hstmt columns-nr-ptr)
415 (deref-pointer columns-nr-ptr :short))))
417 (defun result-rows-count (hstmt)
418 (with-foreign-objects ((row-count-ptr :long))
419 (with-error-handling (:hstmt hstmt)
420 (SQLRowCount hstmt row-count-ptr)
421 (deref-pointer row-count-ptr :long))))
423 ;; column counting is 1-based
424 (defun %describe-column (hstmt column-nr)
425 (let ((column-name-ptr (allocate-foreign-string 256)))
426 (with-foreign-objects ((column-name-length-ptr :short)
427 (column-sql-type-ptr :short)
428 (column-precision-ptr :unsigned-long)
429 (column-scale-ptr :short)
430 (column-nullable-p-ptr :short))
431 (with-error-handling (:hstmt hstmt)
432 (SQLDescribeCol hstmt column-nr column-name-ptr 256
433 column-name-length-ptr
437 column-nullable-p-ptr)
438 (let ((column-name (convert-from-foreign-string column-name-ptr)))
439 (free-foreign-object column-name-ptr)
442 (deref-pointer column-sql-type-ptr :short)
443 (deref-pointer column-precision-ptr :unsigned-long)
444 (deref-pointer column-scale-ptr :short)
445 (deref-pointer column-nullable-p-ptr :short)))))))
447 ;; parameter counting is 1-based
448 (defun %describe-parameter (hstmt parameter-nr)
449 (with-foreign-objects ((column-sql-type-ptr :short)
450 (column-precision-ptr :unsigned-long)
451 (column-scale-ptr :short)
452 (column-nullable-p-ptr :short))
455 (SQLDescribeParam hstmt parameter-nr
459 column-nullable-p-ptr)
461 (deref-pointer column-sql-type-ptr :short)
462 (deref-pointer column-precision-ptr :unsigned-long)
463 (deref-pointer column-scale-ptr :short)
464 (deref-pointer column-nullable-p-ptr :short)))))
466 (defun %column-attributes (hstmt column-nr descriptor-type)
467 (let ((descriptor-info-ptr (allocate-foreign-string 256)))
468 (with-foreign-objects ((descriptor-length-ptr :short)
469 (numeric-descriptor-ptr :long))
472 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr
473 256 descriptor-length-ptr
474 numeric-descriptor-ptr)
475 (let ((desc (convert-from-foreign-string descriptor-info-ptr)))
476 (free-foreign-object descriptor-info-ptr)
479 (deref-pointer numeric-descriptor-ptr :long)))))))
481 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
482 table-name column-name)
483 (with-cstrings ((table-qualifier-ptr table-qualifier)
484 (table-owner-ptr table-owner)
485 (table-name-ptr table-name)
486 (column-name-ptr column-name))
490 table-qualifier-ptr (length table-qualifier)
491 table-owner-ptr (length table-owner)
492 table-name-ptr (length table-name)
493 column-name-ptr (length column-name)))))
495 (defun %describe-columns (hdbc table-qualifier table-owner
496 table-name column-name)
497 (with-statement-handle (hstmt hdbc)
498 (%prepare-describe-columns hstmt table-qualifier table-owner
499 table-name column-name)
500 (fetch-all-rows hstmt)))
502 (defun %sql-data-sources (henv &key (direction :first))
503 (let ((name-ptr (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
504 (description-ptr (allocate-foreign-string 1024)))
505 (with-foreign-objects ((name-length-ptr :short)
506 (description-length-ptr :short))
507 (let ((res (with-error-handling
511 (:first $SQL_FETCH_FIRST)
512 (:next $SQL_FETCH_NEXT))
514 (1+ $SQL_MAX_DSN_LENGTH)
518 description-length-ptr))))
520 ((= res $SQL_NO_DATA_FOUND)
521 (let ((name (convert-from-foreign-string name-ptr))
522 (desc (convert-from-foreign-string description-ptr)))
523 (free-foreign-object name-ptr)
524 (free-foreign-object description-ptr)
529 (free-foreign-object name-ptr)
530 (free-foreign-object description-ptr)
535 (defun sql-to-c-type (sql-type)
537 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
538 #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9) $SQL_C_CHAR)
539 (#.$SQL_INTEGER $SQL_C_SLONG)
540 (#.$SQL_SMALLINT $SQL_C_SSHORT)
541 (#.$SQL_DOUBLE $SQL_C_DOUBLE)
542 (#.$SQL_FLOAT $SQL_C_DOUBLE)
543 (#.$SQL_REAL $SQL_C_FLOAT)
544 (#.$SQL_DATE $SQL_C_DATE)
545 (#.$SQL_TIME $SQL_C_TIME)
546 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
547 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
548 (#.$SQL_TINYINT $SQL_C_STINYINT)
549 (#.$SQL_BIT $SQL_C_BIT)))
551 (def-type byte-pointer-type '(* :byte))
552 (def-type short-pointer-type '(* :short))
553 (def-type int-pointer-type '(* :int))
554 (def-type long-pointer-type '(* :long))
555 (def-type float-pointer-type '(* :float))
556 (def-type double-pointer-type '(* :double))
557 (def-type string-pointer-type '(* :unsigned-char))
559 (defun get-cast-byte (ptr)
560 (locally (declare (type byte-pointer-type ptr))
561 (deref-pointer ptr :byte)))
563 (defun get-cast-short (ptr)
564 (locally (declare (type short-pointer-type ptr))
565 (deref-pointer ptr :short)))
567 (defun get-cast-int (ptr)
568 (locally (declare (type int-pointer-type ptr))
569 (deref-pointer ptr :int)))
571 (defun get-cast-long (ptr)
572 (locally (declare (type long-pointer-type ptr))
573 (deref-pointer ptr :long)))
575 (defun get-cast-single-float (ptr)
576 (locally (declare (type float-pointer-type ptr))
577 (deref-pointer ptr :float)))
579 (defun get-cast-double-float (ptr)
580 (locally (declare (type double-pointer-type ptr))
581 (deref-pointer ptr :double)))
583 (defun get-cast-foreign-string (ptr)
584 (locally (declare (type string-pointer-type ptr))
585 (convert-from-foreign-string ptr)))
587 (defun get-cast-binary (ptr len format)
588 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
589 (with-cast-pointer (casted ptr :byte)
591 (:unsigned-byte-vector
592 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
594 (setf (aref vector i)
595 (deref-array casted '(:array :byte) i)))
598 (let ((vector (make-array (ash len 3) :element-type 'bit)))
600 (let ((byte (deref-array casted '(:array :byte) i)))
602 (setf (bit vector (+ (ash i 3) j))
603 (logand (ash byte (- j 7)) 1)))))
607 (defun read-data (data-ptr c-type sql-type out-len-ptr result-type)
608 (declare (type long-ptr-type out-len-ptr))
609 (let* ((out-len (deref-pointer out-len-ptr :long))
611 (cond ((= out-len $SQL_NULL_DATA)
615 ;; SQL extended datatypes
616 (#.$SQL_TINYINT (get-cast-byte data-ptr))
617 (#.$SQL_C_STINYINT (get-cast-byte data-ptr)) ;; ?
618 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
619 (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
620 (#.$SQL_INTEGER (get-cast-int data-ptr))
621 (#.$SQL_BIGINT (read-from-string
622 (get-cast-foreign-string data-ptr)))
623 (#.$SQL_TINYINT (get-cast-byte data-ptr))
625 (let ((*read-base* 10))
626 (read-from-string (get-cast-foreign-string data-ptr))))
630 (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
632 (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
633 (funcall *time-conversion-function* universal-time frac)))
635 (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
636 (funcall *time-conversion-function* universal-time frac)))
638 (get-cast-int data-ptr))
640 (get-cast-single-float data-ptr))
642 (get-cast-double-float data-ptr))
644 (get-cast-long data-ptr))
646 (#.$SQL_C_BIT ; encountered only in Access
647 (get-cast-byte data-ptr))
649 (get-cast-binary data-ptr out-len *binary-format*))
650 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
651 (get-cast-short data-ptr)) ; LMH
654 (code-char (get-cast-short data-ptr)))
656 (get-cast-foreign-string data-ptr)))))))))
658 ;; FIXME: this could be better optimized for types which use READ-FROM-STRING above
660 (if (and (or (eq result-type t) (eq result-type :string))
661 (not (stringp value)))
662 (write-to-string value)
665 ;; which value is appropriate?
666 (defparameter +max-precision+ 4001)
668 (defvar *break-on-unknown-data-type* t)
670 ;; C. Stacy's idea to factor this out
671 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
672 ;; so that I don't have to remember to make changes in more than one place.
673 ;; Just keep it in synch with READ-DATA."
674 (defun %allocate-bindings (sql-type precision)
675 (let* ((c-type (sql-to-c-type sql-type))
676 (size (if (zerop precision)
677 +max-precision+ ;; if the precision cannot be determined
678 (min precision +max-precision+)))
679 (long-p (= size +max-precision+))
681 (case c-type ;; add more?
682 (#.$SQL_C_SLONG (uffi:allocate-foreign-object :long))
683 (#.$SQL_C_DATE (allocate-foreign-object 'sql-c-date))
684 (#.$SQL_C_TIME (allocate-foreign-object 'sql-c-time))
685 (#.$SQL_C_TIMESTAMP (allocate-foreign-object 'sql-c-timestamp))
686 (#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
687 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
688 (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
689 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :short))
690 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
691 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
692 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
694 ;; Maybe should signal a restartable condition for this?
695 (when *break-on-unknown-data-type*
696 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
697 sql-type precision size c-type))
698 (uffi:allocate-foreign-object :byte (1+ size)))))
699 (out-len-ptr (uffi:allocate-foreign-object :long)))
700 (values c-type data-ptr out-len-ptr size long-p)))
702 (defun fetch-all-rows (hstmt &key free-option flatp)
703 (let ((column-count (result-columns-count hstmt)))
704 (unless (zerop column-count)
705 (let ((names (make-array column-count))
706 (sql-types (make-array column-count :element-type 'fixnum))
707 (c-types (make-array column-count :element-type 'fixnum))
708 (precisions (make-array column-count :element-type 'fixnum))
709 (data-ptrs (make-array column-count :initial-element nil))
710 (out-len-ptrs (make-array column-count :initial-element nil))
711 (scales (make-array column-count :element-type 'fixnum))
712 (nullables-p (make-array column-count :element-type 'fixnum)))
716 (dotimes (col-nr column-count)
717 ;; get column information
718 (multiple-value-bind (name sql-type precision scale nullable-p)
719 (%describe-column hstmt (1+ col-nr))
720 ;; allocate space to bind result rows to
721 (multiple-value-bind (c-type data-ptr out-len-ptr)
722 (%allocate-bindings sql-type precision)
723 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
724 (setf (svref names col-nr) name
725 (aref sql-types col-nr) sql-type
726 (aref c-types col-nr) (sql-to-c-type sql-type)
727 (aref precisions col-nr) (if (zerop precision) 0 precision)
728 (aref scales col-nr) scale
729 (aref nullables-p col-nr) nullable-p
730 (aref data-ptrs col-nr) data-ptr
731 (aref out-len-ptrs col-nr) out-len-ptr))))
735 (when (> column-count 1)
736 (error "If more than one column is to be fetched, flatp has to be nil."))
737 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
739 (read-data (aref data-ptrs 0)
742 (aref out-len-ptrs 0)
745 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
747 (loop for col-nr from 0 to (1- column-count)
749 (read-data (aref data-ptrs col-nr)
750 (aref c-types col-nr)
751 (aref sql-types col-nr)
752 (aref out-len-ptrs col-nr)
755 ;; dispose of memory etc
756 (when free-option (%free-statement hstmt free-option))
757 (dotimes (col-nr column-count)
758 (let ((data-ptr (aref data-ptrs col-nr))
759 (out-len-ptr (aref out-len-ptrs col-nr)))
760 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
761 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
763 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
764 ;; of %do-fetch into sql package (has been done)
766 (defun %sql-prepare (hstmt sql)
767 (with-cstring (sql-ptr sql)
768 (with-error-handling (:hstmt hstmt)
769 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
771 ;; depending on option, we return a long int or a string; string not implemented
772 (defun get-connection-option (hdbc option)
773 (with-foreign-object (param-ptr :long)
774 (with-error-handling (:hdbc hdbc)
775 (SQLGetConnectOption hdbc option param-ptr)
776 (deref-pointer param-ptr :long))))
778 (defun set-connection-option (hdbc option param)
779 (with-error-handling (:hdbc hdbc)
780 (SQLSetConnectOption hdbc option param)))
782 (defun disable-autocommit (hdbc)
783 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
785 (defun enable-autocommit (hdbc)
786 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
788 (defun %sql-set-pos (hstmt row option lock)
791 (SQLSetPos hstmt row option lock)))
793 (defun %sql-extended-fetch (hstmt fetch-type row)
794 (with-foreign-objects ((row-count-ptr :unsigned-long)
795 (row-status-ptr :short))
796 (with-error-handling (:hstmt hstmt)
797 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
799 (values (deref-pointer row-count-ptr :unsigned-long)
800 (deref-pointer row-status-ptr :short)))))
802 ; column-nr is zero-based
803 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
805 (:hstmt hstmt :print-info nil)
806 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
807 c-type data-ptr precision out-len-ptr)))
809 (defun %sql-param-data (hstmt param-ptr)
810 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
811 (SQLParamData hstmt param-ptr)))
813 (defun %sql-put-data (hstmt data-ptr size)
815 (:hstmt hstmt :print-info t) ;; nil
816 (SQLPutData hstmt data-ptr size)))
818 (defconstant $sql-data-truncated (intern "01004" :keyword))
820 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
821 out-len-ptr result-type)
822 (declare (type long-ptr-type out-len-ptr))
823 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
824 +max-precision+ out-len-ptr))
825 (out-len (deref-pointer out-len-ptr :long))
829 (return-from read-data-in-chunks *null*))
830 (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
831 (let ((str (make-array 0 :element-type 'character :adjustable t)))
832 (loop do (if (= c-type #.$SQL_CHAR)
833 (let ((data-length (foreign-string-length data-ptr)))
834 (adjust-array str (+ offset data-length)
835 :initial-element #\?)
836 (setf offset (%cstring-into-vector
840 (error "wrong type. preliminary."))
841 while (and (= res $SQL_SUCCESS_WITH_INFO)
842 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
844 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
845 +max-precision+ out-len-ptr)))
846 (setf str (coerce str 'string))
847 (if (= sql-type $SQL_DECIMAL)
848 (let ((*read-base* 10))
849 (read-from-string str))
852 (let ((str (make-string out-len)))
853 (loop do (if (= c-type #.$SQL_CHAR)
854 (setf offset (%cstring-into-vector ;string
857 (min out-len (1- +max-precision+))))
858 (error "wrong type. preliminary."))
860 (and (= res $SQL_SUCCESS_WITH_INFO)
861 #+ingore(eq (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
863 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
865 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
866 +max-precision+ out-len-ptr)
867 out-len (deref-pointer out-len-ptr :long)))
868 (if (= sql-type $SQL_DECIMAL)
869 (let ((*read-base* 10))
870 (read-from-string str))
873 (def-type c-timestamp-ptr-type '(* (:struct sql-c-timestamp)))
874 (def-type c-time-ptr-type '(* (:struct sql-c-time)))
875 (def-type c-date-ptr-type '(* (:struct sql-c-date)))
877 (defun timestamp-to-universal-time (ptr)
878 (declare (type c-timestamp-ptr-type ptr))
880 (encode-universal-time
881 (get-slot-value ptr 'sql-c-timestamp 'second)
882 (get-slot-value ptr 'sql-c-timestamp 'minute)
883 (get-slot-value ptr 'sql-c-timestamp 'hour)
884 (get-slot-value ptr 'sql-c-timestamp 'day)
885 (get-slot-value ptr 'sql-c-timestamp 'month)
886 (get-slot-value ptr 'sql-c-timestamp 'year))
887 (get-slot-value ptr 'sql-c-timestamp 'fraction)))
889 (defun universal-time-to-timestamp (time &optional (fraction 0))
890 (multiple-value-bind (sec min hour day month year)
891 (decode-universal-time time)
892 (let ((ptr (allocate-foreign-object 'sql-c-timestamp)))
893 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
894 (get-slot-value ptr 'sql-c-timestamp 'minute) min
895 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
896 (get-slot-value ptr 'sql-c-timestamp 'day) day
897 (get-slot-value ptr 'sql-c-timestamp 'month) month
898 (get-slot-value ptr 'sql-c-timestamp 'year) year
899 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
902 (defun %put-timestamp (ptr time &optional (fraction 0))
903 (declare (type c-timestamp-ptr-type ptr))
904 (multiple-value-bind (sec min hour day month year)
905 (decode-universal-time time)
906 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
907 (get-slot-value ptr 'sql-c-timestamp 'minute) min
908 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
909 (get-slot-value ptr 'sql-c-timestamp 'day) day
910 (get-slot-value ptr 'sql-c-timestamp 'month) month
911 (get-slot-value ptr 'sql-c-timestamp 'year) year
912 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
915 (defun date-to-universal-time (ptr)
916 (declare (type c-date-ptr-type ptr))
917 (encode-universal-time
919 (get-slot-value ptr 'sql-c-timestamp 'day)
920 (get-slot-value ptr 'sql-c-timestamp 'month)
921 (get-slot-value ptr 'sql-c-timestamp 'year)))
923 (defun time-to-universal-time (ptr)
924 (declare (type c-time-ptr-type ptr))
925 (encode-universal-time
926 (get-slot-value ptr 'sql-c-timestamp 'second)
927 (get-slot-value ptr 'sql-c-timestamp 'minute)
928 (get-slot-value ptr 'sql-c-timestamp 'hour)
934 (defun %set-attr-odbc-version (henv version)
935 (with-error-handling (:henv henv)
936 (SQLSetEnvAttr henv $SQL_ATTR_ODBC_VERSION
937 (make-pointer version :void) 0)))
939 (defun %list-tables (hstmt)
940 (with-error-handling (:hstmt hstmt)
941 (SQLTables hstmt +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0)))