superbasic

package module
v0.0.8 Latest Latest
Warning

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

Go to latest
Published: Jul 27, 2022 License: MIT Imports: 3 Imported by: 1

README

The superbasic SQL-Builder

go.dev reference Go Report Card golangci-lint codecov GitHub tag (latest SemVer)

This package provides utilities to build complex and dynamic but secure SQL queries.

go get github.com/wroge/superbasic

Simply put, superbasic helps you write prepared statements, and is also a kind of template engine that compiles expressions.

insert := superbasic.SQL("INSERT INTO presidents (?) VALUES ? RETURNING id",
	superbasic.Columns{"id", "first", "last"},
	superbasic.Values{
		{46, "Joe", "Biden"},
		{45, "Donald", "trump"},
		{44, "Barack", "Obama"},
		{43, "George W.", "Bush"},
		{42, "Bill", "Clinton"},
		{41, "George H. W.", "Bush"},
	},
)

fmt.Println(superbasic.ToPostgres(insert))
// INSERT INTO presidents (id, first, last) VALUES ($1, $2, $3), ($4, $5, $6), ($7, $8, $9), ($10, $11, $12), ($13, $14, $15), ($16, $17, $18) RETURNING id 
// [46 Joe Biden 45 Donald trump 44 Barack Obama 43 George W. Bush 42 Bill Clinton 41 George H. W. Bush]


update := superbasic.SQL("UPDATE presidents SET ? WHERE ?",
		[]superbasic.Sqlizer{
			superbasic.SQL("first = ?", "Donald"),
			superbasic.SQL("last = ?", "Trump"),
		},
		superbasic.SQL("id = ?", 45),
	)

fmt.Println(update.ToSQL())
// UPDATE presidents SET first = ?, last = ? WHERE id = ? 
// [Trump Donald 45]


columns := []string{"id", "first", "last"}
lastnames := []any{"Bush", "Clinton"}
sort := "first"

query := superbasic.Append(
	superbasic.SQL("SELECT ? FROM presidents", superbasic.Columns(columns)),
	superbasic.If(len(lastnames) > 0, superbasic.SQL(" WHERE last IN ?", superbasic.Values{lastnames})),
	superbasic.If(sort != "", superbasic.SQL(" ORDER BY ?", superbasic.SQL(sort))),
)

fmt.Println(query.ToSQL())
// SELECT id, first, last FROM presidents WHERE last IN (?, ?) ORDER BY first [Bush,Clinton]


delete := superbasic.SQL("DELETE FROM presidents WHERE ?",
	superbasic.Join(" AND ",
		superbasic.SQL("last = ?", "Bush"),
		superbasic.SQL("first = ?", "Joe"),
	),
)

fmt.Println(superbasic.ToPostgres(delete))
// DELETE FROM presidents WHERE last = $1 AND first = $2 [Bush Joe]

builder := superbasic.NewBuilder().WriteSQL("SELECT ")

builder.Write(superbasic.Columns(columns)).WriteSQL(" FROM presidents")

builder.Write(superbasic.SQL(" WHERE first IN (?, ?)", "Barack", "Donald"))

fmt.Println(builder.ToSQL())
// SELECT id, first, last FROM presidents WHERE first IN (?, ?) [Barack Donald]

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidNumberOfArguments = errors.New("invalid number of arguments")

Functions

func ToPostgres

func ToPostgres(expr Sqlizer) (string, []any, error)

ToPostgres transforms a Sqlizer to a valid postgres statement.

Types

type Columns added in v0.0.7

type Columns []string

Columns in a Sqlizer that joins a list of identifiers with ", ".

func (Columns) ToSQL added in v0.0.7

func (c Columns) ToSQL() (string, []any, error)

ToSQL is the implementation of the Sqlizer interface.

type Expression

type Expression struct {
	SQL  string
	Args []any
	Err  error
}

Expression represents a prepared statement.

func Append

func Append(expr ...Sqlizer) Expression

Append builds an Expression by appending Sqlizer's.

func If

func If(condition bool, then Sqlizer) Expression

If returns then if condition is true. If the condition is false, an empty Expression is returned.

func IfElse added in v0.0.4

func IfElse(condition bool, then, els Sqlizer) Expression

IfElse returns then Sqlizer on true and els on false as Expression.

func Join

func Join(sep string, expr ...Sqlizer) Expression

Join builds an Expression by joining Sqlizer's with a separator.

func SQL

func SQL(template string, expressions ...any) Expression

SQL takes a template with placeholders into which expressions can be compiled. Expressions can be of type Sqlizer or []Sqlizer. []Sqlizer gets joined to an Expression with ", " as separator. All other values will be put into the arguments of a prepared statement. You can escape '?' by using '??'.

func ToExpression added in v0.0.5

func ToExpression(expr Sqlizer) Expression

ToExpression returns a valid Expression from a Sqlizer.

func (Expression) ToSQL

func (e Expression) ToSQL() (string, []any, error)

ToSQL is the implementation of the Sqlizer interface.

type Sqlizer

type Sqlizer interface {
	ToSQL() (string, []any, error)
}

Sqlizer returns a prepared statement.

type Values added in v0.0.3

type Values [][]any

Values is a Sqlizer that takes a list of values.

func (Values) ToSQL added in v0.0.7

func (v Values) ToSQL() (string, []any, error)

ToSQL is the implementation of the Sqlizer interface.

Jump to

Keyboard shortcuts

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