mirror of
https://github.com/astaxie/beego.git
synced 2024-11-22 15:10:55 +00:00
Fix #1274: Add QuerySeter.Distinct()
This commit is contained in:
parent
9775e3e3a4
commit
19d82ab62c
@ -814,7 +814,11 @@ func (d *dbBase) ReadBatch(q dbQuerier, qs *querySet, mi *modelInfo, cond *Condi
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
query := fmt.Sprintf("SELECT %s FROM %s%s%s T0 %s%s%s%s", sels, Q, mi.table, Q, join, where, orderBy, limit)
|
sqlSelect := "SELECT"
|
||||||
|
if qs.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)
|
||||||
|
|
||||||
d.ins.ReplaceMarks(&query)
|
d.ins.ReplaceMarks(&query)
|
||||||
|
|
||||||
|
@ -61,6 +61,7 @@ type querySet struct {
|
|||||||
limit int64
|
limit int64
|
||||||
offset int64
|
offset int64
|
||||||
orders []string
|
orders []string
|
||||||
|
distinct bool
|
||||||
orm *orm
|
orm *orm
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -112,24 +113,30 @@ func (o querySet) OrderBy(exprs ...string) QuerySeter {
|
|||||||
return &o
|
return &o
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// add DISTINCT to SELECT
|
||||||
|
func (o querySet) Distinct() QuerySeter {
|
||||||
|
o.distinct = true
|
||||||
|
return &o
|
||||||
|
}
|
||||||
|
|
||||||
// set relation model to query together.
|
// set relation model to query together.
|
||||||
// it will query relation models and assign to parent model.
|
// it will query relation models and assign to parent model.
|
||||||
func (o querySet) RelatedSel(params ...interface{}) QuerySeter {
|
func (o querySet) RelatedSel(params ...interface{}) QuerySeter {
|
||||||
if len(params) == 0 {
|
if len(params) == 0 {
|
||||||
o.relDepth = DefaultRelsDepth
|
o.relDepth = DefaultRelsDepth
|
||||||
} else {
|
} else {
|
||||||
for _, p := range params {
|
for _, p := range params {
|
||||||
switch val := p.(type) {
|
switch val := p.(type) {
|
||||||
case string:
|
case string:
|
||||||
o.related = append(o.related, val)
|
o.related = append(o.related, val)
|
||||||
case int:
|
case int:
|
||||||
o.relDepth = val
|
o.relDepth = val
|
||||||
default:
|
default:
|
||||||
panic(fmt.Errorf("<QuerySeter.RelatedSel> wrong param kind: %v", val))
|
panic(fmt.Errorf("<QuerySeter.RelatedSel> wrong param kind: %v", val))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return &o
|
return &o
|
||||||
}
|
}
|
||||||
|
|
||||||
// set condition to QuerySeter.
|
// set condition to QuerySeter.
|
||||||
|
@ -67,6 +67,7 @@ type QuerySeter interface {
|
|||||||
Limit(interface{}, ...interface{}) QuerySeter
|
Limit(interface{}, ...interface{}) QuerySeter
|
||||||
Offset(interface{}) QuerySeter
|
Offset(interface{}) QuerySeter
|
||||||
OrderBy(...string) QuerySeter
|
OrderBy(...string) QuerySeter
|
||||||
|
Distinct() QuerySeter
|
||||||
RelatedSel(...interface{}) QuerySeter
|
RelatedSel(...interface{}) QuerySeter
|
||||||
Count() (int64, error)
|
Count() (int64, error)
|
||||||
Exist() bool
|
Exist() bool
|
||||||
|
Loading…
Reference in New Issue
Block a user