Remove bad things (sum, min, max, avg, etc)
This commit is contained in:
parent
2179570afd
commit
26bb719563
4 changed files with 24 additions and 124 deletions
|
@ -136,38 +136,3 @@ func Offset(offset int) QueryMod {
|
||||||
boil.SetOffset(q, offset)
|
boil.SetOffset(q, offset)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
|
@ -18,22 +18,22 @@ const (
|
||||||
|
|
||||||
// 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
|
||||||
load []string
|
load []string
|
||||||
delete bool
|
delete bool
|
||||||
update map[string]interface{}
|
update map[string]interface{}
|
||||||
selectCols []string
|
selectCols []string
|
||||||
modFunction string
|
count bool
|
||||||
from []string
|
from []string
|
||||||
joins []join
|
joins []join
|
||||||
where []where
|
where []where
|
||||||
in []in
|
in []in
|
||||||
groupBy []string
|
groupBy []string
|
||||||
orderBy []string
|
orderBy []string
|
||||||
having []having
|
having []having
|
||||||
limit int
|
limit int
|
||||||
offset int
|
offset int
|
||||||
}
|
}
|
||||||
|
|
||||||
type where struct {
|
type where struct {
|
||||||
|
@ -121,27 +121,7 @@ func SetLoad(q *Query, relationships ...string) {
|
||||||
|
|
||||||
// SetCount on the query.
|
// SetCount on the query.
|
||||||
func SetCount(q *Query) {
|
func SetCount(q *Query) {
|
||||||
q.modFunction = "COUNT"
|
q.count = true
|
||||||
}
|
|
||||||
|
|
||||||
// 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.
|
||||||
|
|
|
@ -41,15 +41,13 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
||||||
|
|
||||||
buf.WriteString("SELECT ")
|
buf.WriteString("SELECT ")
|
||||||
|
|
||||||
// Wrap the select in the modifier function
|
if q.count {
|
||||||
hasModFunc := len(q.modFunction) != 0
|
buf.WriteString("COUNT(")
|
||||||
if hasModFunc {
|
|
||||||
fmt.Fprintf(buf, "%s(", q.modFunction)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
hasSelectCols := len(q.selectCols) != 0
|
hasSelectCols := len(q.selectCols) != 0
|
||||||
hasJoins := len(q.joins) != 0
|
hasJoins := len(q.joins) != 0
|
||||||
if hasSelectCols && hasJoins && !hasModFunc {
|
if hasJoins && hasSelectCols && !q.count {
|
||||||
selectColsWithAs := writeAsStatements(q)
|
selectColsWithAs := writeAsStatements(q)
|
||||||
// Don't identQuoteSlice - writeAsStatements does this
|
// Don't identQuoteSlice - writeAsStatements does this
|
||||||
buf.WriteString(strings.Join(selectColsWithAs, ", "))
|
buf.WriteString(strings.Join(selectColsWithAs, ", "))
|
||||||
|
@ -62,7 +60,8 @@ func buildSelectQuery(q *Query) (*bytes.Buffer, []interface{}) {
|
||||||
buf.WriteByte('*')
|
buf.WriteByte('*')
|
||||||
}
|
}
|
||||||
|
|
||||||
if hasModFunc {
|
// close SQL COUNT function
|
||||||
|
if q.count {
|
||||||
buf.WriteByte(')')
|
buf.WriteByte(')')
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -296,52 +296,8 @@ func TestSetCount(t *testing.T) {
|
||||||
q := &Query{}
|
q := &Query{}
|
||||||
SetCount(q)
|
SetCount(q)
|
||||||
|
|
||||||
if q.modFunction != "COUNT" {
|
if q.count != true {
|
||||||
t.Errorf("Wrong modFunction, got %s", q.modFunction)
|
t.Errorf("got false")
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetAvg(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
q := &Query{}
|
|
||||||
SetAvg(q)
|
|
||||||
|
|
||||||
if q.modFunction != "AVG" {
|
|
||||||
t.Errorf("Wrong modFunction, got %s", q.modFunction)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetMax(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
q := &Query{}
|
|
||||||
SetMax(q)
|
|
||||||
|
|
||||||
if q.modFunction != "MAX" {
|
|
||||||
t.Errorf("Wrong modFunction, got %s", q.modFunction)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetMin(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
q := &Query{}
|
|
||||||
SetMin(q)
|
|
||||||
|
|
||||||
if q.modFunction != "MIN" {
|
|
||||||
t.Errorf("Wrong modFunction, got %s", q.modFunction)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestSetSum(t *testing.T) {
|
|
||||||
t.Parallel()
|
|
||||||
|
|
||||||
q := &Query{}
|
|
||||||
SetSum(q)
|
|
||||||
|
|
||||||
if q.modFunction != "SUM" {
|
|
||||||
t.Errorf("Wrong modFunction, got %s", q.modFunction)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue