superbasic

package module
v0.0.10 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 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)

superbasic.Compile compiles expressions into a template.

In addition, this package provides a set of functions that can be used to create expressions.

go get github.com/wroge/superbasic

// Sqlizer represents a prepared statement.
type Sqlizer interface {
	ToSQL() (string, []any, error)
}


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

fmt.Println(insert.ToPositional("$"))
// 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.Compile("UPDATE presidents SET ? WHERE ?",
	superbasic.Join(", ",
		superbasic.Equals("first", "Donald"),
		superbasic.Equals("last", "Trump"),
	),
	superbasic.Equals("id", 45),
)

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


search := superbasic.In("last", "Bush", "Clinton")
sort := "first"

query := superbasic.Append(
	superbasic.SQL("SELECT id, first, last FROM presidents"),
	superbasic.If(search.SQL != "", superbasic.Compile(" WHERE ?", search)),
	superbasic.If(sort != "", superbasic.SQL(fmt.Sprintf(" ORDER BY %s", sort))),
)

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

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrInvalidExpression = errors.New("invalid expression")

ErrInvalidExpression is returned if expressions are nil.

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

ErrInvalidNumberOfArguments is returned if arguments doesn't match the number of placeholders.

Functions

This section is empty.

Types

type Expression

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

Expression represents a prepared statement.

func Append

func Append(expressions ...Sqlizer) Expression

Append builds an Expression by appending Sqlizer's.

func Compile added in v0.0.10

func Compile(template string, expressions ...Sqlizer) Expression

Compile takes a template with placeholders into which expressions can be compiled. You can escape '?' by using '??'.

func Equals added in v0.0.6

func Equals(ident string, value any) Expression

Equals returns an expression with an '=' sign.

func Greater added in v0.0.6

func Greater(ident string, value any) Expression

Greater returns an expression with an '>' sign.

func GreaterOrEquals added in v0.0.6

func GreaterOrEquals(ident string, value any) Expression

GreaterOrEquals returns an expression with an '>=' sign.

func Idents added in v0.0.10

func Idents(idents ...string) Expression

Idents joins idents with ", " to an expression.

func If

func If(condition bool, then Sqlizer) Expression

If returns an expression based on a condition. If false an empty expression is returned.

func IfElse added in v0.0.4

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

IfElse returns an expression based on a condition.

func In added in v0.0.10

func In(ident string, values ...any) Expression

In returns an expression with an 'IN' sign.

func Join

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

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

func Less added in v0.0.6

func Less(ident string, value any) Expression

Less returns an expression with an '<' sign.

func LessOrEquals added in v0.0.6

func LessOrEquals(ident string, value any) Expression

LessOrEquals returns an expression with an '<=' sign.

func NotEquals added in v0.0.6

func NotEquals(ident string, value any) Expression

NotEquals returns an expression with an '<>' sign.

func SQL

func SQL(sql string, args ...any) Expression

SQL returns an expression.

func Value added in v0.0.10

func Value(a any) Expression

Value returns an expression with a placeholder.

func Values added in v0.0.3

func Values(a ...any) Expression

Values returns an expression with placeholders.

func (Expression) ToPositional added in v0.0.10

func (e Expression) ToPositional(placeholder string) (string, []any, error)

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 represents a prepared statement.

Jump to

Keyboard shortcuts

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