query

package
v0.4.1 Latest Latest
Warning

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

Go to latest
Published: Nov 13, 2026 License: MIT Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const MaxLimit = 100

Variables

View Source
var ErrIncludeForbidden = fmt.Errorf("included table is not accessible")

ErrIncludeForbidden is returned when the authorizer rejects a table.

Functions

func ApplyCursor

func ApplyCursor(cursor *Cursor, paramOffset int) (string, []any)

func ApplyPagination

func ApplyPagination(limit, offset, paramOffset int) (string, []any)

func BuildCount

func BuildCount(tableName string, whereSql string, whereArgs []any) (string, []any)

func BuildDelete

func BuildDelete(tableName string, whereSql string, whereArgs []any) (string, []any)

func BuildInsert

func BuildInsert(tableName string, data map[string]any) (string, []any)

BuildInsert returns SQL and args for an INSERT, plus the deterministically-ordered column list. SQLite has no RETURNING * across all versions; callers should look up the inserted row via last_insert_rowid() (see ReadInsertedRow in helpers).

Column order is alphabetical so the generated SQL is stable for tests/logs.

func BuildOrderBy

func BuildOrderBy(orderBy map[string]string, table *tenant.SchemaTable) (string, error)

func BuildRowPolicy

func BuildRowPolicy(expression string, claims *tenant.AuthClaims, paramOffset int) (string, []any, error)

BuildRowPolicy takes a row_policy expression (e.g. "user_id = $auth.id") and replaces each $auth.id, $auth.email and $auth.role with a `?` placeholder, returning the values in the order they appear. paramOffset is unused for SQLite (positional `?`) but kept in the signature for API parity.

func BuildSelect

func BuildSelect(tableName string, columns []string, whereSql string, whereArgs []any, orderBySql string, paginationSql string, paginationArgs []any) (string, []any)

func BuildUpdate

func BuildUpdate(tableName string, data map[string]any, whereSql string, whereArgs []any) (string, []any)

BuildUpdate returns SQL and args for an UPDATE. SQLite does not support RETURNING portably here, so callers fetch updated rows separately via the same WHERE.

Args order: SET values first (textual order: alphabetical column order), then WHERE values.

func BuildWhere

func BuildWhere(where map[string]any, table *tenant.SchemaTable, paramOffset int) (string, []any, error)

BuildWhere validates columns against the schema and returns parameterized SQLite SQL. All placeholders use `?`; the caller appends args in the order returned. paramOffset is accepted for API compatibility with the multi-segment builders (cursor / row_policy / pagination append more args after) but is unused — SQLite uses positional placeholders, so only args order matters.

func EncodeCursor

func EncodeCursor(column string, value any, direction string) string

func NormalizeRow

func NormalizeRow(row map[string]any, loc *time.Location)

NormalizeRow converts SQLite-driver raw types to JSON-friendly values. time.Time values are converted to loc (defaults to UTC when nil). modernc.org/sqlite returns datetimes as time.Time directly when columns are declared TIMESTAMP/DATETIME, and as strings otherwise — those cases are handled by the row scanner in scan.go before reaching here.

func QueryMaps added in v0.4.0

func QueryMaps(ctx context.Context, q tenant.Querier, loc *time.Location, stmt string, args ...any) ([]map[string]any, error)

QueryMaps runs a query and reads all its rows; with UPDATE/DELETE ... RETURNING * they are exactly the rows the statement changed.

func QuoteIdent added in v0.1.1

func QuoteIdent(name string) string

QuoteIdent quotes a table or column name for SQLite. Embedded double quotes are doubled, so the result is always a single identifier. Never use Go's %q for this: its backslash escapes mean nothing to SQLite.

func ResolveIncludes

func ResolveIncludes(
	ctx context.Context,
	dbPool tenant.Querier,
	mainTableUUID string,
	mainResults []map[string]any,
	includes []IncludeRequest,
	sch *tenant.Schema,
	loc *time.Location,
	authorize IncludeAuthorizer,
) error

func ScanAllRowsToMaps

func ScanAllRowsToMaps(rows *sql.Rows, loc *time.Location) ([]map[string]any, error)

ScanAllRowsToMaps reads every row from a *sql.Rows result set, returning []map[colname]value. Driver-returned []byte values that look like UTF-8 strings are converted to string. time.Time values pass through.

Types

type Cursor

type Cursor struct {
	Column    string `json:"col"`
	Value     any    `json:"val"`
	Direction string `json:"dir"`
}

func DecodeCursor

func DecodeCursor(cursor string) (*Cursor, error)

type IncludeAuthorizer added in v0.1.1

type IncludeAuthorizer func(table *tenant.SchemaTable) (policySQL string, policyArgs []any, err error)

IncludeAuthorizer decides whether a related table may be included. It returns the extra WHERE fragment (e.g. the table's row policy) to apply, or an error when the caller may not read the table.

type IncludeRequest

type IncludeRequest struct {
	TableID string   `json:"table_id"`
	Select  []string `json:"select"`
}

Jump to

Keyboard shortcuts

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