Documentation
¶
Index ¶
- Constants
- func ApplyFilters(query string, startArg int, filters Filters) (string, []interface{}, error)
- func ApplyPaginate(query string, p Paginate) string
- func DropDatabase(ctx context.Context, dbURL string) error
- func UnmarshalJSONTextFields(input interface{}) error
- type ConnectOption
- type DB
- func (d *DB) BeginTxx(ctx context.Context, opts *sql.TxOptions) (*sqlx.Tx, error)
- func (d *DB) Beginx() (*sqlx.Tx, error)
- func (d *DB) BuildSelect(ctx context.Context, model Model) (string, error)
- func (d *DB) Close() error
- func (d *DB) Conn() *sqlx.DB
- func (d *DB) Exec(ctx context.Context, queryName string, data interface{}) (sql.Result, error)
- func (d *DB) Fetch(ctx context.Context, dest interface{}, ids ...interface{}) error
- func (d *DB) Get(ctx context.Context, dest interface{}, queryName string, args ...interface{}) error
- func (d *DB) GetRelations(ctx context.Context, table string) ([]Relation, error)
- func (d *DB) GetWhere(ctx context.Context, dest interface{}, queryName string, filters Filters, ...) error
- func (d *DB) LoadQuery(queryName string) (string, error)
- func (d *DB) Populate(ctx context.Context, dest interface{}, filters Filters, args ...interface{}) error
- func (d *DB) PopulatePage(ctx context.Context, dest interface{}, filters Filters, page Paginate, ...) error
- func (d *DB) Query(ctx context.Context, queryName string, args ...interface{}) (*sqlx.Rows, error)
- func (d *DB) QueryPage(ctx context.Context, queryName string, filters Filters, page Paginate, ...) (*sqlx.Rows, error)
- func (d *DB) QueryWhere(ctx context.Context, queryName string, filters Filters, args ...interface{}) (*sqlx.Rows, error)
- func (d *DB) Select(ctx context.Context, dest interface{}, queryName string, args ...interface{}) error
- func (d *DB) SelectPage(ctx context.Context, dest interface{}, queryName string, filters Filters, ...) error
- func (d *DB) SelectWhere(ctx context.Context, dest interface{}, queryName string, filters Filters, ...) error
- func (d *DB) TxExec(ctx context.Context, tx *sqlx.Tx, queryName string, data interface{}) (sql.Result, error)
- func (d *DB) TxQuery(ctx context.Context, tx *sqlx.Tx, queryName string, args ...interface{}) (*sqlx.Rows, error)
- type FetchList
- type Filters
- type Model
- type Paginate
- type Relation
Constants ¶
const QUERY_NAME = "relations"
Variables ¶
This section is empty.
Functions ¶
func ApplyFilters ¶
ApplyFilters injects filters into query as a WHERE (or AND, if a WHERE is already present) clause, placed before any trailing ORDER BY / LIMIT / GROUP BY / etc. Placeholders use PostgreSQL $N numbering continuing from startArg — the number of positional arguments the base query already consumes — so the returned args must be appended after the caller's existing args.
func ApplyPaginate ¶
ApplyPaginate appends LIMIT/OFFSET to query. The values are integers, so they are inlined rather than parameterized. A zero Limit appends no LIMIT.
func DropDatabase ¶
DropDatabase terminates active connections to the database named in dbURL and drops it. Intended for test setup/teardown.
func UnmarshalJSONTextFields ¶
func UnmarshalJSONTextFields(input interface{}) error
UnmarshalJSONTextFields processes a single struct or a slice of structs to unmarshal fields of type types.JSONText into corresponding Go struct fields based on matching `db` and `json` tags.
Types ¶
type ConnectOption ¶
type ConnectOption struct {
// URL is the PostgreSQL connection string. The referenced database is
// created automatically if it does not yet exist.
URL string
// SqlDir is the directory named queries are loaded from ("<name>.sql").
SqlDir string
// MaxRequests, Interval and Timeout are forwarded to the circuit breaker.
MaxRequests uint32
Interval time.Duration
Timeout time.Duration
// StateChan, if non-nil, receives every circuit-breaker state transition.
StateChan chan<- gobreaker.State
}
ConnectOption configures a DB created by Connect.
type DB ¶
type DB struct {
// contains filtered or unexported fields
}
DB is a database handle wrapping a sqlx connection guarded by a circuit breaker, together with a directory of named SQL query files. Create one with Connect and share it across the application; all methods are safe for concurrent use.
func Connect ¶
func Connect(ctx context.Context, options *ConnectOption) (*DB, error)
Connect ensures the target database exists, opens and pings a connection, and returns a ready DB handle. It never terminates the process; all failures are returned as errors.
func (*DB) BuildSelect ¶
BuildSelect generates a populated SELECT for model using foreign-key metadata from the database. The result embeds each relation field (pointer-to-Model or slice-of-Model) as a to_jsonb / jsonb_agg sub-select aliased to the field's json tag, which the matching types.JSONText column then hydrates.
func (*DB) Conn ¶
Conn returns the underlying sqlx connection for callers that need direct access (transactions, custom queries, migrations).
func (*DB) Exec ¶
Exec runs a named write query (INSERT/UPDATE/DELETE) with named parameters bound from data.
func (*DB) Fetch ¶
Fetch loads a single record or a slice of records by id. The query name is derived from the destination's FetchQuery (it must implement Model). After a successful read, JSONText fields are hydrated into their typed siblings.
func (*DB) Get ¶
func (d *DB) Get(ctx context.Context, dest interface{}, queryName string, args ...interface{}) error
Get runs a named query expected to return a single row into dest.
func (*DB) GetRelations ¶
GetRelations returns the foreign-key relations for the given table by running the "relations" named query.
func (*DB) GetWhere ¶
func (d *DB) GetWhere(ctx context.Context, dest interface{}, queryName string, filters Filters, args ...interface{}) error
GetWhere runs a named query with filters appended and scans a single row into dest, hydrating JSONText fields afterwards.
func (*DB) LoadQuery ¶
LoadQuery reads the SQL for queryName from the configured SqlDir and caches the result for subsequent calls.
func (*DB) Populate ¶
func (d *DB) Populate(ctx context.Context, dest interface{}, filters Filters, args ...interface{}) error
Populate fetches model(s) into dest with their relation fields auto-populated, optionally narrowed by filters. dest must be a (pointer to a) Model or a pointer to a slice of Model.
func (*DB) PopulatePage ¶
func (d *DB) PopulatePage(ctx context.Context, dest interface{}, filters Filters, page Paginate, args ...interface{}) error
PopulatePage is Populate with LIMIT/OFFSET applied (useful for slice destinations).
func (*DB) QueryPage ¶
func (d *DB) QueryPage(ctx context.Context, queryName string, filters Filters, page Paginate, args ...interface{}) (*sqlx.Rows, error)
QueryPage runs a named query with filters and LIMIT/OFFSET appended and returns the raw rows.
func (*DB) QueryWhere ¶
func (d *DB) QueryWhere(ctx context.Context, queryName string, filters Filters, args ...interface{}) (*sqlx.Rows, error)
QueryWhere runs a named query with filters appended and returns the raw rows.
func (*DB) Select ¶
func (d *DB) Select(ctx context.Context, dest interface{}, queryName string, args ...interface{}) error
Select runs a named query into a slice destination.
func (*DB) SelectPage ¶
func (d *DB) SelectPage(ctx context.Context, dest interface{}, queryName string, filters Filters, page Paginate, args ...interface{}) error
SelectPage runs a named query with filters and LIMIT/OFFSET appended and scans the result into a slice destination.
func (*DB) SelectWhere ¶
func (d *DB) SelectWhere(ctx context.Context, dest interface{}, queryName string, filters Filters, args ...interface{}) error
SelectWhere runs a named query with filters appended and scans the result into a slice destination.
type Filters ¶
type Filters map[string]interface{}
Filters is a set of conditions appended to a query as a WHERE clause. The map key is a column identifier (optionally "table.column") with an optional trailing operator, e.g. "age >", "name LIKE", "id NOT IN". With no operator the value's type decides one:
nil -> "col IS NULL" ("col IS NOT NULL" with != / <>)
slice/array -> "col IN (...)" ("col NOT IN (...)" with != / <> / NOT IN)
anything else -> "col = $n"
Supported explicit operators: = != <> > < >= <= LIKE ILIKE NOT LIKE NOT ILIKE IN NOT IN. Conditions are combined with AND in deterministic (sorted) order.