package
0.0.3
Repository: https://github.com/mutablelogic/go-accessory.git
Documentation: pkg.go.dev

# README

Query Builder

This package provides a query builder for sqlite. There are a small number of primitives and then a number of functions which build on these primitives to provide support for building queries.

It's not really intended you would use this query builder directly, but it would be incorpporated into a higher level packages.

Primitives

The following primitives exist, which are used to build queries:

PrimitiveDescriptionExample
S()SELECT statementS("id", "name").From("users")
E()ExpressionE("string")
N()Table or column nameN("column")
Q()Bare queryQ("SELECT * FROM users")

Query

The Q(string) primitive is used to create a bare query. This is useful if you want to use a query which is not supported by the query builder. In order to create an SQL query:

    import (
        . "github.com/mutablelogic/go-accessory/pkg/sqlite/query"
    )

    sql := Q("SELECT * FROM users").Query()

Any query can include "flags" which are used to control the behaviour of the query. The With function should always be included at the end of an expression. For example,

    import (
        . "github.com/mutablelogic/go-accessory/pkg/sqlite/query"
    )

    // SELECT DISTINCT * FROM users
    sql := S().From(N("users")).With(DISTINCT).Query()

Name

The N(string) primitive is used to create a table or column name. This can be used as an argument to a S() primitive or E() primitive, for example. To create a table, use:

    import (
        . "github.com/mutablelogic/go-accessory/pkg/sqlite/query"
    )

    // CREATE TABLE users (a, b)
    sql := N("users").CreateTable(N("a"), N("b"))

You can modify a name primitive with the following modifiers:

ModifierDescriptionExample
N(string).As(string)Alias nameN("column").As("alias")
N(string).WithSchema(string)Schema nameN("column").WithSchema("main")
N(string).WithType(string)Declared column typeN("column").WithType("TIMESTAMP")

Expression

The E(any) primitive is used to create an expression which can be used in a S() primitive. This can be used to create a literal value, a column name, or a function. To create a literal value, use:

    import (
        . "github.com/mutablelogic/go-accessory/pkg/sqlite/query"
    )

    // SELECT 1
    sql := S(E(1)).Query()

    // SELECT name AS uid FROM users
    sql := S(N("users").As("uid")).From("users").Query()

# Functions

N returns a new table source.
Q returns a new ad-hoc query.