Help us shape the future of Go! Take the 2026 Go Developer Survey

sqlcrud

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 17 Imported by: 0

Documentation

Overview

Package sqlcrud runs the typed insert, update and delete actions for Orchard's SQL providers over database/sql. Values are always bound as parameters, converted by the target column's kind; identifiers are quoted per dialect. Callers validate with pkg/dbschema first.

Index

Constants

This section is empty.

Variables

View Source
var (
	Postgres = Dialect{Placeholder: dollar, Quote: doubleQuote, Returning: true}
	SQLite   = Dialect{Placeholder: question, Quote: doubleQuote, Returning: true}
	MySQL    = Dialect{Placeholder: question, Quote: backtick, Returning: false}
)

The three built-in dialects.

Functions

func Delete

func Delete(ctx context.Context, q querier, d Dialect, t *dbschema.Table, where cty.Value) (map[string]cty.Value, error)

Delete deletes the rows of t matching where, and returns rows_affected.

func Insert

func Insert(ctx context.Context, q querier, d Dialect, t *dbschema.Table, values cty.Value) (map[string]cty.Value, error)

Insert inserts one row into t and returns the full row, every column decoded by kind. values is a known object of column = value.

func PlanAction

func PlanAction(ctx context.Context, a *Actions, prefix, action string, attrs map[string]cty.Value) (provider.Plan, error)

PlanAction implements provider.ActionPlanner for a provider whose typed actions are a (nil before Configure or a snapshot). Actions that aren't typed have nothing to check.

func Restore

func Restore(ctx context.Context, q querier, d Dialect, t *dbschema.Table, rows cty.Value) (map[string]cty.Value, error)

Restore writes each row's values back to t, matching on the primary key. rows is a list or tuple of objects, as RowsMatching returned them; the key columns locate a row and the rest are set.

func RowKey

func RowKey(t *dbschema.Table, row map[string]cty.Value) (key string, ok bool)

RowKey identifies a row by its primary key values, for ownership tracking. ok is false when a key column is missing or null.

func RowsMatching

func RowsMatching(ctx context.Context, q querier, d Dialect, t *dbschema.Table, cols []string, where cty.Value, limit int) ([]map[string]cty.Value, error)

RowsMatching reads the primary key and cols of the rows of t matching where, up to limit+1 rows, so a caller can tell when there were more than limit. Values are decoded by kind.

func SelectRow

func SelectRow(ctx context.Context, q querier, d Dialect, t *dbschema.Table, where cty.Value) (map[string]cty.Value, error)

SelectRow reads the first row of t matching where, every column decoded by kind.

func Update

func Update(ctx context.Context, q querier, d Dialect, t *dbschema.Table, set, where cty.Value) (map[string]cty.Value, error)

Update sets columns on the rows of t matching where, and returns rows_affected.

func Verb

func Verb(prefix, action string) (string, bool)

Verb returns "insert", "update" or "delete" when action is one of the typed actions for prefix, or false.

Types

type Actions

type Actions struct {
	// Prefix is the provider's action prefix, e.g. "postgres".
	Prefix  string
	Dialect Dialect
	// Conn returns a connection for one statement.
	Conn func(ctx context.Context) (*sql.Conn, error)
	// Introspect reads the live schema. It must acquire and release its
	// own connection: Actions calls it before taking one for the
	// statement, which matters for SQLite's single-connection pool.
	// Introspect is nil for actions built from a snapshot.
	Introspect func(ctx context.Context) (*dbschema.Schema, error)
	// Strict makes Plan reject tables that don't exist, rather than
	// skipping them. Snapshots are strict: they're the database's
	// shape as committed, so a missing table is most likely a typo.
	Strict bool
	// contains filtered or unexported fields
}

Actions runs one provider's typed actions (<prefix>_insert, _update, _delete) against the live database: it reads the schema once per run, validates each action against it, then executes.

func FromSnapshot

func FromSnapshot(prefix string, d Dialect, dialect string, snap *snapshot.Snapshot) (*Actions, error)

FromSnapshot builds plan-only Actions from a "database" snapshot, which must be of the given dialect ("postgres", ...).

func (*Actions) Execute

func (a *Actions) Execute(ctx context.Context, action string, attrs map[string]cty.Value) (map[string]cty.Value, error)

Execute validates and runs a typed action, returning its outputs.

func (*Actions) ExecuteUndo

func (a *Actions) ExecuteUndo(ctx context.Context, action string, attrs map[string]cty.Value, opts provider.InverseOptions) (map[string]cty.Value, *provider.Invocation, error)

ExecuteUndo runs a typed action like Execute and also describes how to undo it (see undo).

func (*Actions) Plan

func (a *Actions) Plan(ctx context.Context, action string, attrs map[string]cty.Value) (provider.Plan, error)

Plan checks a typed action before the run and returns its output type (for an insert, an object of the table's columns) and any hints. Against the live database a table that doesn't exist yet is skipped, because an earlier action in the scenario may create it (Execute checks again); against a snapshot (Strict) it's an error.

func (*Actions) Verb

func (a *Actions) Verb(action string) (string, bool)

Verb returns "insert", "update" or "delete" for one of this provider's typed actions, or false.

type Dialect

type Dialect struct {
	// Placeholder returns the nth (1-based) bind parameter.
	Placeholder func(n int) string
	// Quote quotes an identifier.
	Quote func(ident string) string
	// Returning is true when INSERT ... RETURNING * gives the new row.
	// Otherwise Insert reads it back by primary key.
	Returning bool
}

Dialect is how one database spells the statements.

Jump to

Keyboard shortcuts

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