superbasic

package module
v0.0.13 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.SQL 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 is compiled to Join(", ", expr...).  
// []any is compiled to (?, ?).  
// [][]any is compiled to (?, ?), (?, ?).  
// Escape '?' by using '??'.

insert := superbasic.SQL("INSERT INTO presidents (?) VALUES ? RETURNING id",
	superbasic.SQL("nr, first, last"),
	[][]any{
		{46, "Joe", "Biden"},
		{45, "Donald", "trump"},
		{44, "Barack", "Obama"},
		{43, "George W.", "Bush"},
		{42, "Bill", "Clinton"},
		{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.SQL("UPDATE presidents SET ? WHERE ?",
	superbasic.Join(", ",
		superbasic.EqualsIdent("first", "Donald"),
		superbasic.EqualsIdent("last", "Trump"),
	),
	superbasic.EqualsIdent("nr", 45),
)

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


search := superbasic.And(
	superbasic.InIdent("last", []any{"Bush", "Clinton"}),
	superbasic.Not(superbasic.GreaterIdent("nr", 42)),
)
sort := "first"

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

fmt.Println(query.ToSQL())
// SELECT nr, first, last FROM presidents WHERE last IN (?, ?) AND NOT (nr > ?) ORDER BY first
// [Bush Clinton 42]

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 ...any) Expression

And returns a AND expression.

func Append

func Append(expressions ...any) Expression

Append expressions.

func Between added in v0.0.12

func Between(expr, lower, higher any) Expression

Between returns a BETWEEN expression.

func BetweenIdent added in v0.0.13

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

BetweenIdent returns a BETWEEN expression.

func Cast added in v0.0.12

func Cast(expr any, as string) Expression

Cast returns a CAST expression.

func CastIdent added in v0.0.13

func CastIdent(ident string, as string) Expression

CastIdent returns a CAST expression.

func Equals added in v0.0.6

func Equals(left, right any) Expression

Equals returns an expression with an '=' sign.

func EqualsIdent added in v0.0.13

func EqualsIdent(ident string, value any) Expression

EqualsIdent returns an expression with an '=' sign.

func Greater added in v0.0.6

func Greater(left, right any) Expression

Greater returns an expression with an '>' sign.

func GreaterIdent added in v0.0.13

func GreaterIdent(ident string, value any) Expression

GreaterIdent returns an expression with an '>' sign.

func GreaterOrEquals added in v0.0.6

func GreaterOrEquals(left, right any) Expression

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

func GreaterOrEqualsIdent added in v0.0.13

func GreaterOrEqualsIdent(ident string, value any) Expression

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

func If

func If(condition bool, then any) 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 any) Expression

IfElse returns an expression based on a condition.

func In added in v0.0.10

func In(left, right any) Expression

In returns a IN expression.

func InIdent added in v0.0.13

func InIdent(ident string, value any) Expression

InIdent returns a IN expression.

func IsNotNull added in v0.0.12

func IsNotNull(expr any) Expression

IsNotNull returns a IS NOT NULL expression.

func IsNotNullIdent added in v0.0.13

func IsNotNullIdent(ident string) Expression

IsNotNullIdent returns a IS NOT NULL expression.

func IsNull added in v0.0.12

func IsNull(expr any) Expression

IsNull returns a IS NULL expression.

func IsNullIdent added in v0.0.13

func IsNullIdent(ident string) Expression

IsNullIdent returns a IS NULL expression.

func Join

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

Join joins expressions by a separator.

func Less added in v0.0.6

func Less(left, right any) Expression

Less returns an expression with an '<' sign.

func LessIdent added in v0.0.13

func LessIdent(ident string, value any) Expression

LessIdent returns an expression with an '<' sign.

func LessOrEquals added in v0.0.6

func LessOrEquals(left, right any) Expression

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

func LessOrEqualsIdent added in v0.0.13

func LessOrEqualsIdent(ident string, value any) Expression

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

func Like added in v0.0.12

func Like(left, right any) Expression

Like returns a LIKE expression.

func LikeIdent added in v0.0.13

func LikeIdent(ident string, value any) Expression

LikeIdent returns a LIKE expression.

func Not added in v0.0.6

func Not(expr any) Expression

Not returns a NOT expression.

func NotBetween added in v0.0.12

func NotBetween(expr, lower, higher any) Expression

NotBetween returns a NOT BETWEEN expression.

func NotBetweenIdent added in v0.0.13

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

NotBetweenIdent returns a NOT BETWEEN expression.

func NotEquals added in v0.0.6

func NotEquals(left, right any) Expression

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

func NotEqualsIdent added in v0.0.13

func NotEqualsIdent(ident string, value any) Expression

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

func NotIn added in v0.0.12

func NotIn(left, right any) Expression

NotIn returns a NOT IN expression.

func NotInIdent added in v0.0.13

func NotInIdent(ident string, value any) Expression

NotInIdent returns a NOT IN expression.

func NotLike added in v0.0.12

func NotLike(left, right any) Expression

NotLike returns a NOT LIKE expression.

func NotLikeIdent added in v0.0.13

func NotLikeIdent(ident string, value any) Expression

NotLikeIdent returns a LIKE expression.

func Or added in v0.0.6

func Or(left, right any) Expression

Or returns a OR expression.

func SQL

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

SQL takes a template with placeholders into which expressions can be compiled. Expression []Expression is compiled to Join(", ", expr...). Expression []any is compiled to (?, ?). Expression [][]any is compiled to (?, ?), (?, ?). Escape '?' by using '??'.

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