Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support negatives INT128 #132

Merged
merged 1 commit into from Jul 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 35 additions & 0 deletions driver_test.go
Expand Up @@ -480,6 +480,41 @@ func TestInt128(t *testing.T) {
conn.Close()
}

func TestNegativeInt128(t *testing.T) {
conn, err := sql.Open("firebirdsql_createdb", GetTestDSN("test_negative_int128_"))
if err != nil {
t.Fatalf("Error connecting: %v", err)
}

firebird_major_version := get_firebird_major_version(conn)
if firebird_major_version < 4 {
return
}

sql := `
CREATE TABLE test_negative_int128 (
i int128
)
`
conn.Exec(sql)
conn.Exec("insert into test_negative_int128(i) values (-170141183460469231731687303715884105727)")

var i128 *big.Int
err = conn.QueryRow("SELECT i FROM test_negative_int128").Scan(&i128)
if err != nil {
t.Fatalf("Error SELECT: %v", err)
}

var toCmp = new(big.Int)
toCmp, _ = toCmp.SetString("-170141183460469231731687303715884105727", 10)

if i128.Cmp(toCmp) != 0 {
t.Fatalf("Negative INT128 Error: %v", i128)
}

conn.Close()
}

func TestLegacyAuthWireCrypt(t *testing.T) {
test_dsn := GetTestDSN("test_legacy_auth_")
var n int
Expand Down
20 changes: 20 additions & 0 deletions xsqlvar.go
Expand Up @@ -463,6 +463,20 @@ func (x *xSQLVAR) value(raw_value []byte, timezone string, charset string) (v in
v = i64
}
case SQL_TYPE_INT128:
var isNegative bool

// when raw_value[0] is > 127, then subtract 255 in every index
if raw_value[0] > 127 {
for i := range raw_value {
if raw_value[i] < 255 {
raw_value[i] = 255 - raw_value[i]
} else {
raw_value[i] -= 255
}
}
isNegative = true
}

// reverse
for i, j := 0, len(raw_value)-1; i < j; i, j = i+1, j-1 {
raw_value[i], raw_value[j] = raw_value[j], raw_value[i]
Expand All @@ -489,6 +503,12 @@ func (x *xSQLVAR) value(raw_value []byte, timezone string, charset string) (v in
// add to x
x.Add(x, xx)
}

// when negative, add 1 and mul -1
if isNegative {
x.Add(x, big.NewInt(1))
x.Mul(x, big.NewInt(-1))
}
v = x
case SQL_TYPE_DATE:
v = x.parseDate(raw_value, timezone)
Expand Down