superbasic

package module
v1.1.11 Latest Latest
Warning

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

Go to latest
Published: Sep 15, 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.

To scan rows to types, i recommend wroge/scan.

Have a look at wroge/esperanto as well. It is a database access layer that can help you better organize your queries.

package main

import (
	"fmt"

	"github.com/wroge/superbasic"
)

func main() {
	// 1. CREATE SCHEMA

	create := superbasic.Compile("CREATE TABLE presidents (\n\t?\n)",
		superbasic.Join(",\n\t",
			superbasic.SQL("nr SERIAL PRIMARY KEY"),
			superbasic.SQL("first TEXT NOT NULL"),
			superbasic.SQL("last TEXT NOT NULL"),
		),
	)

	fmt.Println(create.ToSQL())
	// CREATE TABLE presidents (
	//	nr SERIAL PRIMARY KEY,
	//	first TEXT NOT NULL,
	//	last TEXT NOT NULL
	// )

	// 2. INSERT

	insert := superbasic.Join(" ",
		superbasic.SQL("INSERT INTO presidents (first, last)"),
		superbasic.Compile("VALUES ?",
			superbasic.Join(", ",
				superbasic.Map(presidents,
					func(_ int, president President) superbasic.Expression {
						return superbasic.Values{president.First, president.Last}
					})...,
			),
		),
		superbasic.SQL("RETURNING nr"),
	)

	fmt.Println(superbasic.Finalize("$%d", insert))
	// INSERT INTO presidents (first, last) VALUES ($1, $2), ($3, $4) RETURNING nr [George Washington John Adams]
}

type President struct {
	First string
	Last  string
}

var presidents = []President{
	{"George", "Washington"},
	{"John", "Adams"},
	// {"Thomas", "Jefferson"},
	// {"James", "Madison"},
	// {"James", "Monroe"},
	// {"John Quincy", "Adams"},
	// {"Andrew", "Jackson"},
	// {"Martin", "Van Buren"},
	// {"William Henry", "Harrison"},
	// {"John", "Tyler"},
	// {"James K.", "Polk"},
	// {"Zachary", "Taylor"},
	// {"Millard", "Fillmore"},
	// {"Franklin", "Pierce"},
	// {"James", "Buchanan"},
	// {"Abraham", "Lincoln"},
	// {"Andrew", "Johnson"},
	// {"Ulysses S.", "Grant"},
	// {"Rutherford B.", "Hayes"},
	// {"James A.", "Garfield"},
	// {"Chester A.", "Arthur"},
	// {"Grover", "Cleveland"},
	// {"Benjamin", "Harrison"},
	// {"Grover", "Cleveland"},
	// {"William", "McKinley"},
	// {"Theodore", "Roosevelt"},
	// {"William Howard", "Taft"},
	// {"Woodrow", "Wilson"},
	// {"Warren G.", "Harding"},
	// {"Calvin", "Coolidge"},
	// {"Herbert", "Hoover"},
	// {"Franklin D.", "Roosevelt"},
	// {"Harry S.", "Truman"},
	// {"Dwight D.", "Eisenhower"},
	// {"John F.", "Kennedy"},
	// {"Lyndon B.", "Johnson"},
	// {"Richard", "Nixon"},
	// {"Gerald", "Ford"},
	// {"Jimmy", "Carter"},
	// {"Ronald", "Reagan"},
	// {"George H. W.", "Bush"},
	// {"Bill", "Clinton"},
	// {"George W.", "Bush"},
	// {"Barack", "Obama"},
	// {"Donald", "Trump"},
	// {"Joe", "Biden"},
}

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 Map added in v1.1.7

func Map[From any, To any](from []From, mapper func(int, From) To) []To

Map is a generic function for mapping one slice to another slice. It is useful for creating a slice of expressions as input to the join function.

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 Caser added in v1.1.11

type Caser[T any] struct {
	Value T
	Then  Expression
}

func Case added in v1.1.11

func Case[T any](value T, then Expression) Caser[T]

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 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

func Switch added in v1.1.11

func Switch[T comparable](value T, cases ...Caser[T]) 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