mirror of
https://github.com/bufanyun/hotgo.git
synced 2025-11-14 05:03:49 +08:00
feat 调整 mysql 、pgsql 初始化数据; 完善pgsql 兼容性,适配代码生成逻辑
This commit is contained in:
@@ -9,12 +9,15 @@ package hgorm
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strings"
|
||||
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/db"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/errors/gerror"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"hotgo/utility/convert"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type daoInstance interface {
|
||||
@@ -35,7 +38,8 @@ func LeftJoin(m *gdb.Model, masterTable, masterField, joinTable, alias, onField
|
||||
|
||||
// GenJoinOnRelation 生成关联表关联条件
|
||||
func GenJoinOnRelation(masterTable, masterField, joinTable, alias, onField string) []string {
|
||||
relation := fmt.Sprintf("`%s`.`%s` = `%s`.`%s`", alias, onField, masterTable, masterField)
|
||||
q := db.GetQuoteChar()
|
||||
relation := fmt.Sprintf("%s%s%s.%s%s%s = %s%s%s.%s%s%s", q, alias, q, q, onField, q, q, masterTable, q, q, masterField, q)
|
||||
return []string{joinTable, alias, relation}
|
||||
}
|
||||
|
||||
@@ -62,7 +66,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, db.QuoteFieldAs(dao.Table(), field, v))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -117,7 +121,7 @@ func GenJoinSelect(ctx context.Context, entity interface{}, dao daoInstance, joi
|
||||
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, db.QuoteFieldAs(jd.Alias, joinField, field))
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -125,7 +129,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, db.QuoteField(dao.Table(), originalField))
|
||||
continue
|
||||
}
|
||||
}
|
||||
@@ -143,7 +147,7 @@ func GetPkField(ctx context.Context, dao daoInstance) (string, error) {
|
||||
}
|
||||
|
||||
for _, field := range fields {
|
||||
if strings.ToUpper(field.Key) == "PRI" {
|
||||
if strings.ToUpper(field.Key) == "PRI" {
|
||||
return field.Name, nil
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,16 +6,18 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"regexp"
|
||||
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/model/input/form"
|
||||
"hotgo/utility/convert"
|
||||
"hotgo/utility/db"
|
||||
|
||||
"github.com/gogf/gf/v2/database/gdb"
|
||||
"github.com/gogf/gf/v2/frame/g"
|
||||
"github.com/gogf/gf/v2/text/gregex"
|
||||
"github.com/gogf/gf/v2/text/gstr"
|
||||
"github.com/gogf/gf/v2/util/gutil"
|
||||
"hotgo/internal/consts"
|
||||
"hotgo/internal/model/input/form"
|
||||
"hotgo/utility/convert"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
// ISorter 排序器接口,实现该接口即可使用Handler匹配排序,支持多字段排序
|
||||
@@ -62,7 +64,7 @@ func Sorter(in ISorter) func(m *gdb.Model) *gdb.Model {
|
||||
if len(sorter2) > 0 {
|
||||
sorter2 = mappingAndFilterToTableFields(fds, sorter2)
|
||||
for _, v := range sorter2 {
|
||||
v.ColumnKey = fmt.Sprintf("`%v`.`%v`", as, v.ColumnKey)
|
||||
v.ColumnKey = db.QuoteField(as, v.ColumnKey)
|
||||
}
|
||||
newSorters = append(newSorters, sorter2...)
|
||||
}
|
||||
@@ -71,7 +73,7 @@ func Sorter(in ISorter) func(m *gdb.Model) *gdb.Model {
|
||||
// 移除关联表字段
|
||||
sorters = mappingAndFilterToTableFields(fields, removeSorterIndexes(sorters, removeIndex))
|
||||
for _, v := range sorters {
|
||||
v.ColumnKey = fmt.Sprintf("`%v`.`%v`", masterTable, v.ColumnKey)
|
||||
v.ColumnKey = db.QuoteField(masterTable, v.ColumnKey)
|
||||
}
|
||||
|
||||
sorters = append(newSorters, sorters...)
|
||||
@@ -79,7 +81,7 @@ func Sorter(in ISorter) func(m *gdb.Model) *gdb.Model {
|
||||
// 单表
|
||||
sorters = mappingAndFilterToTableFields(fields, sorters)
|
||||
for _, v := range sorters {
|
||||
v.ColumnKey = fmt.Sprintf("`%v`.`%v`", masterTable, v.ColumnKey)
|
||||
v.ColumnKey = db.QuoteField(masterTable, v.ColumnKey)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -123,11 +125,11 @@ func Sorter(in ISorter) func(m *gdb.Model) *gdb.Model {
|
||||
if len(aliases) > 0 {
|
||||
for as, table := range aliases {
|
||||
if table == masterTable {
|
||||
return m.OrderDesc(fmt.Sprintf("`%v`.`%v`", as, pk))
|
||||
return m.OrderDesc(db.QuoteField(as, pk))
|
||||
}
|
||||
}
|
||||
}
|
||||
return m.OrderDesc(fmt.Sprintf("`%v`.`%v`", masterTable, pk))
|
||||
return m.OrderDesc(db.QuoteField(masterTable, pk))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user