This commit is contained in:
John Guo
2022-04-11 20:38:48 +08:00
parent f9a3fa3c23
commit 190a53647e
3 changed files with 20 additions and 3 deletions

View File

@ -43,12 +43,12 @@ func (m *Model) Fields(fieldNamesOrMapStruct ...interface{}) *Model {
return m.appendFieldsByStr(gstr.Join(
m.mappingAndFilterToTableFields([]string{r}, false), ",",
))
case []string:
return m.appendFieldsByStr(gstr.Join(
m.mappingAndFilterToTableFields(r, true), ",",
))
case Raw, *Raw:
return m.appendFieldsByStr(gconv.String(structOrMap))
default:
return m.appendFieldsByStr(gstr.Join(
m.mappingAndFilterToTableFields(getFieldsFromStructOrMap(structOrMap), true), ",",
@ -92,6 +92,8 @@ func (m *Model) FieldsEx(fieldNamesOrMapStruct ...interface{}) *Model {
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields([]string{r}, false), ",")
case []string:
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(r, true), ",")
case Raw, *Raw:
model.fieldsEx = gconv.String(fieldNamesOrMapStruct[0])
default:
model.fieldsEx = gstr.Join(m.mappingAndFilterToTableFields(getFieldsFromStructOrMap(r), true), ",")
}

View File

@ -50,8 +50,12 @@ func (m *Model) doGetAll(ctx context.Context, limit1 bool, where ...interface{})
// really be committed to underlying database driver.
func (m *Model) getFieldsFiltered() string {
if m.fieldsEx == "" {
// No filtering, containing special chars.
if gstr.ContainsAny(m.fields, "()") {
return m.fields
}
// No filtering.
if !gstr.Contains(m.fields, ".") && !gstr.Contains(m.fields, " ") {
if !gstr.ContainsAny(m.fields, ". ") {
return m.db.GetCore().QuoteString(m.fields)
}
return m.fields

View File

@ -4275,3 +4275,14 @@ func Test_Model_Issue1700(t *testing.T) {
}
})
}
// https://github.com/gogf/gf/issues/1701
func Test_Model_Issue1701(t *testing.T) {
table := createInitTable()
defer dropTable(table)
gtest.C(t, func(t *gtest.T) {
value, err := db.Model(table).Fields(gdb.Raw("if(id=1,100,null)")).WherePri(1).Value()
t.AssertNil(err)
t.Assert(value.String(), 100)
})
}