Superbasic Save

The superbasic SQL-Builder.

Project README

Take a look at github.com/wroge/esquel.


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.

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

Open Source Agenda is not affiliated with "Superbasic" Project. README Source: wroge/superbasic
Stars
39
Open Issues
0
Last Commit
2 months ago
Repository
License
MIT

Open Source Agenda Badge

Open Source Agenda Rating