qb

package module
v0.0.0-...-2f595f0 Latest Latest
Warning

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

Go to latest
Published: May 21, 2020 License: MIT Imports: 4 Imported by: 0

README

github.com/tetratom/qb

GoDoc CircleCI Codecov

qb is a simple SQL query builder for Go

highlights

  • go get -u github.com/tetratom/qb
  • GoDoc
  • More examples can be found in qb_test.go.
  • All methods take value receivers and return values.
  • Select the placeholder dialect with the DialectOption(Dialect) method.

example

import "github.com/tetratom/qb"

q := qb.
    Select("*").From("my_table").
    Where(qb.
        And("id = ?", 1).
        Or("time < ?", qb.Lit("now()"))).
    OrderBy("time ASC").
    Limit(10)

// q.SQL() is "SELECT * FROM my_table WHERE id = ?".
// q.Args() is []interface{1}.
row := tx.QueryRow(q.SQL(), q.Args()...)

thread-safe and reusable builders

qb builders should be used the same as the append() built-in. The builders take and return values, and internally keep state such that one builder value can be re-used for different queries. For example:

qbase := qb.Select("col1", "col2")
q1 := qbase.From("t1") // q1 is: SELECT col1, col2 FROM t1
q2 := qbase.From("t2") // q2 is: SELECT col1, col2 FROM t2
// qbase is: SELECT col1, col2

Just like with append(), the return value of calling a builder method must be assigned back into a variable to be used. For example:

func Search(name string, ordered bool) qb.Query {
    q := qb.Select("*").From("members")

    if name != "" {
        q = q.Where(qb.And("name = ?", name))
    }
    
    if ordered {
        q = q.OrderBy("created_at DESC")
    }
    
    return q
}

Documentation

Overview

Example (SearchMembers)
package main

import (
	"fmt"

	"github.com/tetratom/qb"
)

var membersQueryBase = qb.Select("*").From("members")

func BuildQuery(name string, ordered bool) qb.Query {
	q := membersQueryBase

	if name != "" {
		q = q.Where(qb.And("name = ?", name))
	}

	if ordered {
		q = q.OrderBy("created_at DESC")
	}

	return q
}

func main() {
	allMembers := BuildQuery("", false)
	fmt.Println(allMembers.String())

	allMembersOrdered := BuildQuery("", true)
	fmt.Println(allMembersOrdered.String())

	allMembersJohnDoe := BuildQuery("John Doe", false)
	fmt.Println(allMembersJohnDoe.String())

	allMembersJohnDoeOrdered := BuildQuery("John Doe", true)
	fmt.Println(allMembersJohnDoeOrdered.String())

}
Output:
SELECT * FROM members
SELECT * FROM members ORDER BY created_at DESC
SELECT * FROM members WHERE name = ?
SELECT * FROM members WHERE name = ? ORDER BY created_at DESC

Index

Examples

Constants

This section is empty.

Variables

View Source
var NULL = Lit(`NULL`)

Functions

func As

func As(lhs, alias string) string

func Lit

func Lit(s string, args ...interface{}) interface{}

Types

type Dialect

type Dialect int
const (
	DialectDefault Dialect = iota
	DialectPq
	DialectGoracle
	DialectMssql
)

type Predicate

type Predicate struct {
	// contains filtered or unexported fields
}

func And

func And(expr string, args ...interface{}) Predicate

func AndP

func AndP(predicate Predicate) Predicate

func Pred

func Pred(expr string, args ...interface{}) Predicate

func (Predicate) And

func (my Predicate) And(expr string, args ...interface{}) Predicate

func (Predicate) AndP

func (my Predicate) AndP(predicate Predicate) Predicate

func (Predicate) IsEmpty

func (p Predicate) IsEmpty() bool

func (Predicate) Map

func (my Predicate) Map(f func(p Predicate) Predicate) Predicate

func (Predicate) Or

func (my Predicate) Or(expr string, args ...interface{}) Predicate

func (Predicate) OrP

func (my Predicate) OrP(predicate Predicate) Predicate

func (Predicate) String

func (p Predicate) String() string

type Query

type Query struct {
	Dialect
	// contains filtered or unexported fields
}

func Append

