sequel

package module
v0.4.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jun 21, 2024 License: Apache-2.0 Imports: 11 Imported by: 0

README

sequel

License

Package for inserting and scanning data from a PostgreSQL database into Go structs.

Documentation

Index

Constants

View Source
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

func ArrayScan[T any](oid uint32, src any, dest *[]T) error

ArrayScan scans the source using the PostgresType with the given oid and stores the result in the destination.

func Context

Context returns the default database context with a 15s timeout.

func IsErrNotFound

func IsErrNotFound(err error) bool

IsErrNotFound returns true if the given error is equal to sql.ErrNoRows

func IsUniqueViolation

func IsUniqueViolation(err error) bool

IsUniqueViolation returns true if the given error is equal to the postgres unique violation error (23505).

func NewContext

func NewContext(ctx context.Context, db *DB) context.Context

NewContext returns a new context with the given DB.

func NullBool

func NullBool(b bool) sql.NullBool

NullBool is a helper that returns a sql.NullBool with the valid set to false if the zero value is given.

func NullByte

func NullByte(b byte) sql.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

func NullInt16(i int16) sql.NullInt16

NullInt16 is a helper that returns a sql.NullInt16 with the valid set to false if the zero value is given.

func NullInt32

func NullInt32(i int32) sql.NullInt32

NullInt32 is a helper that returns a sql.NullInt32 with the valid set to false if the zero value is given.

func NullInt64

func NullInt64(i int64) sql.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

func NullTime(t time.Time) sql.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)

func RowsAffected

func RowsAffected(res sql.Result, n int64) error

RowsAffected checks that the numbers of rows affected matches the given one, if not it will return an error.

Types

type Array

type Array[T any] []T

Array is a generic type that implements the sql.Scanner interface.

func (*Array[T]) Scan

func (a *Array[T]) Scan(src any) error

Scan implements the sql.Scanner interface on the Array.

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) GetID

func (m Base) GetID() string

func (*Base) SetCreatedAt

func (m *Base) SetCreatedAt(t time.Time)

func (*Base) SetDeletedAt

func (m *Base) SetDeletedAt(t time.Time)

func (*Base) SetID

func (m *Base) SetID(id string)

func (*Base) SetUpdatedAt

func (m *Base) SetUpdatedAt(t time.Time)

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

func FromContext(ctx context.Context) (db *DB, ok bool)

FromContext returns the DB associated with this context.

func New

func New(dataSourceName string, opts ...Option) (*DB, error)

New creates a new DB. It will fail if it cannot ping it.

func (*DB) Begin

func (d *DB) Begin(ctx context.Context) (*Tx, error)

Begin begins a transaction and returns a new Tx.

func (*DB) Close

func (d *DB) Close() error

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

func (d *DB) Delete(ctx context.Context, arg Model) error

Delete soft-deletes the given model in the database setting the deleted_at column to the current date.

func (*DB) Driver added in v0.4.0

func (d *DB) Driver() string

Driver returns the name of the driver used.

func (*DB) Exec

func (d *DB) Exec(ctx context.Context, query string, args ...any) (sql.Result, error)

Exec executes a query without returning any rows. The args are for any placeholder parameters in the query.

func (*DB) Get

func (d *DB) Get(ctx context.Context, dest Model, query string, args ...any) error

Get populates the given model for the result of the given select query.

func (*DB) GetAll

func (d *DB) GetAll(ctx context.Context, dest any, query string, args ...any) error

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) Insert

func (d *DB) Insert(ctx context.Context, arg Model) error

Insert inserts the given model in the database.

func (*DB) InsertBatch

func (d *DB) InsertBatch(ctx context.Context, args []Model) error

InsertBatch inserts the given modules in a database using a transaction.

func (*DB) NamedExec added in v0.2.0

func (d *DB) NamedExec(ctx context.Context, query string, arg any) (sql.Result, error)

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

func (d *DB) NamedQuery(ctx context.Context, query string, arg any) (*sqlx.Rows, error)

NamedQuery executes a query that returns rows. Any named placeholder parameters are replaced with fields from arg.

func (*DB) Query

func (d *DB) Query(ctx context.Context, query string, args ...any) (*sql.Rows, error)

Query executes a query that returns rows, typically a SELECT. The args are for any placeholder parameters in the query.

func (*DB) QueryRow

func (d *DB) QueryRow(ctx context.Context, query string, args ...any) *sql.Row

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

func (d *DB) Rebind(query string) string

Rebind transforms a query from `?` to the DB driver's bind type.

func (*DB) RebindExec added in v0.2.0

func (d *DB) RebindExec(ctx context.Context, query string, args ...any) (sql.Result, error)

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

func (d *DB) RebindQuery(ctx context.Context, query string, args ...any) (*sql.Rows, error)

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

func (d *DB) RebindQueryRow(ctx context.Context, query string, args ...any) *sql.Row

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.

func (*DB) Select

func (d *DB) Select(ctx context.Context, dest Model, id string) error

Select populates the given model with the result of a select by id query.

func (*DB) Update

func (d *DB) Update(ctx context.Context, arg Model) error

Update updates the given model in the datastore.

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

type ModelWithHardDelete interface {
	Model
	HardDelete() string
}

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 WithClock

func WithClock(c clock.Clock) Option

WithClock sets a custom clock to the database.

func WithDriver added in v0.2.0

func WithDriver(driverName string) Option

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 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) Commit

func (t *Tx) Commit() error

Commit commits the transaction.

func (*Tx) Delete

func (t *Tx) Delete(arg Model) error

Delete adds a new soft-delete query in the transaction.

func (*Tx) Exec

func (t *Tx) Exec(query string, args ...any) (sql.Result, error)

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

func (t *Tx) Get(dest Model, query string, args ...any) error

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) Insert

func (t *Tx) Insert(arg Model) error

Insert adds a new insert query for the given model in the transaction.

func (*Tx) NamedExec added in v0.2.0

func (t *Tx) NamedExec(query string, arg any) (sql.Result, error)

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

func (t *Tx) NamedQuery(query string, arg any) (*sqlx.Rows, error)

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

func (t *Tx) Query(query string, args ...any) (*sql.Rows, error)

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

func (t *Tx) QueryRow(query string, args ...any) *sql.Row

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

func (t *Tx) Rebind(query string) string

Rebind transforms a query from QUESTION to the DB driver's bind type.

func (*Tx) RebindExec added in v0.2.0

func (t *Tx) RebindExec(query string, args ...any) (sql.Result, error)

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

func (t *Tx) RebindQuery(query string, args ...any) (*sql.Rows, error)

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

func (t *Tx) RebindQueryRow(query string, args ...any) *sql.Row

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.

func (*Tx) Rollback

func (t *Tx) Rollback() error

Rollback aborts the transaction.

func (*Tx) Select added in v0.3.0

func (t *Tx) Select(dest Model, id string) error

Select populates the given model with the result of a select by id query.

func (*Tx) Update

func (t *Tx) Update(arg Model) error

Update adds a new update query for the given model in the transaction.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL