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)
155 (deref-pointer phenv 'sql-handle)))))
156 (%set-attr-odbc-version henv $SQL_OV_ODBC2)
160 (defun %sql-free-environment (henv)
165 (defun %new-db-connection-handle (henv)
166 (with-foreign-object (phdbc 'sql-handle)
169 (SQLAllocConnect henv phdbc)
170 (deref-pointer phdbc 'sql-handle))))
172 (defun %free-statement (hstmt option)
180 (:unbind $SQL_UNBIND)
181 (:reset $SQL_RESET_PARAMS)))))
183 (defmacro with-statement-handle ((hstmt hdbc) &body body)
184 `(let ((,hstmt (%new-statement-handle ,hdbc)))
187 (%free-statement ,hstmt :drop))))
189 ;; functional interface
191 (defun %sql-connect (hdbc server uid pwd)
192 (with-cstrings ((server-ptr server)
197 (SQLConnect hdbc server-ptr $SQL_NTS uid-ptr
198 $SQL_NTS pwd-ptr $SQL_NTS))))
201 (defun %disconnect (hdbc)
204 (SQLDisconnect hdbc)))
206 (defun %commit (henv hdbc)
208 (:henv henv :hdbc hdbc)
210 henv hdbc $SQL_COMMIT)))
212 (defun %rollback (henv hdbc)
214 (:henv henv :hdbc hdbc)
216 henv hdbc $SQL_ROLLBACK)))
218 ; col-nr is zero-based in Lisp
219 ; col-nr = :bookmark retrieves a bookmark.
220 (defun %bind-column (hstmt column-nr c-type data-ptr precision out-len-ptr)
224 (if (eq column-nr :bookmark) 0 (1+ column-nr))
225 c-type data-ptr precision out-len-ptr)))
227 ; parameter-nr is zero-based in Lisp
228 (defun %sql-bind-parameter (hstmt parameter-nr parameter-type c-type
229 sql-type precision scale data-ptr
230 max-value out-len-ptr)
233 (SQLBindParameter hstmt (1+ parameter-nr)
234 parameter-type ;$SQL_PARAM_INPUT
236 sql-type ;$SQL_VARCHAR
237 precision ;(1- (length str))
241 out-len-ptr ;#.+null-ptr+
244 (defun %sql-fetch (hstmt)
249 (defun %new-statement-handle (hdbc)
250 (let ((statement-handle
251 (with-foreign-object (hstmt-ptr 'sql-handle)
254 (SQLAllocStmt hdbc hstmt-ptr)
255 (deref-pointer hstmt-ptr 'sql-handle)))))
256 (if (uffi:null-pointer-p statement-handle)
257 (error 'clsql:sql-database-error :message "Received null statement handle.")
260 (defun %sql-get-info (hdbc info-type)
262 ;; those return string
263 ((#.$SQL_ACCESSIBLE_PROCEDURES
264 #.$SQL_ACCESSIBLE_TABLES
266 #.$SQL_DATA_SOURCE_NAME
267 #.$SQL_DATA_SOURCE_READ_ONLY
271 #.$SQL_DRIVER_ODBC_VER
273 #.$SQL_EXPRESSIONS_IN_ORDERBY
274 #.$SQL_IDENTIFIER_QUOTE_CHAR
276 #.$SQL_LIKE_ESCAPE_CLAUSE
277 #.$SQL_MAX_ROW_SIZE_INCLUDES_LONG
278 #.$SQL_MULT_RESULT_SETS
279 #.$SQL_MULTIPLE_ACTIVE_TXN
280 #.$SQL_NEED_LONG_DATA_LEN
281 #.$SQL_ODBC_SQL_OPT_IEF
283 #.$SQL_ORDER_BY_COLUMNS_IN_SELECT
286 #.$SQL_PROCEDURE_TERM
288 #.$SQL_QUALIFIER_NAME_SEPARATOR
289 #.$SQL_QUALIFIER_TERM
291 #.$SQL_SEARCH_PATTERN_ESCAPE
293 #.$SQL_SPECIAL_CHARACTERS
296 (let ((info-ptr (allocate-foreign-string 1024)))
297 (with-foreign-object (info-length-ptr :short)
300 (SQLGetInfo hdbc info-type info-ptr 1023 info-length-ptr)
301 (let ((info (convert-from-foreign-string info-ptr)))
302 (free-foreign-object info-ptr)
304 ;; those returning a word
305 ((#.$SQL_ACTIVE_CONNECTIONS
306 #.$SQL_ACTIVE_STATEMENTS
307 #.$SQL_CONCAT_NULL_BEHAVIOR
308 #.$SQL_CORRELATION_NAME
309 #.$SQL_CURSOR_COMMIT_BEHAVIOR
310 #.$SQL_CURSOR_ROLLBACK_BEHAVIOR
311 #.$SQL_MAX_COLUMN_NAME_LEN
312 #.$SQL_MAX_COLUMNS_IN_GROUP_BY
313 #.$SQL_MAX_COLUMNS_IN_INDEX
314 #.$SQL_MAX_COLUMNS_IN_ORDER_BY
315 #.$SQL_MAX_COLUMNS_IN_SELECT
316 #.$SQL_MAX_COLUMNS_IN_TABLE
317 #.$SQL_MAX_CURSOR_NAME_LEN
318 #.$SQL_MAX_OWNER_NAME_LEN
319 #.$SQL_MAX_PROCEDURE_NAME_LEN
320 #.$SQL_MAX_QUALIFIER_NAME_LEN
321 #.$SQL_MAX_TABLE_NAME_LEN
322 #.$SQL_MAX_TABLES_IN_SELECT
323 #.$SQL_MAX_USER_NAME_LEN
324 #.$SQL_NON_NULLABLE_COLUMNS
325 #.$SQL_NULL_COLLATION
326 #.$SQL_ODBC_API_CONFORMANCE
327 #.$SQL_ODBC_SAG_CLI_CONFORMANCE
328 #.$SQL_ODBC_SQL_CONFORMANCE
329 #.$SQL_QUALIFIER_LOCATION
330 #.$SQL_QUOTED_IDENTIFIER_CASE
332 (with-foreign-objects ((info-ptr :short)
333 (info-length-ptr :short))
341 (deref-pointer info-ptr :short)))
343 ;; those returning a long bitmask
345 #.$SQL_BOOKMARK_PERSISTENCE
346 #.$SQL_CONVERT_BIGINT
347 #.$SQL_CONVERT_BINARY
351 #.$SQL_CONVERT_DECIMAL
352 #.$SQL_CONVERT_DOUBLE
354 #.$SQL_CONVERT_INTEGER
355 #.$SQL_CONVERT_LONGVARCHAR
356 #.$SQL_CONVERT_NUMERIC
358 #.$SQL_CONVERT_SMALLINT
360 #.$SQL_CONVERT_TIMESTAMP
361 #.$SQL_CONVERT_TINYINT
362 #.$SQL_CONVERT_VARBINARY
363 #.$SQL_CONVERT_VARCHAR
364 #.$SQL_CONVERT_LONGVARBINARY
365 #.$SQL_CONVERT_FUNCTIONS
366 #.$SQL_FETCH_DIRECTION
368 #.$SQL_GETDATA_EXTENSIONS
370 #.$SQL_MAX_INDEX_SIZE
372 #.$SQL_MAX_STATEMENT_LEN
373 #.$SQL_NUMERIC_FUNCTIONS
375 #.$SQL_POS_OPERATIONS
376 #.$SQL_POSITIONED_STATEMENTS
377 #.$SQL_QUALIFIER_USAGE
378 #.$SQL_SCROLL_CONCURRENCY
379 #.$SQL_SCROLL_OPTIONS
380 #.$SQL_STATIC_SENSITIVITY
381 #.$SQL_STRING_FUNCTIONS
383 #.$SQL_SYSTEM_FUNCTIONS
384 #.$SQL_TIMEDATE_ADD_INTERVALS
385 #.$SQL_TIMEDATE_DIFF_INTERVALS
386 #.$SQL_TIMEDATE_FUNCTIONS
387 #.$SQL_TXN_ISOLATION_OPTION
389 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
390 (info-length-ptr :short))
398 (deref-pointer info-ptr #.$ODBC-LONG-TYPE)))
400 ;; those returning a long integer
401 ((#.$SQL_DEFAULT_TXN_ISOLATION
407 #.$SQL_IDENTIFIER_CASE
408 #.$SQL_MAX_BINARY_LITERAL_LEN
409 #.$SQL_MAX_CHAR_LITERAL_LEN
410 #.$SQL_ACTIVE_ENVIRONMENTS
412 (with-foreign-objects ((info-ptr #.$ODBC-LONG-TYPE)
413 (info-length-ptr :short))
416 (SQLGetInfo hdbc info-type info-ptr 255 info-length-ptr)
417 (deref-pointer info-ptr #.$ODBC-LONG-TYPE))))))
419 (defun %sql-exec-direct (sql hstmt henv hdbc)
420 (with-cstring (sql-ptr sql)
422 (:hstmt hstmt :henv henv :hdbc hdbc)
423 (SQLExecDirect hstmt sql-ptr $SQL_NTS))))
425 (defun %sql-cancel (hstmt)
430 (defun %sql-execute (hstmt)
435 (defun result-columns-count (hstmt)
436 (with-foreign-objects ((columns-nr-ptr :short))
437 (with-error-handling (:hstmt hstmt)
438 (SQLNumResultCols hstmt columns-nr-ptr)
439 (deref-pointer columns-nr-ptr :short))))
441 (defun result-rows-count (hstmt)
442 (with-foreign-objects ((row-count-ptr #.$ODBC-LONG-TYPE))
443 (with-error-handling (:hstmt hstmt)
444 (SQLRowCount hstmt row-count-ptr)
445 (deref-pointer row-count-ptr #.$ODBC-LONG-TYPE))))
447 ;; column counting is 1-based
448 (defun %describe-column (hstmt column-nr)
449 (let ((column-name-ptr (allocate-foreign-string 256)))
450 (with-foreign-objects ((column-name-length-ptr :short)
451 (column-sql-type-ptr :short)
452 (column-precision-ptr #.$ODBC-ULONG-TYPE)
453 (column-scale-ptr :short)
454 (column-nullable-p-ptr :short))
455 (with-error-handling (:hstmt hstmt)
456 (SQLDescribeCol hstmt column-nr column-name-ptr 256
457 column-name-length-ptr
461 column-nullable-p-ptr)
462 (let ((column-name (convert-from-foreign-string column-name-ptr)))
463 (free-foreign-object column-name-ptr)
466 (deref-pointer column-sql-type-ptr :short)
467 (deref-pointer column-precision-ptr #.$ODBC-ULONG-TYPE)
468 (deref-pointer column-scale-ptr :short)
469 (deref-pointer column-nullable-p-ptr :short)))))))
471 ;; parameter counting is 1-based
472 (defun %describe-parameter (hstmt parameter-nr)
473 (with-foreign-objects ((column-sql-type-ptr :short)
474 (column-precision-ptr #.$ODBC-ULONG-TYPE)
475 (column-scale-ptr :short)
476 (column-nullable-p-ptr :short))
479 (SQLDescribeParam hstmt parameter-nr
483 column-nullable-p-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 (defun %column-attributes (hstmt column-nr descriptor-type)
491 (let ((descriptor-info-ptr (allocate-foreign-string 256)))
492 (with-foreign-objects ((descriptor-length-ptr :short)
493 (numeric-descriptor-ptr #.$ODBC-LONG-TYPE))
496 (SQLColAttributes hstmt column-nr descriptor-type descriptor-info-ptr
497 256 descriptor-length-ptr
498 numeric-descriptor-ptr)
499 (let ((desc (convert-from-foreign-string descriptor-info-ptr)))
500 (free-foreign-object descriptor-info-ptr)
503 (deref-pointer numeric-descriptor-ptr #.$ODBC-LONG-TYPE)))))))
505 (defun %prepare-describe-columns (hstmt table-qualifier table-owner
506 table-name column-name)
507 (with-cstrings ((table-qualifier-ptr table-qualifier)
508 (table-owner-ptr table-owner)
509 (table-name-ptr table-name)
510 (column-name-ptr column-name))
514 table-qualifier-ptr (length table-qualifier)
515 table-owner-ptr (length table-owner)
516 table-name-ptr (length table-name)
517 column-name-ptr (length column-name)))))
519 (defun %describe-columns (hdbc table-qualifier table-owner
520 table-name column-name)
521 (with-statement-handle (hstmt hdbc)
522 (%prepare-describe-columns hstmt table-qualifier table-owner
523 table-name column-name)
524 (fetch-all-rows hstmt)))
526 (defun %sql-data-sources (henv &key (direction :first))
527 (let ((name-ptr (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
528 (description-ptr (allocate-foreign-string 1024)))
529 (with-foreign-objects ((name-length-ptr :short)
530 (description-length-ptr :short))
531 (let ((res (with-error-handling
535 (:first $SQL_FETCH_FIRST)
536 (:next $SQL_FETCH_NEXT))
538 (1+ $SQL_MAX_DSN_LENGTH)
542 description-length-ptr))))
544 ((= res $SQL_NO_DATA_FOUND)
545 (let ((name (convert-from-foreign-string name-ptr))
546 (desc (convert-from-foreign-string description-ptr)))
547 (free-foreign-object name-ptr)
548 (free-foreign-object description-ptr)
553 (free-foreign-object name-ptr)
554 (free-foreign-object description-ptr)
559 (defun sql-to-c-type (sql-type)
561 ((#.$SQL_CHAR #.$SQL_VARCHAR #.$SQL_LONGVARCHAR
562 #.$SQL_NUMERIC #.$SQL_DECIMAL #.$SQL_BIGINT -8 -9) $SQL_C_CHAR)
563 (#.$SQL_INTEGER $SQL_C_SLONG)
564 (#.$SQL_SMALLINT $SQL_C_SSHORT)
565 (#.$SQL_DOUBLE $SQL_C_DOUBLE)
566 (#.$SQL_FLOAT $SQL_C_DOUBLE)
567 (#.$SQL_REAL $SQL_C_FLOAT)
568 (#.$SQL_DATE $SQL_C_DATE)
569 (#.$SQL_TIME $SQL_C_TIME)
570 (#.$SQL_TIMESTAMP $SQL_C_TIMESTAMP)
571 ((#.$SQL_BINARY #.$SQL_VARBINARY #.$SQL_LONGVARBINARY) $SQL_C_BINARY)
572 (#.$SQL_TINYINT $SQL_C_STINYINT)
573 (#.$SQL_BIT $SQL_C_BIT)))
575 (def-type byte-pointer-type '(* :byte))
576 (def-type short-pointer-type '(* :short))
577 (def-type int-pointer-type '(* :int))
578 (def-type long-pointer-type '(* #.$ODBC-LONG-TYPE))
579 (def-type float-pointer-type '(* :float))
580 (def-type double-pointer-type '(* :double))
581 (def-type string-pointer-type '(* :unsigned-char))
583 (defun get-cast-byte (ptr)
584 (locally (declare (type byte-pointer-type ptr))
585 (deref-pointer ptr :byte)))
587 (defun get-cast-short (ptr)
588 (locally (declare (type short-pointer-type ptr))
589 (deref-pointer ptr :short)))
591 (defun get-cast-int (ptr)
592 (locally (declare (type int-pointer-type ptr))
593 (deref-pointer ptr :int)))
595 (defun get-cast-long (ptr)
596 (locally (declare (type long-pointer-type ptr))
597 (deref-pointer ptr #.$ODBC-LONG-TYPE)))
599 (defun get-cast-single-float (ptr)
600 (locally (declare (type float-pointer-type ptr))
601 (deref-pointer ptr :float)))
603 (defun get-cast-double-float (ptr)
604 (locally (declare (type double-pointer-type ptr))
605 (deref-pointer ptr :double)))
607 (defun get-cast-foreign-string (ptr)
608 (locally (declare (type string-pointer-type ptr))
609 (convert-from-foreign-string ptr)))
611 (defun get-cast-binary (ptr len format)
612 "FORMAT is one of :unsigned-byte-vector, :bit-vector (:string, :hex-string)"
613 (with-cast-pointer (casted ptr :byte)
615 (:unsigned-byte-vector
616 (let ((vector (make-array len :element-type '(unsigned-byte 8))))
618 (setf (aref vector i)
619 (deref-array casted '(:array :byte) i)))
622 (let ((vector (make-array (ash len 3) :element-type 'bit)))
624 (let ((byte (deref-array casted '(:array :byte) i)))
626 (setf (bit vector (+ (ash i 3) j))
627 (logand (ash byte (- j 7)) 1)))))
631 (defun read-data (data-ptr c-type sql-type out-len-ptr result-type)
632 (declare (type long-ptr-type out-len-ptr))
633 (let* ((out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
635 (cond ((= out-len $SQL_NULL_DATA)
639 ;; SQL extended datatypes
640 (#.$SQL_TINYINT (get-cast-byte data-ptr))
641 (#.$SQL_C_STINYINT (get-cast-byte data-ptr)) ;; ?
642 (#.$SQL_C_SSHORT (get-cast-short data-ptr)) ;; ?
643 (#.$SQL_SMALLINT (get-cast-short data-ptr)) ;; ??
644 (#.$SQL_INTEGER (get-cast-int data-ptr))
645 (#.$SQL_BIGINT (read-from-string
646 (get-cast-foreign-string data-ptr)))
647 (#.$SQL_TINYINT (get-cast-byte data-ptr))
649 (let ((*read-base* 10))
650 (read-from-string (get-cast-foreign-string data-ptr))))
654 (funcall *time-conversion-function* (date-to-universal-time data-ptr)))
656 (multiple-value-bind (universal-time frac) (time-to-universal-time data-ptr)
657 (funcall *time-conversion-function* universal-time frac)))
659 (multiple-value-bind (universal-time frac) (timestamp-to-universal-time data-ptr)
660 (funcall *time-conversion-function* universal-time frac)))
662 (get-cast-int data-ptr))
664 (get-cast-single-float data-ptr))
666 (get-cast-double-float data-ptr))
668 (get-cast-long data-ptr))
670 (#.$SQL_C_BIT ; encountered only in Access
671 (get-cast-byte data-ptr))
673 (get-cast-binary data-ptr out-len *binary-format*))
674 ((#.$SQL_C_SSHORT #.$SQL_C_STINYINT) ; LMH short ints
675 (get-cast-short data-ptr)) ; LMH
678 (code-char (get-cast-short data-ptr)))
680 (get-cast-foreign-string data-ptr)))))))))
682 ;; FIXME: this could be better optimized for types which use READ-FROM-STRING above
684 (if (and (or (eq result-type t) (eq result-type :string))
686 (not (stringp value)))
687 (write-to-string value)
690 ;; which value is appropriate?
691 (defparameter +max-precision+ 4001)
693 (defvar *break-on-unknown-data-type* t)
695 ;; C. Stacy's idea to factor this out
696 ;; "Make it easy to add new datatypes by making new subroutine %ALLOCATE-BINDINGS,
697 ;; so that I don't have to remember to make changes in more than one place.
698 ;; Just keep it in synch with READ-DATA."
699 (defun %allocate-bindings (sql-type precision)
700 (let* ((c-type (sql-to-c-type sql-type))
701 (size (if (zerop precision)
702 +max-precision+ ;; if the precision cannot be determined
703 (min precision +max-precision+)))
704 (long-p (= size +max-precision+))
706 (case c-type ;; add more?
707 (#.$SQL_C_SLONG (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE))
708 (#.$SQL_C_DATE (allocate-foreign-object 'sql-c-date))
709 (#.$SQL_C_TIME (allocate-foreign-object 'sql-c-time))
710 (#.$SQL_C_TIMESTAMP (allocate-foreign-object 'sql-c-timestamp))
711 (#.$SQL_C_FLOAT (uffi:allocate-foreign-object :float))
712 (#.$SQL_C_DOUBLE (uffi:allocate-foreign-object :double))
713 (#.$SQL_C_BIT (uffi:allocate-foreign-object :byte))
714 (#.$SQL_C_STINYINT (uffi:allocate-foreign-object :byte))
715 (#.$SQL_C_SSHORT (uffi:allocate-foreign-object :short))
716 (#.$SQL_C_CHAR (uffi:allocate-foreign-string (1+ size)))
717 (#.$SQL_C_BINARY (uffi:allocate-foreign-string (1+ (* 2 size))))
719 ;; Maybe should signal a restartable condition for this?
720 (when *break-on-unknown-data-type*
721 (break "SQL type is ~A, precision ~D, size ~D, C type is ~A"
722 sql-type precision size c-type))
723 (uffi:allocate-foreign-object :byte (1+ size)))))
724 (out-len-ptr (uffi:allocate-foreign-object #.$ODBC-LONG-TYPE)))
725 (values c-type data-ptr out-len-ptr size long-p)))
727 (defun fetch-all-rows (hstmt &key free-option flatp)
728 (let ((column-count (result-columns-count hstmt)))
729 (unless (zerop column-count)
730 (let ((names (make-array column-count))
731 (sql-types (make-array column-count :element-type 'fixnum))
732 (c-types (make-array column-count :element-type 'fixnum))
733 (precisions (make-array column-count :element-type 'fixnum))
734 (data-ptrs (make-array column-count :initial-element nil))
735 (out-len-ptrs (make-array column-count :initial-element nil))
736 (scales (make-array column-count :element-type 'fixnum))
737 (nullables-p (make-array column-count :element-type 'fixnum)))
741 (dotimes (col-nr column-count)
742 ;; get column information
743 (multiple-value-bind (name sql-type precision scale nullable-p)
744 (%describe-column hstmt (1+ col-nr))
745 ;; allocate space to bind result rows to
746 (multiple-value-bind (c-type data-ptr out-len-ptr)
747 (%allocate-bindings sql-type precision)
748 (%bind-column hstmt col-nr c-type data-ptr (1+ precision) out-len-ptr)
749 (setf (svref names col-nr) name
750 (aref sql-types col-nr) sql-type
751 (aref c-types col-nr) (sql-to-c-type sql-type)
752 (aref precisions col-nr) (if (zerop precision) 0 precision)
753 (aref scales col-nr) scale
754 (aref nullables-p col-nr) nullable-p
755 (aref data-ptrs col-nr) data-ptr
756 (aref out-len-ptrs col-nr) out-len-ptr))))
760 (when (> column-count 1)
761 (error 'clsql:sql-database-error
762 :message "If more than one column is to be fetched, flatp has to be nil."))
763 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
765 (read-data (aref data-ptrs 0)
768 (aref out-len-ptrs 0)
771 (loop until (= (%sql-fetch hstmt) $SQL_NO_DATA_FOUND)
773 (loop for col-nr from 0 to (1- column-count)
775 (read-data (aref data-ptrs col-nr)
776 (aref c-types col-nr)
777 (aref sql-types col-nr)
778 (aref out-len-ptrs col-nr)
781 ;; dispose of memory etc
782 (when free-option (%free-statement hstmt free-option))
783 (dotimes (col-nr column-count)
784 (let ((data-ptr (aref data-ptrs col-nr))
785 (out-len-ptr (aref out-len-ptrs col-nr)))
786 (when data-ptr (free-foreign-object data-ptr)) ; we *did* allocate them
787 (when out-len-ptr (free-foreign-object out-len-ptr)))))))))
789 ;; to do: factor out common parts, put the sceleton (the obligatory macro part)
790 ;; of %do-fetch into sql package (has been done)
792 (defun %sql-prepare (hstmt sql)
793 (with-cstring (sql-ptr sql)
794 (with-error-handling (:hstmt hstmt)
795 (SQLPrepare hstmt sql-ptr $SQL_NTS))))
797 ;; depending on option, we return a long int or a string; string not implemented
798 (defun get-connection-option (hdbc option)
799 (with-foreign-object (param-ptr #.$ODBC-LONG-TYPE)
800 (with-error-handling (:hdbc hdbc)
801 (SQLGetConnectOption hdbc option param-ptr)
802 (deref-pointer param-ptr #.$ODBC-LONG-TYPE))))
804 (defun set-connection-option (hdbc option param)
805 (with-error-handling (:hdbc hdbc)
806 (SQLSetConnectOption hdbc option param)))
808 (defun disable-autocommit (hdbc)
809 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_OFF))
811 (defun enable-autocommit (hdbc)
812 (set-connection-option hdbc $SQL_AUTOCOMMIT $SQL_AUTOCOMMIT_ON))
814 (defun %sql-set-pos (hstmt row option lock)
817 (SQLSetPos hstmt row option lock)))
819 (defun %sql-extended-fetch (hstmt fetch-type row)
820 (with-foreign-objects ((row-count-ptr #.$ODBC-ULONG-TYPE)
821 (row-status-ptr :short))
822 (with-error-handling (:hstmt hstmt)
823 (SQLExtendedFetch hstmt fetch-type row row-count-ptr
825 (values (deref-pointer row-count-ptr #.$ODBC-ULONG-TYPE)
826 (deref-pointer row-status-ptr :short)))))
828 ; column-nr is zero-based
829 (defun %sql-get-data (hstmt column-nr c-type data-ptr precision out-len-ptr)
831 (:hstmt hstmt :print-info nil)
832 (SQLGetData hstmt (if (eq column-nr :bookmark) 0 (1+ column-nr))
833 c-type data-ptr precision out-len-ptr)))
835 (defun %sql-param-data (hstmt param-ptr)
836 (with-error-handling (:hstmt hstmt :print-info t) ;; nil
837 (SQLParamData hstmt param-ptr)))
839 (defun %sql-put-data (hstmt data-ptr size)
841 (:hstmt hstmt :print-info t) ;; nil
842 (SQLPutData hstmt data-ptr size)))
844 (defconstant $sql-data-truncated (intern "01004" :keyword))
846 (defun read-data-in-chunks (hstmt column-nr data-ptr c-type sql-type
847 out-len-ptr result-type)
848 (declare (type long-ptr-type out-len-ptr))
849 (let* ((res (%sql-get-data hstmt column-nr c-type data-ptr
850 +max-precision+ out-len-ptr))
851 (out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE))
855 (return-from read-data-in-chunks *null*))
856 (#.$SQL_NO_TOTAL ;; don't know how long it is going to be
857 (let ((str (make-array 0 :element-type 'character :adjustable t)))
858 (loop do (if (= c-type #.$SQL_CHAR)
859 (let ((data-length (foreign-string-length data-ptr)))
860 (adjust-array str (+ offset data-length)
861 :initial-element #\?)
862 (setf offset (%cstring-into-vector
866 (error 'clsql:sql-database-error :message "wrong type. preliminary."))
867 while (and (= res $SQL_SUCCESS_WITH_INFO)
868 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
870 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
871 +max-precision+ out-len-ptr)))
872 (setf str (coerce str 'string))
873 (if (= sql-type $SQL_DECIMAL)
874 (let ((*read-base* 10))
875 (read-from-string str))
878 (let ((str (make-string out-len)))
879 (loop do (if (= c-type #.$SQL_CHAR)
880 (setf offset (%cstring-into-vector ;string
883 (min out-len (1- +max-precision+))))
884 (error 'clsql:sql-database-error :message "wrong type. preliminary."))
886 (and (= res $SQL_SUCCESS_WITH_INFO)
887 #+ingore(eq (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
889 (equal (sql-state +null-handle-ptr+ +null-handle-ptr+ hstmt)
891 do (setf res (%sql-get-data hstmt column-nr c-type data-ptr
892 +max-precision+ out-len-ptr)
893 out-len (deref-pointer out-len-ptr #.$ODBC-LONG-TYPE)))
894 (if (= sql-type $SQL_DECIMAL)
895 (let ((*read-base* 10))
896 (read-from-string str))
899 (def-type c-timestamp-ptr-type '(* (:struct sql-c-timestamp)))
900 (def-type c-time-ptr-type '(* (:struct sql-c-time)))
901 (def-type c-date-ptr-type '(* (:struct sql-c-date)))
903 (defun timestamp-to-universal-time (ptr)
904 (declare (type c-timestamp-ptr-type ptr))
906 (encode-universal-time
907 (get-slot-value ptr 'sql-c-timestamp 'second)
908 (get-slot-value ptr 'sql-c-timestamp 'minute)
909 (get-slot-value ptr 'sql-c-timestamp 'hour)
910 (get-slot-value ptr 'sql-c-timestamp 'day)
911 (get-slot-value ptr 'sql-c-timestamp 'month)
912 (get-slot-value ptr 'sql-c-timestamp 'year))
913 (get-slot-value ptr 'sql-c-timestamp 'fraction)))
915 (defun universal-time-to-timestamp (time &optional (fraction 0))
916 (multiple-value-bind (sec min hour day month year)
917 (decode-universal-time time)
918 (let ((ptr (allocate-foreign-object 'sql-c-timestamp)))
919 (setf (get-slot-value ptr 'sql-c-timestamp 'second) sec
920 (get-slot-value ptr 'sql-c-timestamp 'minute) min
921 (get-slot-value ptr 'sql-c-timestamp 'hour) hour
922 (get-slot-value ptr 'sql-c-timestamp 'day) day
923 (get-slot-value ptr 'sql-c-timestamp 'month) month
924 (get-slot-value ptr 'sql-c-timestamp 'year) year
925 (get-slot-value ptr 'sql-c-timestamp 'fraction) fraction)
928 (defun %put-timestamp (ptr time &optional (fraction 0))
929 (declare (type c-timestamp-ptr-type ptr))
930 (multiple-value-bind (sec min hour day month year)
931 (decode-universal-time time)
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 date-to-universal-time (ptr)
942 (declare (type c-date-ptr-type ptr))
943 (encode-universal-time
945 (get-slot-value ptr 'sql-c-timestamp 'day)
946 (get-slot-value ptr 'sql-c-timestamp 'month)
947 (get-slot-value ptr 'sql-c-timestamp 'year)))
949 (defun time-to-universal-time (ptr)
950 (declare (type c-time-ptr-type ptr))
951 (encode-universal-time
952 (get-slot-value ptr 'sql-c-timestamp 'second)
953 (get-slot-value ptr 'sql-c-timestamp 'minute)
954 (get-slot-value ptr 'sql-c-timestamp 'hour)
960 (defun %set-attr-odbc-version (henv version)
961 (with-error-handling (:henv henv)
962 (SQLSetEnvAttr henv $SQL_ATTR_ODBC_VERSION
963 (make-pointer version :void) 0)))
965 (defun %list-tables (hstmt)
966 (with-error-handling (:hstmt hstmt)
967 (SQLTables hstmt +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0 +null-ptr+ 0)))
969 (defun %table-statistics (table hstmt &key unique (ensure t))
970 (with-cstrings ((table-cs table))
971 (with-error-handling (:hstmt hstmt)
977 (if unique $SQL_INDEX_UNIQUE $SQL_INDEX_ALL)
978 (if ensure $SQL_ENSURE $SQL_QUICK)))))
980 (defun %list-data-sources (henv)
981 (let ((dsn (allocate-foreign-string (1+ $SQL_MAX_DSN_LENGTH)))
982 (desc (allocate-foreign-string 256))
985 (with-foreign-objects ((dsn-len :short)
987 (let ((res (with-error-handling (:henv henv)
988 (SQLDataSources henv $SQL_FETCH_FIRST dsn
989 (1+ $SQL_MAX_DSN_LENGTH)
990 dsn-len desc 256 desc-len))))
991 (when (or (eql res $SQL_SUCCESS)
992 (eql res $SQL_SUCCESS_WITH_INFO))
993 (push (convert-from-foreign-string dsn) results))
995 (do ((res (with-error-handling (:henv henv)
996 (SQLDataSources henv $SQL_FETCH_NEXT dsn
997 (1+ $SQL_MAX_DSN_LENGTH)
998 dsn-len desc 256 desc-len))
999 (with-error-handling (:henv henv)
1000 (SQLDataSources henv $SQL_FETCH_NEXT dsn
1001 (1+ $SQL_MAX_DSN_LENGTH)
1002 dsn-len desc 256 desc-len))))
1003 ((not (or (eql res $SQL_SUCCESS)
1004 (eql res $SQL_SUCCESS_WITH_INFO))))
1005 (push (convert-from-foreign-string dsn) results))))
1007 (free-foreign-object dsn)
1008 (free-foreign-object desc)))
1009 (nreverse results)))