From 99d69857faa2cd42d79558b5b9a447aa89882523 Mon Sep 17 00:00:00 2001 From: Colin <811687790@qq.com> Date: Fri, 21 Nov 2025 17:27:09 +0800 Subject: [PATCH] refactor(database/gdb): add quote for FieldsPrefix (#4485) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Code example: ``` go var res *BasicInfo err := g.DB().Model("basic_info"). FieldsPrefix("basic_info", basicInfoColumns). Where("id", 35813305356386305).Scan(&res) if err != nil { panic(err) } g.Dump(res) ``` SQL generated before modification : ``` sql SELECT basic_info.id,basic_info.full_name,basic_info.contact FROM `basic_info` WHERE (`id`=35813305356386305) AND `delete_time` IS NULL LIMIT 1 ``` SQL generated after modification: ``` sql SELECT `basic_info`.`id`,`basic_info`.`full_name`,`basic_info`.`contact` FROM `basic_info` WHERE (`id`=35813305356386305) AND `delete_time` IS NULL LIMIT 1 ``` --------- Co-authored-by: hailaz <739476267@qq.com> --- .../mysql/testdata/fix_gdb_join_expect.sql | 2 +- database/gdb/gdb_model_fields.go | 26 ++++++++++++------- database/gdb/gdb_model_select.go | 7 +++-- 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/contrib/drivers/mysql/testdata/fix_gdb_join_expect.sql b/contrib/drivers/mysql/testdata/fix_gdb_join_expect.sql index c391675cb..2ee4a53a4 100644 --- a/contrib/drivers/mysql/testdata/fix_gdb_join_expect.sql +++ b/contrib/drivers/mysql/testdata/fix_gdb_join_expect.sql @@ -1 +1 @@ -SELECT managed_resource.resource_id,managed_resource.user,managed_resource.status,managed_resource.status_message,managed_resource.safe_publication,managed_resource.rule_template_id,managed_resource.created_at,managed_resource.comments,managed_resource.expired_at,managed_resource.resource_mark_id,managed_resource.instance_id,managed_resource.resource_name,managed_resource.pay_mode,resource_mark.mark_name,resource_mark.color,rules_template.name,common_resource.src_instance_id,common_resource.database_kind,common_resource.source_type,common_resource.ip,common_resource.port FROM `managed_resource` LEFT JOIN `common_resource` ON (`managed_resource`.`resource_id`=`common_resource`.`resource_id`) LEFT JOIN `resource_mark` ON (`managed_resource`.`resource_mark_id` = `resource_mark`.`id`) LEFT JOIN `rules_template` ON (`managed_resource`.`rule_template_id` = `rules_template`.`template_id`) ORDER BY `src_instance_id` ASC \ No newline at end of file +SELECT `managed_resource`.`resource_id`,`managed_resource`.`user`,`managed_resource`.`status`,`managed_resource`.`status_message`,`managed_resource`.`safe_publication`,`managed_resource`.`rule_template_id`,`managed_resource`.`created_at`,`managed_resource`.`comments`,`managed_resource`.`expired_at`,`managed_resource`.`resource_mark_id`,`managed_resource`.`instance_id`,`managed_resource`.`resource_name`,`managed_resource`.`pay_mode`,`resource_mark`.`mark_name`,`resource_mark`.`color`,`rules_template`.`name`,`common_resource`.`src_instance_id`,`common_resource`.`database_kind`,`common_resource`.`source_type`,`common_resource`.`ip`,`common_resource`.`port` FROM `managed_resource` LEFT JOIN `common_resource` ON (`managed_resource`.`resource_id`=`common_resource`.`resource_id`) LEFT JOIN `resource_mark` ON (`managed_resource`.`resource_mark_id` = `resource_mark`.`id`) LEFT JOIN `rules_template` ON (`managed_resource`.`rule_template_id` = `rules_template`.`template_id`) ORDER BY `src_instance_id` ASC \ No newline at end of file diff --git a/database/gdb/gdb_model_fields.go b/database/gdb/gdb_model_fields.go index 04ad3638e..3b9d5698e 100644 --- a/database/gdb/gdb_model_fields.go +++ b/database/gdb/gdb_model_fields.go @@ -45,8 +45,9 @@ func (m *Model) FieldsPrefix(prefixOrAlias string, fieldNamesOrMapStruct ...any) if len(fields) == 0 { return m } + prefixOrAlias = m.QuoteWord(prefixOrAlias) for i, field := range fields { - fields[i] = prefixOrAlias + "." + gconv.String(field) + fields[i] = fmt.Sprintf("%s.%s", prefixOrAlias, m.QuoteWord(gconv.String(field))) } model := m.getModel() return model.appendToFields(fields...) @@ -81,14 +82,21 @@ func (m *Model) doFieldsEx(table string, fieldNamesOrMapStruct ...any) *Model { } // FieldsExPrefix performs as function FieldsEx but add extra prefix for each field. +// Note that this function must be used together with FieldsPrefix, otherwise it will be invalid. func (m *Model) FieldsExPrefix(prefixOrAlias string, fieldNamesOrMapStruct ...any) *Model { - model := m.doFieldsEx( + fields := m.filterFieldsFrom( m.getTableNameByPrefixOrAlias(prefixOrAlias), fieldNamesOrMapStruct..., ) - for i, field := range model.fieldsEx { - model.fieldsEx[i] = prefixOrAlias + "." + gconv.String(field) + if len(fields) == 0 { + return m } + prefixOrAlias = m.QuoteWord(prefixOrAlias) + for i, field := range fields { + fields[i] = fmt.Sprintf("%s.%s", prefixOrAlias, m.QuoteWord(gconv.String(field))) + } + model := m.getModel() + model.fieldsEx = append(model.fieldsEx, fields...) return model } @@ -96,7 +104,7 @@ func (m *Model) FieldsExPrefix(prefixOrAlias string, fieldNamesOrMapStruct ...an func (m *Model) FieldCount(column string, as ...string) *Model { asStr := "" if len(as) > 0 && as[0] != "" { - asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0])) + asStr = fmt.Sprintf(` AS %s`, m.QuoteWord(as[0])) } model := m.getModel() return model.appendToFields( @@ -108,7 +116,7 @@ func (m *Model) FieldCount(column string, as ...string) *Model { func (m *Model) FieldSum(column string, as ...string) *Model { asStr := "" if len(as) > 0 && as[0] != "" { - asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0])) + asStr = fmt.Sprintf(` AS %s`, m.QuoteWord(as[0])) } model := m.getModel() return model.appendToFields( @@ -120,7 +128,7 @@ func (m *Model) FieldSum(column string, as ...string) *Model { func (m *Model) FieldMin(column string, as ...string) *Model { asStr := "" if len(as) > 0 && as[0] != "" { - asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0])) + asStr = fmt.Sprintf(` AS %s`, m.QuoteWord(as[0])) } model := m.getModel() return model.appendToFields( @@ -132,7 +140,7 @@ func (m *Model) FieldMin(column string, as ...string) *Model { func (m *Model) FieldMax(column string, as ...string) *Model { asStr := "" if len(as) > 0 && as[0] != "" { - asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0])) + asStr = fmt.Sprintf(` AS %s`, m.QuoteWord(as[0])) } model := m.getModel() return model.appendToFields( @@ -144,7 +152,7 @@ func (m *Model) FieldMax(column string, as ...string) *Model { func (m *Model) FieldAvg(column string, as ...string) *Model { asStr := "" if len(as) > 0 && as[0] != "" { - asStr = fmt.Sprintf(` AS %s`, m.db.GetCore().QuoteWord(as[0])) + asStr = fmt.Sprintf(` AS %s`, m.QuoteWord(as[0])) } model := m.getModel() return model.appendToFields( diff --git a/database/gdb/gdb_model_select.go b/database/gdb/gdb_model_select.go index 2e3f72570..96b6324ab 100644 --- a/database/gdb/gdb_model_select.go +++ b/database/gdb/gdb_model_select.go @@ -752,7 +752,7 @@ func (m *Model) getHolderAndArgsAsSubModel(ctx context.Context) (holder string, func (m *Model) getAutoPrefix() string { autoPrefix := "" if gstr.Contains(m.tables, " JOIN ") { - autoPrefix = m.db.GetCore().QuoteWord( + autoPrefix = m.QuoteWord( m.db.GetCore().guessPrimaryTableName(m.tablesInit), ) } @@ -762,7 +762,6 @@ func (m *Model) getAutoPrefix() string { func (m *Model) getFieldsAsStr() string { var ( fieldsStr string - core = m.db.GetCore() ) for _, v := range m.fields { field := gconv.String(v) @@ -773,7 +772,7 @@ func (m *Model) getFieldsAsStr() string { switch v.(type) { case Raw, *Raw: default: - field = core.QuoteString(field) + field = m.QuoteWord(field) } } if fieldsStr != "" { @@ -829,7 +828,7 @@ func (m *Model) getFieldsFiltered() string { if len(newFields) > 0 { newFields += "," } - newFields += m.db.GetCore().QuoteWord(k) + newFields += m.QuoteWord(k) } return newFields }