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 ;;;; This file, part of CLSQL, is Copyright (c) 2004 by Kevin M. Rosenberg
10 ;;;; and Copyright (C) Paul Meurer 1999 - 2001. All rights reserved.
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
20 "Lisp representation of SQL Null value, default = nil.
21 May be locally bound to something else if a certain type is necessary.")
24 (defvar *binary-format* :unsigned-byte-vector)
25 (defvar *time-conversion-function*
26 (lambda (universal-time &optional fraction)
27 (let ((time (clsql-sys:utime->time universal-time)))
28 (setf time (clsql-sys:time+
30 (clsql-sys:make-duration :usec (/ fraction 1000))))
31 (clsql-sys:format-time nil time :format :iso))
34 "Bound to a function that converts from a Lisp universal time fixnum (and a fractional
35 as possible second argument) to the desired representation of date/time/timestamp. By default, returns an iso-timestring.")
37 (defvar +null-ptr+ (make-null-pointer :byte))
38 (defparameter +null-handle-ptr+ (make-null-pointer :void))
39 (defvar *info-output* nil
40 "Stream to send SUCCESS_WITH_INFO messages.")
42 (defmacro %put-str (ptr string &optional max-length)
43 (let ((size (gensym)))
44 `(let ((,size (length ,string)))
45 (when (and ,max-length (> ,size ,max-length))
46 (error 'clsql:sql-database-data-error
48 (format nil "string \"~a\" of length ~d is longer than max-length: ~d"
49 ,string ,size ,max-length)))
50 (with-cast-pointer (char-ptr ,ptr :byte)
52 (setf (deref-array char-ptr '(:array :byte) i)
53 (char-code (char ,string i))))
54 (setf (deref-array char-ptr '(:array :byte) ,size) 0)))))
56 (defun %cstring-into-vector (ptr vector offset size-in-bytes)
57 (dotimes (i size-in-bytes)
58 (setf (schar vector offset)
59 (ensure-char-character
60 ;; this is MUCH faster than (sb-alien:deref ptr i) even though
61 ;; sb-alien:deref makes more sense. I snagged this by looking at
62 ;; cffi which we had used previously without this bug
63 #+(and sbcl (not cffi))
64 (sb-sys:sap-ref-8 (sb-alien:alien-sap ptr) i)
65 #-(and sbcl (not cffi))
66 (deref-array ptr '(:array :unsigned-char) i)
71 (defmacro with-allocate-foreign-string ((var len) &body body)
72 "Safely does uffi:allocate-foreign-string-- making sure we do the uffi:free-foreign-object"
76 (setf ,var (uffi:allocate-foreign-string ,len))
79 (uffi:free-foreign-object ,var)))))
81 (defmacro with-allocate-foreign-strings (bindings &rest body)
83 `(with-allocate-foreign-string ,(car bindings)
84 (with-allocate-foreign-strings ,(cdr bindings)
88 (defun handle-error (henv hdbc hstmt)
89 (with-allocate-foreign-strings ((sql-state 256)
90 (error-message #.$SQL_MAX_MESSAGE_LENGTH))
91 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
93 (SQLError henv hdbc hstmt sql-state
94 error-code error-message
95 #.$SQL_MAX_MESSAGE_LENGTH msg-length)
97 (convert-from-foreign-string error-message)
98 (convert-from-foreign-string sql-state)
99 (deref-pointer msg-length :short)
100 (deref-pointer error-code #.$ODBC-LONG-TYPE)))))
102 (defun sql-state (henv hdbc hstmt)
103 (with-allocate-foreign-strings ((sql-state 256)
104 (error-message #.$SQL_MAX_MESSAGE_LENGTH))
105 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
107 (SQLError henv hdbc hstmt sql-state error-code
108 error-message #.$SQL_MAX_MESSAGE_LENGTH msg-length)
109 (convert-from-foreign-string sql-state)
110 ;; test this: return a keyword for efficiency
111 ;;(%cstring-to-keyword state)
114 (defmacro with-error-handling ((&key henv hdbc hstmt (print-info t))
115 odbc-call &body body)
116 (let ((result-code (gensym "RC-")))
117 `(let ((,result-code ,odbc-call))
119 ;; Check for allegro v7 & v8 bug with ODBC calls returning
120 ;; 32-bit unsigned ints, not 16-bit signed ints
121 #+(and allegro mswindows)
122 (when (> ,result-code #xFFFF)
123 (warn (format nil "16-bit return bug: result-code #x~X for expression ~S"
124 ,result-code (quote ,odbc-call)))
125 (setq ,result-code (logand ,result-code #xFFFF))
126 (when (> ,result-code #x7FFF)
127 (setq ,result-code (- ,result-code #x10000))))
131 (progn ,result-code ,@body))
132 (#.$SQL_SUCCESS_WITH_INFO
134 (multiple-value-bind (error-message sql-state)
135 (handle-error (or ,henv +null-handle-ptr+)
136 (or ,hdbc +null-handle-ptr+)
137 (or ,hstmt +null-handle-ptr+))
139 (format *info-output* "[ODBC info ~A] ~A state: ~A"
140 ,result-code error-message
142 (progn ,result-code ,@body))
143 (#.$SQL_INVALID_HANDLE
145 'clsql-sys:sql-database-error
146 :message "ODBC: Invalid handle"))
147 (#.$SQL_STILL_EXECUTING
149 'clsql-sys:sql-temporary-error
150 :message "ODBC: Still executing"))
152 (multiple-value-bind (error-message sql-state)
153 (handle-error (or ,henv +null-handle-ptr+)
154 (or ,hdbc +null-handle-ptr+)
155 (or ,hstmt +null-handle-ptr+))
157 'clsql-sys:sql-database-error
158 :message error-message
159 :secondary-error-id sql-state)))
160 (#.$SQL_NO_DATA_FOUND
161 (progn ,result-code ,@body))
162 ;; work-around for Allegro 7.0beta AMD64 which returns negative numbers
164 (multiple-value-bind (error-message sql-state)
165 (handle-error (or ,henv +null-handle-ptr+)
166 (or ,hdbc +null-handle-ptr+)
167 (or ,hstmt +null-handle-ptr+))
169 'clsql-sys:sql-database-error
170 :message error-message
171 :secondary-error-id sql-state))
173 (progn ,result-code ,@body))))))
175 (defun %new-environment-handle ()
177 (with-foreign-object (phenv 'sql-handle)
180 (SQLAllocHandle $SQL_HANDLE_ENV +null-handle-ptr+ phenv)
181 (deref-pointer phenv 'sql-handle)))))
182 (%set-attr-odbc-version henv $SQL_OV_ODBC3)
186 (defun %sql-free-environment (henv)
191 (defun %new-db-connection-handle (henv)
192 (with-foreign-object (phdbc 'sql-handle)
193 (setf (deref-pointer phdbc 'sql-handle) +null-handle-ptr+)
196 (SQLAllocHandle $SQL_HANDLE_DBC henv phdbc)
197 (deref-pointer phdbc 'sql-handle))))
199 (defun %free-statement (hstmt option)
207 (:unbind $SQL_UNBIND)
208 (:reset $SQL_RESET_PARAMS)))))
210 (defmacro with-statement-handle ((hstmt hdbc) &body body)
211 `(let ((,hstmt (%new-statement-handle ,hdbc)))
214 (%free-statement ,hstmt :drop))))
216 ;; functional interface
218 (defun %sql-connect (hdbc server uid pwd)
219 (with-cstrings ((server-ptr server)
224 (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr
225 $SQL_NTS pwd-ptr $SQL_NTS))))
227 (defun %sql-driver-connect (hdbc connection-string completion window-handle)
228 (with-cstring (connection-ptr connection-string)
229 (with-allocate-foreign-string (completed-connection-string-ptr $SQL_MAX_CONN_OUT)
230 (with-foreign-object (completed-connection-length :short)
233 (SQLDriverConnect hdbc
235 connection-ptr $SQL_NTS
236 completed-connection-string-ptr $SQL_MAX_CONN_OUT
237 completed-connection-length
240 (defun %disconnect (hdbc)
246 (SQLFreeHandle $SQL_HANDLE_DBC hdbc))))
248 (defun %commit (henv hdbc)
250 (:henv henv :hdbc hdbc)
252 henv hdbc $SQL_COMMIT)))
254 (defun %rollback (henv hdbc)
256 (:henv henv :hdbc hdbc)
258 henv hdbc $SQL_ROLLBACK)))
260 ; col-nr is zero-based in Lisp but 1 based in sql
261 ; col-nr = :bookmark retrieves a bookmark.
262 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
266 (if (eq column-nr :bookmark) 0 (1+ column-nr))
267 c-type data-ptr precision out-len-ptr)))
269 ; parameter-nr is zero-based in Lisp
270 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
271 sql-type precision scale data-ptr
272 max-value out-len-ptr)
275 (SQLBindParameter hstmt (1+ parameter-nr)
276 parameter-type ;$SQL_PARAM_INPUT
278 sql-type ;$SQL_VARCHAR
279 precision ;(1- (length str))
283 out-len-ptr ;#.+null-ptr+
286 (defun %sql-fetch (hstmt)
291 (defun %new-statement-handle (hdbc)
292 (let ((statement-handle
293 (with-foreign-object (phstmt 'sql-handle)
296 (SQLAllocHandle $SQL_HANDLE_STMT hdbc phstmt)
297 (deref-pointer phstmt 'sql-handle)))))
298 (if (uffi:null-pointer-p statement-handle)
299 (error 'clsql:sql-database-error :message "Received null statement handle.")
302 (defun %sql-get-info (hdbc info-type)
304 ;; those return string
305 ((#.$SQL_ACCESSIBLE_PROCEDURES
306 #.$SQL_ACCESSIBLE_TABLES
308 #.$SQL_DATA_SOURCE_NAME
309 #.$SQL_DATA_SOURCE_READ_ONLY
313 #.$SQL_DRIVER_ODBC_VER
315 #.$SQL_EXPRESSIONS_IN_ORDERBY
316 #.$SQL_IDENTIFIER_QUOTE_CHAR
318 #.$SQL_LIKE_ESCAPE_CLAUSE
319 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
320 #.$SQL_MULT_RESULT_SETS
321 #.$SQL_MULTIPLE_ACTIVE_TXN
322 #.$SQL_NEED_LONG_DATA_LEN
323 #.$SQL_ODBC_SQL_OPT_IEF
325 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
328 #.$SQL_PROCEDURE_TERM
330 #.$SQL_QUALIFIER_NAME_SEPARATOR
331 #.$SQL_QUALIFIER_TERM
333 #.$SQL_SEARCH_PATTERN_ESCAPE
335 #.$SQL_SPECIAL_CHARACTERS
338 (with-allocate-foreign-string (info-ptr 1024)
339 (with-foreign-object (info-length-ptr :short)
342 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
343 (convert-from-foreign-string info-ptr)))))
344 ;; those returning a word
345 ((#.$SQL_ACTIVE_CONNECTIONS
346 #.$SQL_ACTIVE_STATEMENTS
347 #.$SQL_CONCAT_NULL_BEHAVIOR
348 #.$SQL_CORRELATION_NAME
349 #.$SQL_CURSOR_COMMIT_BEHAVIOR
350 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
351 #.$SQL_MAX_COLUMN_NAME_LEN
352 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
353 #.$SQL_MAX_COLUMNS_IN_INDEX
354 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
355 #.$SQL_MAX_COLUMNS_IN_SELECT
356 #.$SQL_MAX_COLUMNS_IN_TABLE
357 #.$SQL_MAX_CURSOR_NAME_LEN
358 #.$SQL_MAX_OWNER_NAME_LEN
359 #.$SQL_MAX_PROCEDURE_NAME_LEN
360 #.$SQL_MAX_QUALIFIER_NAME_LEN
361 #.$SQL_MAX_TABLE_NAME_LEN
362 #.$SQL_MAX_TABLES_IN_SELECT
363 #.$SQL_MAX_USER_NAME_LEN
364 #.$SQL_NON_NULLABLE_COLUMNS
365 #.$SQL_NULL_COLLATION
366 #.$SQL_ODBC_API_CONFORMANCE
367 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
368 #.$SQL_ODBC_SQL_CONFORMANCE
369 #.$SQL_QUALIFIER_LOCATION
370 #.$SQL_QUOTED_IDENTIFIER_CASE
372 (with-foreign-objects ((info-ptr :short)
373 (info-length-ptr :short))
381 (deref-pointer info-ptr :short)))
383 ;; those returning a long bitmask
385 #.$SQL_BOOKMARK_PERSISTENCE
386 #.$SQL_CONVERT_BIGINT
387 #.$SQL_CONVERT_BINARY
391 #.$SQL_CONVERT_DECIMAL
392 #.$SQL_CONVERT_DOUBLE
394 #.$SQL_CONVERT_INTEGER
395 #.$SQL_CONVERT_LONGVARCHAR
396 #.$SQL_CONVERT_NUMERIC
398 #.$SQL_CONVERT_SMALLINT
400 #.$SQL_CONVERT_TIMESTAMP
401 #.$SQL_CONVERT_TINYINT
402 #.$SQL_CONVERT_VARBINARY
403 #.$SQL_CONVERT_VARCHAR
404 #.$SQL_CONVERT_LONGVARBINARY
405 #.$SQL_CONVERT_FUNCTIONS
406 #.$SQL_FETCH_DIRECTION
408 #.$SQL_GETDATA_EXTENSIONS
410 #.$SQL_MAX_INDEX_SIZE
412 #.$SQL_MAX_STATEMENT_LEN
413 #.$SQL_NUMERIC_FUNCTIONS
415 #.$SQL_POS_OPERATIONS
416 #.$SQL_POSITIONED_STATEMENTS
417 #.$SQL_QUALIFIER_USAGE
418 #.$SQL_SCROLL_CONCURRENCY
419 #.$SQL_SCROLL_OPTIONS
420 #.$SQL_STATIC_SENSITIVITY
421 #.$SQL_STRING_FUNCTIONS
423 #.$SQL_SYSTEM_FUNCTIONS
424 #.$SQL_TIMEDATE_ADD_INTERVALS
425 #.$SQL_TIMEDATE_DIFF_INTERVALS
426 #.$SQL_TIMEDATE_FUNCTIONS
427 #.$SQL_TXN_ISOLATION_OPTION
429 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
430 (info-length-ptr :short))
438 (deref-pointer info-ptr #.$ODBC-LONG-TYPE)))
440 ;; those returning a long integer
441 ((#.$SQL_DEFAULT_TXN_ISOLATION
447 #.$SQL_IDENTIFIER_CASE
448 #.$SQL_MAX_BINARY_LITERAL_LEN
449 #.$SQL_MAX_CHAR_LITERAL_LEN
450 #.$SQL_ACTIVE_ENVIRONMENTS
452 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
453 (info-length-ptr :short))
456 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
457 (deref-pointer info-ptr #.$ODBC-LONG-TYPE))))))
459 (defun %sql-exec-direct (sql hstmt henv hdbc)
460 (with-cstring (sql-ptr sql)
462 (:hstmt hstmt :henv henv :hdbc hdbc)
463 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
465 (defun %sql-cancel (hstmt)
470 (defun %sql-execute (hstmt)
475 (defun result-columns-count (hstmt)
476 (with-foreign-objects ((columns-nr-ptr :short))
477 (with-error-handling (:hstmt hstmt)
478 (SQLNumResultCols hstmt columns-nr-ptr)
479 (deref-pointer columns-nr-ptr :short))))
481 (defun result-rows-count (hstmt)
482 (with-foreign-objects ((row-count-ptr #.$ODBC-LONG-TYPE))
483 (with-error-handling (:hstmt hstmt)
484 (SQLRowCount hstmt row-count-ptr)
485 (deref-pointer row-count-ptr #.$ODBC-LONG-TYPE))))
487 ;; column counting is 1-based
488 (defun %describe-column (hstmt column-nr)
489 (with-allocate-foreign-string (column-name-ptr 256)
490 (with-foreign-objects ((column-name-length-ptr :short)
491 (column-sql-type-ptr :short)
492 (column-precision-ptr #.$ODBC-ULONG-TYPE)
493 (column-scale-ptr :short)
494 (column-nullable-p-ptr :short))
495 (with-error-handling (:hstmt hstmt)
496 (SQLDescribeCol hstmt column-nr column-name-ptr 256
497 column-name-length-ptr
501 column-nullable-p-ptr)
503 (convert-from-foreign-string column-name-ptr)
504 (deref-pointer column-sql-type-ptr :short)
505 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
506 (deref-pointer column-scale-ptr :short)
507 (deref-pointer column-nullable-p-ptr :short))))))
509 ;; parameter counting is 1-based
510 ;; this function isn't used, which is good because FreeTDS dosn't support it.
511 (defun %describe-parameter (hstmt parameter-nr)
512 (with-foreign-objects ((column-sql-type-ptr :short)
513 (column-precision-ptr #.$ODBC-ULONG-TYPE)
514 (column-scale-ptr :short)
515 (column-nullable-p-ptr :short))
518 (SQLDescribeParam hstmt parameter-nr
522 column-nullable-p-ptr)
524 (deref-pointer column-sql-type-ptr :short)
525 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
526 (deref-pointer column-scale-ptr :short)
527 (deref-pointer column-nullable-p-ptr :short)))))
529 (defun %column-attributes (hstmt column-nr descriptor-type)
530 (with-allocate-foreign-string (descriptor-info-ptr 256)
531 (with-foreign-objects ((descriptor-length-ptr :short)
532 (numeric-descriptor-ptr #.$ODBC-LONG-TYPE))
535 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr
536 256 descriptor-length-ptr
537 numeric-descriptor-ptr)
539 (convert-from-foreign-string descriptor-info-ptr)
540 (deref-pointer numeric-descriptor-ptr #.$ODBC-LONG-TYPE))))))
542 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
543 table-name column-name)
544 (with-cstrings ((table-qualifier-ptr table-qualifier)
545 (table-owner-ptr table-owner)
546 (table-name-ptr table-name)
547 (column-name-ptr column-name))
551 table-qualifier-ptr (length table-qualifier)
552 table-owner-ptr (length table-owner)
553 table-name-ptr (length table-name)
554 column-name-ptr (length column-name)))))
556 (defun %describe-columns (hdbc table-qualifier table-owner
557 table-name column-name)
558 (with-statement-handle (hstmt hdbc)
559 (%prepare-describe-columns hstmt table-qualifier table-owner
560 table-name column-name)
561 (fetch-all-rows hstmt)))
563 (defun %sql-data-sources (henv &key (direction :first))
564 (with-allocate-foreign-strings ((name-ptr (1+ $SQL_MAX_DSN_LENGTH))
565 (description-ptr 1024))
566 (with-foreign-objects ((name-length-ptr :short)
567 (description-length-ptr :short))
568 (let ((res (with-error-handling
572 (:first $SQL_FETCH_FIRST)
573 (:next $SQL_FETCH_NEXT))
575 (1+ $SQL_MAX_DSN_LENGTH)
579 description-length-ptr))))
580 (when (= res $SQL_NO_DATA_FOUND)
582 (convert-from-foreign-string name-ptr)
583 (convert-from-foreign-string description-ptr)))))))
587 (defun sql-to-c-type (sql-type)
589 ;; Added -10 for MSSQL ntext type and -11 for nvarchar
590 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
591 #.$SQL_NUMERIC #.$sql_decimal -8 -9 -10 -11) $SQL_C_CHAR)
592 (#.$SQL_INTEGER $SQL_C_SLONG)
593 (#.$SQL_BIGINT $SQL_C_SBIGINT)
594 (#.$SQL_SMALLINT $SQL_C_SSHORT)
595 (#.$SQL_DOUBLE $SQL_C_DOUBLE)
596 (#.$SQL_FLOAT $SQL_C_DOUBLE)
597 (#.$SQL_REAL $SQL_C_FLOAT)
598 (#.$SQL_DATE $SQL_C_DATE)
599 (#.$SQL_TIME $SQL_C_TIME)
600 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
601 (#.$SQL_TYPE_DATE $SQL_C_TYPE_DATE)
602 (#.$SQL_TYPE_TIME $SQL_C_TYPE_TIME)
603 (#.$SQL_TYPE_TIMESTAMP $SQL_C_TYPE_TIMESTAMP)
604 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
605 (#.$SQL_TINYINT $SQL_C_STINYINT)
606 (#.$SQL_BIT $SQL_C_BIT)))
608 (def-type byte-pointer-type (* :byte))
609 (def-type short-pointer-type (* :short))
610 (def-type int-pointer-type (* :int))
611 (def-type long-pointer-type (* #.$ODBC-LONG-TYPE))
612 (def-type big-pointer-type (* #.$ODBC-BIG-TYPE))
613 (def-type float-pointer-type (* :float))
614 (def-type double-pointer-type (* :double))
615 (def-type string-pointer-type (* :unsigned-char))
617 (defun get-cast-byte (ptr)
618 (locally (declare (type byte-pointer-type ptr))
619 (deref-pointer ptr :byte)))
621 (defun get-cast-short (ptr)
622 (locally (declare (type short-pointer-type ptr))
623 (deref-pointer ptr :short)))
625 (defun get-cast-int (ptr)
626 (locally (declare (type int-pointer-type ptr))
627 (deref-pointer ptr :int)))
629 (defun get-cast-long (ptr)
630 (locally (declare (type long-pointer-type ptr))
631 (deref-pointer ptr #.$ODBC-LONG-TYPE)))
633 (defun get-cast-big (ptr)
634 (locally (declare (type big-pointer-type ptr))
635 (deref-pointer ptr #.$ODBC-BIG-TYPE)))
637 (defun get-cast-single-float (ptr)
638 (locally (declare (type float-pointer-type ptr))
639 (deref-pointer ptr :float)))
641 (defun get-cast-double-float (ptr)
642 (locally (declare (type double-pointer-type ptr))
643 (deref-pointer ptr :double)))
645 (defun get-cast-foreign-string (ptr)
646 (locally (declare (type string-pointer-type ptr))
647 (convert-from-foreign-string ptr)))
649 (defun get-cast-binary (ptr len format)
650 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
651 (with-cast-pointer (casted ptr :unsigned-byte)
653 (:unsigned-byte-vector
654 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
656 (setf (aref vector i)
657 (deref-array casted '(:array :unsigned-byte) i)))
660 (let ((vector (make-array (ash len 3) :element-type 'bit)))
662 (let ((byte (deref-array casted '(:array :byte) i)))
664 (setf (bit vector (+ (ash i 3) j))
665 (logand (ash byte (- j 7)) 1)))))
669 (defun read-data (data-ptr c-type sql-type out-len-ptr result-type)
670 (declare (type long-ptr-type out-len-ptr))
671 (let* ((out-len (get-cast-long out-len-ptr))
673 (cond ((= out-len $SQL_NULL_DATA) *null*)
676 ;; SQL extended datatypes
677 (#.$SQL_TINYINT (get-cast-byte data-ptr))
678 (#.$SQL_C_STINYINT (get-cast-byte data-ptr)) ;; ?
679 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
680 (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
681 (#.$SQL_INTEGER (get-cast-int data-ptr))
682 (#.$SQL_BIGINT (get-cast-big data-ptr))
683 ;; TODO: Change this to read in rationals instead of doubles
684 ((#.$SQL_DECIMAL #.$SQL_NUMERIC)
685 (let* ((*read-base* 10)
686 (*read-default-float-format* 'double-float)
687 (str (get-cast-foreign-string data-ptr)))
688 (read-from-string str)))
689 (#.$SQL_BIT (get-cast-byte data-ptr))
692 ((#.$SQL_C_DATE #.$SQL_C_TYPE_DATE)
693 (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
694 ((#.$SQL_C_TIME #.$SQL_C_TYPE_TIME)
695 (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
696 (funcall *time-conversion-function* universal-time frac)))
697 ((#.$SQL_C_TIMESTAMP #.$SQL_C_TYPE_TIMESTAMP)
698 (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
699 (funcall *time-conversion-function* universal-time frac)))
701 (get-cast-int data-ptr))
703 (get-cast-single-float data-ptr))
705 (get-cast-double-float data-ptr))
707 (get-cast-long data-ptr))
709 (#.$SQL_C_BIT ; encountered only in Access
710 (get-cast-byte data-ptr))
712 (get-cast-binary data-ptr out-len *binary-format*))
713 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
714 (get-cast-short data-ptr)) ; LMH
715 (#.$SQL_C_SBIGINT (get-cast-big data-ptr))
718 (code-char (get-cast-short data-ptr)))
720 (get-cast-foreign-string data-ptr)))))))))
722 ;; FIXME: this could be better optimized for types which use READ-FROM-STRING above
724 (if (and (or (eq result-type t) (eq result-type :string))
726 (not (stringp value)))
727 (write-to-string value)
730 ;; which value is appropriate?
731 (defparameter +max-precision+ 4001)
733 (defvar *break-on-unknown-data-type* t)
735 ;; C. Stacy's idea to factor this out
736 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
737 ;; so that I don't have to remember to make changes in more than one place.
738 ;; Just keep it in synch with READ-DATA."
739 (defun %allocate-bindings (sql-type precision)
740 (let* ((c-type (sql-to-c-type sql-type))
741 (size (if (zerop precision)
742 +max-precision+ ;; if the precision cannot be determined
743 (min precision +max-precision+)))
744 (long-p (= size +max-precision+))
746 (case c-type ;; add more?
747 (#.$SQL_C_SLONG (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE))
748 ((#.$SQL_C_DATE #.$SQL_C_TYPE_DATE) (allocate-foreign-object 'sql-c-date))
749 ((#.$SQL_C_TIME #.$SQL_C_TYPE_TIME) (allocate-foreign-object 'sql-c-time))
750 ((#.$SQL_C_TIMESTAMP #.$SQL_C_TYPE_TIMESTAMP) (allocate-foreign-object 'sql-c-timestamp))
751 (#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
752 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
753 (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
754 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
755 (#.$SQL_C_SBIGINT (uffi:allocate-foreign-object #.$ODBC-BIG-TYPE))
756 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
757 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
758 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
760 ;; Maybe should signal a restartable condition for this?
761 (when *break-on-unknown-data-type*
762 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
763 sql-type precision size c-type))
764 (uffi:allocate-foreign-object :byte (1+ size)))))
765 (out-len-ptr (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE)))
766 (values c-type data-ptr out-len-ptr size long-p)))
768 (defun fetch-all-rows (hstmt &key free-option flatp)
769 (let ((column-count (result-columns-count hstmt)))
770 (unless (zerop column-count)
771 (let ((names (make-array column-count))
772 (sql-types (make-array column-count :element-type 'fixnum))
773 (c-types (make-array column-count :element-type 'fixnum))
774 (precisions (make-array column-count :element-type 'fixnum))
775 (data-ptrs (make-array column-count :initial-element nil))
776 (out-len-ptrs (make-array column-count :initial-element nil))
777 (scales (make-array column-count :element-type 'fixnum))
778 (nullables-p (make-array column-count :element-type 'fixnum)))
782 (dotimes (col-nr column-count)
783 ;; get column information
784 (multiple-value-bind (name sql-type precision scale nullable-p)
785 (%describe-column hstmt (1+ col-nr))
786 ;; allocate space to bind result rows to
787 (multiple-value-bind (c-type data-ptr out-len-ptr)
788 (%allocate-bindings sql-type precision)
789 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
790 (setf (svref names col-nr) name
791 (aref sql-types col-nr) sql-type
792 (aref c-types col-nr) (sql-to-c-type sql-type)
793 (aref precisions col-nr) (if (zerop precision) 0 precision)
794 (aref scales col-nr) scale
795 (aref nullables-p col-nr) nullable-p
796 (aref data-ptrs col-nr) data-ptr
797 (aref out-len-ptrs col-nr) out-len-ptr))))
801 (when (> column-count 1)
802 (error 'clsql:sql-database-error
803 :message "If more than one column is to be fetched, flatp has to be nil."))
804 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
806 (read-data (aref data-ptrs 0)
809 (aref out-len-ptrs 0)
812 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
814 (loop for col-nr from 0 to (1- column-count)
816 (read-data (aref data-ptrs col-nr)
817 (aref c-types col-nr)
818 (aref sql-types col-nr)
819 (aref out-len-ptrs col-nr)
822 ;; dispose of memory etc
823 (when free-option (%free-statement hstmt free-option))
824 (dotimes (col-nr column-count)
825 (let ((data-ptr (aref data-ptrs col-nr))
826 (out-len-ptr (aref out-len-ptrs col-nr)))
827 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
828 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
830 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
831 ;; of %do-fetch into sql package (has been done)
833 (defun %sql-prepare (hstmt sql)
834 (with-cstring (sql-ptr sql)
835 (with-error-handling (:hstmt hstmt)
836 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
838 ;; depending on option, we return a long int or a string; string not implemented
839 (defun get-connection-option (hdbc option)
840 (with-foreign-object (param-ptr #.$ODBC-LONG-TYPE)
841 (with-error-handling (:hdbc hdbc)
842 (SQLGetConnectOption hdbc option param-ptr)
843 (deref-pointer param-ptr #.$ODBC-LONG-TYPE))))
845 (defun set-connection-option (hdbc option param)
846 (with-error-handling (:hdbc hdbc)
847 (SQLSetConnectOption hdbc option param)))
849 (defun disable-autocommit (hdbc)
850 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
852 (defun enable-autocommit (hdbc)
853 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
855 (defun %sql-set-pos (hstmt row option lock)
858 (SQLSetPos hstmt row option lock)))
860 (defun %sql-extended-fetch (hstmt fetch-type row)
861 (with-foreign-objects ((row-count-ptr #.$ODBC-ULONG-TYPE)
862 (row-status-ptr :short))
863 (with-error-handling (:hstmt hstmt)
864 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
866 (values (deref-pointer row-count-ptr #.$ODBC-ULONG-TYPE)
867 (deref-pointer row-status-ptr :short)))))
869 ; column-nr is zero-based
870 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
872 (:hstmt hstmt :print-info nil)
873 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
874 c-type data-ptr precision out-len-ptr)))
876 (defun %sql-param-data (hstmt param-ptr)
877 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
878 (SQLParamData hstmt param-ptr)))
880 (defun %sql-put-data (hstmt data-ptr size)
882 (:hstmt hstmt :print-info t) ;; nil
883 (SQLPutData hstmt data-ptr size)))
885 (defconstant $sql-data-truncated (intern "01004" :keyword))
888 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
889 out-len-ptr result-type)
890 (declare (type long-ptr-type out-len-ptr)
891 (ignore result-type))
892 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
893 +max-precision+ out-len-ptr))
894 (out-len (get-cast-long out-len-ptr))
896 (result (case out-len
898 (return-from read-data-in-chunks *null*))
899 (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
900 (let ((str (make-array 0 :element-type 'character :adjustable t)))
901 (loop do (if (= c-type #.$SQL_CHAR)
902 (let ((data-length (foreign-string-length data-ptr)))
903 (adjust-array str (+ offset data-length)
904 :initial-element #\?)
905 (setf offset (%cstring-into-vector
909 (error 'clsql:sql-database-error :message "wrong type. preliminary."))
910 while (and (= res $SQL_SUCCESS_WITH_INFO)
911 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
913 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
914 +max-precision+ out-len-ptr)))
915 (setf str (coerce str 'string))
916 (if (= sql-type $SQL_DECIMAL)
917 (let ((*read-base* 10))
918 (read-from-string str))
921 (let ((str (make-string out-len)))
924 (if (= c-type #.$SQL_CHAR)
925 (setf offset (%cstring-into-vector ;string
928 (min out-len (1- +max-precision+))))
929 (error 'clsql:sql-database-error :message "wrong type. preliminary."))
931 (and (= res $SQL_SUCCESS_WITH_INFO)
932 (>= out-len +max-precision+))
933 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
934 +max-precision+ out-len-ptr)
935 out-len (get-cast-long out-len-ptr)))
936 (if (= sql-type $SQL_DECIMAL)
937 (let ((*read-base* 10)
938 (*read-default-float-format* 'double-float))
939 (read-from-string str))
942 (setf (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE) #.$SQL_NO_TOTAL) ;; reset the out length for the next row
946 (def-type c-timestamp-ptr-type (* (:struct sql-c-timestamp)))
947 (def-type c-time-ptr-type (* (:struct sql-c-time)))
948 (def-type c-date-ptr-type (* (:struct sql-c-date)))
950 (defun timestamp-to-universal-time (ptr)
951 (declare (type c-timestamp-ptr-type ptr))
953 (encode-universal-time
954 (get-slot-value ptr 'sql-c-timestamp 'second)
955 (get-slot-value ptr 'sql-c-timestamp 'minute)
956 (get-slot-value ptr 'sql-c-timestamp 'hour)
957 (get-slot-value ptr 'sql-c-timestamp 'day)
958 (get-slot-value ptr 'sql-c-timestamp 'month)
959 (get-slot-value ptr 'sql-c-timestamp 'year))
960 (get-slot-value ptr 'sql-c-timestamp 'fraction)))
962 (defun universal-time-to-timestamp (time &optional (fraction 0))
963 "TODO: Dead function?"
964 (multiple-value-bind (sec min hour day month year)
965 (decode-universal-time time)
966 (let ((ptr (allocate-foreign-object 'sql-c-timestamp)))
967 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
968 (get-slot-value ptr 'sql-c-timestamp 'minute) min
969 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
970 (get-slot-value ptr 'sql-c-timestamp 'day) day
971 (get-slot-value ptr 'sql-c-timestamp 'month) month
972 (get-slot-value ptr 'sql-c-timestamp 'year) year
973 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
976 (defun %put-timestamp (ptr time &optional (fraction 0))
977 "TODO: Dead function?"
978 (declare (type c-timestamp-ptr-type ptr))
979 (multiple-value-bind (sec min hour day month year)
980 (decode-universal-time time)
981 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
982 (get-slot-value ptr 'sql-c-timestamp 'minute) min
983 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
984 (get-slot-value ptr 'sql-c-timestamp 'day) day
985 (get-slot-value ptr 'sql-c-timestamp 'month) month
986 (get-slot-value ptr 'sql-c-timestamp 'year) year
987 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
990 (defun date-to-universal-time (ptr)
991 (declare (type c-date-ptr-type ptr))
992 (encode-universal-time
994 (get-slot-value ptr 'sql-c-timestamp 'day)
995 (get-slot-value ptr 'sql-c-timestamp 'month)
996 (get-slot-value ptr 'sql-c-timestamp 'year)))
998 (defun time-to-universal-time (ptr)
999 (declare (type c-time-ptr-type ptr))
1000 (encode-universal-time
1001 (get-slot-value ptr 'sql-c-timestamp 'second)
1002 (get-slot-value ptr 'sql-c-timestamp 'minute)
1003 (get-slot-value ptr 'sql-c-timestamp 'hour)
1009 (defun %set-attr-odbc-version (henv version)
1010 (with-error-handling (:henv henv)
1011 ;;note that we are passing version as an integer that happens to be
1012 ;;stuffed into a pointer.
1013 ;;http://msdn.microsoft.com/en-us/library/ms709285%28v=VS.85%29.aspx
1014 (SQLSetEnvAttr henv $SQL_ATTR_ODBC_VERSION
1015 (make-pointer version :void) 0)))
1017 (defun %list-tables (hstmt)
1018 (with-error-handling (:hstmt hstmt)
1019 (SQLTables hstmt +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0)))
1021 (defun %table-statistics (table hstmt &key unique (ensure t)
1022 &aux (table (princ-to-string
1023 (clsql-sys::unescaped-database-identifier table))))
1024 (with-cstrings ((table-cs table))
1025 (with-error-handling (:hstmt hstmt)
1031 (if unique $SQL_INDEX_UNIQUE $SQL_INDEX_ALL)
1032 (if ensure $SQL_ENSURE $SQL_QUICK)))))
1034 (defun %list-data-sources (henv)
1035 (let ((results nil))
1036 (with-foreign-strings ((dsn-ptr (1+ $SQL_MAX_DSN_LENGTH))
1038 (with-foreign-objects ((dsn-len :short)
1040 (let ((res (with-error-handling (:henv henv)
1041 (SQLDataSources henv $SQL_FETCH_FIRST dsn-ptr
1042 (1+ $SQL_MAX_DSN_LENGTH)
1043 dsn-len desc-ptr 256 desc-len))))
1044 (when (or (eql res $SQL_SUCCESS)
1045 (eql res $SQL_SUCCESS_WITH_INFO))
1046 (push (convert-from-foreign-string dsn-ptr) results))
1048 (do ((res (with-error-handling (:henv henv)
1049 (SQLDataSources henv $SQL_FETCH_NEXT dsn-ptr
1050 (1+ $SQL_MAX_DSN_LENGTH)
1051 dsn-len desc-ptr 256 desc-len))
1052 (with-error-handling (:henv henv)
1053 (SQLDataSources henv $SQL_FETCH_NEXT dsn-ptr
1054 (1+ $SQL_MAX_DSN_LENGTH)
1055 dsn-len desc-ptr 256 desc-len))))
1056 ((not (or (eql res $SQL_SUCCESS)
1057 (eql res $SQL_SUCCESS_WITH_INFO))))
1058 (push (convert-from-foreign-string dsn-ptr) results)))))
1059 (nreverse results)))