From e465b961a5c474a3969b9c66bcdefe9b3cb2703d Mon Sep 17 00:00:00 2001 From: Jan Bader Date: Thu, 2 Dec 2021 10:44:14 +0000 Subject: [PATCH] Use pgx --- postgres/conn.go | 14 +++++++++++++- postgres/db.go | 13 +++++++------ postgres/models.go | 5 +++-- postgres/schema/202112021109_initial.sql | 2 +- sqlc.yaml | 2 +- 5 files changed, 25 insertions(+), 11 deletions(-) diff --git a/postgres/conn.go b/postgres/conn.go index 6b5c9f4..3020548 100644 --- a/postgres/conn.go +++ b/postgres/conn.go @@ -1,10 +1,12 @@ package postgres import ( + "context" "database/sql" "embed" "fmt" + "github.com/jackc/pgx/v4" _ "github.com/jackc/pgx/v4/stdlib" "github.com/pressly/goose/v3" ) @@ -25,5 +27,15 @@ func Connect(server string, user string, password string, database string) (*Que return nil, err } - return New(conn), nil + err = conn.Close() + if err != nil { + return nil, err + } + + connPG, err := pgx.Connect(context.Background(), connString) + if err != nil { + return nil, err + } + + return New(connPG), nil } diff --git a/postgres/db.go b/postgres/db.go index 8d02508..f2900b8 100644 --- a/postgres/db.go +++ b/postgres/db.go @@ -4,14 +4,15 @@ package postgres import ( "context" - "database/sql" + + "github.com/jackc/pgconn" + "github.com/jackc/pgx/v4" ) type DBTX interface { - ExecContext(context.Context, string, ...interface{}) (sql.Result, error) - PrepareContext(context.Context, string) (*sql.Stmt, error) - QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error) - QueryRowContext(context.Context, string, ...interface{}) *sql.Row + Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error) + Query(context.Context, string, ...interface{}) (pgx.Rows, error) + QueryRow(context.Context, string, ...interface{}) pgx.Row } func New(db DBTX) *Queries { @@ -22,7 +23,7 @@ type Queries struct { db DBTX } -func (q *Queries) WithTx(tx *sql.Tx) *Queries { +func (q *Queries) WithTx(tx pgx.Tx) *Queries { return &Queries{ db: tx, } diff --git a/postgres/models.go b/postgres/models.go index 417aa97..1edc459 100644 --- a/postgres/models.go +++ b/postgres/models.go @@ -7,6 +7,7 @@ import ( "time" "github.com/google/uuid" + "github.com/jackc/pgtype" ) type Account struct { @@ -30,8 +31,8 @@ type Payee struct { type Transaction struct { ID uuid.UUID Date time.Time - Memo sql.NullString - Amount string + Memo string + Amount pgtype.Numeric AccountID uuid.UUID PayeeID uuid.NullUUID } diff --git a/postgres/schema/202112021109_initial.sql b/postgres/schema/202112021109_initial.sql index 596d500..6b7a421 100644 --- a/postgres/schema/202112021109_initial.sql +++ b/postgres/schema/202112021109_initial.sql @@ -37,7 +37,7 @@ CREATE TABLE payees ( CREATE TABLE transactions ( id uuid DEFAULT uuid_generate_v4() PRIMARY KEY, date date NOT NULL, - memo text NULL, + memo text NOT NULL, amount decimal(12,2) NOT NULL, account_id uuid NOT NULL, payee_id uuid diff --git a/sqlc.yaml b/sqlc.yaml index 5f34110..4cb38c9 100644 --- a/sqlc.yaml +++ b/sqlc.yaml @@ -5,4 +5,4 @@ packages: engine: "postgresql" schema: "postgres/schema/" queries: "postgres/queries/" - #sql_package: "pgx/v4" \ No newline at end of file + sql_package: "pgx/v4" \ No newline at end of file