;;;; -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: odbc -*-
;;;; *************************************************************************
;;;; FILE IDENTIFICATION
;;;;
  Name :      
  Purpose :   Function definitions for UFFI interface to ODBC
  Author :      
;;;;
  This file , part of CLSQL , is Copyright ( c ) 2004 by   
  and Copyright ( C )   1999 - 2001 . All rights reserved .
;;;;
  CLSQL users are granted the rights to distribute and use this software
  as governed by the terms of the Lisp Lesser GNU Public License
;;;; (), also known as the LLGPL.
;;;; *************************************************************************

(in-package #:odbc)

(def-foreign-type sql-handle :pointer-void)
(def-foreign-type sql-handle-ptr (* sql-handle))
(def-foreign-type string-ptr (* :unsigned-char))
(def-type long-ptr-type (* #.$ODBC-LONG-TYPE))

;; odbc v3
(def-function "SQLAllocHandle"
    ((handle-type :short)
     (input-handle sql-handle)
     (*phenv sql-handle-ptr))
  :module "odbc"
  :returning :short)

;; deprecated
(def-function "SQLAllocEnv"
  HENV    FAR * phenv
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

;; deprecated
(def-function "SQLAllocConnect"
    ((henv sql-handle)          ; HENV        henv
  HDBC    FAR * phdbc
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLConnect"
  HDBC         hdbc
  UCHAR   FAR * szDSN
     (cbDSN :short)             ; SWORD       cbDSN
  UCHAR   FAR * szUID
     (cbUID :short)             ; SWORD       cbUID
  UCHAR   FAR * szAuthStr
     (cbAuthStr :short)         ; SWORD       cbAuthStr
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLDriverConnect"
  HDBC         hdbc
  SQLHWND      
  UCHAR   FAR * szConnStrIn
  SWORD        cbConnStrIn
  UCHAR   FAR * szConnStrOut
     (cbConnStrOutMax :short)   ; SWORD       cbConnStrOutMax
     (*pcbConnStrOut :pointer-void)      ; SWORD  FAR *pcbConnStrOut
  UWORD        fDriverCompletion
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLDisconnect"
  HDBC         hdbc
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

;; deprecated
(def-function "SQLAllocStmt"
  HDBC         hdbc
  HSTMT   FAR * phstmt
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLGetInfo"
  HDBC         hdbc
  UWORD        fInfoType
  PTR          rgbInfoValue
     (cbInfoValueMax :short)    ; SWORD       cbInfoValueMax
     (*pcbInfoValue :pointer-void)       ; SWORD  FAR *pcbInfoValue
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLPrepare"
  HSTMT        hstmt
  UCHAR   FAR * szSqlStr
     (cbSqlStr :int)           ; SDWORD      cbSqlStr
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLExecute"
  HSTMT        hstmt
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLExecDirect"
  HSTMT        hstmt
  UCHAR   FAR * szSqlStr
     (cbSqlStr :int)           ; SDWORD      cbSqlStr
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLFreeStmt"
  HSTMT        hstmt
  UWORD        fOption
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

  (def-function "SQLCancel"
  HSTMT        hstmt
       )
    :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLError"
    ((henv sql-handle)          ; HENV        henv
  HDBC         hdbc
  HSTMT        hstmt
  UCHAR   FAR * szSqlState
     (*pfNativeError (* :int))      ; SDWORD FAR *pfNativeError
  UCHAR   FAR * szErrorMsg
  SWORD        
     (*pcbErrorMsg (* :short))        ; SWORD  FAR *pcbErrorMsg
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLNumResultCols"
  HSTMT        hstmt
     (*pccol (* :short))              ; SWORD  FAR *pccol
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLRowCount"
  HSTMT        hstmt
  SDWORD FAR * pcrow
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLDescribeCol"
  HSTMT        hstmt
  UWORD        icol
  UCHAR   FAR * szColName
     (cbColNameMax :short)      ; SWORD       cbColNameMax
     (*pcbColName (* :short))         ; SWORD  FAR *pcbColName
     (*pfSqlType (* :short))          ; SWORD  FAR *pfSqlType
  UDWORD FAR * pcbColDef
     (*pibScale (* :short))           ; SWORD  FAR *pibScale
     (*pfNullable (* :short))         ; SWORD  FAR *pfNullable
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLColAttributes"
  HSTMT        hstmt
  UWORD        icol
  UWORD        fDescType
  PTR          rgbDesc
     (cbDescMax :short)         ; SWORD       cbDescMax
     (*pcbDesc (* :short))            ; SWORD  FAR *pcbDesc
  SDWORD FAR * pfDesc
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLColumns"
  HSTMT        hstmt
  UCHAR   FAR * szTableQualifier
     (cbTableQualifier :short)      ; SWORD       cbTableQualifier
  UCHAR   FAR * szTableOwner
     (cbTableOwner :short)          ; SWORD       cbTableOwner
  UCHAR   FAR * szTableName
     (cbTableName :short)           ; SWORD       cbTableName
  UCHAR   FAR * szColumnName
     (cbColumnName :short)          ; SWORD       cbColumnName
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLBindCol"
  HSTMT        hstmt
  UWORD        icol
     (fCType :short)            ; SWORD       fCType
  PTR          rgbValue
     (cbValueMax :int)         ; SDWORD      cbValueMax
     (*pcbValue (* :int))           ; SDWORD FAR *pcbValue
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLFetch"
  HSTMT        hstmt
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLTransact"
    ((henv sql-handle)          ; HENV        henv
  HDBC         hdbc
  UWORD         ( $ SQL_COMMIT or $ SQL_ROLLBACK )
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

;; ODBC 2.0
(def-function "SQLDescribeParam"
  HSTMT        hstmt
  UWORD        
     (*pfSqlType (* :short))          ; SWORD  FAR *pfSqlType
  UDWORD FAR * pcbColDef
     (*pibScale (* :short))           ; SWORD  FAR *pibScale
     (*pfNullable (* :short))         ; SWORD  FAR *pfNullable
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

;; ODBC 2.0
(def-function "SQLBindParameter"
  HSTMT        hstmt
  UWORD        
  SWORD        fParamType
     (fCType :short)            ; SWORD       fCType
  SWORD        fSqlType
  UDWORD       cbColDef
     (ibScale :short)           ; SWORD       ibScale
  PTR          rgbValue
     (cbValueMax :int)         ; SDWORD      cbValueMax
     (*pcbValue :pointer-void)           ; SDWORD FAR *pcbValue
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

  level 1
(def-function "SQLGetData"
  HSTMT        hstmt
  UWORD        icol
     (fCType :short)            ; SWORD       fCType
  PTR          rgbValue
     (cbValueMax :int)         ; SDWORD      cbValueMax
     (*pcbValue :pointer-void)           ; SDWORD FAR *pcbValue
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLParamData"
  HSTMT        hstmt
  PTR     FAR * prgbValue
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLPutData"
  HSTMT        hstmt
  PTR          rgbValue
     (cbValue :int)            ; SDWORD      cbValue
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLGetConnectOption"
  HDBC         hdbc
  UWORD        fOption
  PTR          pvParam
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLSetConnectOption"
  HDBC         hdbc
  UWORD        fOption
  UDWORD       vParam
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLSetPos"
  HSTMT        hstmt
  UWORD        irow
  UWORD        fOption
  UWORD        
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

  level 2
(def-function "SQLExtendedFetch"
  HSTMT        hstmt
  UWORD        fFetchType
  SDWORD       irow
  UDWORD FAR * pcrow
  UWORD   FAR * rgfRowStatus
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLDataSources"
    ((henv sql-handle)          ; HENV        henv
     (fDirection :short)
  UCHAR   FAR * szDSN
     (cbDSNMax :short)          ; SWORD       cbDSNMax
     (*pcbDSN (* :short))             ; SWORD      *pcbDSN
  UCHAR      * szDescription
     (cbDescriptionMax :short)  ; SWORD       cbDescriptionMax
  SWORD       * pcbDescription
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API

(def-function "SQLFreeEnv"
  HSTMT        hstmt
     )
  :module "odbc"
  :returning :short)              ; RETCODE_SQL_API


;;; foreign type definitions

(   sql - len - data - at - exec ( length )
   ` ( - $ SQL_LEN_DATA_AT_EXEC_OFFSET , length ) )


(def-struct sql-c-time
    (hour   :short)
  (minute :short)
  (second :short))

(def-struct sql-c-date
    (year  :short)
  (month :short)
  (day   :short))

(def-struct sql-c-timestamp
    (year     :short)
  (month    :short)
  (day      :short)
  (hour     :short)
  (minute   :short)
  (second   :short)
  (fraction :int))

  Added by KMR

(def-function "SQLSetEnvAttr"
    ((henv sql-handle)          ; HENV        henv
     (attr :int)
     (*value :pointer-void)
     (szLength :int))
  :module "odbc"
  :returning :short)

(def-function "SQLGetEnvAttr"
    ((henv sql-handle)          ; HENV        henv
     (attr :int)
     (*value :pointer-void)
     (szLength :int)
     (string-length-ptr (* :int)))
  :module "odbc"
  :returning :short)

(def-function "SQLTables"
    ((hstmt :pointer-void)
     (catalog-name :pointer-void)
     (catalog-name-length :short)
     (schema-name :pointer-void)
     (schema-name-length :short)
     (table-name :pointer-void)
     (table-name-length :short)
     (table-type-name :pointer-void)
     (table-type-name-length :short))
  :module "odbc"
  :returning :short)


(def-function "SQLStatistics"
    ((hstmt :pointer-void)
     (catalog-name :pointer-void)
     (catalog-name-length :short)
     (schema-name :pointer-void)
     (schema-name-length :short)
     (table-name :cstring)
     (table-name-length :short)
     (unique :short)
     (reserved :short))
  :module "odbc"
  :returning :short)



 -*- Mode: LISP; Syntax: ANSI-Common-Lisp; Package: odbc -*-
 *************************************************************************
 FILE IDENTIFICATION



 (), also known as the LLGPL.
 *************************************************************************
 odbc v3
 deprecated
 RETCODE_SQL_API
 deprecated
 HENV        henv
 RETCODE_SQL_API
 SWORD       cbDSN
 SWORD       cbUID
 SWORD       cbAuthStr
 RETCODE_SQL_API
 SWORD       cbConnStrOutMax
 SWORD  FAR *pcbConnStrOut
 RETCODE_SQL_API
 RETCODE_SQL_API
 deprecated
 RETCODE_SQL_API
 SWORD       cbInfoValueMax
 SWORD  FAR *pcbInfoValue
 RETCODE_SQL_API
 SDWORD      cbSqlStr
 RETCODE_SQL_API
 RETCODE_SQL_API
 SDWORD      cbSqlStr
 RETCODE_SQL_API
 RETCODE_SQL_API
 RETCODE_SQL_API
 HENV        henv
 SDWORD FAR *pfNativeError
 SWORD  FAR *pcbErrorMsg
 RETCODE_SQL_API
 SWORD  FAR *pccol
 RETCODE_SQL_API
 RETCODE_SQL_API
 SWORD       cbColNameMax
 SWORD  FAR *pcbColName
 SWORD  FAR *pfSqlType
 SWORD  FAR *pibScale
 SWORD  FAR *pfNullable
 RETCODE_SQL_API
 SWORD       cbDescMax
 SWORD  FAR *pcbDesc
 RETCODE_SQL_API
 SWORD       cbTableQualifier
 SWORD       cbTableOwner
 SWORD       cbTableName
 SWORD       cbColumnName
 RETCODE_SQL_API
 SWORD       fCType
 SDWORD      cbValueMax
 SDWORD FAR *pcbValue
 RETCODE_SQL_API
 RETCODE_SQL_API
 HENV        henv
 RETCODE_SQL_API
 ODBC 2.0
 SWORD  FAR *pfSqlType
 SWORD  FAR *pibScale
 SWORD  FAR *pfNullable
 RETCODE_SQL_API
 ODBC 2.0
 SWORD       fCType
 SWORD       ibScale
 SDWORD      cbValueMax
 SDWORD FAR *pcbValue
 RETCODE_SQL_API
 SWORD       fCType
 SDWORD      cbValueMax
 SDWORD FAR *pcbValue
 RETCODE_SQL_API
 RETCODE_SQL_API
 SDWORD      cbValue
 RETCODE_SQL_API
 RETCODE_SQL_API
 RETCODE_SQL_API
 RETCODE_SQL_API
 RETCODE_SQL_API
 HENV        henv
 SWORD       cbDSNMax
 SWORD      *pcbDSN
 SWORD       cbDescriptionMax
 RETCODE_SQL_API
 RETCODE_SQL_API
 foreign type definitions
 HENV        henv
 HENV        henv
  Name :      
  Purpose :   Function definitions for UFFI interface to ODBC
  Author :      
  This file , part of CLSQL , is Copyright ( c ) 2004 by   
  and Copyright ( C )   1999 - 2001 . All rights reserved .
  CLSQL users are granted the rights to distribute and use this software
  as governed by the terms of the Lisp Lesser GNU Public License

(in-package #:odbc)

(def-foreign-type sql-handle :pointer-void)
(def-foreign-type sql-handle-ptr (* sql-handle))
(def-foreign-type string-ptr (* :unsigned-char))
(def-type long-ptr-type (* #.$ODBC-LONG-TYPE))

(def-function "SQLAllocHandle"
    ((handle-type :short)
     (input-handle sql-handle)
     (*phenv sql-handle-ptr))
  :module "odbc"
  :returning :short)

(def-function "SQLAllocEnv"
  HENV    FAR * phenv
     )
  :module "odbc"

(def-function "SQLAllocConnect"
  HDBC    FAR * phdbc
     )
  :module "odbc"

(def-function "SQLConnect"
  HDBC         hdbc
  UCHAR   FAR * szDSN
  UCHAR   FAR * szUID
  UCHAR   FAR * szAuthStr
     )
  :module "odbc"

(def-function "SQLDriverConnect"
  HDBC         hdbc
  SQLHWND      
  UCHAR   FAR * szConnStrIn
  SWORD        cbConnStrIn
  UCHAR   FAR * szConnStrOut
  UWORD        fDriverCompletion
     )
  :module "odbc"

(def-function "SQLDisconnect"
  HDBC         hdbc
  :module "odbc"

(def-function "SQLAllocStmt"
  HDBC         hdbc
  HSTMT   FAR * phstmt
     )
  :module "odbc"

(def-function "SQLGetInfo"
  HDBC         hdbc
  UWORD        fInfoType
  PTR          rgbInfoValue
     )
  :module "odbc"

(def-function "SQLPrepare"
  HSTMT        hstmt
  UCHAR   FAR * szSqlStr
     )
  :module "odbc"

(def-function "SQLExecute"
  HSTMT        hstmt
     )
  :module "odbc"

(def-function "SQLExecDirect"
  HSTMT        hstmt
  UCHAR   FAR * szSqlStr
     )
  :module "odbc"

(def-function "SQLFreeStmt"
  HSTMT        hstmt
  UWORD        fOption
  :module "odbc"

  (def-function "SQLCancel"
  HSTMT        hstmt
       )
    :module "odbc"

(def-function "SQLError"
  HDBC         hdbc
  HSTMT        hstmt
  UCHAR   FAR * szSqlState
  UCHAR   FAR * szErrorMsg
  SWORD        
     )
  :module "odbc"

(def-function "SQLNumResultCols"
  HSTMT        hstmt
     )
  :module "odbc"

(def-function "SQLRowCount"
  HSTMT        hstmt
  SDWORD FAR * pcrow
     )
  :module "odbc"

(def-function "SQLDescribeCol"
  HSTMT        hstmt
  UWORD        icol
  UCHAR   FAR * szColName
  UDWORD FAR * pcbColDef
     )
  :module "odbc"

(def-function "SQLColAttributes"
  HSTMT        hstmt
  UWORD        icol
  UWORD        fDescType
  PTR          rgbDesc
  SDWORD FAR * pfDesc
     )
  :module "odbc"

(def-function "SQLColumns"
  HSTMT        hstmt
  UCHAR   FAR * szTableQualifier
  UCHAR   FAR * szTableOwner
  UCHAR   FAR * szTableName
  UCHAR   FAR * szColumnName
     )
  :module "odbc"

(def-function "SQLBindCol"
  HSTMT        hstmt
  UWORD        icol
  PTR          rgbValue
     )
  :module "odbc"

(def-function "SQLFetch"
  HSTMT        hstmt
     )
  :module "odbc"

(def-function "SQLTransact"
  HDBC         hdbc
  UWORD         ( $ SQL_COMMIT or $ SQL_ROLLBACK )
     )
  :module "odbc"

(def-function "SQLDescribeParam"
  HSTMT        hstmt
  UWORD        
  UDWORD FAR * pcbColDef
     )
  :module "odbc"

(def-function "SQLBindParameter"
  HSTMT        hstmt
  UWORD        
  SWORD        fParamType
  SWORD        fSqlType
  UDWORD       cbColDef
  PTR          rgbValue
     )
  :module "odbc"

  level 1
(def-function "SQLGetData"
  HSTMT        hstmt
  UWORD        icol
  PTR          rgbValue
     )
  :module "odbc"

(def-function "SQLParamData"
  HSTMT        hstmt
  PTR     FAR * prgbValue
     )
  :module "odbc"

(def-function "SQLPutData"
  HSTMT        hstmt
  PTR          rgbValue
     )
  :module "odbc"

(def-function "SQLGetConnectOption"
  HDBC         hdbc
  UWORD        fOption
  PTR          pvParam
     )
  :module "odbc"

(def-function "SQLSetConnectOption"
  HDBC         hdbc
  UWORD        fOption
  UDWORD       vParam
     )
  :module "odbc"

(def-function "SQLSetPos"
  HSTMT        hstmt
  UWORD        irow
  UWORD        fOption
  UWORD        
     )
  :module "odbc"

  level 2
(def-function "SQLExtendedFetch"
  HSTMT        hstmt
  UWORD        fFetchType
  SDWORD       irow
  UDWORD FAR * pcrow
  UWORD   FAR * rgfRowStatus
     )
  :module "odbc"

(def-function "SQLDataSources"
     (fDirection :short)
  UCHAR   FAR * szDSN
  UCHAR      * szDescription
  SWORD       * pcbDescription
     )
  :module "odbc"

(def-function "SQLFreeEnv"
  HSTMT        hstmt
     )
  :module "odbc"



(   sql - len - data - at - exec ( length )
   ` ( - $ SQL_LEN_DATA_AT_EXEC_OFFSET , length ) )


(def-struct sql-c-time
    (hour   :short)
  (minute :short)
  (second :short))

(def-struct sql-c-date
    (year  :short)
  (month :short)
  (day   :short))

(def-struct sql-c-timestamp
    (year     :short)
  (month    :short)
  (day      :short)
  (hour     :short)
  (minute   :short)
  (second   :short)
  (fraction :int))

  Added by KMR

(def-function "SQLSetEnvAttr"
     (attr :int)
     (*value :pointer-void)
     (szLength :int))
  :module "odbc"
  :returning :short)

(def-function "SQLGetEnvAttr"
     (attr :int)
     (*value :pointer-void)
     (szLength :int)
     (string-length-ptr (* :int)))
  :module "odbc"
  :returning :short)

(def-function "SQLTables"
    ((hstmt :pointer-void)
     (catalog-name :pointer-void)
     (catalog-name-length :short)
     (schema-name :pointer-void)
     (schema-name-length :short)
     (table-name :pointer-void)
     (table-name-length :short)
     (table-type-name :pointer-void)
     (table-type-name-length :short))
  :module "odbc"
  :returning :short)


(def-function "SQLStatistics"
    ((hstmt :pointer-void)
     (catalog-name :pointer-void)
     (catalog-name-length :short)
     (schema-name :pointer-void)
     (schema-name-length :short)
     (table-name :cstring)
     (table-name-length :short)
     (unique :short)
     (reserved :short))
  :module "odbc"
  :returning :short)


