nakagami / firebirdsql

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

Wrong error message (op_response: 0) in transaction when dealing with RETURNING clause #76

Closed ctengiz closed 5 years ago

ctengiz commented 5 years ago

Please see the below test case (ma be related to #41, but I am not sure)

package main

import (
    "database/sql"
    "encoding/hex"
    "log"
    "os"
    "path/filepath"
    "crypto/rand"

    _ "github.com/nakagami/firebirdsql"
)

func TempFileName(prefix string) string {
    randBytes := make([]byte, 16)
    rand.Read(randBytes)
    return filepath.Join(os.TempDir(), prefix+hex.EncodeToString(randBytes)+".fdb")
}

func handleError(err error) {
    if err != nil {
        log.Fatal(err)
    }
}

func beginTx(conn *sql.DB) *sql.Tx {
    tx, err := conn.Begin()
    handleError(err)
    return tx
}

func main() {
    var tx *sql.Tx
    var err error
    var lastId int64

    temppath := TempFileName("test_basic_")
    conn, err := sql.Open("firebirdsql_createdb", "sysdba:masterkey@localhost:3050"+temppath)
    handleError(err)

    tx = beginTx(conn)
    sql := `
    create table t1 (
        id bigint generated by default as identity primary key,
        testval integer,
        testval2 integer
    )
    `
    _, err = tx.Exec(sql)
    handleError(err)

    err = tx.Commit()
    handleError(err)

    tx = beginTx(conn)
    sql = "insert into t1(testval, testval2) values (?, ?) returning id"
    err = tx.QueryRow(sql, "a", 2).Scan(&lastId)
    handleError(err)
    log.Println(lastId)

    /* *********************************************************
           We get Error op_response: 0 instead of :
        SQL error code = -303
        conversion error from string "a"
        exit status 1
     ****************************************************** */

    err = tx.Commit()
    handleError(err)
}
nakagami commented 5 years ago

Thnks!

Probably fix it

ctengiz commented 5 years ago

Thank you very much, yes the last commit seems to resolve issue.