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

update

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