func Append(expr string, args ...interface{}) Query

func Begin

func Begin() Query

func Commit

func Commit() Query

func DeleteFrom

func DeleteFrom(table string) Query

func DeleteFromAs

func DeleteFromAs(table, alias string) Query

func DialectOption

func DialectOption(d Dialect) Query

func InsertInto

func InsertInto(expr string, columns ...string) Query

func InsertValuesInto

func InsertValuesInto(table string, values Values) Query

func Multiple

func Multiple(qs ...Query) Query

Creates a query with multiple statements.

qs0; [qs1; [qs2; ...]]
Example
package main

import (
	"fmt"

	"github.com/tetratom/qb"
)

func main() {
	q := qb.Multiple(
		qb.Select("*").From("t1"),
		qb.Select("*").From("t2"))
	fmt.Print(q.String())
}
Output:
SELECT * FROM t1 ; SELECT * FROM t2 ;

func Select

func Select(columns ...string) Query

func SelectColumn

func SelectColumn(expr string, args ...interface{}) Query

func Subquery

func Subquery(sq Query) Query

func Update

func Update(table string) Query

func With

func With(name string, query Query) Query

func WithDialectDefault

func WithDialectDefault() Query

func WithDialectGoracle

func WithDialectGoracle() Query

func WithDialectMssql

func WithDialectMssql() Query

func WithDialectPQ

func WithDialectPQ() Query

func (Query) Append

func (q Query) Append(expr string, args ...interface{}) Query

func (*Query) Args

func (q *Query) Args() []interface{}

func (Query) As

func (q Query) As(alias string) Query

func (Query) Begin

func (q Query) Begin() Query

func (Query) Build

func (q Query) Build() (string, []interface{})

func (Query) Commit

func (q Query) Commit() Query

func (Query) DefaultValues

func (q Query) DefaultValues() Query

func (Query) DeleteFrom

func (q Query) DeleteFrom(table string) Query

func (Query) DeleteFromAs

func (q Query) DeleteFromAs(table, alias string) Query

func (Query) DialectOption

func (q Query) DialectOption(d Dialect) Query

func (Query) Except

func (q Query) Except() Query

func (Query) ExceptAll

func (q Query) ExceptAll() Query

func (Query) ForShare

func (q Query) ForShare() Query

func (Query) ForUpdate

func (q Query) ForUpdate() Query

func (Query) From

func (q Query) From(expr string) Query

func (Query) FromAs

func (q Query) FromAs(table, alias string) Query

func (Query) FromSubquery

func (q Query) FromSubquery(sq Query) Query

func (Query) FullJoinAsOn

func (q Query) FullJoinAsOn(table, alias string, predicate Predicate) Query

func (Query) FullJoinAsUsing

func (q Query) FullJoinAsUsing(table, alias string, columns ...string) Query

func (Query) FullJoinOn

func (q Query) FullJoinOn(table string, predicate Predicate) Query

func (Query) FullJoinUsing

func (q Query) FullJoinUsing(table string, columns ...string) Query

func (Query) GroupBy

func (q Query) GroupBy(fields ...string) Query

Appends a GROUP BY clause.

... GROUP BY field0[, field1[, ...]].
Example
package main

import (
	"fmt"

	"github.com/tetratom/qb"
)

func main() {
	q := qb.Select("*").From("t1").GroupBy("a", "b")
	fmt.Print(q.String())
}
Output:
SELECT * FROM t1 GROUP BY a, b

func (Query) Having

func (q Query) Having(predicate Predicate) Query

Appends a HAVING clause. This should follow a GROUP BY clause.

... HAVING predicate
Example
package main

import (
	"fmt"

	"github.com/tetratom/qb"
)

func main() {
	q := qb.Select("*").From("t1").GroupBy("a", "b").Having(qb.And("a < ?", 100))
	fmt.Print(q.String())
}
Output:
SELECT * FROM t1 GROUP BY a, b HAVING a < ?

func (Query) InsertInto

func (q Query) InsertInto(expr string, columns ...string) Query

func (Query) InsertValuesInto

func (q Query) InsertValuesInto(table string, values Values) Query

func (Query) Intersect

func (q Query) Intersect() Query

