sqlp

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2026 License: MIT Imports: 5 Imported by: 0

README

go-sqlp Godoc

Development In Progress: sqlp's public API may change prior to version 1.0.0

Go SQL Parameters works with Go's text/template package to build parameterized SQL queries from templates and input context. Evaluating a template with go-sqlp produces parameterized SQL and a corresponding slice of input parameters ready to pass to db.Query.

Example with positional parameters:

type Params struct{
    Foo string
    Bar int
}

sqlTemplate := `
SELECT *
FROM table
WHERE true
{{if .Foo}}
AND foo = {{param .Foo}}
{{end}}
{{if .Bar}}
AND bar = {{param .Bar}}
{{end}}
`
p := Params{Foo: "value"}

exec := sqlp.NewExecution(sqlp.DefaultExecutionConfig)
// sqlp.DefaultFuncs binds `param` with defaults
tmpl, err := template.New("sql template").Funcs(exec.Funcs()).Parse(sqlTemplate)
if err != nil {
    return fmt.Errorf("failed to parse template: %w", err)
}

b := bytes.Buffer{}
if err := tmpl.Execute(&b, p); err != nil {
    return fmt.Errorf("failed to execute template: %w", err)
}

/*
SQL query evaluates to:
SELECT *
FROM table
WHERE true
AND foo = ?
*/
query := b.String()

// params evaluates to ["value"]
args := sqlp.Args()

row := db.QueryRow(query, args)
Slice Unrolling

param automatically unrolls slices (except []byte) into individual placeholders, making it easy to use with IN clauses:

type Params struct {
    IDs []int
}
p := Params{IDs: []int{1, 2, 3}}

// Template: "SELECT * FROM users WHERE id IN ({{param .IDs}})"
// Evaluates to: "SELECT * FROM users WHERE id IN (?, ?, ?)"
// Args: []any{1, 2, 3}

Installation

go get github.com/andrei-m/go-sqlp

Features

  • ? or ordinal numbered ($1, $2, etc.) SQL parameter syntax
  • The param template function name can be changed

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultExecutionConfig = ExecutionConfig{
	NumberedParameters: false,
	FuncName:           "param",
}

Functions

This section is empty.

Types

type Execution

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

Execution provides the "param" function for execution in templates and collects positional parameter values.

The "param" function handles different types as follows:

  • Slices (e.g., []int, []string): The slice is "unrolled" into a comma-separated list of placeholders (e.g., "?, ?, ?"), and each element is added as a separate argument. This is useful for "IN" clauses.
  • []byte: Treated as a single atomic parameter, not unrolled.
  • driver.Valuer: Types implementing this interface are treated as single atomic parameters even if they are slices (e.g., custom JSONB types).
  • All other types: Treated as a single atomic parameter.

func NewExecution

func NewExecution(conf ExecutionConfig) *Execution

NewExecution creates an Execution for use with a single text/template execution using provided configuration options.

func (*Execution) Args

func (e *Execution) Args() []any

Args returns query arguments for use with database/sql query functions.

func (*Execution) Func

func (e *Execution) Func() (string, any)

Func provides the "param" function to be included in a template.FuncMap for use with Template.Funcs

func (*Execution) Funcs

func (e *Execution) Funcs() template.FuncMap

Funcs provides a map containing the "param" function for use with Template.Funcs

type ExecutionConfig

type ExecutionConfig struct {
	NumberedParameters bool
	FuncName           string
}

ExecutionConfig configures options for an Execution.

Jump to

Keyboard shortcuts

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