nakagami / firebirdsql

Firebird RDBMS sql driver for Go (golang)
MIT License
220 stars 60 forks source link

Error selecting CHAR(n) columns #170

Closed palevi67 closed 1 month ago

palevi67 commented 2 months ago

When I do a rows.Next() on a select with CHAR(n) fields, I get the following error:

 interface conversion: interface {} is string, not []uint8

I've being tracing the error, and if in file xsqlvar.go line 438 I change v = strings.TrimRight(string(v.([]uint8)), " ") to v = strings.TrimRight(v.(string), " ") the error goes away ...

nakagami commented 2 months ago

Unfortunately, I have not been able to reproduce this successfully in my environment.

Ideally, write test code that reproduces.

Or it would be nice if you could provide examples of the database charset and select statements.

palevi67 commented 2 months ago

Test using the same test data of https://github.com/nakagami/firebirdsql/issues/169#issuecomment-2110467837:

func TestCHARN(t *testing.T) {
    conn, err := sql.Open("firebirdsql", "sysdba:secreto@localhost:3050/users/pablo/tmp/hbde.fdb5")
    checkErr(t, err)
    rows, err := conn.Query("select * from T2")
    checkErr(t, err)
    fmt.Println("before rows.Next()")
    rows.Next()
    fmt.Println("after rows.Next()")
    err = rows.Close()
    checkErr(t, err)
    err = conn.Close()
    checkErr(t, err)
}

func checkErr(t *testing.T, err error) {
    if err != nil {
        t.Fatal(err)
    }
}

And I get:

before rows.Next()
--- FAIL: TestCHARN (0.11s)
panic: interface conversion: interface {} is string, not []uint8 [recovered]
    panic: interface conversion: interface {} is string, not []uint8

goroutine 34 [running]:
testing.tRunner.func1.2({0x7ff6a06e9060, 0xc000024810})
    C:/Program Files/Go/src/testing/testing.go:1631 +0x49e
testing.tRunner.func1()
    C:/Program Files/Go/src/testing/testing.go:1634 +0x669
panic({0x7ff6a06e9060?, 0xc000024810?})
    C:/Program Files/Go/src/runtime/panic.go:770 +0x136
github.com/nakagami/firebirdsql.(*xSQLVAR).value(0xc0002b5468, {0xc0000178e0, 0x1e, 0x20}, {0x0, 0x0}, {0x7ff6a074d0b1, 0x4})
    C:/Users/pablo/go/pkg/mod/github.com/nakagami/firebirdsql@v0.9.8/xsqlvar.go:439 +0x13b1
...

I think the charset involved is NONE:

select RDB$CHARACTER_SET_NAME from RDB$DATABASE -> NONE

Let me know if you need any other info ...

nakagami commented 2 months ago

I haven't yet run the test code, but I think it's because the charset is None.

It cannot be assumed to be utf-8, so it might be better to return []uint8.

I'll think about fixing it.

nakagami commented 2 months ago

Fixed with this pull request. https://github.com/nakagami/firebirdsql/pull/171 Does this solve the problem?

If charset is None, the value of []int8 before conversion is returned as is.

There was a problem with the fix in #164