query

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Jun 8, 2026 License: MIT Imports: 5 Imported by: 0

Documentation

Overview

Package query is part of the GoFastr framework. See https://github.com/DonaldMurillo/gofastr for documentation.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func MustIdent

func MustIdent(s string) string

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

func QuoteIdent(s string) string

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

func SafeIdent(s string) (string, error)

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_.].

func SafeQuote

func SafeQuote(s string) (string, error)

SafeQuote validates and quotes a SQL identifier in one step.

func Transaction

func Transaction(ctx context.Context, db *sql.DB, fn func(tx *sql.Tx) error) error

Transaction executes fn inside a database transaction. It begins a transaction, calls fn, and commits on success or rolls back on error.

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

Jump to

Keyboard shortcuts

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