1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
6 ;;;; Purpose: Package definition for CLSQL Oracle interface
10 ;;;; This file is part of CLSQL.
12 ;;;; CLSQL users are granted the rights to distribute and use this software
13 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
14 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
15 ;;;; *************************************************************************
17 (in-package #:clsql-oracle)
23 (uffi:def-foreign-type ub2 :unsigned-short)
24 (uffi:def-foreign-type sb2 :short)
25 (uffi:def-foreign-type ub4 :unsigned-int)
26 (uffi:def-foreign-type sb4 :int)
27 (uffi:def-foreign-type size_t :unsigned-long)
30 ;; Opaque pointer types
33 (uffi:def-foreign-type void-pointer :pointer-void)
34 (uffi:def-foreign-type oci-env :pointer-void)
35 (uffi:def-foreign-type oci-server :pointer-void)
36 (uffi:def-foreign-type oci-error :pointer-void)
37 (uffi:def-foreign-type oci-svc-ctx :pointer-void)
38 (uffi:def-foreign-type oci-stmt :pointer-void)
40 (uffi:def-pointer-var +null-void-pointer+
41 (uffi:make-null-pointer :void))
42 (uffi:def-pointer-var +null-void-pointer-pointer+
43 (uffi:make-null-pointer :pointer-void))
45 ;;; Check an OCI return code for erroricity and signal a reasonably
46 ;;; informative condition if so.
48 ;;; ERRHP provides an error handle which can be used to find
49 ;;; subconditions; if it's not provided, subcodes won't be checked.
51 ;;; NULLS-OK says that a NULL-VALUE-RETURNED subcondition condition is
52 ;;; normal and needn't cause any signal. An error handle is required
53 ;;; to detect this subcondition, so it doesn't make sense to set ERRHP
54 ;;; unless NULLS-OK is set.
56 (defmacro def-oci-routine ((c-oci-symbol lisp-oci-fn) c-return &rest c-parms)
57 (let ((ll (mapcar (lambda (x) (declare (ignore x)) (gensym)) c-parms))
58 (c-oci-fn (intern (concatenate 'string "%" (symbol-name lisp-oci-fn)))))
60 (declaim (inline ,c-oci-fn ,lisp-oci-fn))
61 (uffi:def-function (,c-oci-symbol ,c-oci-fn)
64 (defun ,lisp-oci-fn (,@ll &key database nulls-ok)
65 (let ((result (,c-oci-fn ,@ll)))
66 (if (= result #.+oci-success+)
68 (handle-oci-result result database nulls-ok)))))))
71 (defmacro def-raw-oci-routine
72 ((c-oci-symbol lisp-oci-fn) c-return &rest c-parms)
73 (let ((ll (mapcar (lambda (x) (declare (ignore x)) (gensym)) c-parms)))
74 `(let ((%lisp-oci-fn (uffi:def-function (,c-oci-symbol ,(intern (concatenate 'string "%" (symbol-name lisp-oci-fn))))
76 :returning ,c-return)))
77 (defun ,lisp-oci-fn (,@ll &key database nulls-ok)
78 (funcall %lisp-oci-fn ,@ll)))))
81 (def-oci-routine ("OCIInitialize" oci-initialize)
84 (ctxp :pointer-void) ; dvoid *
85 (malocfp :pointer-void) ; dvoid *(*)
86 (ralocfp :pointer-void) ; dvoid *(*)
87 (mfreefp (* :pointer-void))) ; void *(*)
90 (def-oci-routine ("OCIEnvInit" oci-env-init)
92 (envpp :pointer-void) ; OCIEnv **
94 (xtramem-sz size_t) ; size_t
95 (usermempp (* :pointer-void))) ; dvoid **
98 (def-oci-routine ("OCIEnvCreate" oci-env-create)
100 (envhpp (* :pointer-void))
103 (malocfp :pointer-void)
104 (ralocfp :pointer-void)
105 (mfreefp :pointer-void)
107 (usrmempp (* :pointer-void)))
109 (def-oci-routine ("OCIHandleAlloc" oci-handle-alloc)
111 (parenth :pointer-void) ; const dvoid *
112 (hndlpp (* :pointer-void)) ; dvoid **
114 (xtramem_sz size_t) ; size_t
115 (usrmempp (* :pointer-void))) ; dvoid **
117 (def-oci-routine ("OCIServerAttach" oci-server-attach)
119 (srvhp :pointer-void) ; oci-server
120 (errhp :pointer-void) ; oci-error
121 (dblink :cstring) ; :in
122 (dblink-len sb4) ; sb4
126 (def-oci-routine ("OCIHandleFree" oci-handle-free)
128 (p0 :pointer-void) ;; handle
131 (def-oci-routine ("OCILogon" oci-logon)
133 (envhp :pointer-void) ; env
134 (errhp :pointer-void) ; err
135 (svchpp (* :pointer-void)) ; svc
136 (username (* :unsigned-char)) ; username
138 (passwd (* :unsigned-char)) ; passwd
140 (dsn (* :unsigned-char)) ; datasource
143 (def-oci-routine ("OCILogoff" oci-logoff)
145 (p0 :pointer-void) ; svc
146 (p1 :pointer-void)) ; err
148 (declaim (inline oci-error-get))
149 (uffi:def-function ("OCIErrorGet" oci-error-get)
150 ((handlp :pointer-void)
154 (bufp (* :unsigned-char))
159 (def-oci-routine ("OCIStmtPrepare" oci-stmt-prepare)
161 (stmtp :pointer-void)
162 (errhp :pointer-void)
163 (stmt (* :unsigned-char))
168 (def-oci-routine ("OCIStmtExecute" oci-stmt-execute)
170 (svchp :pointer-void)
171 (stmtp1 :pointer-void)
172 (errhp :pointer-void)
175 (snap_in :pointer-void)
176 (snap_out :pointer-void)
179 (def-raw-oci-routine ("OCIParamGet" oci-param-get)
181 (hndlp :pointer-void)
183 (errhp :pointer-void)
184 (parmdpp (* :pointer-void))
187 (def-oci-routine ("OCIAttrGet" oci-attr-get)
189 (trgthndlp :pointer-void)
191 (attributep :pointer-void)
194 (errhp :pointer-void))
196 (def-oci-routine ("OCIAttrSet" oci-attr-set)
198 (trgthndlp :pointer-void)
199 (trgthndltyp ub4 :in)
200 (attributep :pointer-void)
205 (def-oci-routine ("OCIDefineByPos" oci-define-by-pos)
207 (stmtp :pointer-void)
208 (defnpp (* :pointer-void))
209 (errhp :pointer-void)
211 (valuep :pointer-void)
219 (def-oci-routine ("OCIStmtFetch" oci-stmt-fetch)
221 (stmthp :pointer-void)
222 (errhp :pointer-void)
228 (def-oci-routine ("OCITransStart" oci-trans-start)
230 (svchp :pointer-void)
231 (errhp :pointer-void)
233 (p3 :unsigned-short))
235 (def-oci-routine ("OCITransCommit" oci-trans-commit)
237 (svchp :pointer-void)
238 (errhp :pointer-void)
239 (p2 :unsigned-short))
241 (def-oci-routine ("OCITransRollback" oci-trans-rollback)
243 (svchp :pointer-void)
244 (errhp :pointer-void)
245 (p2 :unsigned-short))
248 (def-oci-routine ("OCIServerVersion" oci-server-version)
250 (handlp :pointer-void)
251 (errhp :pointer-void)
252 (bufp (* :unsigned-char))
258 ;;; Low-level routines that don't do error checking. They are used
259 ;;; for setting up global environment.
261 (uffi:def-function "OCIInitialize"
263 (ctxp :pointer-void) ; dvoid *
264 (malocfp :pointer-void) ; dvoid *(*)
265 (ralocfp :pointer-void) ; dvoid *(*)
266 (mfreefp (* :pointer-void)))
269 (uffi:def-function "OCIEnvInit"
270 ((envpp :pointer-void) ; OCIEnv **
272 (xtramem-sz size_t) ; size_t
273 (usermempp (* :pointer-void)))
277 (uffi:def-function "OCIHandleAlloc"
278 ((parenth :pointer-void) ; const dvoid *
279 (hndlpp (* :pointer-void)) ; dvoid **
281 (xtramem_sz size_t) ; size_t
282 (usrmempp (* :pointer-void)))
285 (defstruct oci-handle
287 (pointer (uffi:allocate-foreign-object :pointer-void)))
289 (defvar *oci-initialized* nil)
290 (defvar *oci-env* nil)
292 (defvar *oci-handle-types*
293 '(:error ; error report handle (OCIError)
294 :service-context ; service context handle (OCISvcCtx)
295 :statement ; statement (application request) handle (OCIStmt)
296 :describe ; select list description handle (OCIDescribe)
297 :server ; server context handle (OCIServer)
298 :session ; user session handle (OCISession)
299 :transaction ; transaction context handle (OCITrans)
300 :complex-object ; complex object retrieval handle (OCIComplexObject)
301 :security)) ; security handle (OCISecurity)
305 (defun oci-init (&key (mode +oci-default+))
306 (let ((x (OCIInitialize mode +null-void-pointer+ +null-void-pointer+
307 +null-void-pointer+ +null-void-pointer-pointer+)))
309 (let ((env (uffi:allocate-foreign-object :pointer-void)))
310 (setq *oci-initialized* mode)
311 (let ((x (OCIEnvInit env +oci-default+ 0 +null-void-pointer+)))
312 (format t ";; OEI: returned ~d~%" x)
313 (setq *oci-env* env))))))
315 (defun oci-check-return (value)
316 (when (= value +oci-invalid-handle+)
317 (error 'sql-database-error :message "Invalid Handle")))
319 (defun oci-get-handle (&key type)
320 (if (null *oci-initialized*)
324 (let ((ptr (uffi:allocate-foreign-object :pointer-void)))
325 (let ((x (OCIHandleAlloc
326 (uffi:deref-pointer *oci-env* void-pointer)
330 +null-void-pointer-pointer+)))
350 (error 'sql-database-error
352 (format nil "'~s' is not a valid OCI handle type" type)))))
354 (defun oci-environment ()
355 (let ((envhp (oci-get-handle :type :env)))
356 (oci-env-init envhp 0 0 +null-void-pointer+)