2016-06-14 16:01:28 +02:00
|
|
|
package qm
|
2016-04-13 15:51:58 +02:00
|
|
|
|
2017-05-08 19:25:15 +02:00
|
|
|
import "github.com/lbryio/sqlboiler/queries"
|
2016-04-13 15:51:58 +02:00
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// QueryMod to modify the query object
|
2016-09-15 05:45:09 +02:00
|
|
|
type QueryMod func(q *queries.Query)
|
2016-04-19 04:02:32 +02:00
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// Apply the query mods to the Query object
|
2016-09-15 05:45:09 +02:00
|
|
|
func Apply(q *queries.Query, mods ...QueryMod) {
|
2016-04-20 14:51:04 +02:00
|
|
|
for _, mod := range mods {
|
|
|
|
mod(q)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// SQL allows you to execute a plain SQL statement
|
|
|
|
func SQL(sql string, args ...interface{}) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.SetSQL(q, sql, args...)
|
2016-07-19 06:07:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 19:56:00 +02:00
|
|
|
// Load allows you to specify foreign key relationships to eager load
|
|
|
|
// for your query. Passed in relationships need to be in the format
|
|
|
|
// MyThing or MyThings.
|
|
|
|
// Relationship name plurality is important, if your relationship is
|
|
|
|
// singular, you need to specify the singular form and vice versa.
|
|
|
|
func Load(relationships ...string) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
2016-10-01 08:24:42 +02:00
|
|
|
queries.AppendLoad(q, relationships...)
|
2016-08-01 04:21:06 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// InnerJoin on another table
|
2016-08-07 22:37:51 +02:00
|
|
|
func InnerJoin(clause string, args ...interface{}) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendInnerJoin(q, clause, args...)
|
2016-04-19 07:31:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// Select specific columns opposed to all columns
|
2016-04-16 09:25:00 +02:00
|
|
|
func Select(columns ...string) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendSelect(q, columns...)
|
2016-04-16 09:25:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// Where allows you to specify a where clause for your statement
|
2016-04-13 15:51:58 +02:00
|
|
|
func Where(clause string, args ...interface{}) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendWhere(q, clause, args...)
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 13:05:42 +02:00
|
|
|
// And allows you to specify a where clause separated by an AND for your statement
|
2016-08-11 08:44:15 +02:00
|
|
|
// And is a duplicate of the Where function, but allows for more natural looking
|
|
|
|
// query mod chains, for example: (Where("a=?"), And("b=?"), Or("c=?")))
|
|
|
|
func And(clause string, args ...interface{}) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendWhere(q, clause, args...)
|
2016-08-11 08:44:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-05 13:05:42 +02:00
|
|
|
// Or allows you to specify a where clause separated by an OR for your statement
|
2016-08-11 08:44:15 +02:00
|
|
|
func Or(clause string, args ...interface{}) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendWhere(q, clause, args...)
|
|
|
|
queries.SetLastWhereAsOr(q)
|
2016-08-11 08:44:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-17 07:19:23 +02:00
|
|
|
// WhereIn allows you to specify a "x IN (set)" clause for your where statement
|
|
|
|
// Example clauses: "column in ?", "(column1,column2) in ?"
|
|
|
|
func WhereIn(clause string, args ...interface{}) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendIn(q, clause, args...)
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// AndIn allows you to specify a "x IN (set)" clause separated by an AndIn
|
|
|
|
// for your where statement. AndIn is a duplicate of the WhereIn function, but
|
|
|
|
// allows for more natural looking query mod chains, for example:
|
|
|
|
// (WhereIn("column1 in ?"), AndIn("column2 in ?"), OrIn("column3 in ?"))
|
|
|
|
func AndIn(clause string, args ...interface{}) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendIn(q, clause, args...)
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// OrIn allows you to specify an IN clause separated by
|
|
|
|
// an OR for your where statement
|
|
|
|
func OrIn(clause string, args ...interface{}) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendIn(q, clause, args...)
|
|
|
|
queries.SetLastInAsOr(q)
|
2016-08-17 07:19:23 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// GroupBy allows you to specify a group by clause for your statement
|
2016-04-13 15:51:58 +02:00
|
|
|
func GroupBy(clause string) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendGroupBy(q, clause)
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// OrderBy allows you to specify a order by clause for your statement
|
2016-04-13 15:51:58 +02:00
|
|
|
func OrderBy(clause string) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendOrderBy(q, clause)
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// Having allows you to specify a having clause for your statement
|
2016-08-12 19:08:09 +02:00
|
|
|
func Having(clause string, args ...interface{}) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendHaving(q, clause, args...)
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-19 06:07:12 +02:00
|
|
|
// From allows to specify the table for your statement
|
|
|
|
func From(from string) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.AppendFrom(q, from)
|
2016-04-13 15:51:58 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-07 00:10:35 +02:00
|
|
|
|
2016-08-17 19:56:00 +02:00
|
|
|
// Limit the number of returned rows
|
|
|
|
func Limit(limit int) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.SetLimit(q, limit)
|
2016-08-17 19:56:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Offset into the results
|
|
|
|
func Offset(offset int) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.SetOffset(q, offset)
|
2016-08-17 19:56:00 +02:00
|
|
|
}
|
|
|
|
}
|
2016-08-30 05:13:00 +02:00
|
|
|
|
|
|
|
// For inserts a concurrency locking clause at the end of your statement
|
|
|
|
func For(clause string) QueryMod {
|
2016-09-15 05:45:09 +02:00
|
|
|
return func(q *queries.Query) {
|
|
|
|
queries.SetFor(q, clause)
|
2016-08-30 05:13:00 +02:00
|
|
|
}
|
|
|
|
}
|