From 190a53647e3393641906e645935679e92864795f Mon Sep 17 00:00:00 2001 From: John Guo Date: Mon, 11 Apr 2022 20:38:48 +0800 Subject: [PATCH] fix issue #1701 --- database/gdb/gdb_model_fields.go | 6 ++++-- database/gdb/gdb_model_select.go | 6 +++++- database/gdb/gdb_z_mysql_model_test.go | 11 +++++++++++ 3 files changed, 20 insertions(+), 3 deletions(-) diff --git a/database/gdb/gdb_model_fields.go b/database/gdb/gdb_model_fields.go index 9d2cd34a8..cc3f13011 100644 --- a/database/gdb/gdb_model_fields.go +++ b/database/gdb/gdb_model_fields.go @@ -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), ",") } diff --git a/database/gdb/gdb_model_select.go b/database/gdb/gdb_model_select.go index 02e8b8be5..255327915 100644 --- a/database/gdb/gdb_model_select.go +++ b/database/gdb/gdb_model_select.go @@ -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 diff --git a/database/gdb/gdb_z_mysql_model_test.go b/database/gdb/gdb_z_mysql_model_test.go index 7e23c60cc..1744a5a8d 100644 --- a/database/gdb/gdb_z_mysql_model_test.go +++ b/database/gdb/gdb_z_mysql_model_test.go @@ -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) + }) +}