superbasic

package module
v0.2.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)

This package compiles expressions into SQL strings and thus offers an alternative to conventional query builders.

Compile Values into SQL

In this example, a list of values is compiled into an SQL string. https://github.com/wroge/superbasic/blob/5daab8f309f59becbe89872659894345e5976221/example/main.go#L11-L26

Compile Expressions into SQL

Similarly, expressions can be compiled in place of placeholders, which offers many new possibilities to create prepared statements.

https://github.com/wroge/superbasic/blob/5daab8f309f59becbe89872659894345e5976221/example/main.go#L28-L38

Queries

With this library it is particularly easy to create dynamic queries based on conditions. In this example, the WHERE-clause is only included if a corresponding expression exists.

https://github.com/wroge/superbasic/blob/5daab8f309f59becbe89872659894345e5976221/example/main.go#L40-L52

Of course you can do the same with this Query.

https://github.com/wroge/superbasic/blob/5daab8f309f59becbe89872659894345e5976221/example/main.go#L54-L62

Insert, Update, Delete

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

https://github.com/wroge/superbasic/blob/5daab8f309f59becbe89872659894345e5976221/example/main.go#L64-L80

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 Append

type Append []Expression

func (Append) ToSQL added in v0.2.1

func (b Append) ToSQL() (string, []any, error)

type Builder added in v0.0.5

type Builder struct {
	// contains filtered or unexported fields
}

func Build added in v0.2.1

func Build() *Builder

func (*Builder) Append added in v0.2.1

func (b *Builder) Append(expressions ...Expression) *Builder

func (*Builder) If added in v0.2.1

func (b *Builder) If(condition bool, then Expression) *Builder

func (*Builder) IfElse added in v0.2.1

func (b *Builder) IfElse(condition bool, then Expression, els Expression) *Builder

func (*Builder) Join added in v0.2.1

func (b *Builder) Join(sep string, expressions ...Expression) *Builder

func (*Builder) SQL added in v0.2.1

func (b *Builder) SQL(sql string, args ...any) *Builder

func (*Builder) Space added in v0.2.1

func (b *Builder) Space() *Builder

func (*Builder) ToSQL added in v0.0.5

func (b *Builder) 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 Join

type Join struct {
	Sep         string
	Expressions []Expression
}

func (Join) ToSQL added in v0.2.1

func (j Join) 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
	Err  error
}

func SQL

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

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

func (Raw) ToSQL added in v0.2.1

func (e 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)

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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