r9450: 22 May 2004 Kevin Rosenberg
[clsql.git] / db-odbc / odbc-ff-interface.lisp
1 ;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: odbc -*-
2 ;;;; *************************************************************************
3 ;;;; FILE IDENTIFICATION
4 ;;;;
5 ;;;; Name:     odbc-ff-interface.lisp
6 ;;;; Purpose:  Function definitions for UFFI interface to ODBC
7 ;;;; Author:   Kevin M. Rosenberg
8 ;;;;
9 ;;;; $Id$
10 ;;;;
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.
13 ;;;;
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 ;;;; *************************************************************************
18
19 (in-package #:odbc)
20
21 (def-foreign-type sql-handle :pointer-void)
22 (def-foreign-type sql-handle-ptr '(* sql-handle))
23 (def-foreign-type string-ptr '(* :unsigned-char))
24 (def-type long-ptr-type '(* #.$ODBC-LONG-TYPE))
25
26
27 (def-function "SQLAllocEnv"
28     ((*phenv sql-handle-ptr)    ; HENV   FAR *phenv
29      )
30   :module "odbc"
31   :returning :short)              ; RETCODE_SQL_API
32
33 (def-function "SQLAllocConnect"
34     ((henv sql-handle)          ; HENV        henv
35      (*phdbc sql-handle-ptr)    ; HDBC   FAR *phdbc
36      )
37   :module "odbc"
38   :returning :short)              ; RETCODE_SQL_API
39
40 (def-function "SQLConnect"
41     ((hdbc sql-handle)          ; HDBC        hdbc
42      (*szDSN :cstring)        ; UCHAR  FAR *szDSN
43      (cbDSN :short)             ; SWORD       cbDSN
44      (*szUID :cstring)        ; UCHAR  FAR *szUID 
45      (cbUID :short)             ; SWORD       cbUID
46      (*szAuthStr :cstring)    ; UCHAR  FAR *szAuthStr
47      (cbAuthStr :short)         ; SWORD       cbAuthStr
48      )
49   :module "odbc"
50   :returning :short)              ; RETCODE_SQL_API
51
52 (def-function "SQLDriverConnect"
53     ((hdbc sql-handle)          ; HDBC        hdbc
54      (hwnd sql-handle)          ; SQLHWND     hwnd
55      (*szConnStrIn string-ptr)  ; UCHAR  FAR *szConnStrIn
56      (cbConnStrIn :short)       ; SWORD       cbConnStrIn
57      (*szConnStrOut string-ptr) ; UCHAR  FAR *szConnStrOut
58      (cbConnStrOutMax :short)   ; SWORD       cbConnStrOutMax
59      (*pcbConnStrOut :pointer-void)      ; SWORD  FAR *pcbConnStrOut
60      (fDriverCompletion :short) ; UWORD       fDriverCompletion
61      )
62   :module "odbc"
63   :returning :short)              ; RETCODE_SQL_API
64
65 (def-function "SQLDisconnect"
66     ((hdbc sql-handle))         ; HDBC        hdbc
67   :module "odbc"
68   :returning :short)              ; RETCODE_SQL_API
69   
70 (def-function "SQLAllocStmt"
71     ((hdbc sql-handle)          ; HDBC        hdbc
72      (*phstmt sql-handle-ptr)   ; HSTMT  FAR *phstmt
73      )
74   :module "odbc"
75   :returning :short)              ; RETCODE_SQL_API
76
77 (def-function "SQLGetInfo"
78     ((hdbc sql-handle)          ; HDBC        hdbc
79      (fInfoType :short)         ; UWORD       fInfoType
80      (rgbInfoValue :pointer-void)        ; PTR         rgbInfoValue
81      (cbInfoValueMax :short)    ; SWORD       cbInfoValueMax
82      (*pcbInfoValue :pointer-void)       ; SWORD  FAR *pcbInfoValue
83      )
84   :module "odbc"
85   :returning :short)              ; RETCODE_SQL_API
86
87 (def-function "SQLPrepare"
88     ((hstmt sql-handle)         ; HSTMT       hstmt
89      (*szSqlStr :cstring)     ; UCHAR  FAR *szSqlStr
90      (cbSqlStr :int)           ; SDWORD      cbSqlStr
91      )
92   :module "odbc"
93   :returning :short)              ; RETCODE_SQL_API
94
95 (def-function "SQLExecute"
96     ((hstmt sql-handle)         ; HSTMT       hstmt
97      )
98   :module "odbc"
99   :returning :short)              ; RETCODE_SQL_API
100
101 (def-function "SQLExecDirect"
102     ((hstmt sql-handle)         ; HSTMT       hstmt
103      (*szSqlStr :cstring)     ; UCHAR  FAR *szSqlStr
104      (cbSqlStr :int)           ; SDWORD      cbSqlStr
105      )
106   :module "odbc"
107   :returning :short)              ; RETCODE_SQL_API
108
109 (def-function "SQLFreeStmt"
110     ((hstmt sql-handle)         ; HSTMT       hstmt
111      (fOption :short))          ; UWORD       fOption
112   :module "odbc"
113   :returning :short)              ; RETCODE_SQL_API
114
115   (def-function "SQLCancel"
116       ((hstmt sql-handle)         ; HSTMT       hstmt
117        )
118     :module "odbc"
119   :returning :short)              ; RETCODE_SQL_API
120
121 (def-function "SQLError"
122     ((henv sql-handle)          ; HENV        henv
123      (hdbc sql-handle)          ; HDBC        hdbc
124      (hstmt sql-handle)         ; HSTMT       hstmt
125      (*szSqlState string-ptr)   ; UCHAR  FAR *szSqlState
126      (*pfNativeError (* :int))      ; SDWORD FAR *pfNativeError
127      (*szErrorMsg string-ptr)   ; UCHAR  FAR *szErrorMsg
128      (cbErrorMsgMax :short)     ; SWORD       cbErrorMsgMax
129      (*pcbErrorMsg (* :short))        ; SWORD  FAR *pcbErrorMsg
130      )
131   :module "odbc"
132   :returning :short)              ; RETCODE_SQL_API
133
134 (def-function "SQLNumResultCols"
135     ((hstmt sql-handle)         ; HSTMT       hstmt
136      (*pccol (* :short))              ; SWORD  FAR *pccol
137      )
138   :module "odbc"
139   :returning :short)              ; RETCODE_SQL_API
140
141 (def-function "SQLRowCount"
142     ((hstmt sql-handle)         ; HSTMT       hstmt
143      (*pcrow (* :int))              ; SDWORD FAR *pcrow
144      )
145   :module "odbc"
146   :returning :short)              ; RETCODE_SQL_API
147
148 (def-function "SQLDescribeCol"
149     ((hstmt sql-handle)         ; HSTMT       hstmt
150      (icol :short)              ; UWORD       icol
151      (*szColName string-ptr)    ; UCHAR  FAR *szColName
152      (cbColNameMax :short)      ; SWORD       cbColNameMax
153      (*pcbColName (* :short))         ; SWORD  FAR *pcbColName
154      (*pfSqlType (* :short))          ; SWORD  FAR *pfSqlType
155      (*pcbColDef (* #.$ODBC-ULONG-TYPE))          ; UDWORD FAR *pcbColDef
156      (*pibScale (* :short))           ; SWORD  FAR *pibScale
157      (*pfNullable (* :short))         ; SWORD  FAR *pfNullable
158      )
159   :module "odbc"
160   :returning :short)              ; RETCODE_SQL_API
161
162 (def-function "SQLColAttributes"
163     ((hstmt sql-handle)         ; HSTMT       hstmt
164      (icol :short)              ; UWORD       icol
165      (fDescType :short)         ; UWORD       fDescType
166      (rgbDesc string-ptr)             ; PTR         rgbDesc
167      (cbDescMax :short)         ; SWORD       cbDescMax
168      (*pcbDesc (* :short))            ; SWORD  FAR *pcbDesc
169      (*pfDesc (* :int))             ; SDWORD FAR *pfDesc
170      )
171   :module "odbc"
172   :returning :short)              ; RETCODE_SQL_API
173
174 (def-function "SQLColumns"
175     ((hstmt sql-handle)             ; HSTMT       hstmt
176      (*szTableQualifier :cstring) ; UCHAR  FAR *szTableQualifier
177      (cbTableQualifier :short)      ; SWORD       cbTableQualifier
178      (*szTableOwner :cstring)     ; UCHAR  FAR *szTableOwner
179      (cbTableOwner :short)          ; SWORD       cbTableOwner
180      (*szTableName :cstring)      ; UCHAR  FAR *szTableName
181      (cbTableName :short)           ; SWORD       cbTableName
182      (*szColumnName :cstring)     ; UCHAR  FAR *szColumnName
183      (cbColumnName :short)          ; SWORD       cbColumnName
184      )
185   :module "odbc"
186   :returning :short)              ; RETCODE_SQL_API
187
188 (def-function "SQLBindCol"
189     ((hstmt sql-handle)         ; HSTMT       hstmt
190      (icol :short)              ; UWORD       icol
191      (fCType :short)            ; SWORD       fCType
192      (rgbValue :pointer-void)            ; PTR         rgbValue
193      (cbValueMax :int)         ; SDWORD      cbValueMax
194      (*pcbValue (* :int))           ; SDWORD FAR *pcbValue
195      )
196   :module "odbc"
197   :returning :short)              ; RETCODE_SQL_API
198
199 (def-function "SQLFetch"
200     ((hstmt sql-handle)         ; HSTMT       hstmt
201      )
202   :module "odbc"
203   :returning :short)              ; RETCODE_SQL_API
204
205 (def-function "SQLTransact"
206     ((henv sql-handle)          ; HENV        henv
207      (hdbc sql-handle)          ; HDBC        hdbc
208      (fType :short)             ; UWORD       fType ($SQL_COMMIT or $SQL_ROLLBACK)
209      )
210   :module "odbc"
211   :returning :short)              ; RETCODE_SQL_API
212
213 ;; ODBC 2.0
214 (def-function "SQLDescribeParam"
215     ((hstmt sql-handle)         ; HSTMT       hstmt
216      (ipar :short)              ; UWORD       ipar
217      (*pfSqlType (* :short))          ; SWORD  FAR *pfSqlType
218      (*pcbColDef (* :unsigned-int))          ; UDWORD FAR *pcbColDef
219      (*pibScale (* :short))           ; SWORD  FAR *pibScale
220      (*pfNullable (* :short))         ; SWORD  FAR *pfNullable
221      )
222   :module "odbc"
223   :returning :short)              ; RETCODE_SQL_API
224
225 ;; ODBC 2.0
226 (def-function "SQLBindParameter"
227     ((hstmt sql-handle)         ; HSTMT       hstmt
228      (ipar :short)              ; UWORD       ipar
229      (fParamType :short)        ; SWORD       fParamType
230      (fCType :short)            ; SWORD       fCType
231      (fSqlType :short)          ; SWORD       fSqlType
232      (cbColDef :int)           ; UDWORD      cbColDef
233      (ibScale :short)           ; SWORD       ibScale
234      (rgbValue :pointer-void)            ; PTR         rgbValue
235      (cbValueMax :int)         ; SDWORD      cbValueMax
236      (*pcbValue :pointer-void)           ; SDWORD FAR *pcbValue
237      )
238   :module "odbc"
239   :returning :short)              ; RETCODE_SQL_API
240
241 ;; level 1
242 (def-function "SQLGetData"
243     ((hstmt sql-handle)         ; HSTMT       hstmt
244      (icol :short)              ; UWORD       icol
245      (fCType :short)            ; SWORD       fCType
246      (rgbValue :pointer-void)            ; PTR         rgbValue
247      (cbValueMax :int)         ; SDWORD      cbValueMax
248      (*pcbValue :pointer-void)           ; SDWORD FAR *pcbValue
249      )
250   :module "odbc"
251   :returning :short)              ; RETCODE_SQL_API
252
253 (def-function "SQLParamData"
254     ((hstmt sql-handle)         ; HSTMT       hstmt
255      (*prgbValue :pointer-void)          ; PTR    FAR *prgbValue
256      )
257   :module "odbc"
258   :returning :short)              ; RETCODE_SQL_API
259
260 (def-function "SQLPutData"
261     ((hstmt sql-handle)         ; HSTMT       hstmt
262      (rgbValue :pointer-void)            ; PTR         rgbValue
263      (cbValue :int)            ; SDWORD      cbValue
264      )
265   :module "odbc"
266   :returning :short)              ; RETCODE_SQL_API
267
268 (def-function "SQLGetConnectOption"
269     ((hdbc sql-handle)          ; HDBC        hdbc
270      (fOption :short)           ; UWORD       fOption
271      (pvParam :pointer-void)             ; PTR         pvParam
272      )
273   :module "odbc"
274   :returning :short)              ; RETCODE_SQL_API
275
276 (def-function "SQLSetConnectOption"
277     ((hdbc sql-handle)          ; HDBC        hdbc
278      (fOption :short)           ; UWORD       fOption
279      (vParam :int)             ; UDWORD      vParam
280      )
281   :module "odbc"
282   :returning :short)              ; RETCODE_SQL_API
283
284 (def-function "SQLSetPos"
285     ((hstmt sql-handle)         ; HSTMT       hstmt
286      (irow :short)              ; UWORD       irow
287      (fOption :short)           ; UWORD       fOption
288      (fLock :short)             ; UWORD       fLock
289      )
290   :module "odbc"
291   :returning :short)              ; RETCODE_SQL_API
292
293                                         ; level 2
294 (def-function "SQLExtendedFetch"
295     ((hstmt sql-handle)         ; HSTMT       hstmt
296      (fFetchType :short)        ; UWORD       fFetchType
297      (irow :int)               ; SDWORD      irow
298      (*pcrow :pointer-void)              ; UDWORD FAR *pcrow
299      (*rgfRowStatus :pointer-void)       ; UWORD  FAR *rgfRowStatus
300      )
301   :module "odbc"
302   :returning :short)              ; RETCODE_SQL_API
303
304 (def-function "SQLDataSources"
305     ((henv sql-handle)          ; HENV        henv
306      (fDirection :short)
307      (*szDSN string-ptr)        ; UCHAR  FAR *szDSN
308      (cbDSNMax :short)          ; SWORD       cbDSNMax
309      (*pcbDSN (* :short))             ; SWORD      *pcbDSN
310      (*szDescription string-ptr) ; UCHAR     *szDescription
311      (cbDescriptionMax :short)  ; SWORD       cbDescriptionMax
312      (*pcbDescription (* :short))     ; SWORD      *pcbDescription
313      )
314   :module "odbc"
315   :returning :short)              ; RETCODE_SQL_API
316
317 (def-function "SQLFreeEnv"
318     ((henv sql-handle)          ; HSTMT       hstmt
319      )
320   :module "odbc"
321   :returning :short)              ; RETCODE_SQL_API
322
323
324 ;;; foreign type definitions
325
326 ;;(defmacro %sql-len-data-at-exec (length) 
327 ;;  `(- $SQL_LEN_DATA_AT_EXEC_OFFSET ,length))
328
329
330 (def-struct sql-c-time 
331     (hour   :short)
332   (minute :short)
333   (second :short))
334
335 (def-struct sql-c-date
336     (year  :short)
337   (month :short)
338   (day   :short))
339   
340 (def-struct sql-c-timestamp
341     (year     :short)
342   (month    :short)
343   (day      :short)
344   (hour     :short)
345   (minute   :short)
346   (second   :short)
347   (fraction :int))
348
349 ;;; Added by KMR
350
351 (def-function "SQLSetEnvAttr"
352     ((henv sql-handle)          ; HENV        henv
353      (attr :int)
354      (*value :pointer-void)
355      (szLength :int))
356   :module "odbc"
357   :returning :int)
358
359 (def-function "SQLTables"
360     ((hstmt :pointer-void)
361      (catalog-name :pointer-void)
362      (catalog-name-length :short)
363      (schema-name :pointer-void)
364      (schema-name-length :short)
365      (table-name :pointer-void)
366      (table-name-length :short)
367      (table-type-name :pointer-void)
368      (table-type-name-length :short))
369   :returning :short)
370
371
372 (def-function "SQLStatistics"
373     ((hstmt :pointer-void)
374      (catalog-name :pointer-void)
375      (catalog-name-length :short)
376      (schema-name :pointer-void)
377      (schema-name-length :short)
378      (table-name :cstring)
379      (table-name-length :short)
380      (unique :short)
381      (reserved :short))
382   :returning :short)
383
384