Skip to content

Commit 1cfade4

Browse files
committed
use SQL_INTEGER to pass int64 parameter when possible
Fixes alexbrainman#78
1 parent b78637d commit 1cfade4

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

param.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -75,11 +75,23 @@ func (p *Parameter) BindValue(h api.SQLHSTMT, idx int, v driver.Value) error {
7575
sqltype = api.SQL_WCHAR
7676
}
7777
case int64:
78-
ctype = api.SQL_C_SBIGINT
79-
p.Data = &d
80-
buf = unsafe.Pointer(&d)
81-
sqltype = api.SQL_BIGINT
82-
size = 8
78+
if -0x80000000 < d && d < 0x7fffffff {
79+
// Some ODBC drivers do not support SQL_BIGINT.
80+
// Use SQL_INTEGER if the value fit in int32.
81+
// See issue #78 for details.
82+
d2 := int32(d)
83+
ctype = api.SQL_C_LONG
84+
p.Data = &d2
85+
buf = unsafe.Pointer(&d2)
86+
sqltype = api.SQL_INTEGER
87+
size = 4
88+
} else {
89+
ctype = api.SQL_C_SBIGINT
90+
p.Data = &d
91+
buf = unsafe.Pointer(&d)
92+
sqltype = api.SQL_BIGINT
93+
size = 8
94+
}
8395
case bool:
8496
var b byte
8597
if d {

0 commit comments

Comments
 (0)