sqld

package module
v0.10.0 Latest Latest
Warning

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

Go to latest
Published: Feb 22, 2024 License: MIT Imports: 6 Imported by: 0

README

sqld

A Go library to build and manage dynamic queries. No external dependencies. Slightly inspired by Drizzle ORM

Scope of the project

The scope of sqld is to provide an easy way to organize your dynamic queries in re-usable components, not to validate said queries. I suggest to use other tools like SQLParser and a lot of e2e tests!

Usage

query := sqld.New(
	sqld.Select(
		sqld.Columns(
			"name",
			"pizzas",
		),
	),
	sqld.From(sqld.Just("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.Desc(filters.OrderBy)),
)

s, args, err := query()

Glossary

  • Operators: callbacks that have 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 statement")
View Source
var ErrNoOps = errors.New("operations slice is empty")

Functions

func NoOp

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

func PgPrepare added in v0.7.0

func PgPrepare(query string, args []driver.Value) string

PgPrepare swaps all ? placeholders with postgres ones ($1, $2...)

func TableColumn added in v0.3.1

func TableColumn[M Model](column string) string

TableColumn returns a combination of `Model.TableName()` and the provided column. Panics if the column is not present in the model

func TableColumnErr added in v0.3.1

func TableColumnErr[M Model](column string) (string, error)

TableColumnErr returns a combination of `Model.TableName()` and the provided column. Returns error if the column is not present in the model

func TableColumns added in v0.3.1

func TableColumns[M Model]() []string

TableColumns extracts a list of columns from a `Model`, using sqlx `db` tags and falling back on field names

func TableName added in v0.2.0

func TableName[M Model]() string

TableName is a generic proxy for `Model.TableName()`

Types

type Condition added in v0.5.3

type Condition string
const (
	AND Condition = "AND"
	OR  Condition = "OR"
)

type JoinType added in v0.2.0

type JoinType string
const (
	LEFT_JOIN        JoinType = "LEFT"
	RIGHT_JOIN       JoinType = "RIGHT"
	INNER_JOIN       JoinType = "INNER"
	CROSS_JOIN       JoinType = "CROSS"
	FULL_JOIN        JoinType = "FULL"
	LEFT_OUTER_JOIN  JoinType = "LEFT OUTER"
	RIGHT_OUTER_JOIN JoinType = "RIGHT OUTER"
	INNER_OUTER_JOIN JoinType = "INNER OUTER"
	CROSS_OUTER_JOIN JoinType = "CROSS OUTER"
	FULL_OUTER_JOIN  JoinType = "FULL OUTER"
)

type Model added in v0.2.0

type Model interface {
	TableName() string
}

type SortingOrder added in v0.5.3

type SortingOrder string
const (
	ASC  SortingOrder = "ASC"
	DESC SortingOrder = "DESC"
)

type SqldFn

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

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

func AllWildcard added in v0.5.0

func AllWildcard() SqldFn

AllWildcard builds a callback that just returns a "*" string

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 As added in v0.3.0

func As(op SqldFn, aliasName string) SqldFn

As builds a callback that returns an alias

func Asc

func Asc(columnExpr string) SqldFn

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

func Coalesce added in v0.10.0

func Coalesce(op SqldFn, fallback string) SqldFn

Coalesce builds a callback that returns an coalesced expression

func ColumnEq added in v0.2.1

func ColumnEq(firstColumn string, secondColumn string) SqldFn

ColumnEq builds a callback that returns a comparison statement between two columns

func Columns added in v0.2.0

func Columns(columns ...string) SqldFn

Columns builds a callback that returns a list of columns, comma-separated

func Count added in v0.5.0

func Count(op SqldFn) SqldFn

Count builds a callback that returns a COUNT function with the given argument

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 From added in v0.2.0

func From(op SqldFn) SqldFn

From builds a callback that just returns a FROM statement with the provided table

func GroupBy added in v0.9.0

func GroupBy(ops ...SqldFn) SqldFn

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 IfStringEmpty added in v0.6.0

func IfStringEmpty(val string, op SqldFn) SqldFn

func IfStringEmptyElse added in v0.6.0

func IfStringEmptyElse(val string, trueFn, falseFn SqldFn) SqldFn

func IfStringNotEmpty added in v0.6.0

func IfStringNotEmpty(val string, op SqldFn) SqldFn

func IfStringNotEmptyElse added in v0.6.0

func IfStringNotEmptyElse(val string, trueFn, 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 Join added in v0.2.0

func Join(joinType JoinType, subject SqldFn, op SqldFn) SqldFn

Join builds a callback that returns a JOIN statement of the provided type with the desired subject, with a condition callback

func Just added in v0.3.1

func Just(s string) SqldFn

Just returns a callback that just returns the provided string

func LeftJoin added in v0.2.2

func LeftJoin(subject SqldFn, op SqldFn) SqldFn

LeftJoin is a shortcut for `Join()` with `LEFT_JOIN` type

func Limit added in v0.4.0

func Limit(count *uint) SqldFn

func New

func New(ops ...SqldFn) SqldFn

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

Example usage:

const query := sqld.New(
	sqld.Select(
		sqld.Columns(
			"name",
			"pizzas",
		),
	),
	sqld.From(sqld.Just("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.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 Offset added in v0.4.0

func Offset(skip *uint) SqldFn

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 PgPrepareOp added in v0.8.0

func PgPrepareOp(op SqldFn) SqldFn

PgPrepareOp applies PgPrepare() to the resulting query in the operator. Use this as the last operator!

func RightJoin added in v0.2.2

func RightJoin(subject SqldFn, op SqldFn) SqldFn

RightJoin is a shortcut for `Join()` with `RIGHT_JOIN` type

func Select added in v0.1.0

func Select(ops ...SqldFn) SqldFn

Select builds a callback that returns a SELECT statement with a concatenation of the provided operators.

func Sort added in v0.5.4

func Sort(order SortingOrder, columnExpr string) SqldFn

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

func SubQuery added in v0.4.0

func SubQuery(op SqldFn, aliasName string) SqldFn

SubQuery builds a callback that returns a subquery

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

Jump to

Keyboard shortcuts

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