forked from mirror/gorm
Fix Select with specific symbol, close #3157
This commit is contained in:
parent
58e3241544
commit
362779575c
|
@ -60,11 +60,11 @@ func (db *DB) Table(name string) (tx *DB) {
|
||||||
|
|
||||||
// Distinct specify distinct fields that you want querying
|
// Distinct specify distinct fields that you want querying
|
||||||
func (db *DB) Distinct(args ...interface{}) (tx *DB) {
|
func (db *DB) Distinct(args ...interface{}) (tx *DB) {
|
||||||
tx = db
|
tx = db.getInstance()
|
||||||
|
tx.Statement.Distinct = true
|
||||||
if len(args) > 0 {
|
if len(args) > 0 {
|
||||||
tx = tx.Select(args[0], args[1:]...)
|
tx = tx.Select(args[0], args[1:]...)
|
||||||
}
|
}
|
||||||
tx.Statement.Distinct = true
|
|
||||||
return tx
|
return tx
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -102,6 +102,7 @@ func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
|
||||||
tx.Statement.Selects = append(tx.Statement.Selects, arg...)
|
tx.Statement.Selects = append(tx.Statement.Selects, arg...)
|
||||||
default:
|
default:
|
||||||
tx.Statement.AddClause(clause.Select{
|
tx.Statement.AddClause(clause.Select{
|
||||||
|
Distinct: db.Statement.Distinct,
|
||||||
Expression: clause.Expr{SQL: v, Vars: args},
|
Expression: clause.Expr{SQL: v, Vars: args},
|
||||||
})
|
})
|
||||||
return
|
return
|
||||||
|
@ -109,6 +110,7 @@ func (db *DB) Select(query interface{}, args ...interface{}) (tx *DB) {
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
tx.Statement.AddClause(clause.Select{
|
tx.Statement.AddClause(clause.Select{
|
||||||
|
Distinct: db.Statement.Distinct,
|
||||||
Expression: clause.Expr{SQL: v, Vars: args},
|
Expression: clause.Expr{SQL: v, Vars: args},
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
|
@ -30,6 +30,14 @@ func (s Select) Build(builder Builder) {
|
||||||
|
|
||||||
func (s Select) MergeClause(clause *Clause) {
|
func (s Select) MergeClause(clause *Clause) {
|
||||||
if s.Expression != nil {
|
if s.Expression != nil {
|
||||||
|
if s.Distinct {
|
||||||
|
if expr, ok := s.Expression.(Expr); ok {
|
||||||
|
expr.SQL = "DISTINCT " + expr.SQL
|
||||||
|
clause.Expression = expr
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
clause.Expression = s.Expression
|
clause.Expression = s.Expression
|
||||||
} else {
|
} else {
|
||||||
clause.Expression = s
|
clause.Expression = s
|
||||||
|
|
|
@ -1,8 +1,10 @@
|
||||||
package tests_test
|
package tests_test
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"regexp"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"gorm.io/gorm"
|
||||||
. "gorm.io/gorm/utils/tests"
|
. "gorm.io/gorm/utils/tests"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -57,4 +59,10 @@ func TestDistinct(t *testing.T) {
|
||||||
if err := DB.Model(&User{}).Distinct("name").Where("name like ?", "distinct%").Count(&count).Error; err != nil || count != 3 {
|
if err := DB.Model(&User{}).Distinct("name").Where("name like ?", "distinct%").Count(&count).Error; err != nil || count != 3 {
|
||||||
t.Errorf("failed to query users count, got error: %v, count %v", err, count)
|
t.Errorf("failed to query users count, got error: %v, count %v", err, count)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
dryDB := DB.Session(&gorm.Session{DryRun: true})
|
||||||
|
r := dryDB.Distinct("u.id, u.*").Table("user_speaks as s").Joins("inner join users as u on u.id = s.user_id").Where("s.language_code ='US' or s.language_code ='ES'").Find(&User{})
|
||||||
|
if !regexp.MustCompile(`SELECT DISTINCT u\.id, u\.\* FROM user_speaks as s inner join users as u`).MatchString(r.Statement.SQL.String()) {
|
||||||
|
t.Fatalf("Build Distinct with u.*, but got %v", r.Statement.SQL.String())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue