sql

package
v0.7.5 Latest Latest
Warning

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

Go to latest
Published: Nov 17, 2026 License: Apache-2.0 Imports: 34 Imported by: 0

Documentation

Overview

Package sql provides database steps: services, seeds, selections (with polling and JSONB containment), JSON column assertions, row locks and trigger fault injection, before a row's insert, update or delete (PostgreSQL and Oracle).

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DuplicateKeyHint added in v0.1.10

func DuplicateKeyHint() string

DuplicateKeyHint completes the error of a seed whose row has a key the database already holds: data stays after a scenario, so it is most often a row of an earlier run, or of another scenario's seed.

func Ident

func Ident(s string) (string, error)

Ident validates a (possibly schema-qualified) identifier. Identifiers are emitted unquoted, as before, so the database's case folding still applies.

func Pack

func Pack() core.Pack

Pack returns the SQL pack.

Types

type Dataset

type Dataset struct {
	Tables []DatasetTable
}

Dataset is an ordered set of tables to insert (DbUnit/Database Rider compatible).

func LoadDataset

func LoadDataset(path string) (*Dataset, error)

LoadDataset reads a dataset file, choosing the format by extension.

type DatasetRow

type DatasetRow struct {
	Columns []string
	Values  []any
}

DatasetRow is an ordered column -> value mapping. A nil value is NULL.

type DatasetTable

type DatasetTable struct {
	Name string
	Rows []DatasetRow
}

DatasetTable is one table's rows. Each row lists its own columns, so rows may omit columns (their database defaults apply).

type Dialect

type Dialect struct {
	Name   string
	Driver string
	// ForUpdate is appended to SELECT to lock rows ("" when unsupported).
	ForUpdate string
	// ForUpdateHint is inserted after the table name (SQL Server style).
	ForUpdateHint string
	// Postgres enables JSONB steps and trigger simulation.
	Postgres bool
	// BackslashEscapes means backslashes in string literals are escapes (MySQL).
	BackslashEscapes bool
	// BitBooleans means there is no TRUE or FALSE to count on: booleans are 1 and
	// 0 (SQL Server's bits; Oracle's numbers, and its BOOLEAN takes them too).
	BitBooleans bool
	// Oracle enables its session, catalogs, numbers and triggers.
	Oracle bool
	// TimeOffset means a time's literal has its offset: every column of that database
	// takes one, and one that holds a moment needs it. A column with no time zone keeps
	// the wall clock.
	TimeOffset bool
}

Dialect captures the database-specific parts of the SQL pack.

func (Dialect) Literal

func (d Dialect) Literal(s string) string

Literal renders s as a SQL string literal, escaping quotes (and backslashes where they are escapes), so values containing quotes are safe.

func (Dialect) Time added in v0.2.14

func (d Dialect) Time(t time.Time, layout string) string

Time renders a moment as a literal of its wall-clock time, with its offset where the dialect takes one. A seed's relative times are the database's clock (databaseNow).

func (Dialect) Value added in v0.3.0

func (d Dialect) Value(v core.Value) string

Value renders a value of a step's table as a SQL value of its type (core.ParseValue, ADR 0014): a string is a string literal, an integer or a decimal a number, true and false booleans, and JSON (an object or an array) the string of its text. Null is the caller's: `IS NULL`.

type ScenarioContext

type ScenarioContext struct {
	// contains filtered or unexported fields
}

func Context

func Context(sc *core.Scenario) *ScenarioContext

Context returns the sql pack's context for a scenario: the databases registered in it with their connections, selections, triggers and row locks. The pack's steps and other packs share it.

func (*ScenarioContext) AddService

func (c *ScenarioContext) AddService(svc *Service) error

AddService registers a service under its Name. The first service registered is the default; registering a name twice is an error.

func (*ScenarioContext) Service

func (c *ScenarioContext) Service(name ...string) (*Service, error)

Service returns the named service, or the first one registered (the default) when no name is given.

func (*ScenarioContext) Services

func (c *ScenarioContext) Services() []*Service

Services returns the registered services in registration order.

type Selection

type Selection struct {
	Table   string
	Query   string
	Columns []string
	Rows    [][]any
	// contains filtered or unexported fields
}

Selection is a materialized query result (like DbUnit's ITable).

func (*Selection) Value

func (s *Selection) Value(row int, column string) (any, error)

Value returns the value of column in row (0-based); column names match case-insensitively.

type Service

type Service struct {
	Name     string
	URL      string
	User     string
	Password string
	Schema   string
	Target   Target
	DB       *dbsql.DB
	// contains filtered or unexported fields
}

Service is a database registered in a scenario.

func Connect

func Connect(sc *core.Scenario, name, url, user, password, schema string) (*Service, error)

Connect opens (or reuses, for the whole run) a connection pool and returns an unregistered service; add it with AddService. url may be a JDBC URL (jdbc:postgresql://...) or a native one.

func (*Service) AddSelection

func (svc *Service) AddSelection(sel *Selection)

AddSelection appends a selection, so the pack's selection steps see it as the next one.

func (*Service) Query

func (svc *Service) Query(ctx context.Context, table, query string) (*Selection, error)

Query runs a SELECT on the service's connection pool and returns the rows as a selection, without adding it.

func (*Service) Selection

func (svc *Service) Selection(i int) (*Selection, error)

Selection returns selection i (0-based: the 1st selection is 0).

func (*Service) Selections

func (svc *Service) Selections() []*Selection

Selections returns the service's selections in the order they were retrieved.

func (*Service) Triggers

func (svc *Service) Triggers() []*Trigger

Triggers returns the triggers created in the scenario, in order.

type Target

type Target struct {
	Dialect Dialect
	DSN     string
	// Host, Port and Database are known for PostgreSQL (dblink phantom inserts).
	Host, Port, Database string
}

Target is a resolved connection target.

func Resolve

func Resolve(rawURL, user, password string) (Target, error)

Resolve converts a JDBC URL (as used in feature files) or a native URL into a driver DSN, adding user and password.

type Trigger

type Trigger struct {
	Name, Function, Sequence, RaisedSequence, Table string
	MaxInvocations                                  int
	// Event is the write it goes before: insert (Trigger's zero value), update or delete.
	Event string
}

Trigger is a simulated trigger, before a row's insert, update or delete.

Jump to

Keyboard shortcuts

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