superbasic

package module
v1.1.6 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2022 License: MIT Imports: 2 Imported by: 1

README

The superbasic SQL-Builder

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

superbasic.Compile compiles expressions into an SQL template and thus offers an alternative to conventional query builders.

If you need support for multiple SQL dialects, take a look at wroge/esperanto.

package main

import (
	"fmt"

	"github.com/wroge/superbasic"
)

func main() {
	expr := superbasic.Compile("INSERT INTO presidents (nr, first, last) VALUES ? RETURNING id",
		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.Finalize("$%d", 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]

	expr = superbasic.Compile("UPDATE presidents SET ? WHERE ?",
		superbasic.Join(", ",
			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]
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Finalize added in v1.1.3

func Finalize(placeholder string, expression Expression) (string, []any, error)

Finalize takes a static placeholder like '?' or a positional placeholder containing '%d'. Escaped placeholders ('??') are replaced to '?' when placeholder argument is not '?'.

func Replace added in v1.1.6

func Replace(placeholder string, sql string) (string, int)

Replace takes a static placeholder like '?' or a positional placeholder containing '%d'. Escaped placeholders ('??') are replaced to '?' when placeholder argument is not '?'.

Types

type Compiler added in v0.3.0

type Compiler struct {
	Template    string
	Expressions []Expression
}

func Compile added in v0.0.10

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

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

func (Compiler) ToSQL added in v0.3.0

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

type Error added in v0.0.2

type Error struct {
	Err error
}

Error wraps any error in this package and can be used to create an Expression.

func (Error) Error added in v1.1.6

func (e Error) Error() string

func (Error) ToSQL added in v1.1.6

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

func (Error) Unwrap added in v1.1.6

func (e Error) Unwrap() 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 {
	Position int
}

ExpressionError is returned by the Compile Expression, if an expression is nil.

func (ExpressionError) Error added in v0.0.11

func (e ExpressionError) Error() string

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 {
	SQL                     string
	Placeholders, Arguments 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 Raw added in v0.2.1

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

func SQL

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

func Value added in v0.0.10

func Value(a any) Raw

func (Raw) ToSQL added in v0.2.1

func (r Raw) 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