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