mirror of
https://github.com/astaxie/beego.git
synced 2024-11-05 02:00:54 +00:00
commit
2e75c04ffb
@ -802,6 +802,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
|
|||||||
tables.parseRelated(qs.related, qs.relDepth)
|
tables.parseRelated(qs.related, qs.relDepth)
|
||||||
|
|
||||||
where, args := tables.getCondSql(cond, false, tz)
|
where, args := tables.getCondSql(cond, false, tz)
|
||||||
|
groupBy := tables.getGroupSql(qs.groups)
|
||||||
orderBy := tables.getOrderSql(qs.orders)
|
orderBy := tables.getOrderSql(qs.orders)
|
||||||
limit := tables.getLimitSql(mi, offset, rlimit)
|
limit := tables.getLimitSql(mi, offset, rlimit)
|
||||||
join := tables.getJoinSql()
|
join := tables.getJoinSql()
|
||||||
@ -818,7 +819,7 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
|
|||||||
if qs.distinct {
|
if qs.distinct {
|
||||||
sqlSelect += " DISTINCT"
|
sqlSelect += " DISTINCT"
|
||||||
}
|
}
|
||||||
query := fmt.Sprintf("%s %s FROM %s%s%s T0 %s%s%s%s", sqlSelect, sels, Q, mi.table, Q, join, where, orderBy, limit)
|
query := fmt.Sprintf("%s %s FROM %s%s%s T0 %s%s%s%s%s", sqlSelect, sels, Q, mi.table, Q, join, where, groupBy, orderBy, limit)
|
||||||
|
|
||||||
d.ins.ReplaceMarks(&query)
|
d.ins.ReplaceMarks(&query)
|
||||||
|
|
||||||
@ -1448,13 +1449,14 @@ func (d *dbBase) ReadValues(q dbQuerier, qs *querySet, mi *modelInfo, cond *Cond
|
|||||||
}
|
}
|
||||||
|
|
||||||
where, args := tables.getCondSql(cond, false, tz)
|
where, args := tables.getCondSql(cond, false, tz)
|
||||||
|
groupBy := tables.getGroupSql(qs.groups)
|
||||||
orderBy := tables.getOrderSql(qs.orders)
|
orderBy := tables.getOrderSql(qs.orders)
|
||||||
limit := tables.getLimitSql(mi, qs.offset, qs.limit)
|
limit := tables.getLimitSql(mi, qs.offset, qs.limit)
|
||||||
join := tables.getJoinSql()
|
join := tables.getJoinSql()
|
||||||
|
|
||||||
sels := strings.Join(cols, ", ")
|
sels := strings.Join(cols, ", ")
|
||||||
|
|
||||||
query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s%s%s", sels, Q, mi.table, Q, join, where, orderBy, limit)
|
query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s%s%s%s", sels, Q, mi.table, Q, join, where, groupBy,orderBy, limit)
|
||||||
|
|
||||||
d.ins.ReplaceMarks(&query)
|
d.ins.ReplaceMarks(&query)
|
||||||
|
|
||||||
|
@ -390,6 +390,30 @@ func (t *dbTables) getCondSql(cond *Condition, sub bool, tz *time.Location) (whe
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// generate group sql.
|
||||||
|
func (t *dbTables) getGroupSql(groups []string) (groupSql string) {
|
||||||
|
if len(groups) == 0 {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
Q := t.base.TableQuote()
|
||||||
|
|
||||||
|
groupSqls := make([]string, 0, len(groups))
|
||||||
|
for _, group := range groups {
|
||||||
|
exprs := strings.Split(group, ExprSep)
|
||||||
|
|
||||||
|
index, _, fi, suc := t.parseExprs(t.mi, exprs)
|
||||||
|
if suc == false {
|
||||||
|
panic(fmt.Errorf("unknown field/column name `%s`", strings.Join(exprs, ExprSep)))
|
||||||
|
}
|
||||||
|
|
||||||
|
groupSqls = append(groupSqls, fmt.Sprintf("%s.%s%s%s", index, Q, fi.column, Q))
|
||||||
|
}
|
||||||
|
|
||||||
|
groupSql = fmt.Sprintf("GROUP BY %s ", strings.Join(groupSqls, ", "))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
// generate order sql.
|
// generate order sql.
|
||||||
func (t *dbTables) getOrderSql(orders []string) (orderSql string) {
|
func (t *dbTables) getOrderSql(orders []string) (orderSql string) {
|
||||||
if len(orders) == 0 {
|
if len(orders) == 0 {
|
||||||
|
@ -60,6 +60,7 @@ type querySet struct {
|
|||||||
relDepth int
|
relDepth int
|
||||||
limit int64
|
limit int64
|
||||||
offset int64
|
offset int64
|
||||||
|
groups []string
|
||||||
orders []string
|
orders []string
|
||||||
distinct bool
|
distinct bool
|
||||||
orm *orm
|
orm *orm
|
||||||
@ -106,6 +107,12 @@ func (o querySet) Offset(offset interface{}) QuerySeter {
|
|||||||
return &o
|
return &o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add GROUP expression
|
||||||
|
func (o querySet) GroupBy(exprs ...string) QuerySeter {
|
||||||
|
o.groups = exprs
|
||||||
|
return &o
|
||||||
|
}
|
||||||
|
|
||||||
// add ORDER expression.
|
// add ORDER expression.
|
||||||
// "column" means ASC, "-column" means DESC.
|
// "column" means ASC, "-column" means DESC.
|
||||||
func (o querySet) OrderBy(exprs ...string) QuerySeter {
|
func (o querySet) OrderBy(exprs ...string) QuerySeter {
|
||||||
|
@ -66,6 +66,7 @@ type QuerySeter interface {
|
|||||||
SetCond(*Condition) QuerySeter
|
SetCond(*Condition) QuerySeter
|
||||||
Limit(interface{}, ...interface{}) QuerySeter
|
Limit(interface{}, ...interface{}) QuerySeter
|
||||||
Offset(interface{}) QuerySeter
|
Offset(interface{}) QuerySeter
|
||||||
|
GroupBy(...string) QuerySeter
|
||||||
OrderBy(...string) QuerySeter
|
OrderBy(...string) QuerySeter
|
||||||
Distinct() QuerySeter
|
Distinct() QuerySeter
|
||||||
RelatedSel(...interface{}) QuerySeter
|
RelatedSel(...interface{}) QuerySeter
|
||||||
|
Loading…
Reference in New Issue
Block a user