Documentation ¶
Index ¶
- Constants
- func ArrayScan[T any](oid uint32, src any, dest *[]T) error
- func Context(ctx context.Context) (context.Context, context.CancelFunc)
- func IsErrNotFound(err error) bool
- func IsUniqueViolation(err error) bool
- func NewContext(ctx context.Context, db *DB) context.Context
- func NullBool(b bool) sql.NullBool
- func NullByte(b byte) sql.NullByte
- func NullFloat64(f float64) sql.NullFloat64
- func NullInt16(i int16) sql.NullInt16
- func NullInt32(i int32) sql.NullInt32
- func NullInt64(i int64) sql.NullInt64
- func NullString(s string) sql.NullString
- func NullTime(t time.Time) sql.NullTime
- func Queries(builder *qb.QueryBuilder) (selectQ, insertQ, updateQ, deleteQ string)
- func RowsAffected(res sql.Result, n int64) error
- type Array
- type Base
- type DB
- func (d *DB) Begin(ctx context.Context) (*Tx, error)
- func (d *DB) Close() error
- func (d *DB) DB() *sql.DB
- func (d *DB) Delete(ctx context.Context, arg Model) error
- func (d *DB) Driver() string
- func (d *DB) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (d *DB) Get(ctx context.Context, dest Model, query string, args ...any) error
- func (d *DB) GetAll(ctx context.Context, dest any, query string, args ...any) error
- func (d *DB) HardDelete(ctx context.Context, arg ModelWithHardDelete) error
- func (d *DB) Insert(ctx context.Context, arg Model) error
- func (d *DB) InsertBatch(ctx context.Context, args []Model) error
- func (d *DB) NamedExec(ctx context.Context, query string, arg any) (sql.Result, error)
- func (d *DB) NamedQuery(ctx context.Context, query string, arg any) (*sqlx.Rows, error)
- func (d *DB) Prepare(ctx context.Context, query string) (*sql.Stmt, error)
- func (d *DB) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (d *DB) QueryRow(ctx context.Context, query string, args ...any) *sql.Row
- func (d *DB) Rebind(query string) string
- func (d *DB) RebindExec(ctx context.Context, query string, args ...any) (sql.Result, error)
- func (d *DB) RebindQuery(ctx context.Context, query string, args ...any) (*sql.Rows, error)
- func (d *DB) RebindQueryRow(ctx context.Context, query string, args ...any) *sql.Row
- func (d *DB) Select(ctx context.Context, dest Model, id string) error
- func (d *DB) Update(ctx context.Context, arg Model) error
- type Model
- type ModelWithExecInsert
- type ModelWithHardDelete
- type Option
- type Tx
- func (t *Tx) Commit() error
- func (t *Tx) Delete(arg Model) error
- func (t *Tx) Exec(query string, args ...any) (sql.Result, error)
- func (t *Tx) Get(dest Model, query string, args ...any) error
- func (t *Tx) HardDelete(arg ModelWithHardDelete) error
- func (t *Tx) Insert(arg Model) error
- func (t *Tx) NamedExec(query string, arg any) (sql.Result, error)
- func (t *Tx) NamedQuery(query string, arg any) (*sqlx.Rows, error)
- func (t *Tx) Prepare(query string) (*sql.Stmt, error)
- func (t *Tx) Query(query string, args ...any) (*sql.Rows, error)
- func (t *Tx) QueryRow(query string, args ...any) *sql.Row
- func (t *Tx) Rebind(query string) string
- func (t *Tx) RebindExec(query string, args ...any) (sql.Result, error)
- func (t *Tx) RebindQuery(query string, args ...any) (*sql.Rows, error)
- func (t *Tx) RebindQueryRow(query string, args ...any) *sql.Row
- func (t *Tx) Rollback() error
- func (t *Tx) Select(dest Model, id string) error
- func (t *Tx) Update(arg Model) error
Constants ¶
const MaxOpenConnections = 100
MaxOpenConnections is the maximum number of open connections. If we reach this value, the requests will wait until one connection is free.
Variables ¶
This section is empty.
Functions ¶
func ArrayScan ¶
ArrayScan scans the source using the PostgresType with the given oid and stores the result in the destination.
func IsErrNotFound ¶
IsErrNotFound returns true if the given error is equal to sql.ErrNoRows
func IsUniqueViolation ¶
IsUniqueViolation returns true if the given error is equal to the postgres unique violation error (23505).
func NewContext ¶
NewContext returns a new context with the given DB.
func NullBool ¶
NullBool is a helper that returns a sql.NullBool with the valid set to false if the zero value is given.
func NullByte ¶
NullByte is a helper that returns a sql.NullByte with the valid set to false if the zero value is given.
func NullFloat64 ¶
func NullFloat64(f float64) sql.NullFloat64
NullFloat64 is a helper that returns a sql.NullFloat64 with the valid set to false if the zero value is given.
func NullInt16 ¶
NullInt16 is a helper that returns a sql.NullInt16 with the valid set to false if the zero value is given.
func NullInt32 ¶
NullInt32 is a helper that returns a sql.NullInt32 with the valid set to false if the zero value is given.
func NullInt64 ¶
NullInt64 is a helper that returns a sql.NullInt64 with the valid set to false if the zero value is given.
func NullString ¶
func NullString(s string) sql.NullString
NullString is a helper that returns a sql.NullString with the valid set to false if the zero value is given.
func NullTime ¶
NullTime is a helper that returns a sql.NullTime with the valid set to false if the zero value is given.
func Queries ¶
func Queries(builder *qb.QueryBuilder) (selectQ, insertQ, updateQ, deleteQ string)
Types ¶
type Array ¶
type Array[T any] []T
Array is a generic type that implements the sql.Scanner interface.
type Base ¶
type Base struct { ID string `db:"id"` CreatedAt time.Time `db:"created_at"` UpdatedAt time.Time `db:"updated_at"` DeletedAt sql.NullTime `db:"deleted_at"` }
func (*Base) SetCreatedAt ¶
func (*Base) SetDeletedAt ¶
func (*Base) SetUpdatedAt ¶
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is the type that holds the database client and adds support for database operations on a Model.
func FromContext ¶
FromContext returns the DB associated with this context.
func NewDB ¶ added in v0.5.0
NewDB creates a new DB wrapping the opened database handle with the given driverName. It will fail if it cannot ping it.
func (*DB) Close ¶
Close closes the database and prevents new queries from starting. Close then waits for all queries that have started processing on the server to finish.
func (*DB) Delete ¶
Delete soft-deletes the given model in the database setting the deleted_at column to the current date.
func (*DB) Exec ¶
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*DB) GetAll ¶
GetAll populates the given destination with all the results of the given select query. The method will fail if the destination is not a pointer to a slice.
func (*DB) HardDelete ¶
func (d *DB) HardDelete(ctx context.Context, arg ModelWithHardDelete) error
HardDelete deletes the given model from the database.
func (*DB) InsertBatch ¶
InsertBatch inserts the given modules in a database using a transaction.
func (*DB) NamedExec ¶ added in v0.2.0
NamedExec using executes a query without returning any rows. Any named placeholder parameters are replaced with fields from arg.
func (*DB) NamedQuery ¶ added in v0.2.0
NamedQuery executes a query that returns rows. Any named placeholder parameters are replaced with fields from arg.
func (*DB) Query ¶
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*DB) QueryRow ¶
QueryRow executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called.
If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
func (*DB) Rebind ¶ added in v0.2.0
Rebind transforms a query from `?` to the DB driver's bind type.
func (*DB) RebindExec ¶ added in v0.2.0
Exec executes a query without returning any rows. The query is rebound from `?` to the DB driver's bind type. The args are for any placeholder parameters in the query.
func (*DB) RebindQuery ¶ added in v0.2.0
Query executes a query that returns rows, typically a SELECT. The query is rebound from `?` to the DB driver's bind type. The args are for any placeholder parameters in the query.
func (*DB) RebindQueryRow ¶ added in v0.2.0
QueryRow executes a query that is expected to return at most one row. The query is rebound from `?` to the DB driver's bind type. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called.
If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
type Model ¶
type Model interface { GetID() string SetID(id string) SetCreatedAt(t time.Time) SetUpdatedAt(t time.Time) SetDeletedAt(t time.Time) Select() string Insert() string Update() string Delete() string }
Model is the interface implemented by all the database models.
type ModelWithExecInsert ¶
type ModelWithExecInsert interface { Model WithExecInsert() }
ModelWithExecInsert is the interface implemented by a model which inserts already contains the id and are not returning one.
type ModelWithHardDelete ¶
ModelWithHardDelete is the interface implemented by a model that can be hard deleted.
type Option ¶
type Option func(*options)
Option is the type of options that can be used to modify the database. This can be useful for testing purposes.
func WithDriver ¶ added in v0.2.0
WithDriver defines the driver to use, defaults to pgx/v5. This default driver is automatically loaded by this package, any other driver must be loaded by the user.
func WithMaxOpenConnections ¶ added in v0.5.0
WithMaxOpenConnections sets the maximum number of open connections to the database. If it is not set it will use MaxOpenConnections (100).
func WithRebindModel ¶ added in v0.3.0
func WithRebindModel() Option
WithRebindModel enables query rebind on unnamed queries from the model, the queries from Select(), Delete(), and HardDelete() methods.
type Tx ¶
type Tx struct {
// contains filtered or unexported fields
}
Tx is an wrapper around sqlx.Tx with extra functionality.
func (*Tx) Exec ¶
Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.
func (*Tx) Get ¶ added in v0.2.0
Get populates the given model for the result of the given select query.
func (*Tx) HardDelete ¶
func (t *Tx) HardDelete(arg ModelWithHardDelete) error
HardDelete ads a new hard-delete query in the transaction.
func (*Tx) NamedExec ¶ added in v0.2.0
NamedExec using executes a query without returning any rows. Any named placeholder parameters are replaced with fields from arg.
func (*Tx) NamedQuery ¶ added in v0.2.0
NamedQuery executes a query that returns rows. Any named placeholder parameters are replaced with fields from arg.
func (*Tx) Query ¶ added in v0.2.0
Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.
func (*Tx) QueryRow ¶ added in v0.2.0
QueryRow executes a query that is expected to return at most one row. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called.
If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.
func (*Tx) Rebind ¶ added in v0.2.0
Rebind transforms a query from QUESTION to the DB driver's bind type.
func (*Tx) RebindExec ¶ added in v0.2.0
Exec executes a query without returning any rows. The query is rebound from `?` to the DB driver's bind type. The args are for any placeholder parameters in the query.
func (*Tx) RebindQuery ¶ added in v0.2.0
Query executes a query that returns rows, typically a SELECT. The query is rebound from `?` to the DB driver's bind type. The args are for any placeholder parameters in the query.
func (*Tx) RebindQueryRow ¶ added in v0.2.0
QueryRow executes a query that is expected to return at most one row. The query is rebound from `?` to the DB driver's bind type. QueryRowContext always returns a non-nil value. Errors are deferred until Row's Scan method is called.
If the query selects no rows, the *Row's Scan will return ErrNoRows. Otherwise, the *Row's Scan scans the first selected row and discards the rest.