Git Product home page Git Product logo

go-odbc's Introduction

ODBC database driver for Go

Install:
	cd $GOPATH/src
	git clone git://github.com/weigj/go-odbc.git odbc
	cd odbc
	go install

Example:

package main

import (
	"odbc"
)

func main() {
	conn, _ := odbc.Connect("DSN=dsn;UID=user;PWD=password")
	stmt, _ := conn.Prepare("select * from user where username = ?")
	stmt.Execute("admin")
	rows, _ := stmt.FetchAll()
	for i, row := range rows {
		println(i, row)
	}
	stmt.Close()
	conn.Close()
}

Tested on:
	SQL Server 2005 and Windows 7 
	SQL Server 2005 and Ubuntu 10.4 (UnixODBC+FreeTDS)
	Oracle 10g and Windows 7

go-odbc's People

Contributors

weigj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-odbc's Issues

查询MSSQL GUID截断问题

conn, _ := odbc.Connect(connstr)
    sql := "select CLASS_ID,CLASS_NAME from COMM_RES_CLASS where CLASS_PID='" + media_guid + "' order by CREATE_TIME"
    stmt, _ := conn.Prepare(sql)
    stmt.Execute()
    rows, _ := stmt.FetchAll()
    stmt.Close()
    for _, row := range rows {
        fmt.Println(row.GetString(0))
    }

    conn.Close()

结果中的36位guid编码被截断了,请问如何处理?

Help Compile to Windows

Hi might not be an issue but a help ... trying to compile yours to port to Windows and below error....
How can i solve it ?
Thanks

GOOS=windows GOARCH=amd64 go build -o

command-line-arguments
src/github.com/helkyd/ub_zkaccess1/ub_zkaccess1.go:11: undefined: odbc.Connect

This because the Access DB i need is on a Win PC and to access it i need to use DSN

Can't build 32-bit

Trying to cross compile on a 64 bit windows machine to 32 bit. I'm using the tdm-gcc 32 bit compiler.

So my env is:
GOARCH=386
CGO_LDFLAGS=-lodbc32

When I try to build it gives me an error that I don't get when I compile for 64 bit:
main.go:16: undefined: odbc.Connect

Default case in GetField raises makeslice: len out of range

I'm working with the example to the letter, with the exception of odbc.Debug = true.

Debug dumps quite a few rows successfully using default type but then it reaches one where the field length is for some reason set to -1

default type [0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0] -1

panic: runtime error: makeslice: len out of range

Is there a way to more gracefully handle such a thing? It comes from calling FetchAll() or FetchOne() but neither of those return any sort of error. Or is this a bug?

Not go gettable

Can the driver import the fully qualified package rather than just relying on the user having installed the package locally?

See the last import in https://github.com/AllanCochrane/go-odbc/blob/master/driver/sql.go

If this change is made then the package is go gettable and the driver portion can be used with the standard database/sql package.

To explain, if I import "github.com/weigj/go-odbc/driver" then the sql.Open("odbc",...) call gives an error:

import "odbc": cannot find package

However the small change in my fork works OK.

As an aside, this is the only ODBC package that seems to work correctly with the Vertica ODBC drivers!

golint

I was going to use this package after follow the instructions with no success. I try to figure out how the code work this is the result i get with golint

