superbasic

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 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 an SQL template and thus offers an alternative to conventional query builders.

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

Compile Expressions into SQL

You can compile a list of values into an SQL template...

expr := superbasic.Compile("INSERT INTO presidents (nr, first, last) VALUES ? RETURNING id", 
    []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.ToPositional("$", expr)) 
// 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] 

or any other expression. Lists of expressions are always joined by ", ".

expr := superbasic.Compile("UPDATE presidents SET ? WHERE ?",
    []superbasic.Expression{
        superbasic.SQL("first = ?", "Donald"),
        superbasic.SQL("last = ?", "Trump"),
    },
    superbasic.SQL("nr = ?", 45),
)

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

Query

Additionally, there are Query, Insert, Update and Delete helpers that can be used to create expressions.

query := superbasic.Query{
    From: superbasic.SQL("presidents"),
    Where: superbasic.Compile("(? OR ?)",
        superbasic.SQL("last = ?", "Bush"),
        superbasic.SQL("nr >= ?", 45),
    ),
    OrderBy: superbasic.SQL("nr"),
    Limit:   3,
}

fmt.Println(query.ToSQL())
// SELECT * FROM presidents WHERE (last = ? OR nr >= ?) ORDER BY nr LIMIT 3
// [Bush 44]

The Query helper can be used as a reference to build your own expressions...

type Query struct {
	With    Expression
	Select  Expression
	From    Expression
	Where   Expression
	GroupBy Expression
	Having  Expression
	Window  Expression
	OrderBy Expression
	Limit   uint64
	Offset  uint64
}

func (q Query) ToSQL() (string, []any, error) {
	return Join(" ",
		If(q.With != nil, Compile("WITH ?", q.With)),
		SQL("SELECT"),
		IfElse(q.Select != nil, q.Select, SQL("*")),
		If(q.From != nil, Compile("FROM ?", q.From)),
		If(q.Where != nil, Compile("WHERE ?", q.Where)),
		If(q.GroupBy != nil, Compile("GROUP BY ?", q.GroupBy)),
		If(q.Having != nil, Compile("HAVING ?", q.Having)),
		If(q.Having != nil, Compile("WINDOW ?", q.Window)),
		If(q.OrderBy != nil, Compile("ORDER BY ?", q.OrderBy)),
		If(q.Limit > 0, SQL(fmt.Sprintf("LIMIT %d", q.Limit))),
		If(q.Offset > 0, SQL(fmt.Sprintf("OFFSET %d", q.Offset))),
	).ToSQL()
}

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 Compiler added in v0.3.0

type Compiler struct {
	Template    string
	Expressions []any
}

func Compile added in v0.0.10

func Compile(template string, expressions ...any) Compiler

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

func (Compiler) ToSQL added in v0.3.0

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

type Delete added in v0.0.3

type Delete struct {
	From  string
	Where Expression
}

func (Delete) ToSQL added in v0.0.3

func (d Delete) ToSQL() (string, []any, error)

type Expression

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

func If

func If(condition bool, then Expression) Expression

func IfElse added in v0.0.4

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

type ExpressionError added in v0.0.11

type ExpressionError struct {
	Err error
}

ExpressionError is returned if expressions are nil.

func (ExpressionError) Error added in v0.0.11

func (e ExpressionError) Error() string

func (ExpressionError) Unwrap added in v0.2.0

func (e ExpressionError) Unwrap() error

type Insert added in v0.0.3

type Insert struct {
	Into    string
	Columns []string
	Data    []Values
}

func (Insert) ToSQL added in v0.0.3

func (i Insert) ToSQL() (string, []any, error)

type Joiner added in v0.3.0

type Joiner struct {
	Sep         string
	Expressions []Expression
}

func Append

func Append(expressions ...Expression) Joiner

func Join

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

func (Joiner) ToSQL added in v0.3.0

func (j Joiner) ToSQL() (string, []any, error)

type NumberOfArgumentsError added in v0.0.11

type NumberOfArgumentsError struct {
	Got, Want int
}

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

type Query added in v0.0.6

type Query struct {
	With    Expression
	Select  Expression
	From    Expression
	Where   Expression
	GroupBy Expression
	Having  Expression
	Window  Expression
	OrderBy Expression
	Limit   uint64
	Offset  uint64
}

func (Query) ToSQL added in v0.0.6

func (q Query) ToSQL() (string, []any, error)

type Raw added in v0.2.1

type Raw struct {
	SQL  string
	Args []any
}

func SQL

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

func (Raw) ToSQL added in v0.2.1

func (r Raw) ToSQL() (string, []any, error)

type Update added in v0.0.3

type Update struct {
	Table string
	Sets  []Expression
	Where Expression
}

func (Update) ToSQL added in v0.0.3

func (u Update) ToSQL() (string, []any, error)

type Values added in v0.0.3

type Values []any

func (Values) ToSQL added in v0.0.7

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

Jump to

Keyboard shortcuts

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