Documentation
¶
Overview ¶
Package query is used to build SQL queries with Lingo tables / columns.
Index ¶
- func BuildWhereSQL(d lingo.Dialect, values []lingo.Expression) (sql.Data, error)
- func ExpandTables(paths []lingo.Expression) []lingo.Expression
- func JoinToSQL(d lingo.Dialect, sep string, exp []lingo.Expression) (sql.Data, error)
- func NewErrAroundSQL(s sql.Data, err error) error
- func NewNamedOnlyColumn(name, parent string) lingo.Column
- type DeleteQuery
- type ErrAroundSQL
- type InsertQuery
- func (i *InsertQuery) Columns(columns ...lingo.Column) *InsertQuery
- func (i *InsertQuery) Select(q *SelectQuery) *InsertQuery
- func (i InsertQuery) ToSQL(d lingo.Dialect) (sql.Data, error)
- func (i *InsertQuery) Values(values ...lingo.Expression) *InsertQuery
- func (i *InsertQuery) ValuesConstants(values ...interface{}) *InsertQuery
- type Modifier
- type ModifyDialect
- type SelectQuery
- func (q *SelectQuery) From(e lingo.Table) *SelectQuery
- func (q *SelectQuery) Join(left lingo.Expression, joinType join.Type, on lingo.Expression) *SelectQuery
- func (q *SelectQuery) OrderBy(exp lingo.Expression, direction sort.Direction) *SelectQuery
- func (q *SelectQuery) Restrict(m Modifier) *SelectQuery
- func (q *SelectQuery) ToSQL(d lingo.Dialect) (sql.Data, error)
- func (q *SelectQuery) Where(exp ...lingo.Expression) *SelectQuery
- type UpdateQuery
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func BuildWhereSQL ¶
BuildWhereSQL is to be used by custom queries to build a WHERE clause.
func ExpandTables ¶
func ExpandTables(paths []lingo.Expression) []lingo.Expression
func JoinToSQL ¶
JoinToSQL will call ToSQL on each exp, returning the error if one occurs, and then joins the SQL together with sep in between each sql.Data.
func NewNamedOnlyColumn ¶
NewNamedOnlyColumn creates a `lingo.Column` of which only the name of the column is filled out. Thus, when `ToSQL()` is called, only a single SQL with the value of `name` is returned.
Types ¶
type DeleteQuery ¶
type DeleteQuery struct {
// contains filtered or unexported fields
}
func (DeleteQuery) Join ¶
func (d DeleteQuery) Join(left lingo.Expression, jt join.Type, on lingo.Expression) *DeleteQuery
DELETE w FROM WorkRecord2 w LEFT JOIN Employee e ON EmployeeRun=EmployeeNo WHERE w.Company = '1' AND e.Date = '2013-05-06'
func (DeleteQuery) Where ¶
func (d DeleteQuery) Where(exp ...lingo.Expression) *DeleteQuery
type ErrAroundSQL ¶
type ErrAroundSQL struct {
// contains filtered or unexported fields
}
func (ErrAroundSQL) Error ¶
func (e ErrAroundSQL) Error() string
func (ErrAroundSQL) SQL ¶
func (e ErrAroundSQL) SQL() string
func (ErrAroundSQL) Unwrap ¶
func (e ErrAroundSQL) Unwrap() error
type InsertQuery ¶
type InsertQuery struct {
// contains filtered or unexported fields
}
func InsertInto ¶
func InsertInto(entity lingo.Table) *InsertQuery
func (*InsertQuery) Columns ¶
func (i *InsertQuery) Columns(columns ...lingo.Column) *InsertQuery
func (*InsertQuery) Select ¶
func (i *InsertQuery) Select(q *SelectQuery) *InsertQuery
Select allows passing in a Select Query the following insert statements:
INSERT INTO table1 (uuid, name) SELECT UNHEX("1234567891230"), a.name FROM table2 as a LEFT JOIN table1 as b ON a.name=b.remote_name WHERE b.remote_name = 'other_table';
func (*InsertQuery) Values ¶
func (i *InsertQuery) Values(values ...lingo.Expression) *InsertQuery
Values allows inserting expressions with SQL functions:
INSERT INTO table1 (uuid, name) values (UNHEX("1234567891234"), 'name1');
func (*InsertQuery) ValuesConstants ¶
func (i *InsertQuery) ValuesConstants(values ...interface{}) *InsertQuery
ValuesConstants allows inserting constant values:
INSERT INTO table1 (id, name, internal_name) values (123456, 'name1', 'internal_name');
type Modifier ¶
type Modifier interface { IsZero() bool Limit() (limit uint64, wasSet bool) Offset() (offset uint64, wasSet bool) }
Modifier has information to alter the query - post statements.
type ModifyDialect ¶
ModifyDialect is used by the query builders to check if an interface is satisfied for a dialect.
type SelectQuery ¶
type SelectQuery struct {
// contains filtered or unexported fields
}
func Select ¶
func Select(paths ...lingo.Expression) *SelectQuery
func SelectFrom ¶
func SelectFrom(e lingo.Table) *SelectQuery
func (*SelectQuery) From ¶
func (q *SelectQuery) From(e lingo.Table) *SelectQuery
Example (Where) ¶
package main import ( "fmt" "github.com/weworksandbox/lingo/dialect" "github.com/weworksandbox/lingo/internal/test/schema/tsakila/tfilmactor" "github.com/weworksandbox/lingo/query" ) func main() { d, _ := dialect.NewDefault() fa := tfilmactor.As("fa") s, _ := query.Select(fa.FilmId()).From(fa).Where(fa.ActorId().Between(1, 10)).ToSQL(d) fmt.Println(s.String()) fmt.Println(s.Values()) }
Output: SELECT fa.film_id FROM film_actor AS fa WHERE fa.actor_id BETWEEN ? AND ? [1 10]
func (*SelectQuery) Join ¶
func (q *SelectQuery) Join(left lingo.Expression, joinType join.Type, on lingo.Expression) *SelectQuery
Join an expr with a specific joinType using an on statement.
func (*SelectQuery) OrderBy ¶
func (q *SelectQuery) OrderBy(exp lingo.Expression, direction sort.Direction) *SelectQuery
func (*SelectQuery) Restrict ¶
func (q *SelectQuery) Restrict(m Modifier) *SelectQuery
Restrict the query with things like limits and offsets.
Example ¶
package main import ( "fmt" "github.com/weworksandbox/lingo/dialect" "github.com/weworksandbox/lingo/internal/test/schema/tsakila/tfilmactor" "github.com/weworksandbox/lingo/query" ) func main() { d, _ := dialect.NewDefault() const maxPageNum = 1 // To limit output for example pageSize := uint64(150) fa := tfilmactor.As("fa") q := query.SelectFrom(fa) for page := uint64(0); page < maxPageNum; page++ { s, _ := q.Restrict(query.Page(pageSize, page*pageSize)).ToSQL(d) fmt.Println(s.String()) fmt.Println(s.Values()) } }
Output: SELECT fa.actor_id, fa.film_id, fa.last_update FROM film_actor AS fa LIMIT ? OFFSET ? [150 0]
func (*SelectQuery) Where ¶
func (q *SelectQuery) Where(exp ...lingo.Expression) *SelectQuery
type UpdateQuery ¶
type UpdateQuery struct {
// contains filtered or unexported fields
}
func Update ¶
func Update(table lingo.Table) *UpdateQuery
func (UpdateQuery) Set ¶
func (u UpdateQuery) Set(exp ...lingo.Set) *UpdateQuery
func (UpdateQuery) Where ¶
func (u UpdateQuery) Where(exp ...lingo.Expression) *UpdateQuery