diff --git a/dbtype/bigint.go b/dbtype/bigint.go index 45a2d3b..3a269d7 100644 --- a/dbtype/bigint.go +++ b/dbtype/bigint.go @@ -5,6 +5,9 @@ import ( "fmt" "math/big" "strings" + + "github.com/jackc/pgtype" + "github.com/kr/pretty" ) // BigInt is a type alias for big.Int used for JSON/Database marshalling. @@ -175,3 +178,39 @@ func (b *BigInt) Scan(src interface{}) error { return nil } + +// func (src *Point) AssignTo(dst interface{}) error { +// return fmt.Errorf("cannot assign %v to %T", src, dst) +// } + +func (b BigInt) DecodeText(ci *pgtype.ConnInfo, src []byte) error { + pretty.Println(src) + // panic("geez") + err := b.Scan(src) + if err != nil { + panic(err) + } + return nil +} + +func (dst *BigInt) Set(src interface{}) error { + panic("common") + // return fmt.Errorf("cannot convert %v to Point", src) +} + +func (dst *BigInt) Get() interface{} { + panic("ahh") + // switch dst.Status { + // case pgtype.Present: + // return dst + // case pgtype.Null: + // return nil + // default: + // return dst.Status + // } +} + +// BigInt pgx custom type assignment +func (src *BigInt) AssignTo(dst interface{}) error { + panic("wee") +} diff --git a/go.mod b/go.mod index cbdc627..ae919fd 100644 --- a/go.mod +++ b/go.mod @@ -8,6 +8,7 @@ require ( github.com/jackc/pgconn v1.9.0 github.com/jackc/pgtype v1.8.0 github.com/jackc/pgx/v4 v4.12.0 + github.com/kr/pretty v0.1.0 github.com/stretchr/objx v0.3.0 // indirect github.com/stretchr/testify v1.7.0 golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 // indirect diff --git a/pgkit.go b/pgkit.go index 321a749..8b8733e 100644 --- a/pgkit.go +++ b/pgkit.go @@ -60,6 +60,15 @@ func Connect(appName string, cfg Config) (*DB, error) { } func ConnectWithPGX(appName string, pgxConfig *pgxpool.Config) (*DB, error) { + // pgxConfig.AfterConnect = func(ctx context.Context, conn *pgx.Conn) error { + // conn.ConnInfo().RegisterDataType(pgtype.DataType{ + // Value: &dbtype.BigInt{}, + // Name: "numeric", + // OID: pgtype.NumericOID, + // }) + // return nil + // } + pool, err := pgxpool.ConnectConfig(context.Background(), pgxConfig) if err != nil { return nil, fmt.Errorf("pgkit: failed to connect to db: %w", err) diff --git a/tests/pgkit_test.go b/tests/pgkit_test.go index 475482f..8a248cb 100644 --- a/tests/pgkit_test.go +++ b/tests/pgkit_test.go @@ -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, + } + + // 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") } } diff --git a/tests/schema_test.go b/tests/schema_test.go index 69cb7ef..abc413e 100644 --- a/tests/schema_test.go +++ b/tests/schema_test.go @@ -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 } type Article struct { diff --git a/tests/testdata/pgkit_test_db.sql b/tests/testdata/pgkit_test_db.sql index 0830673..9f819bd 100644 --- a/tests/testdata/pgkit_test_db.sql +++ b/tests/testdata/pgkit_test_db.sql @@ -22,7 +22,8 @@ CREATE TABLE logs ( CREATE TABLE stats ( id SERIAL PRIMARY KEY, key VARCHAR(80), - big_num NUMERIC(78,0) -- representing a *big.Int runtime type + big_num NUMERIC(78,0) NOT NULL, -- representing a big.Int runtime type + rating NUMERIC(78,0) -- representing a *big.Int runtime type ); CREATE TABLE articles (