diff --git a/server/internal/library/hgorm/dao.go b/server/internal/library/hgorm/dao.go index fb91abdb..e96bd378 100644 --- a/server/internal/library/hgorm/dao.go +++ b/server/internal/library/hgorm/dao.go @@ -35,6 +35,13 @@ func LeftJoin(m *gdb.Model, masterTable, masterField, joinTable, alias, onField // GenJoinOnRelation 生成关联表关联条件 func GenJoinOnRelation(masterTable, masterField, joinTable, alias, onField string) []string { + db := g.DB().GetCore() + masterTable = db.QuoteWord(masterTable) + masterField = db.QuoteWord(masterField) + joinTable = db.QuoteWord(joinTable) + alias = db.QuoteWord(alias) + onField = db.QuoteWord(onField) + relation := fmt.Sprintf("`%s`.`%s` = `%s`.`%s`", alias, onField, masterTable, masterField) return []string{joinTable, alias, relation} } @@ -55,6 +62,7 @@ func JoinFields(ctx context.Context, entity interface{}, dao daoInstance, as str } var columns []string + db := g.DB().GetCore() for _, v := range entityFs { if !gstr.HasPrefix(v, as) { continue @@ -62,7 +70,7 @@ func JoinFields(ctx context.Context, entity interface{}, dao daoInstance, as str field := gstr.CaseSnakeFirstUpper(gstr.StrEx(v, as)) if _, ok := fields[field]; ok { - columns = append(columns, fmt.Sprintf("`%s`.`%s` as `%s`", dao.Table(), field, v)) + columns = append(columns, fmt.Sprintf("%s.%s as %s", db.QuoteWord(dao.Table()), db.QuoteWord(field), db.QuoteWord(v))) } } @@ -111,13 +119,13 @@ func GenJoinSelect(ctx context.Context, entity interface{}, dao daoInstance, joi } return nil, "" } - + db := g.DB().GetCore() for _, field := range entityFields { // 关联表 jd, joinField := getJoinAttribute(field) if jd != nil { if _, ok := jd.fields[joinField]; ok { - tmpFields = append(tmpFields, fmt.Sprintf("`%s`.`%s` as `%s`", jd.Alias, joinField, field)) + tmpFields = append(tmpFields, fmt.Sprintf("%s.%s as %s", db.QuoteWord(jd.Alias), db.QuoteWord(joinField), db.QuoteWord(field))) continue } } @@ -125,7 +133,7 @@ func GenJoinSelect(ctx context.Context, entity interface{}, dao daoInstance, joi // 主表 originalField := gstr.CaseSnakeFirstUpper(field) if _, ok := masterFields[originalField]; ok { - tmpFields = append(tmpFields, fmt.Sprintf("`%s`.`%s`", dao.Table(), originalField)) + tmpFields = append(tmpFields, fmt.Sprintf("%s.%s", db.QuoteWord(dao.Table()), db.QuoteWord(originalField))) continue } }