Add min/max/sum/avg to the query gen
This commit is contained in:
parent
5541b4dce9
commit
162746526c
3 changed files with 86 additions and 25 deletions
|
@ -95,3 +95,38 @@ func From(from string) QueryMod {
|
||||||
boil.AppendFrom(q, from)
|
boil.AppendFrom(q, from)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Count turns the query into a counting calculation
|
||||||
|
func Count() QueryMod {
|
||||||
|
return func(q *boil.Query) {
|
||||||
|
boil.SetCount(q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Avg turns the query into an average calculation
|
||||||
|
func Avg() QueryMod {
|
||||||
|
return func(q *boil.Query) {
|
||||||
|
boil.SetAvg(q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Min turns the query into a minimum calculation
|
||||||
|
func Min() QueryMod {
|
||||||
|
return func(q *boil.Query) {
|
||||||
|
boil.SetMin(q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Max turns the query into a maximum calculation
|
||||||
|
func Max() QueryMod {
|
||||||
|
return func(q *boil.Query) {
|
||||||
|
boil.SetMax(q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sum turns the query into a sum calculation
|
||||||
|
func Sum() QueryMod {
|
||||||
|
return func(q *boil.Query) {
|
||||||
|
boil.SetSum(q)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
@ -7,20 +7,20 @@ import (
|
||||||
|
|
||||||
// Query holds the state for the built up query
|
// Query holds the state for the built up query
|
||||||
type Query struct {
|
type Query struct {
|
||||||
executor Executor
|
executor Executor
|
||||||
plainSQL plainSQL
|
plainSQL plainSQL
|
||||||
delete bool
|
delete bool
|
||||||
update map[string]interface{}
|
update map[string]interface{}
|
||||||
selectCols []string
|
selectCols []string
|
||||||
count bool
|
modFunction string
|
||||||
from []string
|
from []string
|
||||||
innerJoins []join
|
innerJoins []join
|
||||||
where []where
|
where []where
|
||||||
groupBy []string
|
groupBy []string
|
||||||
orderBy []string
|
orderBy []string
|
||||||
having []string
|
having []string
|
||||||
limit int
|
limit int
|
||||||
offset int
|
offset int
|
||||||
}
|
}
|
||||||
|
|
||||||
type where struct {
|
type where struct {
|
||||||
|
@ -64,7 +64,27 @@ func SetSQL(q *Query, sql string, args ...interface{}) {
|
||||||
|
|
||||||
// SetCount on the query.
|
// SetCount on the query.
|
||||||
func SetCount(q *Query) {
|
func SetCount(q *Query) {
|
||||||
q.count = true
|
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"
|
||||||
}
|
}
|
||||||
|
|
||||||
// SetDelete on the query.
|
// SetDelete on the query.
|
||||||
|
|
|
@ -37,22 +37,22 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
||||||
|
|
||||||
buf.WriteString("SELECT ")
|
buf.WriteString("SELECT ")
|
||||||
|
|
||||||
if q.count {
|
// Wrap the select in the modifier function
|
||||||
buf.WriteString("COUNT(")
|
hasModFunc := len(q.modFunction) != 0
|
||||||
|
if hasModFunc {
|
||||||
|
fmt.Fprintf(buf, "%s(", q.modFunction)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(q.innerJoins) > 0 && !q.count {
|
hasSelectCols := len(q.selectCols) != 0
|
||||||
|
if len(q.innerJoins) != 0 && hasSelectCols && !hasModFunc {
|
||||||
writeComplexSelect(q, buf)
|
writeComplexSelect(q, buf)
|
||||||
|
} else if hasSelectCols {
|
||||||
|
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.selectCols), `, `))
|
||||||
} else {
|
} else {
|
||||||
if len(q.selectCols) > 0 {
|
buf.WriteByte('*')
|
||||||
buf.WriteString(strings.Join(strmangle.IdentQuoteSlice(q.selectCols), `, `))
|
|
||||||
} else {
|
|
||||||
buf.WriteByte('*')
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// close sql COUNT function
|
if hasModFunc {
|
||||||
if q.count {
|
|
||||||
buf.WriteString(")")
|
buf.WriteString(")")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -78,6 +78,12 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func writeComplexSelect(q *Query, buf *bytes.Buffer) {
|
func writeComplexSelect(q *Query, buf *bytes.Buffer) {
|
||||||
|
cols := make([]string, len(q.selectCols))
|
||||||
|
for _, col := range q.selectCols {
|
||||||
|
if !rgxIdentifier.Match {
|
||||||
|
cols
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildDeleteQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
func buildDeleteQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue