mirror of
https://gitee.com/johng/gf
synced 2026-07-05 13:22:16 +08:00
refactor(database/gdb): add quote for FieldsPrefix (#4485)
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>
This commit is contained in:
@ -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
|
||||
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
|
||||
@ -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(
|
||||
|
||||
@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user