mirror of
https://gitee.com/johng/gf
synced 2026-06-29 02:26:29 +08:00
fix issue in unnacessary quoting of fields in select statement of gdb.Model
This commit is contained in:
@ -42,10 +42,12 @@ func (m *Model) All(where ...interface{}) (Result, error) {
|
||||
}
|
||||
conditionWhere += softDeletingCondition
|
||||
}
|
||||
// DO NOT quote the m.fields where, in case of fields like:
|
||||
// DISTINCT t.user_id uid
|
||||
return m.doGetAll(
|
||||
fmt.Sprintf(
|
||||
"SELECT %s FROM %s%s",
|
||||
m.db.QuoteString(m.fields),
|
||||
m.fields,
|
||||
m.tables,
|
||||
conditionWhere+conditionExtra,
|
||||
),
|
||||
@ -249,7 +251,9 @@ func (m *Model) Count(where ...interface{}) (int, error) {
|
||||
}
|
||||
countFields := "COUNT(1)"
|
||||
if m.fields != "" && m.fields != "*" {
|
||||
countFields = fmt.Sprintf(`COUNT(%s)`, m.db.QuoteString(m.fields))
|
||||
// DO NOT quote the m.fields here, in case of fields like:
|
||||
// DISTINCT t.user_id uid
|
||||
countFields = fmt.Sprintf(`COUNT(%s)`, m.fields)
|
||||
}
|
||||
var (
|
||||
softDeletingCondition = m.getConditionForSoftDeleting()
|
||||
|
||||
@ -2297,3 +2297,30 @@ func Test_Model_Having(t *testing.T) {
|
||||
t.Assert(len(all), 1)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Distinct(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
all, err := db.Table(table, "t").Fields("distinct t.id").Where("id > 1").Having("id > 8").All()
|
||||
t.Assert(err, nil)
|
||||
t.Assert(len(all), 2)
|
||||
})
|
||||
}
|
||||
|
||||
func Test_Model_Min_Max(t *testing.T) {
|
||||
table := createInitTable()
|
||||
defer dropTable(table)
|
||||
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Table(table, "t").Fields("min(t.id)").Where("id > 1").Value()
|
||||
t.Assert(err, nil)
|
||||
t.Assert(value.Int(), 2)
|
||||
})
|
||||
gtest.C(t, func(t *gtest.T) {
|
||||
value, err := db.Table(table, "t").Fields("max(t.id)").Where("id > 1").Value()
|
||||
t.Assert(err, nil)
|
||||
t.Assert(value.Int(), 10)
|
||||
})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user