sijms / go-ora

Pure go oracle client
MIT License
786 stars 174 forks source link

Issue with Converting Booleans to varchar2(1) Oracle with Valuer Interface using go-ora/v2 v2.7.11 #420

Closed schackoa closed 1 year ago

schackoa commented 1 year ago

Hey there,

I'm currently working on a project where I need to convert boolean values to varchar2(1) in Oracle. These values should be represented as "J" for true (similar to "yes") and "N" for false (akin to "no").

For this task, I'm using the go-ora/v2 v2.7.11 library. In my code, I have defined a type called OracleBool, which is essentially a boolean with a custom Valuer interface:

type OracleBool bool

func (b OracleBool) Value() (driver.Value, error) {
    if b {
        return "J", nil
    }
    return "N", nil
}

Now, the challenge arises when I try to execute the insert operation using the defined OracleBool type:

func Insert(...) {
    var b OracleBool = true
    result, err := t.tx.ExecContext(ctx, `INSERT INTO sometable (bool_col) VALUES (:myBool)`, b)
}

However, I'm encountering an issue with the error message: strconv.ParseFloat: parsing "n": invalid syntax.

After some investigation, I suspect that this error might be related to the type detection in the library's code, particularly this section (you can find it here):

I think this could be the reason (https://github.com/sijms/go-ora/blob/master/v2/parameter_encode.go#L264C2-L264C45)

func tNumber(input reflect.Type) bool {
    return tInteger(input) || tFloat(input) || input.Kind() == reflect.Bool
}

I'm reaching out for assistance in resolving this hiccup. Any insights or guidance would be highly appreciated.

Thanks for your awesome work!

Best regards, Andy

sijms commented 1 year ago

I do change in the above code replacing input.Kind() == reflect.Bool when I add new type PLSQL Boolean I will also test you code before make the release

sijms commented 1 year ago

fixed in v2.7.12