42 lines
1.1 KiB
Go
42 lines
1.1 KiB
Go
package postgresql
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
|
|
_ "github.com/lib/pq"
|
|
)
|
|
|
|
type PQClient interface {
|
|
Prepare(query string) (*sql.Stmt, error)
|
|
Exec(query string, arguments ...interface{}) (sql.Result, error)
|
|
Query(query string, args ...interface{}) (*sql.Rows, error)
|
|
QueryRow(query string, args ...interface{}) *sql.Row
|
|
// Begin(ctx context.Context) (*sql.Tx, error)
|
|
}
|
|
|
|
type PQClientContext interface {
|
|
Prepare(query string) (*sql.Stmt, error)
|
|
ExecContext(ctx context.Context, query string, arguments ...interface{}) (sql.Result, error)
|
|
QueryContext(ctx context.Context, query string, args ...interface{}) (*sql.Rows, error)
|
|
QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row
|
|
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
|
|
}
|
|
|
|
func NewPQClient(psqlInfo string) (*sql.DB, error) {
|
|
const op = "repository.postgresql.pq.NewConnection"
|
|
|
|
db, err := sql.Open("postgres", psqlInfo)
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
err = db.Ping()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("%s: %w", op, err)
|
|
}
|
|
|
|
return db, nil
|
|
}
|