sqld

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 18, 2024 License: MIT Imports: 4 Imported by: 1

README

sqld

A pure-Go library to build and manage dynamic queries. Slightly inspired by Drizzle ORM

Usage

query := sqld.New(
	sqld.Block(`
		SELECT
			name,
			pizzas
		FROM Table`,
	),
	sqld.Where(
		sqld.And( 
			sqld.IfNotNil(filters.Name,
				sqld.Eq("name", filters.Name),
			),
			sqld.IfNotEmpty(filters.Pizzas,
				sqld.In("pizzas", filters.Pizzas),
			),
		),
	),
	sqld.OrderBy(
		sqld.IfNotNil(filters.OrderBy,
			sqld.Desc(filters.OrderBy),
		),
	),
)

s, args, err := query()

Glossary

  • Operators: the callbacks that respect this signature, used to build various parts of the query
  • Statements: major "blocks" of the query, like a whole WHERE statement with all its conditions
  • Conditionals: functions that return different operators depending on the inputs (usually boolean checks). Check this file

Customize

You can provide your own operators: every function (anonymous or not) that has this signature can be used by the library.

Check here for the built-in errors.

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrArgNotSlice = errors.New("argument is not a slice")
View Source
var ErrEmptySlice = errors.New("slice is empty")
View Source
var ErrNilColumnExpr = errors.New("column expression is nil")
View Source
var ErrNilVal = errors.New("value is nil")
View Source
var ErrNoColumns = errors.New("no columns in select statement")
View Source
var ErrNoOps = errors.New("operations slice is empty")

Functions

func NoOp

func NoOp() (string, []driver.Value, error)

Types

type SqldFn

type SqldFn func() (string, []driver.Value, error)

SqldFn is the type describing all callbacks used in the library.

func And

func And(ops ...SqldFn) SqldFn

And builds a callback combining all the operators with AND conditions.

sqld.And(
	sqld.IfNotNil(filters.Name,
		sqld.Eq("name", filters.Name),
	),
	sqld.IfNotEmpty(filters.Pizzas,
		sqld.In("pizzas", filters.Pizzas),
	),
)

func Asc

func Asc(columnExpr *string) SqldFn

Asc builds a callback used to specify the sorting in `OrderBy()`.

func Block

func Block(block string) SqldFn

Block builds a callback that just returns the provided strings. Use it for the "static" parts of your query, like SELECT and JOIN statements.

sqld.Block(`
	SELECT
		name,
		pizzas
	FROM Table`,
),

func Desc

func Desc(columnExpr *string) SqldFn

Desc builds a callback used to specify the sorting in `OrderBy()`.

func Eq

func Eq[T driver.Value](columnExpr string, val *T) SqldFn

Eq builds a callback that compares a column with the provided value.

sqld.Eq("name", filters.Name)

func Having

func Having(ops ...SqldFn) SqldFn

Having builds a callback combining all the operators in a HAVING statement.

sqld.Having(
	sqld.And(
		sqld.IfNotNil(filters.Name,
			sqld.Eq("name", filters.Name),
		),
		sqld.IfNotEmpty(filters.Pizzas,
			sqld.In("pizzas", filters.Pizzas),
		),
	),
)

func If

func If(pred func() bool, op SqldFn) SqldFn

func IfElse

func IfElse(pred func() bool, trueFn SqldFn, falseFn SqldFn) SqldFn

func IfEmpty

func IfEmpty[T driver.Value](vals []T, op SqldFn) SqldFn

func IfEmptyElse

func IfEmptyElse[T driver.Value](vals []T, trueFn SqldFn, falseFn SqldFn) SqldFn

func IfNil

func IfNil[T driver.Value](val *T, op SqldFn) SqldFn

func IfNilElse

func IfNilElse[T driver.Value](val *T, trueFn SqldFn, falseFn SqldFn) SqldFn

func IfNotEmpty

func IfNotEmpty[T driver.Value](vals []T, op SqldFn) SqldFn

func IfNotEmptyElse

func IfNotEmptyElse[T driver.Value](vals []T, trueFn SqldFn, falseFn SqldFn) SqldFn

func IfNotNil

func IfNotNil[T driver.Value](val *T, op SqldFn) SqldFn

func IfNotNilElse

func IfNotNilElse[T driver.Value](val *T, trueFn SqldFn, falseFn SqldFn) SqldFn

func In

func In[T driver.Value](columnExpr string, vals []T) SqldFn

In builds a callback that checks if a column value is contained in the provided slice of values.

sqld.In("pizzas", filters.Pizzas)

func New

func New(ops ...SqldFn) SqldFn

New builds a `SqldFn` callback combining the provided operators.

Example usage:

const query := sqld.New(
	sqld.Block(`
		SELECT
			name,
			pizzas
		FROM Table`,
	),
	sqld.Where(
		sqld.And(
			sqld.IfNotNil(filters.Name,
				sqld.Eq("name", filters.Name),
			),
			sqld.IfNotEmpty(filters.Pizzas,
				sqld.In("pizzas", filters.Pizzas),
			),
		),
	),
	sqld.OrderBy(
		sqld.IfNotNil(filters.OrderBy,
			sqld.Desc(filters.OrderBy),
		),
	),
)

func Not

func Not(op SqldFn) SqldFn

Not negates the provided operator.

func Null

func Null(columnExpr string) SqldFn

Eq builds a callback that checks if a column is NULL.

sqld.Null("name")

func Or

func Or(ops ...SqldFn) SqldFn

Or builds a callback combining all the operators with OR conditions.

sqld.Or(
	sqld.IfNotNil(filters.Name,
		sqld.Eq("name", filters.Name),
	),
	sqld.IfNotEmpty(filters.Pizzas,
		sqld.In("pizzas", filters.Pizzas),
	),
)

func OrderBy

func OrderBy(ops ...SqldFn) SqldFn

OrderBy builds a callback combining all the operators in a ORDER BY statement.

sqld.OrderBy(
	sqld.IfNotNil(filters.OrderBy,
		sqld.Desc(filters.OrderBy),
	),
)

func Select added in v0.1.0

func Select(columns ...string) SqldFn

func Where

func Where(ops ...SqldFn) SqldFn

Where builds a callback combining all the operators in a WHERE statement.

sqld.Where(
	sqld.And(
		sqld.IfNotNil(filters.Name,
			sqld.Eq("name", filters.Name),
		),
		sqld.IfNotEmpty(filters.Pizzas,
			sqld.In("pizzas", filters.Pizzas),
		),
	),
)

Directories

Path Synopsis
integration module
legacy module

Jump to

Keyboard shortcuts

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