I am trying to develop a go driver for IBM (db2) database.
For windows the API definition is : -
(i will load the dll and use the functions)
func SQLColAttribute(statementHandle SQLHSTMT, ColumnNumber SQLUSMALLINT, FieldIdentifier SQLUSMALLINT, CharacterAttributePtr SQLPOINTER, BufferLength SQLSMALLINT, StringLengthPtr *SQLSMALLINT, NumericAttributePtr *SQLLEN) (ret SQLRETURN) {
r0, _, _ := syscall.Syscall9(procSQLColAttribute.Addr(), 7, uintptr(statementHandle), uintptr(ColumnNumber), uintptr(FieldIdentifier), uintptr(CharacterAttributePtr), uintptr(BufferLength), uintptr(unsafe.Pointer(StringLengthPtr)), uintptr(unsafe.Pointer(NumericAttributePtr)), 0, 0)
ret = SQLRETURN(r0)
return
}
For other platforms it is : -
(using cgo that means i will include the sqlcli.h file and then import C)
func SQLColAttribute(statementHandle SQLHSTMT, ColumnNumber SQLUSMALLINT, FieldIdentifier SQLUSMALLINT, CharacterAttributePtr SQLPOINTER, BufferLength SQLSMALLINT, StringLengthPtr *SQLSMALLINT, NumericAttributePtr SQLPOINTER) (ret SQLRETURN) {
r := C.SQLColAttribute(C.SQLHSTMT(statementHandle),C.SQLUSMALLINT(ColumnNumber),C.SQLUSMALLINT(FieldIdentifier),C.SQLPOINTER(CharacterAttributePtr),C.SQLSMALLINT(BufferLength),(*C.SQLSMALLINT)(StringLengthPtr),(C.SQLPOINTER)(NumericAttributePtr))
return SQLRETURN(r)
}
My Code of using the functions:-
func (r *Rows) ColumnTypeLength(index int) (length int64, ok bool) {
if runtime.GOOS == "windows" {
ret := api.SQLColAttribute(r.os.h, api.SQLUSMALLINT(index+1), api.SQL_DESC_LENGTH, api.SQLPOINTER(unsafe.Pointer(nil)), 0, (*api.SQLSMALLINT)(nil), (*api.SQLLEN)(&length)))
if IsError(ret) {
fmt.Println(ret)
return 0, false
}
return length, true
} else {
ret := api.SQLColAttribute(r.os.h, api.SQLUSMALLINT(index+1), api.SQL_DESC_LENGTH, api.SQLPOINTER(unsafe.Pointer(nil)), 0, (*api.SQLSMALLINT)(nil), api.SQLPOINTER(unsafe.Pointer(&length)))
if IsError(ret) {
fmt.Println(ret)
return 0, false
}
return length, true
}
}
Error:
when i run the code in windows it produces that
# github.com/ibmdb/go_ibm_db
C:\Users\rakhil\go\src\github.com\ibmdb\go_ibm_db\rows.go:55:165: cannot use api.SQLPOINTER(unsafe.Pointer(&length)) (type api.SQLPOINTER) as type *api.SQLLEN in argument to api.SQLColAttribute
when i run the code in linux it produces that
cannot convert &length (type *int64) to type *api.SQLLEN
why is that go compiling the code without checking the if condition or else is there any other way to run the code.
Thanks.