odbc.go:54:2: don't use ALL_CAPS in Go names; use CamelCase
odbc.go:54:2: exported const BUFFER_SIZE should have comment (or a comment on this block) or be unexported
odbc.go:55:2: don't use ALL_CAPS in Go names; use CamelCase
odbc.go:59:2: exported var Genv should have comment or be unexported
odbc.go:62:6: exported type Connection should have comment or be unexported
odbc.go:67:6: exported type Statement should have comment or be unexported
odbc.go:75:6: exported type ODBCError should have comment or be unexported
odbc.go:75:6: type name will be used as odbc.ODBCError by other packages, and that stutters; consider calling this Error
odbc.go:106:1: exported function Connect should have comment or be unexported
odbc.go:134:1: exported method Connection.ExecDirect should have comment or be unexported
odbc.go:160:1: exported method Connection.Prepare should have comment or be unexported
odbc.go:176:1: exported method Connection.Commit should have comment or be unexported
odbc.go:184:1: exported method Connection.AutoCommit should have comment or be unexported
odbc.go:198:1: exported method Connection.BeginTransaction should have comment or be unexported
odbc.go:206:1: exported method Connection.Rollback should have comment or be unexported
odbc.go:214:1: exported method Connection.ServerInfo should have comment or be unexported
odbc.go:215:6: don't use underscores in Go names; var info_len should be infoLen
odbc.go:238:1: exported method Connection.ClientInfo should have comment or be unexported
odbc.go:239:6: don't use underscores in Go names; var info_len should be infoLen
odbc.go:246:2: don't use underscores in Go names; var drv_name should be drvName
odbc.go:252:2: don't use underscores in Go names; var drv_odbc_ver should be drvOdbcVer
odbc.go:258:2: don't use underscores in Go names; var drv_ver should be drvVer
odbc.go:262:1: exported method Connection.Close should have comment or be unexported
odbc.go:279:1: exported method Statement.RowsAffected should have comment or be unexported
odbc.go:289:1: exported method Statement.Cancel should have comment or be unexported
odbc.go:298:1: exported method Statement.NumParams should have comment or be unexported
odbc.go:307:1: exported method Statement.Execute should have comment or be unexported
odbc.go:333:1: exported method Statement.Execute2 should have comment or be unexported
odbc.go:359:1: exported method Statement.Fetch should have comment or be unexported
odbc.go:371:6: exported type Row should have comment or be unexported
odbc.go:375:1: comment on exported method Row.Get should be of the form "Get ..."
odbc.go:391:1: exported method Row.GetInt should have comment or be unexported
odbc.go:403:1: exported method Row.GetFloat should have comment or be unexported
odbc.go:413:1: exported method Row.GetString should have comment or be unexported
odbc.go:423:1: exported method Row.Length should have comment or be unexported
odbc.go:427:1: exported method Statement.FetchAll should have comment or be unexported
odbc.go:439:1: exported method Statement.FetchOne should have comment or be unexported
odbc.go:454:1: exported method Statement.FetchOne2 should have comment or be unexported
odbc.go:469:1: exported method Statement.GetField should have comment or be unexported
odbc.go:469:33: don't use underscores in Go names; method parameter field_index should be fieldIndex
odbc.go:470:6: don't use underscores in Go names; var field_type should be fieldType
odbc.go:471:6: don't use underscores in Go names; var field_len should be fieldLen
odbc.go:549:1: exported method Statement.NumFields should have comment or be unexported
odbc.go:559:1: exported method Statement.GetParamType should have comment or be unexported
odbc.go:560:6: don't use underscores in Go names; var data_type should be dataType
odbc.go:560:17: don't use underscores in Go names; var dec_ptr should be decPtr
odbc.go:560:26: don't use underscores in Go names; var null_ptr should be nullPtr
odbc.go:561:6: don't use underscores in Go names; var size_ptr should be sizePtr
odbc.go:570:1: exported method Statement.BindParam should have comment or be unexported
odbc.go:577:6: don't use underscores in Go names; var StrLen_or_IndPt should be StrLenOrIndPt
odbc.go:653:1: exported method Statement.NextResult should have comment or be unexported
odbc.go:661:1: exported method Statement.NumRows should have comment or be unexported
odbc.go:671:1: exported method Statement.HasRows should have comment or be unexported
odbc.go:676:6: exported type Field should have comment or be unexported
odbc.go:684:1: exported method Statement.FieldMetadata should have comment or be unexported
odbc.go:713:1: exported method Statement.Close should have comment or be unexported
odbc.go:717:1: exported function Success should have comment or be unexported
odbc.go:721:1: exported function FormatError should have comment or be unexported

Install failed in Linux

display missing sql.h when installing. would you please advise? Thanks.

Message:

localhost odbc # go install

odbc

odbc.go:18:17: fatal error: sql.h: No such file or directory
compilation terminated.

Unable to insert int values into tables

BindParam doesnt binding int value. and Execute throws error saying COUNT error.
When I club lines 607 and 608 to a single line in odbc.go, it works fine.

Connect odbc vs tibero

I runing but error. Please support
Not connect ODBC: I [unixODBC]
signal 11 received but handler not on signal stack
fatal error: non-Go code set up signal handler without SA_ONSTACK flag

