Closed xhit closed 1 year ago
The string length returned by query selecting a CHAR column is exactly 4 times of size of column.
To reproduce the problem:
package main import ( "database/sql" "fmt" "log" "net/url" _ "github.com/nakagami/firebirdsql" ) func main() { //connecto to DB db, err := connectFirebird("localhost", "firebird/data/testdb", "usr", "password", 3050) if err != nil { panic(err) } defer db.Close() tableName := "ISSUEX" queryDrop := `drop table "` + tableName + `";` db.Exec(queryDrop) // modify this len charLen := 52 queryCreate := fmt.Sprintf("CREATE TABLE %s (A CHAR(%d))", tableName, charLen) _, err = db.Exec(queryCreate) if err != nil { panic(err) } defer db.Exec(queryDrop) queryInsert := `insert into ` + tableName + ` (A) values ('Dixer')` _, err = db.Exec(queryInsert) if err != nil { panic(err) } var t string if err := db.QueryRow(`SELECT A FROM ` + tableName).Scan(&t); err != nil { panic(err) } if len(t) != charLen { log.Fatalf("ERROR: Length expected: %d, Returned: %d", charLen, len(t)) } log.Printf("SUCCESS: Data: %s, Len: %d\n\n", t, len(t)) } //connectFirebird : Conectar a la base de datos Firebird func connectFirebird(host, database, username, password string, port int) (*sql.DB, error) { // default port if port == 0 { port = 3050 } //Password in firebird dsn not accept spaces, so escape with url.QueryEscape // in form user:pass@host:port/database dsn := fmt.Sprintf("%s:%s@%s:%d/%s", username, url.QueryEscape(password), host, port, database) db, err := sql.Open("firebirdsql", dsn) if err != nil { return nil, err } err = db.Ping() if err != nil { return nil, err } return db, nil }
Tested with Firebird 2.5.9 and 3.0.7 on:
Revert because of Issue #134 I've left charsetLen() in there, so if you have a better way, please pull request it.
Sorry, char(n) now trims spaces on the right side.
The string length returned by query selecting a CHAR column is exactly 4 times of size of column.
To reproduce the problem:
Tested with Firebird 2.5.9 and 3.0.7 on: