superbasic

package module
v0.0.12 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2022 License: MIT Imports: 2 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

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


insert := superbasic.Compile("INSERT INTO presidents (?) VALUES ? RETURNING id",
	superbasic.Idents("nr", "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(superbasic.ToPositional("$", insert))
// INSERT INTO presidents (nr, 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("nr", 45),
)

fmt.Println(update.ToSQL())
// UPDATE presidents SET first = ?, last = ? WHERE nr = ?
// [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 != nil, 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

This section is empty.

Functions

func ToPositional added in v0.0.11

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

Types

type Expression

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

Expression represents a prepared statement.

func And added in v0.0.6

func And(expr ...Expression) Expression

And returns a AND expression.

func Append

func Append(expressions ...Expression) Expression

Append expressions.

func Between added in v0.0.12

func Between(ident string, lower, higher any) Expression

Between returns a BETWEEN expression.

func Cast added in v0.0.12

func Cast(value any, as string) Expression

Cast returns a CAST expression.

func Compile added in v0.0.10

func Compile(template string, expressions ...Expression) 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 Expression) 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 Expression) 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 a IN expression.

func IsNotNull added in v0.0.12

func IsNotNull(ident string) Expression

IsNotNull returns a IS NOT NULL expression.

func IsNull added in v0.0.12

func IsNull(ident string) Expression

IsNull returns a IS NULL expression.

func Join

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

Join joins expressions by 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 Like added in v0.0.12

func Like(ident string, value any) Expression

Like returns a LIKE expression.

func Not added in v0.0.6

func Not(expr Expression) Expression

Not returns a NOT expression.

func NotBetween added in v0.0.12

func NotBetween(ident string, lower, higher any) Expression

NotBetween returns a NOT BETWEEN expression.

func NotEquals added in v0.0.6

func NotEquals(ident string, value any) Expression

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

func NotIn added in v0.0.12

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

NotIn returns a NOT IN expression.

func NotLike added in v0.0.12

func NotLike(ident string, value any) Expression

NotLike returns a NOT LIKE expression.

func Or added in v0.0.6

func Or(left, right Expression) Expression

Or returns a OR expression.

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.

type ExpressionError added in v0.0.11

type ExpressionError struct{}

ExpressionError is returned if expressions are nil.

func (ExpressionError) Error added in v0.0.11

func (e ExpressionError) Error() string

func (ExpressionError) ToSQL added in v0.0.11

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

type NumberOfArgumentsError added in v0.0.11

type NumberOfArgumentsError struct{}

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

func (NumberOfArgumentsError) Error added in v0.0.11

func (e NumberOfArgumentsError) Error() string

func (NumberOfArgumentsError) ToSQL added in v0.0.11

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

Jump to

Keyboard shortcuts

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