# README
The superbasic SQL-Builder
superbasic.Compile
compiles expressions into an SQL template and thus offers an alternative to conventional query builders.
Compile
replaces placeholders?
with expressions.Join
joins expressions by a separator.
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
// )
Map
is a generic mapper function particularly helpful in the context of Join.Finalize
replaces?
placeholders with a string that can contain a positional part by%d
.
presidents := []President{
{"George", "Washington"},
{"John", "Adams"},
}
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]
If
condition is true, return expression, else skip.Switch
returns an expression matching a value.
dialect := "sqlite"
contains := "Joe"
query := superbasic.Join(" ", superbasic.SQL("SELECT * FROM presidents"),
superbasic.If(contains != "", superbasic.Compile("WHERE ?",
superbasic.Switch(dialect,
superbasic.Case("postgres", superbasic.SQL("POSITION(? IN presidents.first) > 0", contains)),
superbasic.Case("sqlite", superbasic.SQL("INSTR(presidents.first, ?) > 0", contains)),
))))
fmt.Println(superbasic.Finalize("?", query))
// SELECT * FROM presidents WHERE INSTR(presidents.first, ?) > 0 [Joe] <nil>
To scan rows to types, i recommend wroge/scan.
# Functions
No description provided by the author
Case returns a Caser that can be used inside the Switch statement.
Compile takes a template with placeholders into which expressions can be compiled.
Finalize takes a static placeholder like '?' or a positional placeholder containing '%d'.
No description provided by the author
No description provided by the author
No description provided by the author
Map is a generic function for mapping one slice to another slice.
Replace takes a static placeholder like '?' or a positional placeholder containing '%d'.
No description provided by the author
Switch returns a Expression of a Case matching a value.
No description provided by the author
# Structs
No description provided by the author
No description provided by the author
ExpressionError is returned by the Compile Expression, if an expression is nil.
No description provided by the author
NumberOfArgumentsError is returned if arguments doesn't match the number of placeholders.
No description provided by the author
# Interfaces
No description provided by the author
# Type aliases
No description provided by the author