1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-25 21:11:29 +00:00

Merge pull request #1276 from pjoe/orm_distinct

Fix issue #1274: Add QuerySeter.Distinct()
This commit is contained in:
astaxie 2015-08-27 22:54:39 +08:00
commit 0c5f4b48d4
3 changed files with 13 additions and 1 deletions

View File

@ -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)

View File

@ -61,6 +61,7 @@ type querySet struct {
limit int64
offset int64
orders []string
distinct bool
orm *orm
}
@ -112,6 +113,12 @@ func (o querySet) OrderBy(exprs ...string) QuerySeter {
return &o
}
// add DISTINCT to SELECT
func (o querySet) Distinct() QuerySeter {
o.distinct = true
return &o
}
// set relation model to query together.
// it will query relation models and assign to parent model.
func (o querySet) RelatedSel(params ...interface{}) QuerySeter {

View File

@ -67,6 +67,7 @@ type QuerySeter interface {
Limit(interface{}, ...interface{}) QuerySeter
Offset(interface{}) QuerySeter
OrderBy(...string) QuerySeter
Distinct() QuerySeter
RelatedSel(...interface{}) QuerySeter
Count() (int64, error)
Exist() bool