-
Notifications
You must be signed in to change notification settings - Fork 4
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
custom pointer to struct type scan issue #6
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,7 @@ import ( | |
"github.com/goware/pgkit" | ||
"github.com/goware/pgkit/dbtype" | ||
"github.com/jackc/pgx/v4" | ||
"github.com/kr/pretty" | ||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
@@ -331,6 +332,7 @@ func TestRowsWithBigInt(t *testing.T) { | |
assert.NoError(t, err) | ||
assert.Equal(t, "count", sout.Key) | ||
assert.True(t, sout.Num.Int64() == 2) | ||
assert.Nil(t, sout.Rating) | ||
} | ||
|
||
// another one, big number this time | ||
|
@@ -349,6 +351,35 @@ func TestRowsWithBigInt(t *testing.T) { | |
assert.NoError(t, err) | ||
assert.Equal(t, "count2", sout.Key) | ||
assert.True(t, sout.Num.String() == "12323942398472837489234") | ||
|
||
pretty.Println(sout.Rating) | ||
|
||
assert.Nil(t, sout.Rating) | ||
} | ||
|
||
// last, with opt rating | ||
{ | ||
bv := dbtype.NewBigInt(5) | ||
|
||
stat := &Stat{ | ||
Key: "count3", | ||
Num: dbtype.NewBigIntFromString("44", 0), | ||
Rating: &bv, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. passing *dbtype.BigInt here is causing an assignment issue to a nullable NUMERIC(78,0) type |
||
} | ||
|
||
// Insert | ||
q1 := DB.SQL.InsertRecord(stat, "stats") | ||
_, err := DB.Query.Exec(context.Background(), q1) | ||
assert.NoError(t, err) | ||
|
||
// Select | ||
var sout Stat | ||
q2 := DB.SQL.Select("*").From("stats").Where(sq.Eq{"key": "count3"}) | ||
err = DB.Query.GetOne(context.Background(), q2, &sout) | ||
assert.NoError(t, err) | ||
assert.Equal(t, "count3", sout.Key) | ||
assert.True(t, sout.Num.String() == "44") | ||
assert.True(t, sout.Rating.String() == "5") | ||
} | ||
} | ||
|
||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -32,9 +32,10 @@ type Log struct { | |
} | ||
|
||
type Stat struct { | ||
ID int64 `db:"id,omitempty"` | ||
Key string `db:"key"` | ||
Num dbtype.BigInt `db:"big_num"` // using NUMERIC(78,0) postgres datatype | ||
ID int64 `db:"id,omitempty"` | ||
Key string `db:"key"` | ||
Num dbtype.BigInt `db:"big_num"` // using NUMERIC(78,0) postgres datatype | ||
Rating *dbtype.BigInt `db:"rating"` // using NUMERIC(78,0) postgres datatype | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is the problematic runtime type, of *dbtype.BigInt .. resulting in the error on rows.Scan(..) of:
|
||
} | ||
|
||
type Article struct { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can ignore this method, it was just me doing some hacking code. I was thinking that if pgx cannot support a null value of a normal database/sql scanner+valuer, then we can try the add binary encoding support here which might solve our issue in the end.
Alternatively, maybe there is another way to make pgx or scany work, and if not, finally we could adapt pgkit instead of using the pgx-direct driver, we update all of pgkit to use the database/sql interface which pgx also supports. I'm not sure exactly what we'd compromise by not using pgx-driver directly anymore, but it would be nice to know that too if we decide that..