Documentation
¶
Overview ¶
Package query is part of the GoFastr framework. See https://github.com/DonaldMurillo/gofastr for documentation.
Index ¶
- func MustIdent(s string) string
- func QuoteIdent(s string) string
- func SafeIdent(s string) (string, error)
- func SafeQuote(s string) (string, error)
- func Transaction(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error
- type CountBuilder
- type DeleteBuilder
- type InsertBuilder
- type QueryBuilder
- func (qb *QueryBuilder) Build() (string, []any)
- func (qb *QueryBuilder) Cursor(field string, value any, dir string) *QueryBuilder
- func (qb *QueryBuilder) From(table string) *QueryBuilder
- func (qb *QueryBuilder) Join(table, on string) *QueryBuilder
- func (qb *QueryBuilder) LeftJoin(table, on string) *QueryBuilder
- func (qb *QueryBuilder) Limit(n int) *QueryBuilder
- func (qb *QueryBuilder) Offset(n int) *QueryBuilder
- func (qb *QueryBuilder) OrWhere(condition string, args ...any) *QueryBuilder
- func (qb *QueryBuilder) Order(column string, dir string) *QueryBuilder
- func (qb *QueryBuilder) Where(condition string, args ...any) *QueryBuilder
- type UpdateBuilder
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func MustIdent ¶
MustIdent is like SafeIdent but panics on invalid identifiers. Use in init/config-time code where the identifier is a hard-coded constant.
func QuoteIdent ¶
QuoteIdent wraps an identifier in double-quotes with internal quotes escaped. The caller is responsible for validating the identifier first (via SafeIdent).
QuoteIdent("users") → "users"
QuoteIdent(`weird"name`) → "weird""name"
func SafeIdent ¶
SafeIdent validates that s is a safe SQL identifier and returns it quoted. This prevents SQL injection when table or column names must be interpolated into queries (they can't be parameterized with $1 placeholders).
Returns an error if s contains characters outside [a-zA-Z0-9_.].
Types ¶
type CountBuilder ¶
type CountBuilder struct {
// contains filtered or unexported fields
}
CountBuilder builds a SELECT COUNT(*) query with parameterized placeholders.
func Count ¶
func Count(table string) *CountBuilder
Count creates a new CountBuilder for the given table.
func (*CountBuilder) Build ¶
func (cb *CountBuilder) Build() (string, []any)
Build produces the final parameterized SQL and argument slice.
func (*CountBuilder) Where ¶
func (cb *CountBuilder) Where(condition string, args ...any) *CountBuilder
Where appends a WHERE condition (ANDed with previous conditions).
type DeleteBuilder ¶
type DeleteBuilder struct {
// contains filtered or unexported fields
}
DeleteBuilder builds a DELETE query with parameterized placeholders.
func Delete ¶
func Delete(table string) *DeleteBuilder
Delete creates a new DeleteBuilder for the given table.
func (*DeleteBuilder) Build ¶
func (db *DeleteBuilder) Build() (string, []any)
Build produces the final parameterized SQL and argument slice.
func (*DeleteBuilder) Where ¶
func (db *DeleteBuilder) Where(condition string, args ...any) *DeleteBuilder
Where appends a WHERE condition (ANDed with previous conditions).
type InsertBuilder ¶
type InsertBuilder struct {
// contains filtered or unexported fields
}
InsertBuilder builds an INSERT query with parameterized placeholders.
func Insert ¶
func Insert(table string) *InsertBuilder
Insert creates a new InsertBuilder for the given table.
func (*InsertBuilder) Build ¶
func (ib *InsertBuilder) Build() (string, []any)
Build produces the final parameterized SQL and argument slice.
func (*InsertBuilder) Columns ¶
func (ib *InsertBuilder) Columns(cols ...string) *InsertBuilder
Columns sets the columns to insert into.
func (*InsertBuilder) Returning ¶
func (ib *InsertBuilder) Returning(cols ...string) *InsertBuilder
Returning adds a RETURNING clause.
func (*InsertBuilder) Values ¶
func (ib *InsertBuilder) Values(vals ...any) *InsertBuilder
Values sets the values to insert.
type QueryBuilder ¶
type QueryBuilder struct {
// contains filtered or unexported fields
}
QueryBuilder builds a SELECT query with parameterized placeholders.
func Select ¶
func Select(columns ...string) *QueryBuilder
Select creates a new QueryBuilder selecting the given columns.
func (*QueryBuilder) Build ¶
func (qb *QueryBuilder) Build() (string, []any)
Build produces the final parameterized SQL and argument slice. It does not mutate the QueryBuilder — safe to call multiple times.
func (*QueryBuilder) Cursor ¶
func (qb *QueryBuilder) Cursor(field string, value any, dir string) *QueryBuilder
Cursor adds keyset/cursor-based pagination. dir "forward" → WHERE field > value, dir "backward" → WHERE field < value.
func (*QueryBuilder) From ¶
func (qb *QueryBuilder) From(table string) *QueryBuilder
From sets the table to query.
func (*QueryBuilder) Join ¶
func (qb *QueryBuilder) Join(table, on string) *QueryBuilder
Join adds an INNER JOIN clause.
func (*QueryBuilder) LeftJoin ¶
func (qb *QueryBuilder) LeftJoin(table, on string) *QueryBuilder
LeftJoin adds a LEFT JOIN clause.
func (*QueryBuilder) Limit ¶
func (qb *QueryBuilder) Limit(n int) *QueryBuilder
Limit sets the LIMIT clause.
func (*QueryBuilder) Offset ¶
func (qb *QueryBuilder) Offset(n int) *QueryBuilder
Offset sets the OFFSET clause.
func (*QueryBuilder) OrWhere ¶
func (qb *QueryBuilder) OrWhere(condition string, args ...any) *QueryBuilder
OrWhere appends a WHERE condition (ORed with previous conditions).
func (*QueryBuilder) Order ¶
func (qb *QueryBuilder) Order(column string, dir string) *QueryBuilder
Order adds an ORDER BY clause.
func (*QueryBuilder) Where ¶
func (qb *QueryBuilder) Where(condition string, args ...any) *QueryBuilder
Where appends a WHERE condition (ANDed with previous conditions).
type UpdateBuilder ¶
type UpdateBuilder struct {
// contains filtered or unexported fields
}
UpdateBuilder builds an UPDATE query with parameterized placeholders.
func Update ¶
func Update(table string) *UpdateBuilder
Update creates a new UpdateBuilder for the given table.
func (*UpdateBuilder) Build ¶
func (ub *UpdateBuilder) Build() (string, []any)
Build produces the final parameterized SQL and argument slice.
func (*UpdateBuilder) Returning ¶
func (ub *UpdateBuilder) Returning(cols ...string) *UpdateBuilder
Returning adds a RETURNING clause.
func (*UpdateBuilder) Set ¶
func (ub *UpdateBuilder) Set(column string, value any) *UpdateBuilder
Set adds a column = value assignment.
func (*UpdateBuilder) Where ¶
func (ub *UpdateBuilder) Where(condition string, args ...any) *UpdateBuilder
Where appends a WHERE condition (ANDed with previous conditions).