use md5sum-string instead of md5sum-sequence to adjust to upstream changes
[clsql.git] / db-db2 / db2-api.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Base: 10 -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:          db2.lisp
6 ;;;; Purpose:       Package definition for CLSQL Db2 interface
7 ;;;;
8 ;;;; This file is part of CLSQL.
9 ;;;;
10 ;;;; CLSQL users are granted the rights to distribute and use this software
11 ;;;; as governed by the terms of the Lisp Lesser GNU Public License
12 ;;;; (http://opensource.franz.com/preamble.html), also known as the LLGPL.
13 ;;;; *************************************************************************
14
15 (in-package #:clsql-db2)
16
17
18 ;;
19 ;; Opaque pointer types
20 ;;
21
22 (uffi:def-foreign-type cli-handle :pointer-void)
23 (uffi:def-foreign-type cli-pointer :pointer-void)
24 (uffi:def-foreign-type cli-char :byte)
25 (uffi:def-foreign-type cli-ulen :unsigned-int)
26 (uffi:def-foreign-type cli-len :int)
27 (uffi:def-foreign-type cli-smallint :short)
28 (uffi:def-foreign-type cli-usmallint :unsigned-short)
29
30
31 (defvar +null-void-pointer+ (uffi:make-null-pointer :void))
32 (defvar +null-void-pointer-pointer+ (uffi:make-null-pointer :pointer-void))
33
34 ;;; Check an CLI return code for erroricity and signal a reasonably
35 ;;; informative condition if so.
36 ;;;
37 ;;; ERRHP provides an error handle which can be used to find
38 ;;; subconditions; if it's not provided, subcodes won't be checked.
39 ;;;
40 ;;; NULLS-OK says that a NULL-VALUE-RETURNED subcondition condition is
41 ;;; normal and needn't cause any signal. An error handle is required
42 ;;; to detect this subcondition, so it doesn't make sense to set ERRHP
43 ;;; unless NULLS-OK is set.
44
45 (defmacro def-cli-routine ((c-cli-symbol lisp-cli-fn) c-return &rest c-parms)
46   (let ((ll (mapcar (lambda (x) (declare (ignore x)) (gensym)) c-parms)))
47     `(let ((%lisp-cli-fn (uffi:def-function
48                              (,c-cli-symbol ,(intern (concatenate 'string "%" (symbol-name lisp-cli-fn))))
49                              ,c-parms
50                              :returning ,c-return)))
51        (defun ,lisp-cli-fn (,@ll &key database nulls-ok)
52          (let ((result (funcall %lisp-cli-fn ,@ll)))
53            (case result
54              (#.SQL_SUCCESS
55               SQL_SUCCESS)
56              (#.SQL_SUCCESS_WITH_INFO
57               (format *standard-output* "sucess with info")
58               SQL_SUCCESS)
59              (#.SQL_ERROR
60               (error 'sql-database-error
61                      :error-id result
62                      :message
63                      (format nil "DB2 error" result)))
64              (t
65               (error 'sql-database-error
66                      :message
67                      (format nil "DB2 unknown error, code=~A" result)))))))))
68
69
70 (defmacro def-raw-cli-routine
71   ((c-cli-symbol lisp-cli-fn) c-return &rest c-parms)
72   (let ((ll (mapcar (lambda (x) (declare (ignore x)) (gensym)) c-parms)))
73     `(let ((%lisp-cli-fn (uffi:def-function (,c-cli-symbol ,(intern (concatenate 'string "%" (symbol-name lisp-cli-fn))))
74                              ,c-parms
75                            :returning ,c-return)))
76        (defun ,lisp-cli-fn (,@ll &key database nulls-ok)
77          (funcall %lisp-cli-fn ,@ll)))))
78
79
80 (def-cli-routine ("SQLAllocHandle" sql-alloc-handle)
81     :int
82   (fHandleType cli-smallint)
83   (hInput cli-handle)
84   (phOuput (* cli-handle)))
85
86 (def-cli-routine ("SQLConnect" sql-connect)
87     :int
88   (hDb cli-handle)
89   (server :cstring)
90   (server-len cli-smallint)
91   (user :cstring)
92   (user-len cli-smallint)
93   (password :cstring)
94   (passwd-len cli-smallint))
95
96
97 ;;; CLI Functions needed
98 ;;;   SQLBindParameter
99 ;;;   SQLExecDirect
100 ;;;   SQLNumResultCols
101 ;;;   SQLDescribeCol
102 ;;;   SQLColAttribute
103 ;;;   SQLRowCount
104 ;;;   SQLBindCol
105 ;;;   SQLFetch
106 ;;;   SQLGetData
107 ;;;   SQLEndTran
108 ;;;   SQLFreeHandle
109 ;;;   SQLDisconnect
110 ;;;   SQLSetConnectAttr