func (Query) IntersectAll

func (q Query) IntersectAll() Query

func (Query) JoinAsOn

func (q Query) JoinAsOn(table, alias string, predicate Predicate) Query

func (Query) JoinAsUsing

func (q Query) JoinAsUsing(table, alias string, columns ...string) Query

func (Query) JoinOn

func (q Query) JoinOn(table string, predicate Predicate) Query

func (Query) JoinUsing

func (q Query) JoinUsing(table string, columns ...string) Query

func (Query) LeftJoinAsOn

func (q Query) LeftJoinAsOn(table, alias string, predicate Predicate) Query

func (Query) LeftJoinAsUsing

func (q Query) LeftJoinAsUsing(table, alias string, columns ...string) Query

func (Query) LeftJoinOn

func (q Query) LeftJoinOn(table string, predicate Predicate) Query

func (Query) LeftJoinUsing

func (q Query) LeftJoinUsing(table string, columns ...string) Query

func (Query) Limit

func (q Query) Limit(limit int64) Query

func (Query) LimitAll

func (q Query) LimitAll() Query

func (Query) Map

func (q Query) Map(f func(q Query) Query) Query

func (Query) NaturalFullJoin

func (q Query) NaturalFullJoin(table string) Query

Appends a NATURAL FULL JOIN clause.

... NATURAL FULL JOIN table
Example
package main

import (
	"fmt"

	"github.com/tetratom/qb"
)

func main() {
	q := qb.Select("*").From("t1").NaturalFullJoin("t2")
	fmt.Print(q.String())
}
Output:
SELECT * FROM t1 NATURAL FULL JOIN t2

func (Query) NaturalFullJoinAs

func (q Query) NaturalFullJoinAs(table, alias string) Query

func (Query) NaturalJoin

func (q Query) NaturalJoin(table string) Query

func (Query) NaturalJoinAs

func (q Query) NaturalJoinAs(table, alias string) Query

func (Query) NaturalLeftJoin

func (q Query) NaturalLeftJoin(table string) Query

func (Query) NaturalLeftJoinAs

func (q Query) NaturalLeftJoinAs(table, alias string) Query

func (Query) NaturalRightJoin

func (q Query) NaturalRightJoin(table string) Query

func (Query) NaturalRightJoinAs

func (q Query) NaturalRightJoinAs(table, alias string) Query

func (Query) Offset

func (q Query) Offset(offset int64) Query

func (Query) OrderBy

func (q Query) OrderBy(first string, rest ...string) Query

func (Query) Returning

func (q Query) Returning(columns ...string) Query

func (Query) RightJoinAsOn

func (q Query) RightJoinAsOn(table, alias string, predicate Predicate) Query

func (Query) RightJoinAsUsing

func (q Query) RightJoinAsUsing(table, alias string, columns ...string) Query

func (Query) RightJoinOn

func (q Query) RightJoinOn(table string, predicate Predicate) Query

func (Query) RightJoinUsing

func (q Query) RightJoinUsing(table string, columns ...string) Query

func (*Query) SQL

func (q *Query) SQL() string

func (Query) Select

func (q Query) Select(columns ...string) Query

func (Query) SelectColumn

func (q Query) SelectColumn(expr string, args ...interface{}) Query

func (Query) Set

func (q Query) Set(expr string, args ...interface{}) Query

func (Query) SetValues

func (q Query) SetValues(values Values) Query

func (Query) String

func (q Query) String() string

func (Query) Subquery

func (q Query) Subquery(sq Query) Query

func (Query) Union

func (q Query) Union() Query

func (Query) UnionAll

func (q Query) UnionAll() Query

func (Query) Update

func (q Query) Update(table string) Query

func (Query) Using

func (q Query) Using(table string) Query

func (Query) UsingAs

func (q Query) UsingAs(table, alias string) Query

func (Query) ValueTuples

func (q Query) ValueTuples(tuples ...[]interface{}) Query

func (Query) Values

func (q Query) Values(values ...interface{}) Query

func (Query) Where

func (q Query) Where(pred Predicate) Query

func (Query) With

func (q Query) With(name string, query Query) Query

type Values

type Values map[string]interface{}

Jump to

Keyboard shortcuts

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