diff --git a/contrib/drivers/mysql/testdata/fix_gdb_join_expect.sql b/contrib/drivers/mysql/testdata/fix_gdb_join_expect.sql index c391675cbb6..2ee4a53a431 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 04ad3638ea1..3b9d5698e64 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 2e3f7257058..96b6324ab19 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 }