1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
5 ;;;; Name: oracle-sql.lisp
9 ;;;; This file is part of CLSQL.
11 ;;;; CLSQL users are granted the rights to distribute and use this software
12 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
13 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
14 ;;;; *************************************************************************
16 (in-package #:clsql-oracle)
18 (defmethod database-initialize-database-type ((database-type (eql :oracle)))
21 ;;;; arbitrary parameters, tunable for performance or other reasons
23 (eval-when (:compile-toplevel :load-toplevel :execute)
24 (defconstant +errbuf-len+ 512
25 "the number of characters that we allocate for an error message buffer")
26 (defconstant +n-buf-rows+ 200
27 "the number of table rows that we buffer at once when reading a table.
28 CMUCL has a compiled-in limit on how much C data can be allocated
29 (through malloc() and friends) at any given time, typically 8 Mb.
30 Setting this constant to a moderate value should make it less
31 likely that we'll have to worry about the CMUCL limit."))
34 (defmacro deref-vp (foreign-object)
35 `(uffi:deref-pointer ,foreign-object :pointer-void))
37 ;; constants - from OCI?
39 (defconstant +var-not-in-list+ 1007)
40 (defconstant +no-data-found+ 1403)
41 (defconstant +null-value-returned+ 1405)
42 (defconstant +field-truncated+ 1406)
44 (eval-when (:compile-toplevel :load-toplevel :execute)
45 (defconstant SQLT-NUMBER 2)
46 (defconstant SQLT-INT 3)
47 (defconstant SQLT-STR 5)
48 (defconstant SQLT-FLT 4)
49 (defconstant SQLT-DATE 12))
51 ;;; Note that despite the suggestive class name (and the way that the
52 ;;; *DEFAULT-DATABASE* variable holds an object of this class), a DB
53 ;;; object is not actually a database but is instead a connection to a
54 ;;; database. Thus, there's no obstacle to having any number of DB
55 ;;; objects referring to the same database.
57 (uffi:def-type pointer-pointer-void '(* :pointer-void))
59 (defclass oracle-database (database) ; was struct db
63 :type pointer-pointer-void
65 "OCI environment handle")
69 :type pointer-pointer-void
75 :type pointer-pointer-void
77 "OCI service context handle")
82 "optional data source name (used only for debugging/printing)")
88 "the \"user\" value given when data source connection was made")
92 :initform "YYYY-MM-DD HH24:MI:SS\"+00\"")
96 "Each database connection can be configured with its own date
97 output format. In order to extract date strings from output buffers
98 holding multiple date strings in fixed-width fields, we need to know
99 the length of that format.")
102 :initarg :server-version
103 :reader server-version
105 "Version string of Oracle server.")
106 (major-server-version
107 :type (or null fixnum)
108 :initarg :major-server-version
109 :reader major-server-version
111 "The major version number of the Oracle server, should be 8, 9, or 10")
114 :initarg :client-version
115 :reader client-version
117 "Version string of Oracle client.")
118 (major-client-version
119 :type (or null fixnum)
120 :initarg :major-client-version
121 :reader major-client-version
123 "The major version number of the Oracle client, should be 8, 9, or 10")))
126 ;;; Handle the messy case of return code=+oci-error+, querying the
127 ;;; system for subcodes and reporting them as appropriate. ERRHP and
128 ;;; NULLS-OK are as in the OERR function.
130 (defun handle-oci-error (&key database nulls-ok)
134 (uffi:with-foreign-objects ((errbuf '(:array :unsigned-char
137 ;; ensure errbuf empty string
138 (setf (uffi:deref-array errbuf '(:array :unsigned-char) 0)
139 (uffi:ensure-char-storable (code-char 0)))
141 (setf (uffi:deref-pointer errcode :long) 0)
142 (oci-error-get (deref-vp errhp) 1
143 (uffi:make-null-pointer :unsigned-char)
144 errcode errbuf +errbuf-len+ +oci-htype-error+)
145 (let ((subcode (uffi:deref-pointer errcode :long)))
146 (unless (and nulls-ok (= subcode +null-value-returned+))
147 (error 'sql-database-error
150 :message (uffi:convert-from-foreign-string errbuf)))))))
152 (error 'sql-database-error
154 :message "can't handle NULLS-OK without ERRHP"))
156 (error 'sql-database-error
158 :message "OCI Error (and no ERRHP available to find subcode)"))))
160 ;;; Require an OCI success code.
162 ;;; (The ordinary OCI error reporting mechanisms uses a fair amount of
163 ;;; machinery (environments and other handles). In order to get to
164 ;;; where we can use these mechanisms, we have to be able to allocate
165 ;;; the machinery. The functions for allocating the machinery can
166 ;;; return errors (e.g. out of memory) but shouldn't. Wrapping this function
167 ;;; around function calls to such have-to-succeed functions enforces
171 (declare (type fixnum code))
172 (unless (= code +oci-success+)
173 (error 'sql-database-error
174 :message (format nil "unexpected OCI failure, code=~S" code))))
177 ;;; Enabling this can be handy for low-level debugging.
180 (trace oci-initialize #+oci-8-1-5 oci-env-create oci-handle-alloc oci-logon
181 oci-error-get oci-stmt-prepare oci-stmt-execute
182 oci-param-get oci-logon oci-attr-get oci-define-by-pos oci-stmt-fetch)
183 (setf debug::*debug-print-length* nil))
186 ;;;; the OCI library, part V: converting from OCI representations to Lisp
189 ;; Return the INDEXth string of the OCI array, represented as Lisp
190 ;; SIMPLE-STRING. SIZE is the size of the fixed-width fields used by
191 ;; Oracle to store strings within the array.
193 ;; In the wild world of databases, trailing spaces aren't generally
194 ;; significant, since e.g. "LARRY " and "LARRY " are the same string
195 ;; stored in different fixed-width fields. OCI drops trailing spaces
196 ;; for us in some cases but apparently not for fields of fixed
197 ;; character width, e.g.
199 ;; (dbi:sql "create table employees (name char(15), job char(15), city
200 ;; char(15), rate float)" :db orcl :types :auto)
201 ;; In order to map the "same string" property above onto Lisp equality,
202 ;; we drop trailing spaces in all cases:
204 (uffi:def-type string-array (:array :unsigned-char))
206 (defun deref-oci-string (arrayptr string-index size)
207 ;; (declare (type string-array arrayptr))
208 (declare (type (mod #.+n-buf-rows+) string-index))
209 (declare (type (and unsigned-byte fixnum) size))
210 (let* ((raw (uffi:convert-from-foreign-string
212 (+ (uffi:pointer-address arrayptr) (* string-index size))
214 (trimmed (string-trim " " raw)))
215 (if (equal trimmed "NULL") nil trimmed)))
217 ;; the OCI library, part Z: no-longer used logic to convert from
218 ;; Oracle's binary date representation to Common Lisp's native date
222 (defvar +oci-date-bytes+ 7)
224 ;;; Return the INDEXth date in the OCI array, represented as
225 ;;; a Common Lisp "universal time" (i.e. seconds since 1900).
228 (defun deref-oci-date (arrayptr index)
229 (oci-date->universal-time (uffi:pointer-address
230 (uffi:deref-array arrayptr
231 '(:array :unsigned-char)
232 (* index +oci-date-bytes+)))))
234 (defun oci-date->universal-time (oci-date)
235 (declare (type (alien (* :unsigned-char)) oci-date))
236 (flet (;; a character from OCI-DATE, interpreted as an unsigned byte
238 (declare (type (mod #.+oci-date-bytes+) i))
239 (mod (uffi:deref-array oci-date string-array i) 256)))
240 (let* ((century (* (- (ub 0) 100) 100))
241 (year (+ century (- (ub 1) 100)))
246 (second (1- (ub 6))))
247 (encode-universal-time second minute hour day month year))))
250 (defmethod database-list-tables ((database oracle-database) &key owner)
254 "select user_tables.table_name from user_tables,all_tables where user_tables.table_name=all_tables.table_name and all_tables.owner='~:@(~A~)'"
256 "select table_name from user_tables")))
257 (mapcar #'car (database-query query database nil nil))))
260 (defmethod database-list-views ((database oracle-database) &key owner)
264 "select user_views.view_name from user_views,all_views where user_views.view_name=all_views.view_name and all_views.owner='~:@(~A~)'"
266 "select view_name from user_views")))
268 (database-query query database nil nil))))
270 (defmethod database-list-indexes ((database oracle-database)
275 "select user_indexes.index_name from user_indexes,all_indexes where user_indexes.index_name=all_indexes.index_name and all_indexes.owner='~:@(~A~)'"
277 "select index_name from user_indexes")))
278 (mapcar #'car (database-query query database nil nil))))
280 (defmethod database-list-table-indexes (table (database oracle-database)
285 "select user_indexes.index_name from user_indexes,all_indexes where user_indexes.table_name='~A' and user_indexes.index_name=all_indexes.index_name and all_indexes.owner='~:@(~A~)'"
287 (format nil "select index_name from user_indexes where table_name='~A'"
289 (mapcar #'car (database-query query database nil nil))))
291 (defmethod list-all-table-columns (table (db oracle-database))
292 (declare (string table))
293 (let* ((sql-stmt (concatenate
299 "user_tab_columns.COLUMN_NAME,"
300 "user_tab_columns.DATA_TYPE from user_tab_columns,"
301 "all_tables where all_tables.table_name = '" table "'"
302 " and user_tab_columns.table_name = '" table "'"))
303 (preresult (database-query sql-stmt db :auto nil)))
304 ;; PRERESULT is like RESULT except that it has a name instead of
305 ;; type codes in the fifth column of each row. To fix this, we
306 ;; destructively modify PRERESULT.
307 (dolist (preresult-row preresult)
308 (setf (fifth preresult-row)
309 (if (find (fifth preresult-row)
316 (defmethod database-list-attributes (table (database oracle-database) &key owner)
320 "select user_tab_columns.column_name from user_tab_columns,all_tables where user_tab_columns.table_name='~A' and all_tables.table_name=user_tab_columns.table_name and all_tables.owner='~:@(~A~)'"
323 "select column_name from user_tab_columns where table_name='~A'"
325 (mapcar #'car (database-query query database nil nil))))
327 (defmethod database-attribute-type (attribute (table string)
328 (database oracle-database)
333 "select data_type,data_length,data_scale,nullable from user_tab_columns,all_tables where user_tab_columns.table_name='~A' and column_name='~A' and all_tables.table_name=user_tab_columns.table_name and all_tables.owner='~:@(~A~)'"
334 table attribute owner)
336 "select data_type,data_length,data_scale,nullable from user_tab_columns where table_name='~A' and column_name='~A'"
338 (destructuring-bind (type length scale nullable) (car (database-query query database :auto nil))
339 (values (ensure-keyword type) length scale
340 (if (char-equal #\Y (schar nullable 0)) 1 0)))))
342 ;; Return one row of the table referred to by QC, represented as a
343 ;; list; or if there are no more rows, signal an error if EOF-ERRORP,
344 ;; or return EOF-VALUE otherwise.
346 ;; KLUDGE: This CASE statement is a strong sign that the code would be
347 ;; cleaner if CD were made into an abstract class, we made variant
348 ;; classes for CD-for-column-of-strings, CD-for-column-of-floats,
349 ;; etc., and defined virtual functions to handle operations like
350 ;; get-an-element-from-column. (For a small special purpose module
351 ;; like this, would arguably be overkill, so I'm not going to do it
352 ;; now, but if this code ends up getting more complicated in
353 ;; maintenance, it would become a really good idea.)
355 ;; Arguably this would be a good place to signal END-OF-FILE, but
356 ;; since the ANSI spec specifically says that END-OF-FILE means a
357 ;; STREAM which has no more data, and QC is not a STREAM, we signal
358 ;; DBI-ERROR instead.
360 (uffi:def-type short-array '(:array :short))
361 (uffi:def-type int-pointer '(* :int))
362 (uffi:def-type double-pointer '(* :double))
364 ;;; the result of a database query: a cursor through a table
365 (defstruct (oracle-result-set (:print-function print-query-cursor)
367 (:constructor %make-query-cursor))
368 (db (error "missing DB") ; db conn. this table is associated with
369 :type oracle-database
371 (stmthp (error "missing STMTHP") ; the statement handle used to create
372 ;; :type alien ; this table. owned by the QUERY-CURSOR
373 :read-only t) ; object, deallocated on CLOSE-QUERY
374 (cds) ; (error "missing CDS") ; column descriptors
375 ; :type (simple-array cd 1)
378 0 ; buffered rows: number of rows recv'd
379 :type (integer 0 #.+n-buf-rows+)) ; from the database on the last read
381 0 ; number of buffered rows returned, i.e.
382 :type (integer 0 #.+n-buf-rows+)) ; the index, within the buffered rows,
383 ; of the next row which hasn't already
386 0 ; total number of bytes recv'd from OCI
387 :type unsigned-byte) ; in all reads
388 (oci-end-seen-p nil)) ; Have we seen the end of OCI
389 ; data, i.e. OCI returning
390 ; less data than we requested?
391 ; OCI doesn't seem to like us
392 ; to try to read more data
393 ; from it after that..
396 (defun fetch-row (qc &optional (eof-errorp t) eof-value)
397 ;;(declare (optimize (speed 3)))
398 (cond ((zerop (qc-n-from-oci qc))
400 (error 'sql-database-error :message
401 (format nil "no more rows available in ~S" qc))
403 ((>= (qc-n-to-dbi qc)
405 (refill-qc-buffers qc)
406 (fetch-row qc nil eof-value))
408 (let ((cds (qc-cds qc))
409 (reversed-result nil)
410 (irow (qc-n-to-dbi qc)))
411 (dotimes (icd (length cds))
412 (let* ((cd (aref cds icd))
413 (b (foreign-resource-buffer (cd-buffer cd)))
415 (let* ((arb (foreign-resource-buffer (cd-indicators cd)))
416 (indicator (uffi:deref-array arb '(:array :short) irow)))
417 ;; b(declare (type short-array arb))
418 (unless (= indicator -1)
419 (ecase (cd-oci-data-type cd)
421 (deref-oci-string b irow (cd-sizeof cd)))
423 (uffi:deref-array b '(:array :double) irow))
425 (uffi:deref-array b '(:array :int) irow))
427 (deref-oci-string b irow (cd-sizeof cd))))))))
428 (when (and (eq :string (cd-result-type cd))
430 (not (stringp value)))
431 (setq value (write-to-string value)))
432 (push value reversed-result)))
433 (incf (qc-n-to-dbi qc))
434 (nreverse reversed-result)))))
436 (defun refill-qc-buffers (qc)
437 (with-slots (errhp) (qc-db qc)
438 (setf (qc-n-to-dbi qc) 0)
439 (cond ((qc-oci-end-seen-p qc)
440 (setf (qc-n-from-oci qc) 0))
442 (let ((oci-code (%oci-stmt-fetch
443 (deref-vp (qc-stmthp qc))
446 +oci-fetch-next+ +oci-default+)))
448 (#.+oci-success+ (values))
449 (#.+oci-no-data+ (setf (qc-oci-end-seen-p qc) t)
451 (#.+oci-error+ (handle-oci-error :database (qc-db qc)
453 (uffi:with-foreign-object (rowcount :long)
454 (oci-attr-get (deref-vp (qc-stmthp qc))
457 (uffi:make-null-pointer :unsigned-long)
460 (setf (qc-n-from-oci qc)
461 (- (uffi:deref-pointer rowcount :long)
462 (qc-total-n-from-oci qc)))
463 (when (< (qc-n-from-oci qc) +n-buf-rows+)
464 (setf (qc-oci-end-seen-p qc) t))
465 (setf (qc-total-n-from-oci qc)
466 (uffi:deref-pointer rowcount :long)))))
469 ;; the guts of the SQL function
471 ;; (like the SQL function, but with the QUERY argument hardwired to T, so
472 ;; that the return value is always a cursor instead of a list)
474 ;; Is this a SELECT statement? SELECT statements are handled
475 ;; specially by OCIStmtExecute(). (Non-SELECT statements absolutely
476 ;; require a nonzero iteration count, while the ordinary choice for a
477 ;; SELECT statement is a zero iteration count.
479 ;; SELECT statements are the only statements which return tables. We
480 ;; don't free STMTHP in this case, but instead give it to the new
481 ;; QUERY-CURSOR, and the new QUERY-CURSOR becomes responsible for
482 ;; freeing the STMTHP when it is no longer needed.
484 (defun sql-stmt-exec (sql-stmt-string db result-types field-names)
485 (with-slots (envhp svchp errhp)
487 (let ((stmthp (uffi:allocate-foreign-object :pointer-void)))
488 (uffi:with-foreign-object (stmttype :unsigned-short)
490 (oci-handle-alloc (deref-vp envhp)
492 +oci-htype-stmt+ 0 +null-void-pointer-pointer+)
493 (oci-stmt-prepare (deref-vp stmthp)
495 (uffi:convert-to-cstring sql-stmt-string)
496 (length sql-stmt-string)
497 +oci-ntv-syntax+ +oci-default+ :database db)
498 (oci-attr-get (deref-vp stmthp)
501 (uffi:make-null-pointer :unsigned-int)
505 (let* ((select-p (= (uffi:deref-pointer stmttype :unsigned-short) 1))
506 (iters (if select-p 0 1)))
508 (oci-stmt-execute (deref-vp svchp)
511 iters 0 +null-void-pointer+ +null-void-pointer+ +oci-default+
514 (make-query-cursor db stmthp result-types field-names))
516 (oci-handle-free (deref-vp stmthp) +oci-htype-stmt+)
520 ;; Return a QUERY-CURSOR representing the table returned from the OCI
521 ;; operation done through STMTHP. TYPES is the argument of the same
522 ;; name from the external SQL function, controlling type conversion
523 ;; of the returned arguments.
525 (defun make-query-cursor (db stmthp result-types field-names)
526 (let ((qc (%make-query-cursor :db db
528 :cds (make-query-cursor-cds db stmthp
531 (refill-qc-buffers qc)
535 ;; the hairy part of MAKE-QUERY-CURSOR: Ask OCI for information
536 ;; about table columns, translate the information into a Lisp
537 ;; vector of column descriptors, and return it.
539 ;; Allegro defines several flavors of type conversion, but this
540 ;; implementation only supports the :AUTO flavor.
542 ;; A note of explanation: OCI's internal number format uses 21
543 ;; bytes (42 decimal digits). 2 separate (?) one-byte fields,
544 ;; scale and precision, are used to deduce the nature of these
545 ;; 21 bytes. See pp. 3-10, 3-26, and 6-13 of OCI documentation
548 ;; When calling OCI C code to handle the conversion, we have
549 ;; only two numeric types available to pass the return value:
550 ;; double-float and signed-long. It would be possible to
551 ;; bypass the OCI conversion functions and write Lisp code
552 ;; which reads the 21-byte field directly and decodes
553 ;; it. However this is left as an exercise for the reader. :-)
555 ;; The following table describes the mapping, based on the implicit
556 ;; assumption that C's "signed long" type is a 32-bit integer.
558 ;; Internal Values SQL Type C Return Type
559 ;; =============== ======== =============
560 ;; Precision > 0 SCALE = -127 FLOAT --> double-float
561 ;; Precision > 0 && <=9 SCALE = 0 INTEGER --> signed-long
562 ;; Precision = 0 || > 9 SCALE = 0 BIG INTEGER --> double-float
563 ;; Precision > 0 SCALE > 0 DECIMAL --> double-float
565 ;; (OCI uses 1-based indexing here.)
567 ;; KLUDGE: This should work for all other data types except those
568 ;; which don't actually fit in their fixed-width field (BLOBs and the
569 ;; like). As Winton says, we (Cadabra) don't need to worry much about
570 ;; those, since we can't reason with them, so we don't use them. But
571 ;; for a more general application it'd be good to have a more
572 ;; selective and rigorously correct test here for whether we can
573 ;; actually handle the given DEREF-DTYPE value. -- WHN 20000106
575 ;; Note: The OCI documentation doesn't seem to say whether the COLNAME
576 ;; value returned here is a newly-allocated copy which we're
577 ;; responsible for freeing, or a pointer into some system copy which
578 ;; will be freed when the system itself is shut down. But judging
579 ;; from the way that the result is used in the cdemodsa.c example
580 ;; program, it looks like the latter: we should make our own copy of
581 ;; the value, but not try to free it.
583 ;; WORKAROUND: OCI seems to return ub2 values for the
584 ;; +oci-attr-data-size+ attribute even though its documentation claims
585 ;; that it returns a ub4, and even though the associated "sizep" value
586 ;; is 4, not 2. In order to make the code here work reliably, without
587 ;; having to patch it later if OCI is ever fixed to match its
588 ;; documentation, we pre-zero COLSIZE before making the call into OCI.
590 ;; To exercise the weird OCI behavior (thereby blowing up the code
591 ;; below, beware!) try setting this value into COLSIZE, calling OCI,
592 ;; then looking at the value in COLSIZE. (setf colsize #x12345678)
596 (defun make-query-cursor-cds (database stmthp result-types field-names)
597 (declare (optimize (safety 3) #+nil (speed 3))
598 (type oracle-database database)
599 (type pointer-pointer-void stmthp))
600 (with-slots (errhp) database
601 (uffi:with-foreign-objects ((dtype-foreign :unsigned-short)
602 (parmdp ':pointer-void)
605 (colname '(* :unsigned-char))
606 (colnamelen :unsigned-long)
607 (colsize :unsigned-long)
608 (colsizesize :unsigned-long)
609 (defnp ':pointer-void))
612 (do ((icolumn 0 (1+ icolumn))
613 (cds-as-reversed-list nil))
614 ((not (eql (oci-param-get (deref-vp stmthp)
618 (1+ icolumn) :database database)
620 (coerce (reverse cds-as-reversed-list) 'simple-vector))
621 ;; Decode type of ICOLUMNth column into a type we're prepared to
623 (oci-attr-get (deref-vp parmdp)
626 (uffi:make-null-pointer :unsigned-int)
629 (let ((dtype (uffi:deref-pointer dtype-foreign :unsigned-short)))
632 (setf buffer (acquire-foreign-resource :unsigned-char
633 (* 32 +n-buf-rows+)))
634 (setf sizeof 32 dtype #.SQLT-STR))
636 (oci-attr-get (deref-vp parmdp)
639 (uffi:make-null-pointer :unsigned-int)
642 (oci-attr-get (deref-vp parmdp)
645 (uffi:make-null-pointer :unsigned-int)
648 (let ((*scale (uffi:deref-pointer scale :byte))
649 (*precision (uffi:deref-pointer precision :byte)))
650 ;;(format t "scale=~d, precision=~d~%" *scale *precision)
653 (and (minusp *scale) (< *precision 10)))
654 (setf buffer (acquire-foreign-resource :int +n-buf-rows+)
655 sizeof 4 ;; sizeof(int)
658 (setf buffer (acquire-foreign-resource :double +n-buf-rows+)
659 sizeof 8 ;; sizeof(double)
660 dtype #.SQLT-FLT)))) )
661 ;; Default to SQL-STR
663 (setf (uffi:deref-pointer colsize :unsigned-long) 0
665 (oci-attr-get (deref-vp parmdp)
668 (uffi:make-null-pointer :unsigned-int) ;; (uffi:pointer-address colsizesize)
671 (let ((colsize-including-null (1+ (uffi:deref-pointer colsize :unsigned-long))))
672 (setf buffer (acquire-foreign-resource
673 :unsigned-char (* +n-buf-rows+ colsize-including-null)))
674 (setf sizeof colsize-including-null))))
675 (let ((retcodes (acquire-foreign-resource :unsigned-short +n-buf-rows+))
676 (indicators (acquire-foreign-resource :short +n-buf-rows+))
679 (oci-attr-get (deref-vp parmdp)
685 (setq colname-string (uffi:convert-from-foreign-string
686 (uffi:deref-pointer colname '(* :unsigned-char))
687 :length (uffi:deref-pointer colnamelen :unsigned-long))))
688 (push (make-cd :name colname-string
693 :indicators indicators
695 ((consp result-types)
696 (nth icolumn result-types))
701 cds-as-reversed-list)
702 (oci-define-by-pos (deref-vp stmthp)
705 (1+ icolumn) ; OCI 1-based indexing again
706 (uffi:with-cast-pointer (vp (foreign-resource-buffer buffer) :void)
710 (uffi:with-cast-pointer (vp (foreign-resource-buffer indicators) :void)
712 (uffi:make-null-pointer :unsigned-short)
713 (uffi:with-cast-pointer (vp (foreign-resource-buffer retcodes) :unsigned-short)
715 +oci-default+))))))))
717 ;; Release the resources associated with a QUERY-CURSOR.
719 (defun close-query (qc)
720 (oci-handle-free (deref-vp (qc-stmthp qc)) +oci-htype-stmt+)
721 (let ((cds (qc-cds qc)))
722 (dotimes (i (length cds))
723 (release-cd-resources (aref cds i))))
727 ;; Release the resources associated with a column description.
729 (defun release-cd-resources (cd)
730 (free-foreign-resource (cd-buffer cd))
731 (free-foreign-resource (cd-retcodes cd))
732 (free-foreign-resource (cd-indicators cd))
736 (defmethod database-name-from-spec (connection-spec (database-type (eql :oracle)))
737 (check-connection-spec connection-spec database-type (dsn user password))
738 (destructuring-bind (dsn user password) connection-spec
739 (declare (ignore password))
740 (concatenate 'string dsn "/" user)))
743 (defmethod database-connect (connection-spec (database-type (eql :oracle)))
744 (check-connection-spec connection-spec database-type (dsn user password))
745 (destructuring-bind (data-source-name user password)
747 (let ((envhp (uffi:allocate-foreign-object :pointer-void))
748 (errhp (uffi:allocate-foreign-object :pointer-void))
749 (svchp (uffi:allocate-foreign-object :pointer-void))
750 (srvhp (uffi:allocate-foreign-object :pointer-void)))
751 ;; Requests to allocate environments and handles should never
752 ;; fail in normal operation, and they're done too early to
753 ;; handle errors very gracefully (since they're part of the
754 ;; error-handling mechanism themselves) so we just assert they
756 (setf (deref-vp envhp) +null-void-pointer+)
759 (oci-env-create envhp +oci-default+ +null-void-pointer+
760 +null-void-pointer+ +null-void-pointer+
761 +null-void-pointer+ 0 +null-void-pointer-pointer+)
762 (oci-handle-alloc envhp
765 +null-void-pointer-pointer+))
768 (oci-initialize +oci-object+ +null-void-pointer+ +null-void-pointer+
769 +null-void-pointer+ +null-void-pointer-pointer+)
770 (ignore-errors (oci-handle-alloc +null-void-pointer+ envhp
772 +null-void-pointer-pointer+)) ;no testing return
773 (oci-env-init envhp +oci-default+ 0 +null-void-pointer-pointer+)
774 (oci-handle-alloc (deref-vp envhp) errhp
775 +oci-htype-error+ 0 +null-void-pointer-pointer+)
776 (oci-handle-alloc (deref-vp envhp) srvhp
777 +oci-htype-server+ 0 +null-void-pointer-pointer+)
778 (uffi:with-cstring (dblink nil)
779 (oci-server-attach (deref-vp srvhp)
783 (oci-handle-alloc (deref-vp envhp) svchp
784 +oci-htype-svcctx+ 0 +null-void-pointer-pointer+)
785 (oci-attr-set (deref-vp svchp)
787 (deref-vp srvhp) 0 +oci-attr-server+
789 ;; oci-handle-alloc((dvoid *)encvhp, (dvoid **)&stmthp, OCI_HTYPE_STMT, 0, 0);
792 ;; Actually, oci-server-version returns the client version, not the server versions
793 ;; will use "SELECT VERSION FROM V$INSTANCE" to get actual server version.
794 (let (db server-version client-version)
795 (declare (ignorable server-version))
796 (uffi:with-foreign-object (buf '(:array :unsigned-char #.+errbuf-len+))
797 (oci-server-version (deref-vp svchp)
799 (uffi:char-array-to-pointer buf)
800 +errbuf-len+ +oci-htype-svcctx+)
801 (setf client-version (uffi:convert-from-foreign-string buf))
802 ;; This returns the client version, not the server version, so diable it
804 (oci-server-version (deref-vp srvhp)
806 (uffi:char-array-to-pointer buf)
807 +errbuf-len+ +oci-htype-server+)
809 (setf server-version (uffi:convert-from-foreign-string buf)))
810 (setq db (make-instance 'oracle-database
811 :name (database-name-from-spec connection-spec
813 :connection-spec connection-spec
816 :database-type :oracle
818 :dsn data-source-name
820 :client-version client-version
821 :server-version server-version
822 :major-client-version (major-client-version-from-string
824 :major-server-version (major-client-version-from-string
826 (oci-logon (deref-vp envhp)
829 (uffi:convert-to-cstring user) (length user)
830 (uffi:convert-to-cstring password) (length password)
831 (uffi:convert-to-cstring data-source-name) (length data-source-name)
833 ;; :date-format-length (1+ (length date-format)))))
834 (setf (slot-value db 'clsql-sys::state) :open)
835 (database-execute-command
836 (format nil "ALTER SESSION SET NLS_DATE_FORMAT='~A'" (date-format db)) db)
837 (let ((server-version (caar (database-query "SELECT BANNER FROM V$VERSION WHERE BANNER LIKE '%Oracle%'" db nil nil))))
838 (setf (slot-value db 'server-version) server-version
839 (slot-value db 'major-server-version) (major-client-version-from-string
844 (defun major-client-version-from-string (str)
846 ((search " 10g " str)
848 ((search "Oracle9i " str)
850 ((search "Oracle8" str)
853 (defun major-server-version-from-string (str)
854 (when (> (length str) 2)
856 ((string= "10." (subseq str 0 3))
858 ((string= "9." (subseq str 0 2))
860 ((string= "8." (subseq str 0 2))
864 ;; Close a database connection.
866 (defmethod database-disconnect ((database oracle-database))
867 (osucc (oci-logoff (deref-vp (svchp database))
868 (deref-vp (errhp database))))
869 (osucc (oci-handle-free (deref-vp (envhp database)) +oci-htype-env+))
870 ;; Note: It's neither required nor allowed to explicitly deallocate the
871 ;; ERRHP handle here, since it's owned by the ENVHP deallocated above,
872 ;; and was therefore automatically deallocated at the same time.
875 ;;; Do the database operation described in SQL-STMT-STRING on database
876 ;;; DB and, if the command is a SELECT, return a representation of the
877 ;;; resulting table. The representation of the table is controlled by the
879 ;;; * If QUERY is NIL, the table is returned as a list of rows, with
880 ;;; each row represented by a list.
881 ;;; * If QUERY is non-NIL, the result is returned as a QUERY-CURSOR
882 ;;; suitable for FETCH-ROW and CLOSE-QUERY
883 ;;; The TYPES argument controls the type conversion method used
884 ;;; to construct the table. The Allegro version supports several possible
885 ;;; values for this argument, but we only support :AUTO.
887 (defmethod database-query (query-expression (database oracle-database) result-types field-names)
888 (let ((cursor (sql-stmt-exec query-expression database result-types field-names)))
889 ;; (declare (type (or query-cursor null) cursor))
890 (if (null cursor) ; No table was returned.
892 (do ((reversed-result nil))
894 (let* ((eof-value :eof)
895 (row (fetch-row cursor nil eof-value)))
896 (when (eq row eof-value)
899 (return (values (nreverse reversed-result)
900 (loop for cd across (qc-cds cursor)
901 collect (cd-name cd))))
902 (return (nreverse reversed-result))))
903 (push row reversed-result))))))
906 (defmethod database-create-sequence
907 (sequence-name (database oracle-database))
909 (concatenate 'string "CREATE SEQUENCE "
910 (sql-escape sequence-name))
913 (defmethod database-drop-sequence
914 (sequence-name (database oracle-database))
916 (concatenate 'string "DROP SEQUENCE "
917 (sql-escape sequence-name))
920 (defmethod database-sequence-next (sequence-name (database oracle-database))
923 (concatenate 'string "SELECT "
924 (sql-escape sequence-name)
927 database :auto nil)))
929 (defmethod database-set-sequence-position (name position database)
930 (let* ((next (database-sequence-next name database))
931 (incr (- position next)))
932 (database-execute-command
933 (format nil "ALTER SEQUENCE ~A INCREMENT BY ~D" name incr)
935 (database-sequence-next name database)
936 (database-execute-command
937 (format nil "ALTER SEQUENCE ~A INCREMENT BY 1" name)
940 (defmethod database-list-sequences ((database oracle-database) &key owner)
944 "select user_sequences.sequence_name from user_sequences,all_sequences where user_sequences.sequence_name=all_sequences.sequence_name and all_sequences.sequence_owner='~:@(~A~)'"
946 "select sequence_name from user_sequences")))
947 (mapcar #'car (database-query query database nil nil))))
949 (defmethod database-execute-command (sql-expression (database oracle-database))
950 (database-query sql-expression database nil nil)
952 (database-query "commit" database nil nil)
956 (defstruct (cd (:constructor make-cd)
957 (:print-function print-cd))
958 "a column descriptor: metadata about the data in a table"
960 ;; name of this column
961 (name (error "missing NAME") :type simple-string :read-only t)
962 ;; the size in bytes of a single element
963 (sizeof (error "missing SIZE") :type fixnum :read-only t)
964 ;; an array of +N-BUF-ROWS+ elements in C representation
965 (buffer (error "Missing BUFFER")
966 :type foreign-resource
968 ;; an array of +N-BUF-ROWS+ OCI return codes in C representation.
969 ;; (There must be one return code for every element of every
970 ;; row in order to be able to represent nullness.)
971 (retcodes (error "Missing RETCODES")
972 :type foreign-resource
974 (indicators (error "Missing INDICATORS")
975 :type foreign-resource
977 ;; the OCI code for the data type of a single element
978 (oci-data-type (error "missing OCI-DATA-TYPE")
981 (result-type (error "missing RESULT-TYPE")
985 (defun print-cd (cd stream depth)
986 (declare (ignore depth))
987 (print-unreadable-object (cd stream :type t)
989 ":NAME ~S :OCI-DATA-TYPE ~S :OCI-DATA-SIZE ~S"
991 (cd-oci-data-type cd)
994 (defun print-query-cursor (qc stream depth)
995 (declare (ignore depth))
996 (print-unreadable-object (qc stream :type t :identity t)
997 (prin1 (qc-db qc) stream)))
1000 (defmethod database-query-result-set ((query-expression string)
1001 (database oracle-database)
1002 &key full-set result-types)
1003 (let ((cursor (sql-stmt-exec query-expression database result-types nil)))
1005 (values cursor (length (qc-cds cursor)) nil)
1006 (values cursor (length (qc-cds cursor))))))
1009 (defmethod database-dump-result-set (result-set (database oracle-database))
1010 (close-query result-set))
1012 (defmethod database-store-next-row (result-set (database oracle-database) list)
1013 (let* ((eof-value :eof)
1014 (row (fetch-row result-set nil eof-value)))
1015 (unless (eq eof-value row)
1016 (loop for i from 0 below (length row)
1017 do (setf (nth i list) (nth i row)))
1020 (defmethod clsql-sys:database-start-transaction ((database oracle-database))
1023 ;;(with-slots (svchp errhp) database
1024 ;; (osucc (oci-trans-start (uffi:deref-pointer svchp)
1025 ;; (uffi:deref-pointer errhp)
1027 ;; +oci-trans-new+)))
1031 (defmethod clsql-sys:database-commit-transaction ((database oracle-database))
1033 (with-slots (svchp errhp) database
1034 (osucc (oci-trans-commit (deref-vp svchp)
1039 (defmethod clsql-sys:database-abort-transaction ((database oracle-database))
1041 (osucc (oci-trans-rollback (deref-vp (svchp database))
1042 (deref-vp (errhp database))
1046 (defparameter *constraint-types*
1047 '(("NOT-NULL" . "NOT NULL")))
1049 (defmethod database-output-sql ((str string) (database oracle-database))
1050 (if (and (null (position #\' str))
1051 (null (position #\\ str)))
1052 (format nil "'~A'" str)
1053 (let* ((l (length str))
1054 (buf (make-string (+ l 3))))
1055 (setf (aref buf 0) #\')
1058 ((= i l) (setf (aref buf j) #\'))
1059 (if (= j (- (length buf) 1))
1060 (setf buf (adjust-array buf (+ (length buf) 1))))
1061 (cond ((eql (aref str i) #\')
1062 (setf (aref buf j) #\')
1064 (setf (aref buf j) (aref str i)))
1070 (defmethod db-type-has-bigint? ((type (eql :oracle)))
1073 (defmethod db-type-has-fancy-math? ((db-type (eql :oracle)))
1076 (defmethod db-type-has-boolean-where? ((db-type (eql :oracle)))
1079 (when (clsql-sys:database-type-library-loaded :oracle)
1080 (clsql-sys:initialize-database-type :database-type :oracle))