runtime stack:
runtime: unexpected return pc for runtime.sigtramp called from 0x7f367a9f4df3
stack: frame={sp:0xc00005b750, fp:0xc00005b7a0} stack=[0xc000053648,0xc00005ba48)
0x000000c00005b650: 0x000000c00005b670 0x000000c00005b6b0
0x000000c00005b660: 0x0000000000000000 0x0000000000000000
0x000000c00005b670: 0x000000c000002000 0x0000000000000000
0x000000c00005b680: 0x0000000000008000 0x0000000000000000
0x000000c00005b690: 0x0000000000000000 0x0000000000000000
0x000000c00005b6a0: 0x000000c00005b718 0x0000000000445345 <runtime.sigtrampgo+0x0000000000000125>
0x000000c00005b6b0: 0x000000c00000000b 0x0000000000745f80
0x000000c00005b6c0: 0x000000c00005b6d8 0x000000c00005bfd0
0x000000c00005b6d0: 0x0000000000000006 0x0000000000000000
0x000000c00005b6e0: 0x0000000000000000 0x0000000000000000
0x000000c00005b6f0: 0x0000000000000000 0x0000000000000000
0x000000c00005b700: 0x000000c0000001a0 0x000000c00005b5c1
0x000000c00005b710: 0x000000000000000b 0x000000c00005b740
0x000000c00005b720: 0x00000000004609ee <runtime.sigtrampgo+0x000000000000002e> 0x000000020000000b
0x000000c00005b730: 0x000000c00005b5c1 0x000000000000000b
0x000000c00005b740: 0x000000c00005b790 0x000000000045ff1d <runtime.sigtramp+0x000000000000003d>
0x000000c00005b750: <0x000000000000000b 0x000000c00005b5c1
0x000000c00005b760: 0x000000000000000b 0x00007f367bfffe18
0x000000c00005b770: 0x000000000000000b 0x00000000004b0f68
0x000000c00005b780: 0x000000c0000001a0 0x00007f36947f6913
0x000000c00005b790: 0x000000c00005b7b0 !0x00007f367a9f4df3
0x000000c00005b7a0: >0x000000c000012270 0x0000000000203000
0x000000c00005b7b0: 0x000000c00005bdb0 0x00007f36bba18980
0x000000c00005b7c0: 0x0000000000000007 0x0000000000000000
0x000000c00005b7d0: 0x000000c000002000 0x00007f3600000000
0x000000c00005b7e0: 0x0000000000008000 0x0000000000000010
0x000000c00005b7f0: 0x0000000000000010 0x00007f36bc078438
0x000000c00005b800: 0x0000000000000000 0x0000000000203000
0x000000c00005b810: 0x00000000004b0f68 0x000000c0000001a0
0x000000c00005b820: 0x00007f36947f6913 0x0000000000000000
0x000000c00005b830: 0x0000000000000001 0x000000c00005bdb0
0x000000c00005b840: 0x000000c000012270 0x0000000000000000
0x000000c00005b850: 0x0000000000000000 0x0000000000000010
0x000000c00005b860: 0x000000c00005bd80 0x000000000048de7f <odbc.(*Connection).newStmt.func1+0x000000000000001f>
0x000000c00005b870: 0x0000000000010246 0x002b000000000033
0x000000c00005b880: 0x0000000000000004 0x000000000000000e
0x000000c00005b890: 0x0000000000000000 0x0000000000000000
runtime.throw({0x4b093a, 0xc00004c000})
/usr/local/go/src/runtime/panic.go:1198 +0x71
runtime.sigNotOnStack(0xb)
/usr/local/go/src/runtime/signal_unix.go:919 +0x65
runtime.adjustSignalStack(0xb, 0x745f80, 0xc00005b6d8)
/usr/local/go/src/runtime/signal_unix.go:510 +0x296
runtime.sigtrampgo(0xb, 0xc00005b5c1, 0xb)
/usr/local/go/src/runtime/signal_unix.go:450 +0x125
runtime.sigtrampgo(0xb, 0xc00005b5c1, 0xb)
:1 +0x2e
runtime: unexpected return pc for runtime.sigtramp called from 0x7f367a9f4df3
stack: frame={sp:0xc00005b750, fp:0xc00005b7a0} stack=[0xc000053648,0xc00005ba48)
0x000000c00005b650: 0x000000c00005b670 0x000000c00005b6b0
0x000000c00005b660: 0x0000000000000000 0x0000000000000000
0x000000c00005b670: 0x000000c000002000 0x0000000000000000
0x000000c00005b680: 0x0000000000008000 0x0000000000000000
0x000000c00005b690: 0x0000000000000000 0x0000000000000000
0x000000c00005b6a0: 0x000000c00005b718 0x0000000000445345 <runtime.sigtrampgo+0x0000000000000125>
0x000000c00005b6b0: 0x000000c00000000b 0x0000000000745f80
0x000000c00005b6c0: 0x000000c00005b6d8 0x000000c00005bfd0
0x000000c00005b6d0: 0x0000000000000006 0x0000000000000000
0x000000c00005b6e0: 0x0000000000000000 0x0000000000000000
0x000000c00005b6f0: 0x0000000000000000 0x0000000000000000
0x000000c00005b700: 0x000000c0000001a0 0x000000c00005b5c1
0x000000c00005b710: 0x000000000000000b 0x000000c00005b740
0x000000c00005b720: 0x00000000004609ee <runtime.sigtrampgo+0x000000000000002e> 0x000000020000000b
0x000000c00005b730: 0x000000c00005b5c1 0x000000000000000b
0x000000c00005b740: 0x000000c00005b790 0x000000000045ff1d <runtime.sigtramp+0x000000000000003d>
0x000000c00005b750: <0x000000000000000b 0x000000c00005b5c1
0x000000c00005b760: 0x000000000000000b 0x00007f367bfffe18
0x000000c00005b770: 0x000000000000000b 0x00000000004b0f68
0x000000c00005b780: 0x000000c0000001a0 0x00007f36947f6913
0x000000c00005b790: 0x000000c00005b7b0 !0x00007f367a9f4df3
0x000000c00005b7a0: >0x000000c000012270 0x0000000000203000
0x000000c00005b7b0: 0x000000c00005bdb0 0x00007f36bba18980
0x000000c00005b7c0: 0x0000000000000007 0x0000000000000000
0x000000c00005b7d0: 0x000000c000002000 0x00007f3600000000
0x000000c00005b7e0: 0x0000000000008000 0x0000000000000010
0x000000c00005b7f0: 0x0000000000000010 0x00007f36bc078438
0x000000c00005b800: 0x0000000000000000 0x0000000000203000
0x000000c00005b810: 0x00000000004b0f68 0x000000c0000001a0
0x000000c00005b820: 0x00007f36947f6913 0x0000000000000000
0x000000c00005b830: 0x0000000000000001 0x000000c00005bdb0
0x000000c00005b840: 0x000000c000012270 0x0000000000000000
0x000000c00005b850: 0x0000000000000000 0x0000000000000010
0x000000c00005b860: 0x000000c00005bd80 0x000000000048de7f <odbc.(*Connection).newStmt.func1+0x000000000000001f>
0x000000c00005b870: 0x0000000000010246 0x002b000000000033
0x000000c00005b880: 0x0000000000000004 0x000000000000000e
0x000000c00005b890: 0x0000000000000000 0x0000000000000000
runtime.sigtramp()
/usr/local/go/src/runtime/sys_linux_amd64.s:343 +0x3d

