Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Broken NullDecimal #40

Closed
xhit opened this issue Jan 28, 2020 · 2 comments
Closed

Broken NullDecimal #40

xhit opened this issue Jan 28, 2020 · 2 comments
Labels

Comments

@xhit
Copy link

xhit commented Jan 28, 2020

I modified the code for documentation (#38 (comment)) for tried with driver.NullDecimal instead driver.Decimal:

	db, err := sql.Open("hdb", connstring)
	if err != nil {
		log.Fatal(err)
	}
	defer db.Close()

	query := "select decimal_column from test"

	rows, err := db.Query(query)
	if err != nil {
		log.Fatal(err)
	}

	defer rows.Close()

	//The 1 is the columns len returned in query
	recordPointers := make([]interface{}, 1)

	//For Decimal Column
	var t driver.NullDecimal
	recordPointers[0] = &t

	for rows.Next() {

		// Scan the result into the column pointers...
		if err := rows.Scan(recordPointers...); err != nil {
			log.Fatal(err)
		}

		for _, v := range recordPointers {
			switch x := v.(type) {

			case *driver.NullDecimal:
				if x.Valid {
					d := (*big.Rat)(x.Decimal)
					log.Print(d.FloatString(6))
				} else {
					log.Print("NULL")
				}

			default:
				log.Print("is not Decimal")
			}
		}
	}

When decimal_column is NULL the code return NULL but when is not null return the error:
sql: Scan error on column index 0, name "decimal_column": invalid decimal value <nil>

I expect the decimal value.

If driver.Decimal is used then I got the value but when column is NULL then the same error is returned.

@stfnmllr
Copy link
Contributor

driver.NullDecimal needs to be 'instantiated' with non-nil Decimal attribute, like
var t = driver.NullDecimal{Decimal: new(driver.Decimal)}
The uninitialized Decimal attribute causes the error message you get after scan.
It's arguable why the Decimal isn't created automatically, but full allocation control should be with the user of the client lib - no lib internal magic.

@xhit
Copy link
Author

xhit commented Jan 28, 2020

Perfect. Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants