提交 ff4eee31 authored 作者: mooncake's avatar mooncake

update

上级 0963312f
...@@ -22,7 +22,7 @@ func MakeScopes(where, resultType any) func(db *gorm.DB) *gorm.DB { ...@@ -22,7 +22,7 @@ func MakeScopes(where, resultType any) func(db *gorm.DB) *gorm.DB {
func makeScopes(db *gorm.DB, where, resultType any) *gorm.DB { func makeScopes(db *gorm.DB, where, resultType any) *gorm.DB {
condition := &gormCondition{} condition := &gormCondition{}
resolveSearchQuery(where, condition, resultType) makeConditions(where, condition, resultType)
for k, v := range condition.Where { for k, v := range condition.Where {
db = db.Where(k, v...) db = db.Where(k, v...)
...@@ -81,7 +81,7 @@ type joinCondition struct { ...@@ -81,7 +81,7 @@ type joinCondition struct {
gormCondition gormCondition
} }
// resolveSearchQuery 解析 // makeConditions 解析
/** /**
* eq 等于(默认不填都可以) * eq 等于(默认不填都可以)
* like 包含 * like 包含
...@@ -93,13 +93,13 @@ type joinCondition struct { ...@@ -93,13 +93,13 @@ type joinCondition struct {
* isnull * isnull
* order 排序 e.g. order[key]=desc order[key]=asc * order 排序 e.g. order[key]=desc order[key]=asc
*/ */
func resolveSearchQuery(q any, condition Condition, resultType any) { func makeConditions(q any, condition Condition, resultType any) {
qType := reflect.TypeOf(q) qType := reflect.TypeOf(q)
qValue := reflect.ValueOf(q) qValue := reflect.ValueOf(q)
var tag string var tag string
var ok bool var ok bool
var t *resolveSearchTag var t *Tagger
var tname string var tname string
if qType.Kind() == reflect.Ptr { if qType.Kind() == reflect.Ptr {
...@@ -121,7 +121,7 @@ func resolveSearchQuery(q any, condition Condition, resultType any) { ...@@ -121,7 +121,7 @@ func resolveSearchQuery(q any, condition Condition, resultType any) {
if !ok { if !ok {
tag, ok = field.Tag.Lookup(Tag2) tag, ok = field.Tag.Lookup(Tag2)
if !ok { if !ok {
resolveSearchQuery(fieldValue.Interface(), condition, resultType) makeConditions(fieldValue.Interface(), condition, resultType)
continue continue
} }
} }
...@@ -151,12 +151,12 @@ func resolveSearchQuery(q any, condition Condition, resultType any) { ...@@ -151,12 +151,12 @@ func resolveSearchQuery(q any, condition Condition, resultType any) {
if t.Column == "" { if t.Column == "" {
if kind == reflect.Array || kind == reflect.Slice { if kind == reflect.Array || kind == reflect.Slice {
if strings.HasSuffix(fieldName, "s") { if strings.HasSuffix(fieldName, "s") {
t.Column = snakeCase(strings.TrimSuffix(fieldName, "s"), false) t.Column = CamelToSnake(strings.TrimSuffix(fieldName, "s"), false)
} else { } else {
t.Column = snakeCase(fieldName, false) t.Column = CamelToSnake(fieldName, false)
} }
} else { } else {
t.Column = snakeCase(fieldName, false) t.Column = CamelToSnake(fieldName, false)
} }
} }
...@@ -168,7 +168,7 @@ func resolveSearchQuery(q any, condition Condition, resultType any) { ...@@ -168,7 +168,7 @@ func resolveSearchQuery(q any, condition Condition, resultType any) {
} }
} }
func mysql(t *resolveSearchTag, condition Condition, qValue reflect.Value, i int, resultType any) { func mysql(t *Tagger, condition Condition, qValue reflect.Value, i int, resultType any) {
if t.Type == "" { if t.Type == "" {
condition.SetWhere(fmt.Sprintf("`%s`.`%s` = ?", t.Table, t.Column), []any{qValue.Field(i).Interface()}) condition.SetWhere(fmt.Sprintf("`%s`.`%s` = ?", t.Table, t.Column), []any{qValue.Field(i).Interface()})
return return
...@@ -222,7 +222,7 @@ func mysql(t *resolveSearchTag, condition Condition, qValue reflect.Value, i int ...@@ -222,7 +222,7 @@ func mysql(t *resolveSearchTag, condition Condition, qValue reflect.Value, i int
} }
} }
type resolveSearchTag struct { type Tagger struct {
Type string Type string
Column string Column string
Table string Table string
...@@ -230,9 +230,8 @@ type resolveSearchTag struct { ...@@ -230,9 +230,8 @@ type resolveSearchTag struct {
Join string Join string
} }
// parseTag 解析search的tag标签 func parseTag(tag string) *Tagger {
func parseTag(tag string) *resolveSearchTag { r := &Tagger{}
r := &resolveSearchTag{}
tags := strings.Split(tag, ";") tags := strings.Split(tag, ";")
var ts []string var ts []string
for _, t := range tags { for _, t := range tags {
...@@ -289,7 +288,7 @@ const ( ...@@ -289,7 +288,7 @@ const (
/** /**
* 驼峰转蛇形 snake string * 驼峰转蛇形 snake string
**/ **/
func snakeCase(s string, allMode bool) string { func CamelToSnake(s string, allMode bool) string {
num := len(s) num := len(s)
data := make([]byte, 0, num*2) data := make([]byte, 0, num*2)
for i := 0; i < num; i++ { for i := 0; i < num; i++ {
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论