goroutine 17 [syscall, locked to thread]:
runtime.goexit()
/usr/local/go/src/runtime/asm_amd64.s:1581 +0x1 fp=0xc00005afe8 sp=0xc00005afe0 pc=0x45e441

goroutine 1 [running]:
goroutine running on other thread; stack unavailable
exit status 2

crash

环境:
windows 7 x64 sp1 + go1.0.2 i386 + oracle 11g

错误发生位置:
82: strReturn += ""
83:
84: for rows.Next() {
85: var recdate string
86: var seqno int32

具体错误信息:
2012/10/19 13:32:31 Handler crashed with error runtime error: index out of range

2012/10/19 13:32:31 C:/Users/ADMINI1/AppData/Local/Temp/2/bindist579160455/go/s
rc/pkg/runtime/proc.c 1443
2012/10/19 13:32:31 C:/Users/ADMINI
1/AppData/Local/Temp/2/bindist579160455/go/s
rc/pkg/runtime/runtime.c 128
2012/10/19 13:32:31 C:/Users/ADMINI1/AppData/Local/Temp/2/bindist579160455/go/s
rc/pkg/runtime/runtime.c 85
2012/10/19 13:32:31 odbc.go 555
2012/10/19 13:32:31 odbc.go 472
2012/10/19 13:32:31 e:/goproject/src/odbc/driver/sql.go 148
2012/10/19 13:32:31 C:/Users/ADMINI
1/AppData/Local/Temp/2/bindist579160455/go/s
rc/pkg/database/sql/sql.go 915
2012/10/19 13:32:31 e:/goproject/src/testodbc/main.go 84

goinstall fails for go-odbc on Mac OS X Snow Leopard

I installed unixodbc, but go-odbc doesn't compile:

[ ijt issac-trotts-computer ~/Dropbox/repos/go-sqlite3 ] goinstall github.com/weigj/go-odbc
/bin/bash: === cd /Users/ijt/vs/src/github.com/weigj/go-odbc; bash gomake -f- install
CGOPKGPATH=github.com/weigj cgo -- odbc.go
touch _obj/_cgo_run
6g -I "/Users/ijt/vs/pkg/darwin_amd64" -o go.6 doc.go util.go _obj/odbc.cgo1.go _obj/_cgo_gotypes.go
odbc.go:243[_obj/odbc.cgo1.go:246]: cannot use &nor (type *_Ctypedef_SQLINTEGER) as type *_Ctypedef_SQLLEN in function argument
odbc.go:397[_obj/odbc.cgo1.go:400]: cannot use _Ctypedef_SQLPOINTER(unsafe.Pointer(&field_type)) (type _Ctypedef_SQLPOINTER) as type *_Ctypedef_SQLLEN in function argument
odbc.go:401[_obj/odbc.cgo1.go:404]: cannot use _Ctypedef_SQLPOINTER(unsafe.Pointer(&field_len)) (type _Ctypedef_SQLPOINTER) as type *_Ctypedef_SQLLEN in function argument
odbc.go:425[_obj/odbc.cgo1.go:428]: cannot use _Ctypedef_SQLINTEGER(int(field_len) + 4) (type _Ctypedef_SQLINTEGER) as type _Ctypedef_SQLLEN in function argument
odbc.go:430[_obj/odbc.cgo1.go:433]: cannot use _Ctypedef_SQLINTEGER(int(field_len) + 4) (type _Ctypedef_SQLINTEGER) as type _Ctypedef_SQLLEN in function argument
odbc.go:532[_obj/odbc.cgo1.go:535]: cannot use slen (type _Ctypedef_SQLUINTEGER) as type _Ctypedef_SQLULEN in assignment
odbc.go:533[_obj/odbc.cgo1.go:536]: cannot use _Ctypedef_SQLINTEGER(slen + 1) (type _Ctypedef_SQLINTEGER) as type _Ctypedef_SQLLEN in assignment
odbc.go:534[_obj/odbc.cgo1.go:537]: cannot use _Ctypedef_SQLINTEGER(slen) (type _Ctypedef_SQLINTEGER) as type _Ctypedef_SQLLEN in assignment

windows 7 64位下 go install 失败

你好,今天使用你的插件但是在安装插件的时候报错
PS > go install odbc

odbc

:0: sorry, unimplemented: 64-bit mode not compiled in

Minor change to get full varchar field.

Need to call SQLGetData multiple times to get long field, limit per Get is set by ODBC driver. I'm using a Windows 2003 PostgreSQL ODBC driver.

Example patch.

--- /weigj-go-odbc-514210d/odbc.go
+++ /weigj-go-odbc-514210d-2/odbc.go
@@ -453,12 +453,22 @@
case C.SQL_WCHAR, C.SQL_WVARCHAR, C.SQL_WLONGVARCHAR:
value := make([]uint16, int(field_len)+8)
ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_WCHAR, C.SQLPOINTER(unsafe.Pointer(&value[0])), C.SQLINTEGER(int(field_len)+4), &fl)

  •   s := UTF16ToString(value)
    
  •   s := ""
    
  •   for Success(ret) {
    
  •       if len(UTF16ToString(value))<=0 { break }
    
  •       s += UTF16ToString(value)
    
  •       ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_WCHAR, C.SQLPOINTER(unsafe.Pointer(&value[0])), C.SQLINTEGER(int(field_len)+4), &fl)
    
  •   }
    v = s
    
    default:
    value := make([]byte, int(fl)+2)
    ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_CHAR, C.SQLPOINTER(unsafe.Pointer(&value[0])), C.SQLINTEGER(int(field_len)+4), &fl)
  •   s := string(value[0:])
    
  •   s := ""
    
  •   for Success(ret) {
    
  •       if len(string(value[0:]))<=0 { break }
    
  •       s += string(value[0:])
    
  •       ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_CHAR, C.SQLPOINTER(unsafe.Pointer(&value[0])), C.SQLINTEGER(int(field_len)+4), &fl)
    
  •   }
    v = s
    debugPrint("default type", value, fl, s)
    
    }

