Use pgx
This commit is contained in:
parent
cdc767a497
commit
e465b961a5
@ -1,10 +1,12 @@
|
|||||||
package postgres
|
package postgres
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"embed"
|
"embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
"github.com/jackc/pgx/v4"
|
||||||
_ "github.com/jackc/pgx/v4/stdlib"
|
_ "github.com/jackc/pgx/v4/stdlib"
|
||||||
"github.com/pressly/goose/v3"
|
"github.com/pressly/goose/v3"
|
||||||
)
|
)
|
||||||
@ -25,5 +27,15 @@ func Connect(server string, user string, password string, database string) (*Que
|
|||||||
return nil, err
|
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
|
||||||
}
|
}
|
||||||
|
@ -4,14 +4,15 @@ package postgres
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"database/sql"
|
|
||||||
|
"github.com/jackc/pgconn"
|
||||||
|
"github.com/jackc/pgx/v4"
|
||||||
)
|
)
|
||||||
|
|
||||||
type DBTX interface {
|
type DBTX interface {
|
||||||
ExecContext(context.Context, string, ...interface{}) (sql.Result, error)
|
Exec(context.Context, string, ...interface{}) (pgconn.CommandTag, error)
|
||||||
PrepareContext(context.Context, string) (*sql.Stmt, error)
|
Query(context.Context, string, ...interface{}) (pgx.Rows, error)
|
||||||
QueryContext(context.Context, string, ...interface{}) (*sql.Rows, error)
|
QueryRow(context.Context, string, ...interface{}) pgx.Row
|
||||||
QueryRowContext(context.Context, string, ...interface{}) *sql.Row
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func New(db DBTX) *Queries {
|
func New(db DBTX) *Queries {
|
||||||
@ -22,7 +23,7 @@ type Queries struct {
|
|||||||
db DBTX
|
db DBTX
|
||||||
}
|
}
|
||||||
|
|
||||||
func (q *Queries) WithTx(tx *sql.Tx) *Queries {
|
func (q *Queries) WithTx(tx pgx.Tx) *Queries {
|
||||||
return &Queries{
|
return &Queries{
|
||||||
db: tx,
|
db: tx,
|
||||||
}
|
}
|
||||||
|
@ -7,6 +7,7 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/google/uuid"
|
"github.com/google/uuid"
|
||||||
|
"github.com/jackc/pgtype"
|
||||||
)
|
)
|
||||||
|
|
||||||
type Account struct {
|
type Account struct {
|
||||||
@ -30,8 +31,8 @@ type Payee struct {
|
|||||||
type Transaction struct {
|
type Transaction struct {
|
||||||
ID uuid.UUID
|
ID uuid.UUID
|
||||||
Date time.Time
|
Date time.Time
|
||||||
Memo sql.NullString
|
Memo string
|
||||||
Amount string
|
Amount pgtype.Numeric
|
||||||
AccountID uuid.UUID
|
AccountID uuid.UUID
|
||||||
PayeeID uuid.NullUUID
|
PayeeID uuid.NullUUID
|
||||||
}
|
}
|
||||||
|
@ -37,7 +37,7 @@ CREATE TABLE payees (
|
|||||||
CREATE TABLE transactions (
|
CREATE TABLE transactions (
|
||||||
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
|
id uuid DEFAULT uuid_generate_v4() PRIMARY KEY,
|
||||||
date date NOT NULL,
|
date date NOT NULL,
|
||||||
memo text NULL,
|
memo text NOT NULL,
|
||||||
amount decimal(12,2) NOT NULL,
|
amount decimal(12,2) NOT NULL,
|
||||||
account_id uuid NOT NULL,
|
account_id uuid NOT NULL,
|
||||||
payee_id uuid
|
payee_id uuid
|
||||||
|
Loading…
x
Reference in New Issue
Block a user