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
11 ;;;; This file, part of CLSQL, is Copyright (c) 2004 by Kevin M. Rosenberg
12 ;;;; and Copyright (C) Paul Meurer 1999 - 2001. All rights reserved.
14 ;;;; CLSQL users are granted the rights to distribute and use this software
15 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
16 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
17 ;;;; *************************************************************************
22 "Lisp representation of SQL Null value, default = nil.
23 May be locally bound to something else if a certain type is necessary.")
26 (defvar *binary-format* :unsigned-byte-vector)
27 (defvar *time-conversion-function*
28 (lambda (universal-time &optional fraction)
29 (declare (ignore fraction))
30 (clsql-sys:format-time
31 nil (clsql-sys:utime->time universal-time)
35 "Bound to a function that converts from a Lisp universal time fixnum (and a fractional
36 as possible second argument) to the desired representation of date/time/timestamp. By default, returns an iso-timestring.")
38 (defvar +null-ptr+ (make-null-pointer :byte))
39 (defparameter +null-handle-ptr+ (make-null-pointer :void))
40 (defvar *info-output* nil
41 "Stream to send SUCCESS_WITH_INFO messages.")
43 (defmacro %put-str (ptr string &optional max-length)
44 (let ((size (gensym)))
45 `(let ((,size (length ,string)))
46 (when (and ,max-length (> ,size ,max-length))
47 (error 'clsql:sql-database-data-error
49 (format nil "string \"~a\" of length ~d is longer than max-length: ~d"
50 ,string ,size ,max-length)))
51 (with-cast-pointer (char-ptr ,ptr :byte)
53 (setf (deref-array char-ptr '(:array :byte) i)
54 (char-code (char ,string i))))
55 (setf (deref-array char-ptr '(:array :byte) ,size) 0)))))
57 (defun %cstring-into-vector (ptr vector offset size-in-bytes)
58 (dotimes (i size-in-bytes)
59 (setf (schar vector offset)
60 (ensure-char-character
61 (deref-array ptr '(:array :unsigned-char) i)))
65 (defun handle-error (henv hdbc hstmt)
66 (let ((sql-state (allocate-foreign-string 256))
67 (error-message (allocate-foreign-string #.$SQL_MAX_MESSAGE_LENGTH)))
68 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
70 (SQLError henv hdbc hstmt sql-state
71 error-code error-message
72 #.$SQL_MAX_MESSAGE_LENGTH msg-length)
73 (let ((err (convert-from-foreign-string error-message))
74 (state (convert-from-foreign-string sql-state)))
75 (free-foreign-object error-message)
76 (free-foreign-object sql-state)
80 (deref-pointer msg-length :short)
81 (deref-pointer error-code #.$ODBC-LONG-TYPE))))))
83 (defun sql-state (henv hdbc hstmt)
84 (let ((sql-state (allocate-foreign-string 256))
85 (error-message (allocate-foreign-string #.$SQL_MAX_MESSAGE_LENGTH)))
86 (with-foreign-objects ((error-code #.$ODBC-LONG-TYPE)
88 (SQLError henv hdbc hstmt sql-state error-code
89 error-message #.$SQL_MAX_MESSAGE_LENGTH msg-length)
90 (let ((state (convert-from-foreign-string sql-state)))
91 (free-foreign-object error-message)
92 (free-foreign-object sql-state)
94 ;; test this: return a keyword for efficiency
95 ;;(%cstring-to-keyword state)
98 (defmacro with-error-handling ((&key henv hdbc hstmt (print-info t))
100 (let ((result-code (gensym "RC-")))
101 `(let ((,result-code ,odbc-call))
104 (progn ,result-code ,@body))
105 (#.$SQL_SUCCESS_WITH_INFO
107 (multiple-value-bind (error-message sql-state)
108 (handle-error (or ,henv +null-handle-ptr+)
109 (or ,hdbc +null-handle-ptr+)
110 (or ,hstmt +null-handle-ptr+))
112 (format *info-output* "[ODBC info ~A] ~A state: ~A"
113 ,result-code error-message
115 (progn ,result-code ,@body))
116 (#.$SQL_INVALID_HANDLE
118 'clsql-sys:sql-database-error
119 :message "ODBC: Invalid handle"))
120 (#.$SQL_STILL_EXECUTING
122 'clsql-sys:sql-temporary-error
123 :message "ODBC: Still executing"))
125 (multiple-value-bind (error-message sql-state)
126 (handle-error (or ,henv +null-handle-ptr+)
127 (or ,hdbc +null-handle-ptr+)
128 (or ,hstmt +null-handle-ptr+))
130 'clsql-sys:sql-database-error
131 :message error-message
132 :secondary-error-id sql-state)))
133 (#.$SQL_NO_DATA_FOUND
134 (progn ,result-code ,@body))
135 ;; work-around for Allegro 7.0beta AMD64 which
136 ;; has for negative numbers
138 (multiple-value-bind (error-message sql-state)
139 (handle-error (or ,henv +null-handle-ptr+)
140 (or ,hdbc +null-handle-ptr+)
141 (or ,hstmt +null-handle-ptr+))
143 'clsql-sys:sql-database-error
144 :message error-message
145 :secondary-error-id sql-state))
147 (progn ,result-code ,@body))))))
149 (defun %new-environment-handle ()
151 (with-foreign-object (phenv 'sql-handle)
154 (SQLAllocHandle $SQL_HANDLE_ENV +null-handle-ptr+ phenv)
155 (deref-pointer phenv 'sql-handle)))))
156 (%set-attr-odbc-version henv $SQL_OV_ODBC3)
160 (defun %sql-free-environment (henv)
165 (defun %new-db-connection-handle (henv)
166 (with-foreign-object (phdbc 'sql-handle)
167 (setf (deref-pointer phdbc 'sql-handle) +null-handle-ptr+)
170 (SQLAllocHandle $SQL_HANDLE_DBC henv phdbc)
171 (deref-pointer phdbc 'sql-handle))))
173 (defun %free-statement (hstmt option)
181 (:unbind $SQL_UNBIND)
182 (:reset $SQL_RESET_PARAMS)))))
184 (defmacro with-statement-handle ((hstmt hdbc) &body body)
185 `(let ((,hstmt (%new-statement-handle ,hdbc)))
188 (%free-statement ,hstmt :drop))))
190 ;; functional interface
192 (defun %sql-connect (hdbc server uid pwd)
193 (with-cstrings ((server-ptr server)
198 (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr
199 $SQL_NTS pwd-ptr $SQL_NTS))))
202 (defun %disconnect (hdbc)
205 (SQLDisconnect hdbc)))
207 (defun %commit (henv hdbc)
209 (:henv henv :hdbc hdbc)
211 henv hdbc $SQL_COMMIT)))
213 (defun %rollback (henv hdbc)
215 (:henv henv :hdbc hdbc)
217 henv hdbc $SQL_ROLLBACK)))
219 ; col-nr is zero-based in Lisp
220 ; col-nr = :bookmark retrieves a bookmark.
221 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
225 (if (eq column-nr :bookmark) 0 (1+ column-nr))
226 c-type data-ptr precision out-len-ptr)))
228 ; parameter-nr is zero-based in Lisp
229 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
230 sql-type precision scale data-ptr
231 max-value out-len-ptr)
234 (SQLBindParameter hstmt (1+ parameter-nr)
235 parameter-type ;$SQL_PARAM_INPUT
237 sql-type ;$SQL_VARCHAR
238 precision ;(1- (length str))
242 out-len-ptr ;#.+null-ptr+
245 (defun %sql-fetch (hstmt)
250 (defun %new-statement-handle (hdbc)
251 (let ((statement-handle
252 (with-foreign-object (phstmt 'sql-handle)
255 (SQLAllocHandle $SQL_HANDLE_STMT hdbc phstmt)
256 (deref-pointer phstmt 'sql-handle)))))
257 (if (uffi:null-pointer-p statement-handle)
258 (error 'clsql:sql-database-error :message "Received null statement handle.")
261 (defun %sql-get-info (hdbc info-type)
263 ;; those return string
264 ((#.$SQL_ACCESSIBLE_PROCEDURES
265 #.$SQL_ACCESSIBLE_TABLES
267 #.$SQL_DATA_SOURCE_NAME
268 #.$SQL_DATA_SOURCE_READ_ONLY
272 #.$SQL_DRIVER_ODBC_VER
274 #.$SQL_EXPRESSIONS_IN_ORDERBY
275 #.$SQL_IDENTIFIER_QUOTE_CHAR
277 #.$SQL_LIKE_ESCAPE_CLAUSE
278 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
279 #.$SQL_MULT_RESULT_SETS
280 #.$SQL_MULTIPLE_ACTIVE_TXN
281 #.$SQL_NEED_LONG_DATA_LEN
282 #.$SQL_ODBC_SQL_OPT_IEF
284 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
287 #.$SQL_PROCEDURE_TERM
289 #.$SQL_QUALIFIER_NAME_SEPARATOR
290 #.$SQL_QUALIFIER_TERM
292 #.$SQL_SEARCH_PATTERN_ESCAPE
294 #.$SQL_SPECIAL_CHARACTERS
297 (let ((info-ptr (allocate-foreign-string 1024)))
298 (with-foreign-object (info-length-ptr :short)
301 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
302 (let ((info (convert-from-foreign-string info-ptr)))
303 (free-foreign-object info-ptr)
305 ;; those returning a word
306 ((#.$SQL_ACTIVE_CONNECTIONS
307 #.$SQL_ACTIVE_STATEMENTS
308 #.$SQL_CONCAT_NULL_BEHAVIOR
309 #.$SQL_CORRELATION_NAME
310 #.$SQL_CURSOR_COMMIT_BEHAVIOR
311 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
312 #.$SQL_MAX_COLUMN_NAME_LEN
313 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
314 #.$SQL_MAX_COLUMNS_IN_INDEX
315 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
316 #.$SQL_MAX_COLUMNS_IN_SELECT
317 #.$SQL_MAX_COLUMNS_IN_TABLE
318 #.$SQL_MAX_CURSOR_NAME_LEN
319 #.$SQL_MAX_OWNER_NAME_LEN
320 #.$SQL_MAX_PROCEDURE_NAME_LEN
321 #.$SQL_MAX_QUALIFIER_NAME_LEN
322 #.$SQL_MAX_TABLE_NAME_LEN
323 #.$SQL_MAX_TABLES_IN_SELECT
324 #.$SQL_MAX_USER_NAME_LEN
325 #.$SQL_NON_NULLABLE_COLUMNS
326 #.$SQL_NULL_COLLATION
327 #.$SQL_ODBC_API_CONFORMANCE
328 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
329 #.$SQL_ODBC_SQL_CONFORMANCE
330 #.$SQL_QUALIFIER_LOCATION
331 #.$SQL_QUOTED_IDENTIFIER_CASE
333 (with-foreign-objects ((info-ptr :short)
334 (info-length-ptr :short))
342 (deref-pointer info-ptr :short)))
344 ;; those returning a long bitmask
346 #.$SQL_BOOKMARK_PERSISTENCE
347 #.$SQL_CONVERT_BIGINT
348 #.$SQL_CONVERT_BINARY
352 #.$SQL_CONVERT_DECIMAL
353 #.$SQL_CONVERT_DOUBLE
355 #.$SQL_CONVERT_INTEGER
356 #.$SQL_CONVERT_LONGVARCHAR
357 #.$SQL_CONVERT_NUMERIC
359 #.$SQL_CONVERT_SMALLINT
361 #.$SQL_CONVERT_TIMESTAMP
362 #.$SQL_CONVERT_TINYINT
363 #.$SQL_CONVERT_VARBINARY
364 #.$SQL_CONVERT_VARCHAR
365 #.$SQL_CONVERT_LONGVARBINARY
366 #.$SQL_CONVERT_FUNCTIONS
367 #.$SQL_FETCH_DIRECTION
369 #.$SQL_GETDATA_EXTENSIONS
371 #.$SQL_MAX_INDEX_SIZE
373 #.$SQL_MAX_STATEMENT_LEN
374 #.$SQL_NUMERIC_FUNCTIONS
376 #.$SQL_POS_OPERATIONS
377 #.$SQL_POSITIONED_STATEMENTS
378 #.$SQL_QUALIFIER_USAGE
379 #.$SQL_SCROLL_CONCURRENCY
380 #.$SQL_SCROLL_OPTIONS
381 #.$SQL_STATIC_SENSITIVITY
382 #.$SQL_STRING_FUNCTIONS
384 #.$SQL_SYSTEM_FUNCTIONS
385 #.$SQL_TIMEDATE_ADD_INTERVALS
386 #.$SQL_TIMEDATE_DIFF_INTERVALS
387 #.$SQL_TIMEDATE_FUNCTIONS
388 #.$SQL_TXN_ISOLATION_OPTION
390 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
391 (info-length-ptr :short))
399 (deref-pointer info-ptr #.$ODBC-LONG-TYPE)))
401 ;; those returning a long integer
402 ((#.$SQL_DEFAULT_TXN_ISOLATION
408 #.$SQL_IDENTIFIER_CASE
409 #.$SQL_MAX_BINARY_LITERAL_LEN
410 #.$SQL_MAX_CHAR_LITERAL_LEN
411 #.$SQL_ACTIVE_ENVIRONMENTS
413 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
414 (info-length-ptr :short))
417 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
418 (deref-pointer info-ptr #.$ODBC-LONG-TYPE))))))
420 (defun %sql-exec-direct (sql hstmt henv hdbc)
421 (with-cstring (sql-ptr sql)
423 (:hstmt hstmt :henv henv :hdbc hdbc)
424 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
426 (defun %sql-cancel (hstmt)
431 (defun %sql-execute (hstmt)
436 (defun result-columns-count (hstmt)
437 (with-foreign-objects ((columns-nr-ptr :short))
438 (with-error-handling (:hstmt hstmt)
439 (SQLNumResultCols hstmt columns-nr-ptr)
440 (deref-pointer columns-nr-ptr :short))))
442 (defun result-rows-count (hstmt)
443 (with-foreign-objects ((row-count-ptr #.$ODBC-LONG-TYPE))
444 (with-error-handling (:hstmt hstmt)
445 (SQLRowCount hstmt row-count-ptr)
446 (deref-pointer row-count-ptr #.$ODBC-LONG-TYPE))))
448 ;; column counting is 1-based
449 (defun %describe-column (hstmt column-nr)
450 (let ((column-name-ptr (allocate-foreign-string 256)))
451 (with-foreign-objects ((column-name-length-ptr :short)
452 (column-sql-type-ptr :short)
453 (column-precision-ptr #.$ODBC-ULONG-TYPE)
454 (column-scale-ptr :short)
455 (column-nullable-p-ptr :short))
456 (with-error-handling (:hstmt hstmt)
457 (SQLDescribeCol hstmt column-nr column-name-ptr 256
458 column-name-length-ptr
462 column-nullable-p-ptr)
463 (let ((column-name (convert-from-foreign-string column-name-ptr)))
464 (free-foreign-object column-name-ptr)
467 (deref-pointer column-sql-type-ptr :short)
468 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
469 (deref-pointer column-scale-ptr :short)
470 (deref-pointer column-nullable-p-ptr :short)))))))
472 ;; parameter counting is 1-based
473 (defun %describe-parameter (hstmt parameter-nr)
474 (with-foreign-objects ((column-sql-type-ptr :short)
475 (column-precision-ptr #.$ODBC-ULONG-TYPE)
476 (column-scale-ptr :short)
477 (column-nullable-p-ptr :short))
480 (SQLDescribeParam hstmt parameter-nr
484 column-nullable-p-ptr)
486 (deref-pointer column-sql-type-ptr :short)
487 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
488 (deref-pointer column-scale-ptr :short)
489 (deref-pointer column-nullable-p-ptr :short)))))
491 (defun %column-attributes (hstmt column-nr descriptor-type)
492 (let ((descriptor-info-ptr (allocate-foreign-string 256)))
493 (with-foreign-objects ((descriptor-length-ptr :short)
494 (numeric-descriptor-ptr #.$ODBC-LONG-TYPE))
497 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr
498 256 descriptor-length-ptr
499 numeric-descriptor-ptr)
500 (let ((desc (convert-from-foreign-string descriptor-info-ptr)))
501 (free-foreign-object descriptor-info-ptr)
504 (deref-pointer numeric-descriptor-ptr #.$ODBC-LONG-TYPE)))))))
506 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
507 table-name column-name)
508 (with-cstrings ((table-qualifier-ptr table-qualifier)
509 (table-owner-ptr table-owner)
510 (table-name-ptr table-name)
511 (column-name-ptr column-name))
515 table-qualifier-ptr (length table-qualifier)
516 table-owner-ptr (length table-owner)
517 table-name-ptr (length table-name)
518 column-name-ptr (length column-name)))))
520 (defun %describe-columns (hdbc table-qualifier table-owner
521 table-name column-name)
522 (with-statement-handle (hstmt hdbc)
523 (%prepare-describe-columns hstmt table-qualifier table-owner
524 table-name column-name)
525 (fetch-all-rows hstmt)))
527 (defun %sql-data-sources (henv &key (direction :first))
528 (let ((name-ptr (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
529 (description-ptr (allocate-foreign-string 1024)))
530 (with-foreign-objects ((name-length-ptr :short)
531 (description-length-ptr :short))
532 (let ((res (with-error-handling
536 (:first $SQL_FETCH_FIRST)
537 (:next $SQL_FETCH_NEXT))
539 (1+ $SQL_MAX_DSN_LENGTH)
543 description-length-ptr))))
545 ((= res $SQL_NO_DATA_FOUND)
546 (let ((name (convert-from-foreign-string name-ptr))
547 (desc (convert-from-foreign-string description-ptr)))
548 (free-foreign-object name-ptr)
549 (free-foreign-object description-ptr)
554 (free-foreign-object name-ptr)
555 (free-foreign-object description-ptr)
560 (defun sql-to-c-type (sql-type)
562 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
563 #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9) $SQL_C_CHAR)
564 (#.$SQL_INTEGER $SQL_C_SLONG)
565 (#.$SQL_SMALLINT $SQL_C_SSHORT)
566 (#.$SQL_DOUBLE $SQL_C_DOUBLE)
567 (#.$SQL_FLOAT $SQL_C_DOUBLE)
568 (#.$SQL_REAL $SQL_C_FLOAT)
569 (#.$SQL_DATE $SQL_C_DATE)
570 (#.$SQL_TIME $SQL_C_TIME)
571 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
572 (#.$SQL_TYPE_DATE $SQL_C_TYPE_DATE)
573 (#.$SQL_TYPE_TIME $SQL_C_TYPE_TIME)
574 (#.$SQL_TYPE_TIMESTAMP $SQL_C_TYPE_TIMESTAMP)
575 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
576 (#.$SQL_TINYINT $SQL_C_STINYINT)
577 (#.$SQL_BIT $SQL_C_BIT)))
579 (def-type byte-pointer-type (* :byte))
580 (def-type short-pointer-type (* :short))
581 (def-type int-pointer-type (* :int))
582 (def-type long-pointer-type (* #.$ODBC-LONG-TYPE))
583 (def-type float-pointer-type (* :float))
584 (def-type double-pointer-type (* :double))
585 (def-type string-pointer-type (* :unsigned-char))
587 (defun get-cast-byte (ptr)
588 (locally (declare (type byte-pointer-type ptr))
589 (deref-pointer ptr :byte)))
591 (defun get-cast-short (ptr)
592 (locally (declare (type short-pointer-type ptr))
593 (deref-pointer ptr :short)))
595 (defun get-cast-int (ptr)
596 (locally (declare (type int-pointer-type ptr))
597 (deref-pointer ptr :int)))
599 (defun get-cast-long (ptr)
600 (locally (declare (type long-pointer-type ptr))
601 (deref-pointer ptr #.$ODBC-LONG-TYPE)))
603 (defun get-cast-single-float (ptr)
604 (locally (declare (type float-pointer-type ptr))
605 (deref-pointer ptr :float)))
607 (defun get-cast-double-float (ptr)
608 (locally (declare (type double-pointer-type ptr))
609 (deref-pointer ptr :double)))
611 (defun get-cast-foreign-string (ptr)
612 (locally (declare (type string-pointer-type ptr))
613 (convert-from-foreign-string ptr)))
615 (defun get-cast-binary (ptr len format)
616 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
617 (with-cast-pointer (casted ptr :byte)
619 (:unsigned-byte-vector
620 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
622 (setf (aref vector i)
623 (deref-array casted '(:array :byte) i)))
626 (let ((vector (make-array (ash len 3) :element-type 'bit)))
628 (let ((byte (deref-array casted '(:array :byte) i)))
630 (setf (bit vector (+ (ash i 3) j))
631 (logand (ash byte (- j 7)) 1)))))
635 (defun read-data (data-ptr c-type sql-type out-len-ptr result-type)
636 (declare (type long-ptr-type out-len-ptr))
637 (let* ((out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
639 (cond ((= out-len $SQL_NULL_DATA)
643 ;; SQL extended datatypes
644 (#.$SQL_TINYINT (get-cast-byte data-ptr))
645 (#.$SQL_C_STINYINT (get-cast-byte data-ptr)) ;; ?
646 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
647 (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
648 (#.$SQL_INTEGER (get-cast-int data-ptr))
649 (#.$SQL_BIGINT (read-from-string
650 (get-cast-foreign-string data-ptr)))
652 (let ((*read-base* 10))
653 (read-from-string (get-cast-foreign-string data-ptr))))
656 ((#.$SQL_C_DATE #.$SQL_C_TYPE_DATE)
657 (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
658 ((#.$SQL_C_TIME #.$SQL_C_TYPE_TIME)
659 (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
660 (funcall *time-conversion-function* universal-time frac)))
661 ((#.$SQL_C_TIMESTAMP #.$SQL_C_TYPE_TIMESTAMP)
662 (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
663 (funcall *time-conversion-function* universal-time frac)))
665 (get-cast-int data-ptr))
667 (get-cast-single-float data-ptr))
669 (get-cast-double-float data-ptr))
671 (get-cast-long data-ptr))
673 (#.$SQL_C_BIT ; encountered only in Access
674 (get-cast-byte data-ptr))
676 (get-cast-binary data-ptr out-len *binary-format*))
677 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
678 (get-cast-short data-ptr)) ; LMH
681 (code-char (get-cast-short data-ptr)))
683 (get-cast-foreign-string data-ptr)))))))))
685 ;; FIXME: this could be better optimized for types which use READ-FROM-STRING above
687 (if (and (or (eq result-type t) (eq result-type :string))
689 (not (stringp value)))
690 (write-to-string value)
693 ;; which value is appropriate?
694 (defparameter +max-precision+ 4001)
696 (defvar *break-on-unknown-data-type* t)
698 ;; C. Stacy's idea to factor this out
699 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
700 ;; so that I don't have to remember to make changes in more than one place.
701 ;; Just keep it in synch with READ-DATA."
702 (defun %allocate-bindings (sql-type precision)
703 (let* ((c-type (sql-to-c-type sql-type))
704 (size (if (zerop precision)
705 +max-precision+ ;; if the precision cannot be determined
706 (min precision +max-precision+)))
707 (long-p (= size +max-precision+))
709 (case c-type ;; add more?
710 (#.$SQL_C_SLONG (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE))
711 ((#.$SQL_C_DATE #.$SQL_C_TYPE_DATE) (allocate-foreign-object 'sql-c-date))
712 ((#.$SQL_C_TIME #.$SQL_C_TYPE_TIME) (allocate-foreign-object 'sql-c-time))
713 ((#.$SQL_C_TIMESTAMP #.$SQL_C_TYPE_TIMESTAMP) (allocate-foreign-object 'sql-c-timestamp))
714 (#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
715 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
716 (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
717 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
718 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
719 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
720 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
722 ;; Maybe should signal a restartable condition for this?
723 (when *break-on-unknown-data-type*
724 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
725 sql-type precision size c-type))
726 (uffi:allocate-foreign-object :byte (1+ size)))))
727 (out-len-ptr (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE)))
728 (values c-type data-ptr out-len-ptr size long-p)))
730 (defun fetch-all-rows (hstmt &key free-option flatp)
731 (let ((column-count (result-columns-count hstmt)))
732 (unless (zerop column-count)
733 (let ((names (make-array column-count))
734 (sql-types (make-array column-count :element-type 'fixnum))
735 (c-types (make-array column-count :element-type 'fixnum))
736 (precisions (make-array column-count :element-type 'fixnum))
737 (data-ptrs (make-array column-count :initial-element nil))
738 (out-len-ptrs (make-array column-count :initial-element nil))
739 (scales (make-array column-count :element-type 'fixnum))
740 (nullables-p (make-array column-count :element-type 'fixnum)))
744 (dotimes (col-nr column-count)
745 ;; get column information
746 (multiple-value-bind (name sql-type precision scale nullable-p)
747 (%describe-column hstmt (1+ col-nr))
748 ;; allocate space to bind result rows to
749 (multiple-value-bind (c-type data-ptr out-len-ptr)
750 (%allocate-bindings sql-type precision)
751 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
752 (setf (svref names col-nr) name
753 (aref sql-types col-nr) sql-type
754 (aref c-types col-nr) (sql-to-c-type sql-type)
755 (aref precisions col-nr) (if (zerop precision) 0 precision)
756 (aref scales col-nr) scale
757 (aref nullables-p col-nr) nullable-p
758 (aref data-ptrs col-nr) data-ptr
759 (aref out-len-ptrs col-nr) out-len-ptr))))
763 (when (> column-count 1)
764 (error 'clsql:sql-database-error
765 :message "If more than one column is to be fetched, flatp has to be nil."))
766 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
768 (read-data (aref data-ptrs 0)
771 (aref out-len-ptrs 0)
774 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
776 (loop for col-nr from 0 to (1- column-count)
778 (read-data (aref data-ptrs col-nr)
779 (aref c-types col-nr)
780 (aref sql-types col-nr)
781 (aref out-len-ptrs col-nr)
784 ;; dispose of memory etc
785 (when free-option (%free-statement hstmt free-option))
786 (dotimes (col-nr column-count)
787 (let ((data-ptr (aref data-ptrs col-nr))
788 (out-len-ptr (aref out-len-ptrs col-nr)))
789 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
790 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
792 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
793 ;; of %do-fetch into sql package (has been done)
795 (defun %sql-prepare (hstmt sql)
796 (with-cstring (sql-ptr sql)
797 (with-error-handling (:hstmt hstmt)
798 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
800 ;; depending on option, we return a long int or a string; string not implemented
801 (defun get-connection-option (hdbc option)
802 (with-foreign-object (param-ptr #.$ODBC-LONG-TYPE)
803 (with-error-handling (:hdbc hdbc)
804 (SQLGetConnectOption hdbc option param-ptr)
805 (deref-pointer param-ptr #.$ODBC-LONG-TYPE))))
807 (defun set-connection-option (hdbc option param)
808 (with-error-handling (:hdbc hdbc)
809 (SQLSetConnectOption hdbc option param)))
811 (defun disable-autocommit (hdbc)
812 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
814 (defun enable-autocommit (hdbc)
815 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
817 (defun %sql-set-pos (hstmt row option lock)
820 (SQLSetPos hstmt row option lock)))
822 (defun %sql-extended-fetch (hstmt fetch-type row)
823 (with-foreign-objects ((row-count-ptr #.$ODBC-ULONG-TYPE)
824 (row-status-ptr :short))
825 (with-error-handling (:hstmt hstmt)
826 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
828 (values (deref-pointer row-count-ptr #.$ODBC-ULONG-TYPE)
829 (deref-pointer row-status-ptr :short)))))
831 ; column-nr is zero-based
832 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
834 (:hstmt hstmt :print-info nil)
835 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
836 c-type data-ptr precision out-len-ptr)))
838 (defun %sql-param-data (hstmt param-ptr)
839 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
840 (SQLParamData hstmt param-ptr)))
842 (defun %sql-put-data (hstmt data-ptr size)
844 (:hstmt hstmt :print-info t) ;; nil
845 (SQLPutData hstmt data-ptr size)))
847 (defconstant $sql-data-truncated (intern "01004" :keyword))
849 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
850 out-len-ptr result-type)
851 (declare (type long-ptr-type out-len-ptr))
852 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
853 +max-precision+ out-len-ptr))
854 (out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
858 (return-from read-data-in-chunks *null*))
859 (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
860 (let ((str (make-array 0 :element-type 'character :adjustable t)))
861 (loop do (if (= c-type #.$SQL_CHAR)
862 (let ((data-length (foreign-string-length data-ptr)))
863 (adjust-array str (+ offset data-length)
864 :initial-element #\?)
865 (setf offset (%cstring-into-vector
869 (error 'clsql:sql-database-error :message "wrong type. preliminary."))
870 while (and (= res $SQL_SUCCESS_WITH_INFO)
871 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
873 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
874 +max-precision+ out-len-ptr)))
875 (setf str (coerce str 'string))
876 (if (= sql-type $SQL_DECIMAL)
877 (let ((*read-base* 10))
878 (read-from-string str))
881 (let ((str (make-string out-len)))
882 (loop do (if (= c-type #.$SQL_CHAR)
883 (setf offset (%cstring-into-vector ;string
886 (min out-len (1- +max-precision+))))
887 (error 'clsql:sql-database-error :message "wrong type. preliminary."))
889 (and (= res $SQL_SUCCESS_WITH_INFO)
890 #+ingore(eq (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
892 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
894 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
895 +max-precision+ out-len-ptr)
896 out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE)))
897 (if (= sql-type $SQL_DECIMAL)
898 (let ((*read-base* 10))
899 (read-from-string str))
902 (def-type c-timestamp-ptr-type (* (:struct sql-c-timestamp)))
903 (def-type c-time-ptr-type (* (:struct sql-c-time)))
904 (def-type c-date-ptr-type (* (:struct sql-c-date)))
906 (defun timestamp-to-universal-time (ptr)
907 (declare (type c-timestamp-ptr-type ptr))
909 (encode-universal-time
910 (get-slot-value ptr 'sql-c-timestamp 'second)
911 (get-slot-value ptr 'sql-c-timestamp 'minute)
912 (get-slot-value ptr 'sql-c-timestamp 'hour)
913 (get-slot-value ptr 'sql-c-timestamp 'day)
914 (get-slot-value ptr 'sql-c-timestamp 'month)
915 (get-slot-value ptr 'sql-c-timestamp 'year))
916 (get-slot-value ptr 'sql-c-timestamp 'fraction)))
918 (defun universal-time-to-timestamp (time &optional (fraction 0))
919 (multiple-value-bind (sec min hour day month year)
920 (decode-universal-time time)
921 (let ((ptr (allocate-foreign-object 'sql-c-timestamp)))
922 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
923 (get-slot-value ptr 'sql-c-timestamp 'minute) min
924 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
925 (get-slot-value ptr 'sql-c-timestamp 'day) day
926 (get-slot-value ptr 'sql-c-timestamp 'month) month
927 (get-slot-value ptr 'sql-c-timestamp 'year) year
928 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
931 (defun %put-timestamp (ptr time &optional (fraction 0))
932 (declare (type c-timestamp-ptr-type ptr))
933 (multiple-value-bind (sec min hour day month year)
934 (decode-universal-time time)
935 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
936 (get-slot-value ptr 'sql-c-timestamp 'minute) min
937 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
938 (get-slot-value ptr 'sql-c-timestamp 'day) day
939 (get-slot-value ptr 'sql-c-timestamp 'month) month
940 (get-slot-value ptr 'sql-c-timestamp 'year) year
941 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
944 (defun date-to-universal-time (ptr)
945 (declare (type c-date-ptr-type ptr))
946 (encode-universal-time
948 (get-slot-value ptr 'sql-c-timestamp 'day)
949 (get-slot-value ptr 'sql-c-timestamp 'month)
950 (get-slot-value ptr 'sql-c-timestamp 'year)))
952 (defun time-to-universal-time (ptr)
953 (declare (type c-time-ptr-type ptr))
954 (encode-universal-time
955 (get-slot-value ptr 'sql-c-timestamp 'second)
956 (get-slot-value ptr 'sql-c-timestamp 'minute)
957 (get-slot-value ptr 'sql-c-timestamp 'hour)
963 (defun %set-attr-odbc-version (henv version)
964 (with-error-handling (:henv henv)
965 (SQLSetEnvAttr henv $SQL_ATTR_ODBC_VERSION
966 (make-pointer version :void) 0)))
968 (defun %list-tables (hstmt)
969 (with-error-handling (:hstmt hstmt)
970 (SQLTables hstmt +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0)))
972 (defun %table-statistics (table hstmt &key unique (ensure t))
973 (with-cstrings ((table-cs table))
974 (with-error-handling (:hstmt hstmt)
980 (if unique $SQL_INDEX_UNIQUE $SQL_INDEX_ALL)
981 (if ensure $SQL_ENSURE $SQL_QUICK)))))
983 (defun %list-data-sources (henv)
984 (let ((dsn (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
985 (desc (allocate-foreign-string 256))
988 (with-foreign-objects ((dsn-len :short)
990 (let ((res (with-error-handling (:henv henv)
991 (SQLDataSources henv $SQL_FETCH_FIRST dsn
992 (1+ $SQL_MAX_DSN_LENGTH)
993 dsn-len desc 256 desc-len))))
994 (when (or (eql res $SQL_SUCCESS)
995 (eql res $SQL_SUCCESS_WITH_INFO))
996 (push (convert-from-foreign-string dsn) results))
998 (do ((res (with-error-handling (:henv henv)
999 (SQLDataSources henv $SQL_FETCH_NEXT dsn
1000 (1+ $SQL_MAX_DSN_LENGTH)
1001 dsn-len desc 256 desc-len))
1002 (with-error-handling (:henv henv)
1003 (SQLDataSources henv $SQL_FETCH_NEXT dsn
1004 (1+ $SQL_MAX_DSN_LENGTH)
1005 dsn-len desc 256 desc-len))))
1006 ((not (or (eql res $SQL_SUCCESS)
1007 (eql res $SQL_SUCCESS_WITH_INFO))))
1008 (push (convert-from-foreign-string dsn) results))))
1010 (free-foreign-object dsn)
1011 (free-foreign-object desc)))
1012 (nreverse results)))