Skip to content

Commit

Permalink
Merge branch 'master' of github.com:nakagami/firebirdsql
Browse files Browse the repository at this point in the history
  • Loading branch information
nakagami committed Jul 16, 2021
2 parents 0e0d03d + 8a04ef4 commit 72ac874
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 0 deletions.
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

0 comments on commit 72ac874

Please sign in to comment.