bind

package
v1.5.1 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2026 License: BSD-3-Clause Imports: 6 Imported by: 0

Documentation

Overview

Package bind rewrites canonical :name SQL and named arguments for database drivers.

Tidal transactions accept :name placeholders regardless of backend. This package converts them to the style the driver expects ($1 for Postgres, named for SQLite, etc.).

Example:

query := "INSERT INTO users (id, email) VALUES (:id, :email)"
args := []sql.NamedArg{
	sql.Named("id", id),
	sql.Named("email", email),
}
bound, err := bind.Rewrite(query, args, bind.Ordered)
if err != nil {
	return err
}
// bound.SQL() => "INSERT INTO users (id, email) VALUES ($1, $2)"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BoundQuery

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

Holds a query and arguments ready for database/sql execution.

func Rewrite

func Rewrite(query string, args []sql.NamedArg, ph PlaceholderType) (*BoundQuery, error)

Rewrites canonical :name SQL and named arguments for the given driver style.

Benchmark note: Run `go test ./bind -run '^$' -bench '^BenchmarkRewrite$' -benchmem -count=1` to capture local Rewrite parser performance.

Last observed benchmark on darwin/arm64 (Apple M2): - OrderedSimple: 937.9 ns/op, 198 B/op, 6 allocs/op - OrderedComplex: 1407 ns/op, 308 B/op, 5 allocs/op - PositionalSimple: 549.4 ns/op, 256 B/op, 4 allocs/op

func (*BoundQuery) Args

func (b *BoundQuery) Args() []any

Args returns argument values in the order required by the rewritten query.

func (*BoundQuery) SQL

func (b *BoundQuery) SQL() string

SQL returns the query string with placeholders rewritten for the target driver.

type PlaceholderType

type PlaceholderType uint8

PlaceholderType selects how :name placeholders are rewritten for a database driver.

const (
	UnknownPlaceholder PlaceholderType = iota
	Positional
	Ordered
	Named
	AtP
)

func PlaceholderFor

func PlaceholderFor(provider string) PlaceholderType

Selects the placeholder type from a DSN provider name.

Jump to

Keyboard shortcuts

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