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)
27 (clsql-sys:format-time nil time :format :iso))
28 "Bound to a function that converts from a clsql:wall-time to the desired
29 representation of date/time/timestamp.
30 By default, returns an iso-timestring.")
32 (defvar +null-ptr+ (make-null-pointer :byte))
33 (defparameter +null-handle-ptr+ (make-null-pointer :void))
34 (defvar *info-output* nil
35 "Stream to send SUCCESS_WITH_INFO messages.")
37 (defmacro %put-str (ptr string &optional max-length)
38 (let ((size (gensym)))
39 `(let ((,size (length ,string)))
40 (when (and ,max-length (> ,size ,max-length))
41 (error 'clsql:sql-database-data-error
43 (format nil "string \"~a\" of length ~d is longer than max-length: ~d"
44 ,string ,size ,max-length)))
45 (with-cast-pointer (char-ptr ,ptr :byte)
47 (setf (deref-array char-ptr '(:array :byte) i)
48 (char-code (char ,string i))))
49 (setf (deref-array char-ptr '(:array :byte) ,size) 0)))))
51 (defmacro with-allocate-foreign-string ((var len) &body body)
52 "Safely does uffi:allocate-foreign-string-- making sure we do the uffi:free-foreign-object"
56 (setf ,var (uffi:allocate-foreign-string ,len))
59 (uffi:free-foreign-object ,var)))))
61 (defmacro with-allocate-foreign-strings (bindings &rest body)
63 `(with-allocate-foreign-string ,(car bindings)
64 (with-allocate-foreign-strings ,(cdr bindings)
68 (defun handle-error (henv hdbc hstmt)
69 (with-allocate-foreign-strings ((sql-state 256)
70 (error-message #.$SQL_MAX_MESSAGE_LENGTH))
71 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
73 (SQLError henv hdbc hstmt sql-state
74 error-code error-message
75 #.$SQL_MAX_MESSAGE_LENGTH msg-length)
77 (convert-from-foreign-string error-message)
78 (convert-from-foreign-string sql-state)
79 (deref-pointer msg-length :short)
80 (deref-pointer error-code #.$ODBC-LONG-TYPE)))))
82 (defun sql-state (henv hdbc hstmt)
83 (with-allocate-foreign-strings ((sql-state 256)
84 (error-message #.$SQL_MAX_MESSAGE_LENGTH))
85 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
87 (SQLError henv hdbc hstmt sql-state error-code
88 error-message #.$SQL_MAX_MESSAGE_LENGTH msg-length)
89 (convert-from-foreign-string sql-state)
90 ;; test this: return a keyword for efficiency
91 ;;(%cstring-to-keyword state)
94 (defmacro with-error-handling ((&key henv hdbc hstmt (print-info t))
96 (let ((result-code (gensym "RC-")))
97 `(let ((,result-code ,odbc-call))
99 ;; Check for allegro v7 & v8 bug with ODBC calls returning
100 ;; 32-bit unsigned ints, not 16-bit signed ints
101 #+(and allegro mswindows)
102 (when (> ,result-code #xFFFF)
103 (warn (format nil "16-bit return bug: result-code #x~X for expression ~S"
104 ,result-code (quote ,odbc-call)))
105 (setq ,result-code (logand ,result-code #xFFFF))
106 (when (> ,result-code #x7FFF)
107 (setq ,result-code (- ,result-code #x10000))))
111 (progn ,result-code ,@body))
112 (#.$SQL_SUCCESS_WITH_INFO
114 (multiple-value-bind (error-message sql-state)
115 (handle-error (or ,henv +null-handle-ptr+)
116 (or ,hdbc +null-handle-ptr+)
117 (or ,hstmt +null-handle-ptr+))
119 (format *info-output* "[ODBC info ~A] ~A state: ~A"
120 ,result-code error-message
122 (progn ,result-code ,@body))
123 (#.$SQL_INVALID_HANDLE
125 'clsql-sys:sql-database-error
126 :message "ODBC: Invalid handle"))
127 (#.$SQL_STILL_EXECUTING
129 'clsql-sys:sql-temporary-error
130 :message "ODBC: Still executing"))
132 (multiple-value-bind (error-message sql-state)
133 (handle-error (or ,henv +null-handle-ptr+)
134 (or ,hdbc +null-handle-ptr+)
135 (or ,hstmt +null-handle-ptr+))
137 'clsql-sys:sql-database-error
138 :message error-message
139 :secondary-error-id sql-state)))
140 (#.$SQL_NO_DATA_FOUND
141 (progn ,result-code ,@body))
142 ;; work-around for Allegro 7.0beta AMD64 which returns negative numbers
144 (multiple-value-bind (error-message sql-state)
145 (handle-error (or ,henv +null-handle-ptr+)
146 (or ,hdbc +null-handle-ptr+)
147 (or ,hstmt +null-handle-ptr+))
149 'clsql-sys:sql-database-error
150 :message error-message
151 :secondary-error-id sql-state))
153 (progn ,result-code ,@body))))))
155 (defun %new-environment-handle ()
157 (with-foreign-object (phenv 'sql-handle)
160 (SQLAllocHandle $SQL_HANDLE_ENV +null-handle-ptr+ phenv)
161 (deref-pointer phenv 'sql-handle)))))
162 (%set-attr-odbc-version henv $SQL_OV_ODBC3)
166 (defun %sql-free-environment (henv)
171 (defun %new-db-connection-handle (henv)
172 (with-foreign-object (phdbc 'sql-handle)
173 (setf (deref-pointer phdbc 'sql-handle) +null-handle-ptr+)
176 (SQLAllocHandle $SQL_HANDLE_DBC henv phdbc)
177 (deref-pointer phdbc 'sql-handle))))
179 (defun %free-statement (hstmt option)
187 (:unbind $SQL_UNBIND)
188 (:reset $SQL_RESET_PARAMS)))))
190 (defmacro with-statement-handle ((hstmt hdbc) &body body)
191 `(let ((,hstmt (%new-statement-handle ,hdbc)))
194 (%free-statement ,hstmt :drop))))
196 ;; functional interface
198 (defun %sql-connect (hdbc server uid pwd)
199 (with-cstrings ((server-ptr server)
204 (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr
205 $SQL_NTS pwd-ptr $SQL_NTS))))
207 (defun %sql-driver-connect (hdbc connection-string completion window-handle)
208 (with-cstring (connection-ptr connection-string)
209 (with-allocate-foreign-string (completed-connection-string-ptr $SQL_MAX_CONN_OUT)
210 (with-foreign-object (completed-connection-length :short)
213 (SQLDriverConnect hdbc
216 connection-ptr $SQL_NTS
217 completed-connection-string-ptr $SQL_MAX_CONN_OUT
218 completed-connection-length
221 (defun %disconnect (hdbc)
227 (SQLFreeHandle $SQL_HANDLE_DBC hdbc))))
229 (defun %commit (henv hdbc)
231 (:henv henv :hdbc hdbc)
233 henv hdbc $SQL_COMMIT)))
235 (defun %rollback (henv hdbc)
237 (:henv henv :hdbc hdbc)
239 henv hdbc $SQL_ROLLBACK)))
241 ; col-nr is zero-based in Lisp but 1 based in sql
242 ; col-nr = :bookmark retrieves a bookmark.
243 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
247 (if (eq column-nr :bookmark) 0 (1+ column-nr))
248 c-type data-ptr precision out-len-ptr)))
250 ; parameter-nr is zero-based in Lisp
251 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
252 sql-type precision scale data-ptr
253 max-value out-len-ptr)
256 (SQLBindParameter hstmt (1+ parameter-nr)
257 parameter-type ;$SQL_PARAM_INPUT
259 sql-type ;$SQL_VARCHAR
260 precision ;(1- (length str))
264 out-len-ptr ;#.+null-ptr+
267 (defun %sql-fetch (hstmt)
272 (defun %new-statement-handle (hdbc)
273 (let ((statement-handle
274 (with-foreign-object (phstmt 'sql-handle)
277 (SQLAllocHandle $SQL_HANDLE_STMT hdbc phstmt)
278 (deref-pointer phstmt 'sql-handle)))))
279 (if (uffi:null-pointer-p statement-handle)
280 (error 'clsql:sql-database-error :message "Received null statement handle.")
283 (defun %sql-get-info (hdbc info-type)
285 ;; those return string
286 ((#.$SQL_ACCESSIBLE_PROCEDURES
287 #.$SQL_ACCESSIBLE_TABLES
289 #.$SQL_DATA_SOURCE_NAME
290 #.$SQL_DATA_SOURCE_READ_ONLY
294 #.$SQL_DRIVER_ODBC_VER
296 #.$SQL_EXPRESSIONS_IN_ORDERBY
297 #.$SQL_IDENTIFIER_QUOTE_CHAR
299 #.$SQL_LIKE_ESCAPE_CLAUSE
300 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
301 #.$SQL_MULT_RESULT_SETS
302 #.$SQL_MULTIPLE_ACTIVE_TXN
303 #.$SQL_NEED_LONG_DATA_LEN
304 #.$SQL_ODBC_SQL_OPT_IEF
306 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
309 #.$SQL_PROCEDURE_TERM
311 #.$SQL_QUALIFIER_NAME_SEPARATOR
312 #.$SQL_QUALIFIER_TERM
314 #.$SQL_SEARCH_PATTERN_ESCAPE
316 #.$SQL_SPECIAL_CHARACTERS
319 (with-allocate-foreign-string (info-ptr 1024)
320 (with-foreign-object (info-length-ptr :short)
323 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
324 (convert-from-foreign-string info-ptr)))))
325 ;; those returning a word
326 ((#.$SQL_ACTIVE_CONNECTIONS
327 #.$SQL_ACTIVE_STATEMENTS
328 #.$SQL_CONCAT_NULL_BEHAVIOR
329 #.$SQL_CORRELATION_NAME
330 #.$SQL_CURSOR_COMMIT_BEHAVIOR
331 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
332 #.$SQL_MAX_COLUMN_NAME_LEN
333 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
334 #.$SQL_MAX_COLUMNS_IN_INDEX
335 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
336 #.$SQL_MAX_COLUMNS_IN_SELECT
337 #.$SQL_MAX_COLUMNS_IN_TABLE
338 #.$SQL_MAX_CURSOR_NAME_LEN
339 #.$SQL_MAX_OWNER_NAME_LEN
340 #.$SQL_MAX_PROCEDURE_NAME_LEN
341 #.$SQL_MAX_QUALIFIER_NAME_LEN
342 #.$SQL_MAX_TABLE_NAME_LEN
343 #.$SQL_MAX_TABLES_IN_SELECT
344 #.$SQL_MAX_USER_NAME_LEN
345 #.$SQL_NON_NULLABLE_COLUMNS
346 #.$SQL_NULL_COLLATION
347 #.$SQL_ODBC_API_CONFORMANCE
348 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
349 #.$SQL_ODBC_SQL_CONFORMANCE
350 #.$SQL_QUALIFIER_LOCATION
351 #.$SQL_QUOTED_IDENTIFIER_CASE
353 (with-foreign-objects ((info-ptr :short)
354 (info-length-ptr :short))
362 (deref-pointer info-ptr :short)))
364 ;; those returning a long bitmask
366 #.$SQL_BOOKMARK_PERSISTENCE
367 #.$SQL_CONVERT_BIGINT
368 #.$SQL_CONVERT_BINARY
372 #.$SQL_CONVERT_DECIMAL
373 #.$SQL_CONVERT_DOUBLE
375 #.$SQL_CONVERT_INTEGER
376 #.$SQL_CONVERT_LONGVARCHAR
377 #.$SQL_CONVERT_NUMERIC
379 #.$SQL_CONVERT_SMALLINT
381 #.$SQL_CONVERT_TIMESTAMP
382 #.$SQL_CONVERT_TINYINT
383 #.$SQL_CONVERT_VARBINARY
384 #.$SQL_CONVERT_VARCHAR
385 #.$SQL_CONVERT_LONGVARBINARY
386 #.$SQL_CONVERT_FUNCTIONS
387 #.$SQL_FETCH_DIRECTION
389 #.$SQL_GETDATA_EXTENSIONS
391 #.$SQL_MAX_INDEX_SIZE
393 #.$SQL_MAX_STATEMENT_LEN
394 #.$SQL_NUMERIC_FUNCTIONS
396 #.$SQL_POS_OPERATIONS
397 #.$SQL_POSITIONED_STATEMENTS
398 #.$SQL_QUALIFIER_USAGE
399 #.$SQL_SCROLL_CONCURRENCY
400 #.$SQL_SCROLL_OPTIONS
401 #.$SQL_STATIC_SENSITIVITY
402 #.$SQL_STRING_FUNCTIONS
404 #.$SQL_SYSTEM_FUNCTIONS
405 #.$SQL_TIMEDATE_ADD_INTERVALS
406 #.$SQL_TIMEDATE_DIFF_INTERVALS
407 #.$SQL_TIMEDATE_FUNCTIONS
408 #.$SQL_TXN_ISOLATION_OPTION
410 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
411 (info-length-ptr :short))
419 (deref-pointer info-ptr #.$ODBC-LONG-TYPE)))
421 ;; those returning a long integer
422 ((#.$SQL_DEFAULT_TXN_ISOLATION
428 #.$SQL_IDENTIFIER_CASE
429 #.$SQL_MAX_BINARY_LITERAL_LEN
430 #.$SQL_MAX_CHAR_LITERAL_LEN
431 #.$SQL_ACTIVE_ENVIRONMENTS
433 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
434 (info-length-ptr :short))
437 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
438 (deref-pointer info-ptr #.$ODBC-LONG-TYPE))))))
440 (defun %sql-exec-direct (sql hstmt henv hdbc)
441 (with-cstring (sql-ptr sql)
443 (:hstmt hstmt :henv henv :hdbc hdbc)
444 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
446 (defun %sql-cancel (hstmt)
451 (defun %sql-execute (hstmt)
456 (defun result-columns-count (hstmt)
457 (with-foreign-objects ((columns-nr-ptr :short))
458 (with-error-handling (:hstmt hstmt)
459 (SQLNumResultCols hstmt columns-nr-ptr)
460 (deref-pointer columns-nr-ptr :short))))
462 (defun result-rows-count (hstmt)
463 (with-foreign-objects ((row-count-ptr #.$ODBC-LONG-TYPE))
464 (with-error-handling (:hstmt hstmt)
465 (SQLRowCount hstmt row-count-ptr)
466 (deref-pointer row-count-ptr #.$ODBC-LONG-TYPE))))
468 ;; column counting is 1-based
469 (defun %describe-column (hstmt column-nr)
470 (with-allocate-foreign-string (column-name-ptr 256)
471 (with-foreign-objects ((column-name-length-ptr :short)
472 (column-sql-type-ptr :short)
473 (column-precision-ptr #.$ODBC-ULONG-TYPE)
474 (column-scale-ptr :short)
475 (column-nullable-p-ptr :short))
476 (with-error-handling (:hstmt hstmt)
477 (SQLDescribeCol hstmt column-nr column-name-ptr 256
478 column-name-length-ptr
482 column-nullable-p-ptr)
484 (convert-from-foreign-string column-name-ptr)
485 (deref-pointer column-sql-type-ptr :short)
486 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
487 (deref-pointer column-scale-ptr :short)
488 (deref-pointer column-nullable-p-ptr :short))))))
490 ;; parameter counting is 1-based
491 ;; this function isn't used, which is good because FreeTDS dosn't support it.
492 (defun %describe-parameter (hstmt parameter-nr)
493 (with-foreign-objects ((column-sql-type-ptr :short)
494 (column-precision-ptr #.$ODBC-ULONG-TYPE)
495 (column-scale-ptr :short)
496 (column-nullable-p-ptr :short))
499 (SQLDescribeParam hstmt parameter-nr
503 column-nullable-p-ptr)
505 (deref-pointer column-sql-type-ptr :short)
506 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
507 (deref-pointer column-scale-ptr :short)
508 (deref-pointer column-nullable-p-ptr :short)))))
510 (defun %column-attributes (hstmt column-nr descriptor-type)
511 (with-allocate-foreign-string (descriptor-info-ptr 256)
512 (with-foreign-objects ((descriptor-length-ptr :short)
513 (numeric-descriptor-ptr #.$ODBC-LONG-TYPE))
516 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr
517 256 descriptor-length-ptr
518 numeric-descriptor-ptr)
520 (convert-from-foreign-string descriptor-info-ptr)
521 (deref-pointer numeric-descriptor-ptr #.$ODBC-LONG-TYPE))))))
523 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
524 table-name column-name)
525 (with-cstrings ((table-qualifier-ptr table-qualifier)
526 (table-owner-ptr table-owner)
527 (table-name-ptr table-name)
528 (column-name-ptr column-name))
532 table-qualifier-ptr (length table-qualifier)
533 table-owner-ptr (length table-owner)
534 table-name-ptr (length table-name)
535 column-name-ptr (length column-name)))))
537 (defun %describe-columns (hdbc table-qualifier table-owner
538 table-name column-name)
539 (with-statement-handle (hstmt hdbc)
540 (%prepare-describe-columns hstmt table-qualifier table-owner
541 table-name column-name)
542 (fetch-all-rows hstmt)))
544 (defun %sql-data-sources (henv &key (direction :first))
545 (with-allocate-foreign-strings ((name-ptr (1+ $SQL_MAX_DSN_LENGTH))
546 (description-ptr 1024))
547 (with-foreign-objects ((name-length-ptr :short)
548 (description-length-ptr :short))
549 (let ((res (with-error-handling
553 (:first $SQL_FETCH_FIRST)
554 (:next $SQL_FETCH_NEXT))
556 (1+ $SQL_MAX_DSN_LENGTH)
560 description-length-ptr))))
561 (when (= res $SQL_NO_DATA_FOUND)
563 (convert-from-foreign-string name-ptr)
564 (convert-from-foreign-string description-ptr)))))))
568 (defun sql-to-c-type (sql-type)
570 ;; Added -10 for MSSQL ntext type and -11 for nvarchar
571 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
572 #.$SQL_NUMERIC #.$sql_decimal -8 -9 -10 -11) $SQL_C_CHAR)
573 (#.$SQL_INTEGER $SQL_C_SLONG)
574 (#.$SQL_BIGINT $SQL_C_SBIGINT)
575 (#.$SQL_SMALLINT $SQL_C_SSHORT)
576 (#.$SQL_DOUBLE $SQL_C_DOUBLE)
577 (#.$SQL_FLOAT $SQL_C_DOUBLE)
578 (#.$SQL_REAL $SQL_C_FLOAT)
579 (#.$SQL_DATE $SQL_C_DATE)
580 (#.$SQL_TIME $SQL_C_TIME)
581 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
582 (#.$SQL_TYPE_DATE $SQL_C_TYPE_DATE)
583 (#.$SQL_TYPE_TIME $SQL_C_TYPE_TIME)
584 (#.$SQL_TYPE_TIMESTAMP $SQL_C_TYPE_TIMESTAMP)
585 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
586 (#.$SQL_TINYINT $SQL_C_STINYINT)
587 (#.$SQL_BIT $SQL_C_BIT)))
589 (def-type byte-pointer-type (* :byte))
590 (def-type short-pointer-type (* :short))
591 (def-type int-pointer-type (* :int))
592 (def-type long-pointer-type (* #.$ODBC-LONG-TYPE))
593 (def-type big-pointer-type (* #.$ODBC-BIG-TYPE))
594 (def-type float-pointer-type (* :float))
595 (def-type double-pointer-type (* :double))
596 (def-type string-pointer-type (* :unsigned-char))
598 (defun get-cast-byte (ptr)
599 (locally (declare (type byte-pointer-type ptr))
600 (deref-pointer ptr :byte)))
602 (defun get-cast-short (ptr)
603 (locally (declare (type short-pointer-type ptr))
604 (deref-pointer ptr :short)))
606 (defun get-cast-int (ptr)
607 (locally (declare (type int-pointer-type ptr))
608 (deref-pointer ptr :int)))
610 (defun get-cast-long (ptr)
611 (locally (declare (type long-pointer-type ptr))
612 (deref-pointer ptr #.$ODBC-LONG-TYPE)))
614 (defun get-cast-big (ptr)
615 (locally (declare (type big-pointer-type ptr))
616 (deref-pointer ptr #.$ODBC-BIG-TYPE)))
618 (defun get-cast-single-float (ptr)
619 (locally (declare (type float-pointer-type ptr))
620 (deref-pointer ptr :float)))
622 (defun get-cast-double-float (ptr)
623 (locally (declare (type double-pointer-type ptr))
624 (deref-pointer ptr :double)))
626 (defun get-cast-foreign-string (ptr)
627 (locally (declare (type string-pointer-type ptr))
628 (convert-from-foreign-string ptr)))
630 (defun get-cast-binary (ptr len format)
631 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
632 (with-cast-pointer (casted ptr :unsigned-byte)
634 (:unsigned-byte-vector
635 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
637 (setf (aref vector i)
638 (deref-array casted '(:array :unsigned-byte) i)))
641 (let ((vector (make-array (ash len 3) :element-type 'bit)))
643 (let ((byte (deref-array casted '(:array :byte) i)))
645 (setf (bit vector (+ (ash i 3) j))
646 (logand (ash byte (- j 7)) 1)))))
650 (defun read-data (data-ptr c-type sql-type out-len-ptr result-type)
651 (declare (type long-ptr-type out-len-ptr))
652 (let* ((out-len (get-cast-long out-len-ptr))
654 (cond ((= out-len $SQL_NULL_DATA) *null*)
657 ;; SQL extended datatypes
658 (#.$SQL_TINYINT (get-cast-byte data-ptr))
659 (#.$SQL_C_STINYINT (get-cast-byte data-ptr)) ;; ?
660 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
661 (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
662 (#.$SQL_INTEGER (get-cast-int data-ptr))
663 (#.$SQL_BIGINT (get-cast-big data-ptr))
664 ;; TODO: Change this to read in rationals instead of doubles
665 ((#.$SQL_DECIMAL #.$SQL_NUMERIC)
666 (let* ((*read-base* 10)
667 (*read-default-float-format* 'double-float)
668 (str (get-cast-foreign-string data-ptr)))
669 (read-from-string str)))
670 (#.$SQL_BIT (get-cast-byte data-ptr))
673 ((#.$SQL_C_DATE #.$SQL_C_TYPE_DATE)
674 (funcall *time-format* (date-to-clsql-time data-ptr)))
675 ((#.$SQL_C_TIME #.$SQL_C_TYPE_TIME)
676 (funcall *time-format* (time-to-clsql-time data-ptr)))
677 ((#.$SQL_C_TIMESTAMP #.$SQL_C_TYPE_TIMESTAMP)
678 (funcall *time-format* (timestamp-to-clsql-time data-ptr)))
680 (get-cast-int data-ptr))
682 (get-cast-single-float data-ptr))
684 (get-cast-double-float data-ptr))
686 (get-cast-long data-ptr))
688 (#.$SQL_C_BIT ; encountered only in Access
689 (get-cast-byte data-ptr))
691 (get-cast-binary data-ptr out-len *binary-format*))
692 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
693 (get-cast-short data-ptr)) ; LMH
694 (#.$SQL_C_SBIGINT (get-cast-big data-ptr))
697 (code-char (get-cast-short data-ptr)))
699 (get-cast-foreign-string data-ptr)))))))))
701 ;; FIXME: this could be better optimized for types which use READ-FROM-STRING above
703 (if (and (or (eq result-type t) (eq result-type :string))
705 (not (stringp value)))
706 (write-to-string value)
709 ;; which value is appropriate?
710 (defparameter +max-precision+ 4001)
712 (defvar *break-on-unknown-data-type* t)
714 ;; C. Stacy's idea to factor this out
715 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
716 ;; so that I don't have to remember to make changes in more than one place.
717 ;; Just keep it in synch with READ-DATA."
718 (defun %allocate-bindings (sql-type precision)
719 (let* ((c-type (sql-to-c-type sql-type))
720 (size (if (zerop precision)
721 +max-precision+ ;; if the precision cannot be determined
722 (min precision +max-precision+)))
723 (long-p (= size +max-precision+))
725 (case c-type ;; add more?
726 (#.$SQL_C_SLONG (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE))
727 ((#.$SQL_C_DATE #.$SQL_C_TYPE_DATE) (allocate-foreign-object 'sql-c-date))
728 ((#.$SQL_C_TIME #.$SQL_C_TYPE_TIME) (allocate-foreign-object 'sql-c-time))
729 ((#.$SQL_C_TIMESTAMP #.$SQL_C_TYPE_TIMESTAMP) (allocate-foreign-object 'sql-c-timestamp))
730 (#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
731 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
732 (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
733 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
734 (#.$SQL_C_SBIGINT (uffi:allocate-foreign-object #.$ODBC-BIG-TYPE))
735 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
736 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
737 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
739 ;; Maybe should signal a restartable condition for this?
740 (when *break-on-unknown-data-type*
741 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
742 sql-type precision size c-type))
743 (uffi:allocate-foreign-object :byte (1+ size)))))
744 (out-len-ptr (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE)))
745 (values c-type data-ptr out-len-ptr size long-p)))
747 (defun fetch-all-rows (hstmt &key free-option flatp)
748 (let ((column-count (result-columns-count hstmt)))
749 (unless (zerop column-count)
750 (let ((names (make-array column-count))
751 (sql-types (make-array column-count :element-type 'fixnum))
752 (c-types (make-array column-count :element-type 'fixnum))
753 (precisions (make-array column-count :element-type 'fixnum))
754 (data-ptrs (make-array column-count :initial-element nil))
755 (out-len-ptrs (make-array column-count :initial-element nil))
756 (scales (make-array column-count :element-type 'fixnum))
757 (nullables-p (make-array column-count :element-type 'fixnum)))
761 (dotimes (col-nr column-count)
762 ;; get column information
763 (multiple-value-bind (name sql-type precision scale nullable-p)
764 (%describe-column hstmt (1+ col-nr))
765 ;; allocate space to bind result rows to
766 (multiple-value-bind (c-type data-ptr out-len-ptr)
767 (%allocate-bindings sql-type precision)
768 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
769 (setf (svref names col-nr) name
770 (aref sql-types col-nr) sql-type
771 (aref c-types col-nr) (sql-to-c-type sql-type)
772 (aref precisions col-nr) (if (zerop precision) 0 precision)
773 (aref scales col-nr) scale
774 (aref nullables-p col-nr) nullable-p
775 (aref data-ptrs col-nr) data-ptr
776 (aref out-len-ptrs col-nr) out-len-ptr))))
780 (when (> column-count 1)
781 (error 'clsql:sql-database-error
782 :message "If more than one column is to be fetched, flatp has to be nil."))
783 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
785 (read-data (aref data-ptrs 0)
788 (aref out-len-ptrs 0)
791 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
793 (loop for col-nr from 0 to (1- column-count)
795 (read-data (aref data-ptrs col-nr)
796 (aref c-types col-nr)
797 (aref sql-types col-nr)
798 (aref out-len-ptrs col-nr)
801 ;; dispose of memory etc
802 (when free-option (%free-statement hstmt free-option))
803 (dotimes (col-nr column-count)
804 (let ((data-ptr (aref data-ptrs col-nr))
805 (out-len-ptr (aref out-len-ptrs col-nr)))
806 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
807 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
809 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
810 ;; of %do-fetch into sql package (has been done)
812 (defun %sql-prepare (hstmt sql)
813 (with-cstring (sql-ptr sql)
814 (with-error-handling (:hstmt hstmt)
815 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
817 ;; depending on option, we return a long int or a string; string not implemented
818 (defun get-connection-option (hdbc option)
819 (with-foreign-object (param-ptr #.$ODBC-LONG-TYPE)
820 (with-error-handling (:hdbc hdbc)
821 (SQLGetConnectOption hdbc option param-ptr)
822 (deref-pointer param-ptr #.$ODBC-LONG-TYPE))))
824 (defun set-connection-option (hdbc option param)
825 (with-error-handling (:hdbc hdbc)
826 (SQLSetConnectOption hdbc option param)))
828 (defun disable-autocommit (hdbc)
829 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
831 (defun enable-autocommit (hdbc)
832 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
834 (defun %sql-set-pos (hstmt row option lock)
837 (SQLSetPos hstmt row option lock)))
839 (defun %sql-extended-fetch (hstmt fetch-type row)
840 (with-foreign-objects ((row-count-ptr #.$ODBC-ULONG-TYPE)
841 (row-status-ptr :short))
842 (with-error-handling (:hstmt hstmt)
843 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
845 (values (deref-pointer row-count-ptr #.$ODBC-ULONG-TYPE)
846 (deref-pointer row-status-ptr :short)))))
848 ; column-nr is zero-based
849 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
851 (:hstmt hstmt :print-info nil)
852 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
853 c-type data-ptr precision out-len-ptr)))
855 (defun %sql-param-data (hstmt param-ptr)
856 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
857 (SQLParamData hstmt param-ptr)))
859 (defun %sql-put-data (hstmt data-ptr size)
861 (:hstmt hstmt :print-info t) ;; nil
862 (SQLPutData hstmt data-ptr size)))
864 (defconstant $sql-data-truncated (intern "01004" :keyword))
867 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
868 out-len-ptr result-type)
869 (declare (type long-ptr-type out-len-ptr)
870 (ignore result-type))
872 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
873 +max-precision+ out-len-ptr))
874 (out-len (get-cast-long out-len-ptr))
875 (result (if (equal out-len #.$SQL_NULL_DATA)
876 (return-from read-data-in-chunks *null*)
878 ;;this isn't the most efficient way of doing it:
879 ;;the foreign string gets copied to lisp, then
880 ;;that is copied into the final string. However,
881 ;;the previous impl that tried to copy one
882 ;;character over at a time failed miserably on
883 ;;multibyte characters.
885 ;;In the face of multibyte characters, the out-len
886 ;;tells us the length in bytes but that doesn't
887 ;;particularly help us here in allocating a lisp
888 ;;array. So our best strategy is to just let the
889 ;;foreign library that's already dealing with
890 ;;encodings do its thing.
892 (with-output-to-string (str)
893 (loop do (if (= c-type #.$SQL_CHAR)
894 (write-sequence (get-cast-foreign-string data-ptr) str)
895 (error 'clsql:sql-database-error
896 :message "wrong type. preliminary."))
897 while (and (= res $SQL_SUCCESS_WITH_INFO)
898 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
900 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
901 +max-precision+ out-len-ptr)))))))
903 ;; reset the out length for the next row
904 (setf (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE) #.$SQL_NO_TOTAL)
905 (if (= sql-type $SQL_DECIMAL)
906 (let ((*read-base* 10))
907 (read-from-string result))
911 (def-type c-timestamp-ptr-type (* (:struct sql-c-timestamp)))
912 (def-type c-time-ptr-type (* (:struct sql-c-time)))
913 (def-type c-date-ptr-type (* (:struct sql-c-date)))
915 (defun timestamp-to-clsql-time (ptr)
916 (declare (type c-timestamp-ptr-type ptr))
918 :second (get-slot-value ptr 'sql-c-timestamp 'second)
919 :minute (get-slot-value ptr 'sql-c-timestamp 'minute)
920 :hour (get-slot-value ptr 'sql-c-timestamp 'hour)
921 :day (get-slot-value ptr 'sql-c-timestamp 'day)
922 :month (get-slot-value ptr 'sql-c-timestamp 'month)
923 :year (get-slot-value ptr 'sql-c-timestamp 'year)
924 :usec (let ((frac (get-slot-value ptr 'sql-c-timestamp 'fraction)))
925 (if frac (/ frac 1000) 0))))
927 (defun universal-time-to-timestamp (time &optional (fraction 0))
928 "TODO: Dead function?"
929 (multiple-value-bind (sec min hour day month year)
930 (decode-universal-time time)
931 (let ((ptr (allocate-foreign-object 'sql-c-timestamp)))
932 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
933 (get-slot-value ptr 'sql-c-timestamp 'minute) min
934 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
935 (get-slot-value ptr 'sql-c-timestamp 'day) day
936 (get-slot-value ptr 'sql-c-timestamp 'month) month
937 (get-slot-value ptr 'sql-c-timestamp 'year) year
938 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
941 (defun %put-timestamp (ptr time &optional (fraction 0))
942 "TODO: Dead function?"
943 (declare (type c-timestamp-ptr-type ptr))
944 (multiple-value-bind (sec min hour day month year)
945 (decode-universal-time time)
946 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
947 (get-slot-value ptr 'sql-c-timestamp 'minute) min
948 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
949 (get-slot-value ptr 'sql-c-timestamp 'day) day
950 (get-slot-value ptr 'sql-c-timestamp 'month) month
951 (get-slot-value ptr 'sql-c-timestamp 'year) year
952 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
955 (defun date-to-clsql-time (ptr)
956 (declare (type c-date-ptr-type ptr))
958 :second 0 :minute 0 :hour 0
959 :day (get-slot-value ptr 'sql-c-timestamp 'day)
960 :month (get-slot-value ptr 'sql-c-timestamp 'month)
961 :year (get-slot-value ptr 'sql-c-timestamp 'year)))
963 (defun time-to-clsql-time (ptr)
964 (declare (type c-time-ptr-type ptr))
966 :second (get-slot-value ptr 'sql-c-timestamp 'second)
967 :minute (get-slot-value ptr 'sql-c-timestamp 'minute)
968 :hour (get-slot-value ptr 'sql-c-timestamp 'hour)))
973 (defun %set-attr-odbc-version (henv version)
974 (with-error-handling (:henv henv)
975 ;;note that we are passing version as an integer that happens to be
976 ;;stuffed into a pointer.
977 ;;http://msdn.microsoft.com/en-us/library/ms709285%28v=VS.85%29.aspx
978 (SQLSetEnvAttr henv $SQL_ATTR_ODBC_VERSION
979 (make-pointer version :void) 0)))
981 (defun %list-tables (hstmt)
982 (with-error-handling (:hstmt hstmt)
983 (SQLTables hstmt +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0)))
985 (defun %table-statistics (table hstmt &key unique (ensure t)
986 &aux (table (princ-to-string
987 (clsql-sys::unescaped-database-identifier table))))
988 (with-cstrings ((table-cs table))
989 (with-error-handling (:hstmt hstmt)
995 (if unique $SQL_INDEX_UNIQUE $SQL_INDEX_ALL)
996 (if ensure $SQL_ENSURE $SQL_QUICK)))))
998 (defun %list-data-sources (henv)
1000 (with-foreign-strings ((dsn-ptr (1+ $SQL_MAX_DSN_LENGTH))
1002 (with-foreign-objects ((dsn-len :short)
1004 (let ((res (with-error-handling (:henv henv)
1005 (SQLDataSources henv $SQL_FETCH_FIRST dsn-ptr
1006 (1+ $SQL_MAX_DSN_LENGTH)
1007 dsn-len desc-ptr 256 desc-len))))
1008 (when (or (eql res $SQL_SUCCESS)
1009 (eql res $SQL_SUCCESS_WITH_INFO))
1010 (push (convert-from-foreign-string dsn-ptr) results))
1012 (do ((res (with-error-handling (:henv henv)
1013 (SQLDataSources henv $SQL_FETCH_NEXT dsn-ptr
1014 (1+ $SQL_MAX_DSN_LENGTH)
1015 dsn-len desc-ptr 256 desc-len))
1016 (with-error-handling (:henv henv)
1017 (SQLDataSources henv $SQL_FETCH_NEXT dsn-ptr
1018 (1+ $SQL_MAX_DSN_LENGTH)
1019 dsn-len desc-ptr 256 desc-len))))
1020 ((not (or (eql res $SQL_SUCCESS)
1021 (eql res $SQL_SUCCESS_WITH_INFO))))
1022 (push (convert-from-foreign-string dsn-ptr) results)))))
1023 (nreverse results)))