Diffs to make compatible with linuxodbc & newer releases of Go

Basically run gofix on odbc.go, comment out the conversion to complex and change calls to SQLColAttribute

diff --git a/odbc.go b/odbc.go
index 0af70bf..d6bc16c 100644
--- a/odbc.go
+++ b/odbc.go
@@ -148,7 +148,7 @@ func (conn *Connection) Prepare(sql string, params ...interface{}) (*Statement,
 }

 //func (conn *Connection) Query(sql string, params ...interface{}) (stmt *Statement, err *ODBCError) {
-// //TODO 
+// //TODO
 //}

 func (conn *Connection) Commit() (err *ODBCError) {
@@ -334,12 +334,12 @@ type Row struct {
 // Get(Columnindex)
 // TODO Get(ColumnName)
 func (r *Row) Get(a interface{}) interface{} {
-   value := reflect.NewValue(a)
-   switch f := value.(type) {
-   case *reflect.IntValue:
-       return r.Data[f.Get()]
-   case *reflect.UintValue:
-       return r.Data[f.Get()]
+   value := reflect.ValueOf(a)
+   switch f := value; f.Kind() {
+   case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+       return r.Data[f.Int()]
+   case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+       return r.Data[f.Uint()]
        //  case *reflect.StringValue:
        //      i := r.Meta[f.Get()]
        //      return r.Data[i]
@@ -349,42 +349,42 @@ func (r *Row) Get(a interface{}) interface{} {

 func (r *Row) GetInt(a interface{}) (ret int64) {
    v := r.Get(a)
-   value := reflect.NewValue(v)
-   switch f := value.(type) {
-   case *reflect.IntValue:
-       ret = int64(f.Get())
-   case *reflect.UintValue:
-       ret = int64(f.Get())
+   value := reflect.ValueOf(v)
+   switch f := value; f.Kind() {
+   case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
+       ret = int64(f.Int())
+   case reflect.Uint, reflect.Uint8, reflect.Uint16, reflect.Uint32, reflect.Uint64, reflect.Uintptr:
+       ret = int64(f.Uint())
    }
    return
 }

 func (r *Row) GetFloat(a interface{}) (ret float64) {
    v := r.Get(a)
-   value := reflect.NewValue(v)
-   switch f := value.(type) {
-   case *reflect.FloatValue:
-       ret = float64(f.Get())
+   value := reflect.ValueOf(v)
+   switch f := value; f.Kind() {
+   case reflect.Float32, reflect.Float64:
+       ret = float64(f.Float())
    }
    return
 }

-func (r *Row) GetComplex(a interface{}) (ret complex) {
-   v := r.Get(a)
-   value := reflect.NewValue(v)
-   switch f := value.(type) {
-   case *reflect.ComplexValue:
-       ret = complex(f.Get())
-   }
-   return
-}
+// func (r *Row) GetComplex(a interface{}) (ret complex) {
+//     v := r.Get(a)
+//     value := reflect.NewValue(v)
+//     switch f := value.(type) {
+//     case *reflect.ComplexValue:
+//         ret = complex(f.Get())
+//     }
+//     return
+// }

 func (r *Row) GetString(a interface{}) (ret string) {
    v := r.Get(a)
-   value := reflect.NewValue(v)
-   switch f := value.(type) {
-   case *reflect.StringValue:
-       ret = f.Get()
+   value := reflect.ValueOf(v)
+   switch f := value; f.Kind() {
+   case reflect.String:
+       ret = f.String()
    }
    return
 }
@@ -421,14 +421,14 @@ func (stmt *Statement) FetchOne() (*Row, *ODBCError) {
 }

 func (stmt *Statement) GetField(field_index int) (v interface{}, ftype int, flen int, err *ODBCError) {
-   var field_type C.int
-   var field_len C.int
+   var field_type C.SQLINTEGER
+   var field_len C.SQLINTEGER
    var ll C.SQLSMALLINT
-   ret := C.SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_CONCISE_TYPE, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, C.SQLPOINTER(unsafe.Pointer(&field_type)))
+   ret := C.SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_CONCISE_TYPE, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, &field_type)
    if !Success(ret) {
        debugPrint("GetFiled type Error")
    }
-   ret = C.SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_LENGTH, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, C.SQLPOINTER(unsafe.Pointer(&field_len)))
+   ret = C.SQLColAttribute(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_DESC_LENGTH, C.SQLPOINTER(unsafe.Pointer(uintptr(0))), C.SQLSMALLINT(0), &ll, &field_len)
    if !Success(ret) {
        debugPrint("GetFiled len Error")
    }
@@ -500,7 +500,7 @@ func (stmt *Statement) BindParam(index int, param interface{}) *ODBCError {
    var ParameterValuePtr C.SQLPOINTER
    var BufferLength C.SQLLEN
    var StrLen_or_IndPt C.SQLLEN
-   v := reflect.NewValue(param)
+   v := reflect.ValueOf(param)
    if param == nil {
        ft, _, _, _, err := stmt.GetParamType(index)
        if err != nil {
@@ -514,12 +514,12 @@ func (stmt *Statement) BindParam(index int, param interface{}) *ODBCError {
        StrLen_or_IndPt = C.SQL_NULL_DATA
        ColumnSize = 1
    } else {
-       switch v := v.(type) {
-       case *reflect.BoolValue:
+       switch v.Kind() {
+       case reflect.Bool:
            ParameterType = C.SQL_BIT
            ValueType = C.SQL_C_BIT
            var b [1]byte
-           if v.Get() {
+           if v.Bool() {
                b[0] = 1
            } else {
                b[0] = 0
@@ -527,37 +527,37 @@ func (stmt *Statement) BindParam(index int, param interface{}) *ODBCError {
            ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&b[0]))
            BufferLength = 1
            StrLen_or_IndPt = 0
-       case *reflect.IntValue:
+       case reflect.Int, reflect.Int8, reflect.Int16, reflect.Int32, reflect.Int64:
            switch v.Type().Kind() {
            case reflect.Int:
            case reflect.Int8, reflect.Int16, reflect.Int32:
                ParameterType = C.SQL_INTEGER
                ValueType = C.SQL_C_LONG
-               var l C.long = C.long(v.Get())
+               var l C.long = C.long(v.Int())
                ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&l))
                BufferLength = 4
                StrLen_or_IndPt = 0
            case reflect.Int64:
                ParameterType = C.SQL_BIGINT
                ValueType = C.SQL_C_SBIGINT
-               var ll C.longlong = C.longlong(v.Get())
+               var ll C.longlong = C.longlong(v.Int())
                ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&ll))
                BufferLength = 8
                StrLen_or_IndPt = 0
            }
-       case *reflect.FloatValue:
+       case reflect.Float32, reflect.Float64:
            ParameterType = C.SQL_DOUBLE
            ValueType = C.SQL_C_DOUBLE
-           var d C.double = C.double(v.Get())
+           var d C.double = C.double(v.Float())
            ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&d))
            BufferLength = 8
            StrLen_or_IndPt = 0
-       case *reflect.ComplexValue:
-       case *reflect.StringValue:
-           var slen C.SQLUINTEGER = C.SQLUINTEGER(len(v.Get()))
+       case reflect.Complex64, reflect.Complex128:
+       case reflect.String:
+           var slen C.SQLUINTEGER = C.SQLUINTEGER(len(v.String()))
            ParameterType = C.SQL_VARCHAR
            ValueType = C.SQL_C_CHAR
-           s := []byte(v.Get())
+           s := []byte(v.String())
            ParameterValuePtr = C.SQLPOINTER(unsafe.Pointer(&s[0]))
            ColumnSize = slen
            BufferLength = C.SQLINTEGER(slen + 1)

Minor change to get SQL_INTEGER working

SQL_INTEGER wasn't working on Windows 2003 server. I found the fix was to change C.HANDLE to C.int.

Example patch.

--- /weigj-go-odbc-514210d/odbc.go
+++ /weigj-go-odbc-514210d-2/odbc.go
@@ -435,7 +435,7 @@
var fl C.SQLLEN = C.SQLLEN(field_len)
switch int(field_type) {
case C.SQL_BIT, C.SQL_INTEGER, C.SQL_SMALLINT, C.SQL_TINYINT:

  •   var value C.HANDLE
    
  •   var value C.int
    ret = C.SQLGetData(C.SQLHSTMT(stmt.handle), C.SQLUSMALLINT(field_index+1), C.SQL_C_LONG, C.SQLPOINTER(unsafe.Pointer(&value)), 0, &fl)
    if fl == -1 {
        v = nil
    

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.