sqlboiler/boil/query.go

229 lines
4.6 KiB
Go
Raw Normal View History

package boil
import (
"database/sql"
"fmt"
)
// joinKind is the type of join
type joinKind int
// Join type constants
const (
JoinInner joinKind = iota
JoinOuterLeft
JoinOuterRight
JoinNatural
)
// Query holds the state for the built up query
type Query struct {
2016-08-07 00:10:35 +02:00
executor Executor
plainSQL plainSQL
load []string
2016-08-07 00:10:35 +02:00
delete bool
update map[string]interface{}
selectCols []string
modFunction string
from []string
joins []join
2016-08-07 00:10:35 +02:00
where []where
2016-08-17 07:19:23 +02:00
in []in
2016-08-07 00:10:35 +02:00
groupBy []string
orderBy []string
having []having
2016-08-07 00:10:35 +02:00
limit int
offset int
}
2016-08-06 23:42:22 +02:00
type where struct {
clause string
orSeparator bool
args []interface{}
}
2016-08-17 07:19:23 +02:00
type in struct {
clause string
orSeparator bool
args []interface{}
}
type having struct {
clause string
args []interface{}
}
2016-08-06 23:42:22 +02:00
type plainSQL struct {
sql string
args []interface{}
}
2016-08-06 23:42:22 +02:00
type join struct {
kind joinKind
clause string
args []interface{}
2016-08-04 06:29:18 +02:00
}
2016-08-08 08:36:11 +02:00
// SQL makes a plainSQL query, usually for use with bind
func SQL(query string, args ...interface{}) *Query {
return &Query{
plainSQL: plainSQL{
sql: query,
args: args,
},
}
}
2016-08-08 03:16:34 +02:00
// ExecQuery executes a query that does not need a row returned
func ExecQuery(q *Query) (sql.Result, error) {
2016-04-20 15:03:33 +02:00
qs, args := buildQuery(q)
if DebugMode {
fmt.Fprintln(DebugWriter, qs)
fmt.Fprintln(DebugWriter, args)
}
2016-04-20 15:03:33 +02:00
return q.executor.Exec(qs, args...)
}
2016-08-08 03:16:34 +02:00
// ExecQueryOne executes the query for the One finisher and returns a row
func ExecQueryOne(q *Query) *sql.Row {
qs, args := buildQuery(q)
if DebugMode {
fmt.Fprintln(DebugWriter, qs)
fmt.Fprintln(DebugWriter, args)
}
return q.executor.QueryRow(qs, args...)
}
// ExecQueryAll executes the query for the All finisher and returns multiple rows
func ExecQueryAll(q *Query) (*sql.Rows, error) {
2016-04-20 15:03:33 +02:00
qs, args := buildQuery(q)
if DebugMode {
fmt.Fprintln(DebugWriter, qs)
fmt.Fprintln(DebugWriter, args)
}
return q.executor.Query(qs, args...)
}
2016-08-17 07:19:23 +02:00
// SetExecutor on the query.
func SetExecutor(q *Query, exec Executor) {
q.executor = exec
}
// SetSQL on the query.
func SetSQL(q *Query, sql string, args ...interface{}) {
q.plainSQL = plainSQL{sql: sql, args: args}
}
// SetLoad on the query.
func SetLoad(q *Query, relationships ...string) {
q.load = append([]string(nil), relationships...)
}
// SetCount on the query.
2016-05-10 12:20:29 +02:00
func SetCount(q *Query) {
2016-08-07 00:10:35 +02:00
q.modFunction = "COUNT"
}
// SetAvg on the query.
func SetAvg(q *Query) {
q.modFunction = "AVG"
}
// SetMax on the query.
func SetMax(q *Query) {
q.modFunction = "MAX"
}
// SetMin on the query.
func SetMin(q *Query) {
q.modFunction = "MIN"
}
// SetSum on the query.
func SetSum(q *Query) {
q.modFunction = "SUM"
2016-05-10 12:20:29 +02:00
}
// SetDelete on the query.
func SetDelete(q *Query) {
q.delete = true
}
2016-08-17 07:19:23 +02:00
// SetLimit on the query.
func SetLimit(q *Query, limit int) {
q.limit = limit
}
// SetOffset on the query.
func SetOffset(q *Query, offset int) {
q.offset = offset
}
2016-08-04 06:29:18 +02:00
// SetUpdate on the query.
func SetUpdate(q *Query, cols map[string]interface{}) {
q.update = cols
}
// AppendSelect on the query.
func AppendSelect(q *Query, columns ...string) {
q.selectCols = append(q.selectCols, columns...)
}
// AppendFrom on the query.
func AppendFrom(q *Query, from ...string) {
2016-08-04 06:50:26 +02:00
q.from = append(q.from, from...)
}
// SetFrom replaces the current from statements.
func SetFrom(q *Query, from ...string) {
q.from = append([]string(nil), from...)
}
// AppendInnerJoin on the query.
func AppendInnerJoin(q *Query, clause string, args ...interface{}) {
q.joins = append(q.joins, join{clause: clause, kind: JoinInner, args: args})
}
2016-08-17 07:19:23 +02:00
// AppendHaving on the query.
func AppendHaving(q *Query, clause string, args ...interface{}) {
q.having = append(q.having, having{clause: clause, args: args})
}
// AppendWhere on the query.
func AppendWhere(q *Query, clause string, args ...interface{}) {
q.where = append(q.where, where{clause: clause, args: args})
}
2016-08-17 07:19:23 +02:00
// AppendIn on the query.
func AppendIn(q *Query, clause string, args ...interface{}) {
q.in = append(q.in, in{clause: clause, args: args})
}
2016-08-17 07:19:23 +02:00
// SetLastWhereAsOr sets the or separator for the tail "WHERE" in the slice
func SetLastWhereAsOr(q *Query) {
if len(q.where) == 0 {
return
}
q.where[len(q.where)-1].orSeparator = true
}
2016-08-17 07:19:23 +02:00
// SetLastInAsOr sets the or separator for the tail "IN" in the slice
func SetLastInAsOr(q *Query) {
if len(q.in) == 0 {
return
}
2016-08-17 07:19:23 +02:00
q.in[len(q.in)-1].orSeparator = true
}
2016-08-17 07:19:23 +02:00
// AppendGroupBy on the query.
func AppendGroupBy(q *Query, clause string) {
q.groupBy = append(q.groupBy, clause)
}
2016-08-17 07:19:23 +02:00
// AppendOrderBy on the query.
func AppendOrderBy(q *Query, clause string) {
q.orderBy = append(q.orderBy, clause)
2016-08-01 04:21:06 +02:00
}