Documentation ¶
Overview ¶
A library for generating SQL programmatically.
Known limitations for SELECT queries:
- does not currently support join table alias (and hence self join)
- does not support NATURAL joins and join USING
Known limitation for INSERT statements:
- does not support "INSERT INTO SELECT"
Known limitation for UPDATE statements:
- does not support update without a WHERE clause (since it is dangerous)
- does not support multi-table update
Known limitation for DELETE statements:
- does not support delete without a WHERE clause (since it is dangerous)
- does not support multi-table delete
Query building functions for expression components
Example ¶
package main import ( "fmt" sb "github.com/coreos/sqlbuilder" ) func main() { t1 := sb.NewTable( "parent_prefix", sb.IntColumn("ns_id", sb.NotNullable), sb.IntColumn("hash", sb.NotNullable), sb.StrColumn( "prefix", sb.UTF8, sb.UTF8CaseInsensitive, sb.NotNullable)) t2 := sb.NewTable( "sfj", sb.IntColumn("ns_id", sb.NotNullable), sb.IntColumn("sjid", sb.NotNullable), sb.StrColumn( "filename", sb.UTF8, sb.UTF8CaseInsensitive, sb.NotNullable)) ns_id1 := t1.C("ns_id") prefix := t1.C("prefix") ns_id2 := t2.C("ns_id") sjid := t2.C("sjid") filename := t2.C("filename") in := []int32{1, 2, 3} join := t2.LeftJoinOn(t1, sb.Eq(ns_id1, ns_id2)) q := join.Select(ns_id2, sjid, prefix, filename).Where( sb.And(sb.EqL(ns_id2, 123), sb.In(sjid, in))) fmt.Println(q.String(sb.NewSQLiteDialect())) }
Output:
Index ¶
- Constants
- Variables
- func EscapeForLike(s string) string
- type BoolExpression
- func And(expressions ...BoolExpression) BoolExpression
- func Eq(lhs, rhs Expression) BoolExpression
- func EqL(lhs Expression, val interface{}) BoolExpression
- func Gt(lhs, rhs Expression) BoolExpression
- func GtL(lhs Expression, val interface{}) BoolExpression
- func Gte(lhs, rhs Expression) BoolExpression
- func GteL(lhs Expression, val interface{}) BoolExpression
- func In(lhs Expression, valList interface{}) BoolExpression
- func InQ(lhs Expression, rhs SubqueryClause) BoolExpression
- func Like(lhs, rhs Expression) BoolExpression
- func LikeL(lhs Expression, val string) BoolExpression
- func Lt(lhs Expression, rhs Expression) BoolExpression
- func LtL(lhs Expression, val interface{}) BoolExpression
- func Lte(lhs, rhs Expression) BoolExpression
- func LteL(lhs Expression, val interface{}) BoolExpression
- func Neq(lhs, rhs Expression) BoolExpression
- func NeqL(lhs Expression, val interface{}) BoolExpression
- func Not(expr BoolExpression) BoolExpression
- func Or(expressions ...BoolExpression) BoolExpression
- type Charset
- type Clause
- type Collation
- type Column
- type DeleteStatement
- type Dialect
- type Expression
- func Add(expressions ...Expression) Expression
- func BitAnd(lhs, rhs Expression) Expression
- func BitOr(lhs, rhs Expression) Expression
- func BitXor(lhs, rhs Expression) Expression
- func ColumnValue(col NonAliasColumn) Expression
- func Div(expressions ...Expression) Expression
- func If(conditional BoolExpression, trueExpression Expression, ...) Expression
- func Literal(v interface{}) Expression
- func Minus(lhs, rhs Expression) Expression
- func Mul(expressions ...Expression) Expression
- func Plus(lhs, rhs Expression) Expression
- func SqlFunc(funcName string, expressions ...Expression) Expression
- func Sub(expressions ...Expression) Expression
- func Tuple(exprs ...Expression) Expression
- type InsertStatement
- type LockStatement
- type NonAliasColumn
- func BoolColumn(name string, nullable NullableColumn) NonAliasColumn
- func BytesColumn(name string, nullable NullableColumn) NonAliasColumn
- func DateTimeColumn(name string, nullable NullableColumn) NonAliasColumn
- func DoubleColumn(name string, nullable NullableColumn) NonAliasColumn
- func IntColumn(name string, nullable NullableColumn) NonAliasColumn
- func StrColumn(name string, charset Charset, collation Collation, nullable NullableColumn) NonAliasColumn
- type NullableColumn
- type OrderByClause
- type Projection
- type ReadableTable
- type SelectStatement
- type Statement
- type SubqueryClause
- type Table
- func (t *Table) C(name string) NonAliasColumn
- func (t *Table) Columns() []NonAliasColumn
- func (t *Table) Delete() DeleteStatement
- func (t *Table) ForceIndex(index string) *Table
- func (t *Table) InnerJoinOn(table ReadableTable, onCondition BoolExpression) ReadableTable
- func (t *Table) Insert(columns ...NonAliasColumn) InsertStatement
- func (t *Table) LeftJoinOn(table ReadableTable, onCondition BoolExpression) ReadableTable
- func (t *Table) Name() string
- func (t *Table) Projections() []Projection
- func (t *Table) RightJoinOn(table ReadableTable, onCondition BoolExpression) ReadableTable
- func (t *Table) Select(projections ...Projection) SelectStatement
- func (t *Table) SerializeSql(d Dialect, out *bytes.Buffer) error
- func (t *Table) Update() UpdateStatement
- type UnionStatement
- type UnlockStatement
- type UpdateStatement
- type WritableTable
Examples ¶
Constants ¶
const ( INNER_JOIN joinType = iota LEFT_JOIN RIGHT_JOIN )
Variables ¶
var ErrColumnCountInUnionInnerSelect = errors.New("all inner selects in Union statement must select the same number of columns")
ErrColumnCountInUnionInnerSelect represents a sanity check. If you get this error, you probably want to select the same table columns in the same order. If you are selecting a multiple tables, use Null to pad to the right number of fields.
Functions ¶
func EscapeForLike ¶
Types ¶
type BoolExpression ¶
type BoolExpression interface { Clause // contains filtered or unexported methods }
func And ¶
func And(expressions ...BoolExpression) BoolExpression
Returns a representation of "c[0] AND ... AND c[n-1]" for c in clauses
func EqL ¶
func EqL(lhs Expression, val interface{}) BoolExpression
Returns a representation of "a=b", where b is a literal
func GtL ¶
func GtL(lhs Expression, val interface{}) BoolExpression
Returns a representation of "a>b", where b is a literal
func GteL ¶
func GteL(lhs Expression, val interface{}) BoolExpression
Returns a representation of "a>=b", where b is a literal
func In ¶
func In(lhs Expression, valList interface{}) BoolExpression
Returns a representation of "a IN (b[0], ..., b[n-1])", where b is a list of literals valList must be a slice type
func InQ ¶
func InQ(lhs Expression, rhs SubqueryClause) BoolExpression
InQ returns a representation of "a IN b" where b is a subquery.
func Like ¶
func Like(lhs, rhs Expression) BoolExpression
func LikeL ¶
func LikeL(lhs Expression, val string) BoolExpression
func LtL ¶
func LtL(lhs Expression, val interface{}) BoolExpression
Returns a representation of "a<b", where b is a literal
func LteL ¶
func LteL(lhs Expression, val interface{}) BoolExpression
Returns a representation of "a<=b", where b is a literal
func NeqL ¶
func NeqL(lhs Expression, val interface{}) BoolExpression
Returns a representation of "a!=b", where b is a literal
func Or ¶
func Or(expressions ...BoolExpression) BoolExpression
Returns a representation of "c[0] OR ... OR c[n-1]" for c in clauses
type Column ¶
type Column interface { Name() string // Serialization for use in column lists SerializeSqlForColumnList(includeTableName bool, d Dialect, out *bytes.Buffer) error // Serialization for use in an expression (Clause) SerializeSql(d Dialect, out *bytes.Buffer) error // contains filtered or unexported methods }
Representation of a table for query generation
func Alias ¶
func Alias(name string, c Expression) Column
Representation of aliased clauses (expression AS name)
type DeleteStatement ¶
type DeleteStatement interface { Statement Where(expression BoolExpression) DeleteStatement OrderBy(clauses ...OrderByClause) DeleteStatement Limit(limit int64) DeleteStatement Comment(comment string) DeleteStatement }
type Dialect ¶
type Dialect interface { EscapeCharacter() rune InsertReturningClause() string Kind() string Name() *string }
func NewMySQLDialect ¶
func NewPostgresDialect ¶
func NewSQLiteDialect ¶
func NewSQLiteDialect() Dialect
type Expression ¶
type Expression interface { Clause // contains filtered or unexported methods }
An expression
func Add ¶
func Add(expressions ...Expression) Expression
Returns a representation of "c[0] + ... + c[n-1]" for c in clauses
func BitAnd ¶
func BitAnd(lhs, rhs Expression) Expression
func BitOr ¶
func BitOr(lhs, rhs Expression) Expression
func BitXor ¶
func BitXor(lhs, rhs Expression) Expression
func ColumnValue ¶
func ColumnValue(col NonAliasColumn) Expression
func Div ¶
func Div(expressions ...Expression) Expression
Returns a representation of "c[0] / ... / c[n-1]" for c in clauses
func If ¶
func If(conditional BoolExpression, trueExpression Expression, falseExpression Expression) Expression
Returns a representation of an if-expression, of the form:
IF (BOOLEAN TEST, VALUE-IF-TRUE, VALUE-IF-FALSE)
func Minus ¶
func Minus(lhs, rhs Expression) Expression
func Mul ¶
func Mul(expressions ...Expression) Expression
Returns a representation of "c[0] * ... * c[n-1]" for c in clauses
func Plus ¶
func Plus(lhs, rhs Expression) Expression
func SqlFunc ¶
func SqlFunc(funcName string, expressions ...Expression) Expression
Returns a representation of sql function call "func_call(c[0], ..., c[n-1])
func Sub ¶
func Sub(expressions ...Expression) Expression
Returns a representation of "c[0] - ... - c[n-1]" for c in clauses
func Tuple ¶
func Tuple(exprs ...Expression) Expression
type InsertStatement ¶
type InsertStatement interface { Statement // Add a row of values to the insert statement. Add(row ...Expression) InsertStatement AddOnDuplicateKeyUpdate(col NonAliasColumn, expr Expression) InsertStatement Comment(comment string) InsertStatement IgnoreDuplicates(ignore bool) InsertStatement }
type LockStatement ¶
type LockStatement interface { Statement AddReadLock(table *Table) LockStatement AddWriteLock(table *Table) LockStatement }
LockStatement is used to take Read/Write lock on tables. See http://dev.mysql.com/doc/refman/5.0/en/lock-tables.html
func NewLockStatement ¶
func NewLockStatement() LockStatement
NewLockStatement returns a SQL representing empty set of locks. You need to use AddReadLock/AddWriteLock to add tables that need to be locked. NOTE: You need at least one lock in the set for it to be a valid statement.
type NonAliasColumn ¶
type NonAliasColumn interface { Column // contains filtered or unexported methods }
A column that can be refer to outside of the projection list
func BoolColumn ¶
func BoolColumn(name string, nullable NullableColumn) NonAliasColumn
Representation of TINYINT used as a bool This function will panic if name is not valid
func BytesColumn ¶
func BytesColumn(name string, nullable NullableColumn) NonAliasColumn
Representation of VARBINARY/BLOB columns This function will panic if name is not valid
func DateTimeColumn ¶
func DateTimeColumn(name string, nullable NullableColumn) NonAliasColumn
Representation of DateTime columns This function will panic if name is not valid
func DoubleColumn ¶
func DoubleColumn(name string, nullable NullableColumn) NonAliasColumn
Representation of any double column This function will panic if name is not valid
func IntColumn ¶
func IntColumn(name string, nullable NullableColumn) NonAliasColumn
Representation of any integer column This function will panic if name is not valid
func StrColumn ¶
func StrColumn( name string, charset Charset, collation Collation, nullable NullableColumn) NonAliasColumn
Representation of VARCHAR/TEXT columns This function will panic if name is not valid
type NullableColumn ¶
type NullableColumn bool
const ( Nullable NullableColumn = true NotNullable NullableColumn = false )
type OrderByClause ¶
type OrderByClause interface { Clause // contains filtered or unexported methods }
A clause that can be used in order by
func Asc ¶
func Asc(expression Expression) OrderByClause
func Desc ¶
func Desc(expression Expression) OrderByClause
type Projection ¶
type Projection interface { Clause SerializeSqlForColumnList(includeTableName bool, d Dialect, out *bytes.Buffer) error // contains filtered or unexported methods }
A clause that is selectable.
type ReadableTable ¶
type ReadableTable interface { // Returns the list of columns that are in the current table expression. Columns() []NonAliasColumn // Generates the sql string for the current table expression. Note: the // generated string may not be a valid/executable sql statement. // The database is the name of the database the table is on SerializeSql(d Dialect, out *bytes.Buffer) error // Generates a select query on the current table. Select(projections ...Projection) SelectStatement // Creates a inner join table expression using onCondition. InnerJoinOn(table ReadableTable, onCondition BoolExpression) ReadableTable // Creates a left join table expression using onCondition. LeftJoinOn(table ReadableTable, onCondition BoolExpression) ReadableTable // Creates a right join table expression using onCondition. RightJoinOn(table ReadableTable, onCondition BoolExpression) ReadableTable }
The SQL table read interface. NOTE: NATURAL JOINs, and join "USING" clause are not supported.
func InnerJoinOn ¶
func InnerJoinOn( lhs ReadableTable, rhs ReadableTable, onCondition BoolExpression) ReadableTable
func LeftJoinOn ¶
func LeftJoinOn( lhs ReadableTable, rhs ReadableTable, onCondition BoolExpression) ReadableTable
func RightJoinOn ¶
func RightJoinOn( lhs ReadableTable, rhs ReadableTable, onCondition BoolExpression) ReadableTable
type SelectStatement ¶
type SelectStatement interface { Statement Where(expression BoolExpression) SelectStatement AndWhere(expression BoolExpression) SelectStatement GroupBy(expressions ...Expression) SelectStatement OrderBy(clauses ...OrderByClause) SelectStatement Limit(limit int64) SelectStatement ForUpdate() SelectStatement Offset(offset int64) SelectStatement Comment(comment string) SelectStatement Copy() SelectStatement }
type SubqueryClause ¶
func Subquery ¶
func Subquery(stmt Statement) SubqueryClause
type Table ¶
type Table struct {
// contains filtered or unexported fields
}
func NewTable ¶
func NewTable(name string, columns ...NonAliasColumn) *Table
Defines a physical table in the database that is both readable and writable. This function will panic if name is not valid
func (*Table) C ¶
func (t *Table) C(name string) NonAliasColumn
Returns a pseudo column representation of the column name. Error checking is deferred to SerializeSql.
func (*Table) Columns ¶
func (t *Table) Columns() []NonAliasColumn
Returns a list of the table's columns
func (*Table) Delete ¶
func (t *Table) Delete() DeleteStatement
func (*Table) ForceIndex ¶
Returns a copy of this table, but with the specified index forced.
func (*Table) InnerJoinOn ¶
func (t *Table) InnerJoinOn( table ReadableTable, onCondition BoolExpression) ReadableTable
Creates a inner join table expression using onCondition.
func (*Table) Insert ¶
func (t *Table) Insert(columns ...NonAliasColumn) InsertStatement
func (*Table) LeftJoinOn ¶
func (t *Table) LeftJoinOn( table ReadableTable, onCondition BoolExpression) ReadableTable
Creates a left join table expression using onCondition.
func (*Table) Projections ¶
func (t *Table) Projections() []Projection
Returns all columns for a table as a slice of projections
func (*Table) RightJoinOn ¶
func (t *Table) RightJoinOn( table ReadableTable, onCondition BoolExpression) ReadableTable
Creates a right join table expression using onCondition.
func (*Table) Select ¶
func (t *Table) Select(projections ...Projection) SelectStatement
Generates a select query on the current table.
func (*Table) SerializeSql ¶
Generates the sql string for the current table expression. Note: the generated string may not be a valid/executable sql statement.
func (*Table) Update ¶
func (t *Table) Update() UpdateStatement
type UnionStatement ¶
type UnionStatement interface { Statement // Warning! You cannot include table names for the next 4 clauses, or // you'll get errors like: // Table 'server_file_journal' from one of the SELECTs cannot be used in // global ORDER clause Where(expression BoolExpression) UnionStatement AndWhere(expression BoolExpression) UnionStatement GroupBy(expressions ...Expression) UnionStatement OrderBy(clauses ...OrderByClause) UnionStatement Limit(limit int64) UnionStatement Offset(offset int64) UnionStatement }
By default, rows selected by a UNION statement are out-of-order If you have an ORDER BY on an inner SELECT statement, the only thing it affects is the LIMIT clause on that inner statement (the ordering will still be out-of-order).
func Union ¶
func Union(selects ...SelectStatement) UnionStatement
type UnlockStatement ¶
type UnlockStatement interface { Statement }
UnlockStatement can be used to release table locks taken using LockStatement. NOTE: You can not selectively release a lock and continue to hold lock on another table. UnlockStatement releases all the lock held in the current session.
func NewUnlockStatement ¶
func NewUnlockStatement() UnlockStatement
NewUnlockStatement returns SQL statement that can be used to release table locks grabbed by the current session.
type UpdateStatement ¶
type UpdateStatement interface { Statement Set(column NonAliasColumn, expression Expression) UpdateStatement Where(expression BoolExpression) UpdateStatement OrderBy(clauses ...OrderByClause) UpdateStatement Limit(limit int64) UpdateStatement Comment(comment string) UpdateStatement }
type WritableTable ¶
type WritableTable interface { // Returns the list of columns that are in the table. Columns() []NonAliasColumn // Generates the sql string for the current table expression. Note: the // generated string may not be a valid/executable sql statement. SerializeSql(d Dialect, out *bytes.Buffer) error Insert(columns ...NonAliasColumn) InsertStatement Update() UpdateStatement Delete() DeleteStatement }
The SQL table write interface.