Skip to content

Commit

Permalink
✨ support dm search (#61)
Browse files Browse the repository at this point in the history
  • Loading branch information
lwnmengjing authored Nov 14, 2023
1 parent 436e79e commit 45d5aa4
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 58 deletions.
2 changes: 1 addition & 1 deletion pkg/config/gormdb/open.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package gormdb

import (
"github.com/nfjBill/gorm-driver-dm"
dm "github.com/nfjBill/gorm-driver-dm"
"gorm.io/driver/mysql"
"gorm.io/driver/postgres"
"gorm.io/gorm"
Expand Down
80 changes: 23 additions & 57 deletions pkg/search/gorms/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ const (
Mysql = "mysql"
// Postgres 数据库标识
Postgres = "postgres"
// Dm 数据库标识
Dm = "dm"
)

// ResolveSearchQuery 解析
Expand Down Expand Up @@ -58,62 +60,20 @@ func ResolveSearchQuery(driver string, q interface{}, condition Condition) {
if qValue.Field(i).IsZero() {
continue
}
//解析 Postgres `语法不支持,单独适配
if driver == Postgres {
pgSQL(driver, t, condition, qValue, i)
} else {
otherSQL(driver, t, condition, qValue, i)
}
}
}

func pgSQL(driver string, searchTag *resolveSearchTag, condition Condition, qValue reflect.Value, i int) {
switch searchTag.Type {
case "left":
//左关联
join := condition.SetJoinOn(searchTag.Type, fmt.Sprintf(
"left join %s on %s.%s = %s.%s", searchTag.Join, searchTag.Join, searchTag.On[0], searchTag.Table, searchTag.On[1],
))
ResolveSearchQuery(driver, qValue.Field(i).Interface(), join)
case "exact", "iexact":
condition.SetWhere(fmt.Sprintf("%s.%s = ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "glt":
condition.SetWhere(fmt.Sprintf("%s.%s <> ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "icontains":
condition.SetWhere(fmt.Sprintf("%s.%s ilike ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String() + "%"})
case "contains":
condition.SetWhere(fmt.Sprintf("%s.%s like ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String() + "%"})
case "gt":
condition.SetWhere(fmt.Sprintf("%s.%s > ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "gte":
condition.SetWhere(fmt.Sprintf("%s.%s >= ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "lt":
condition.SetWhere(fmt.Sprintf("%s.%s < ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "lte":
condition.SetWhere(fmt.Sprintf("%s.%s <= ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "istartswith":
condition.SetWhere(fmt.Sprintf("%s.%s ilike ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).String() + "%"})
case "startswith":
condition.SetWhere(fmt.Sprintf("%s.%s like ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).String() + "%"})
case "iendswith":
condition.SetWhere(fmt.Sprintf("%s.%s ilike ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String()})
case "endswith":
condition.SetWhere(fmt.Sprintf("%s.%s like ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String()})
case "in":
condition.SetWhere(fmt.Sprintf("%s.%s in (?)", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "isnull":
if !(qValue.Field(i).IsZero() && qValue.Field(i).IsNil()) {
condition.SetWhere(fmt.Sprintf("%s.%s isnull", searchTag.Table, searchTag.Column), make([]interface{}, 0))
}
case "order":
switch strings.ToLower(qValue.Field(i).String()) {
case "desc", "asc":
condition.SetOrder(fmt.Sprintf("%s.%s %s", searchTag.Table, searchTag.Column, qValue.Field(i).String()))
}
parseSQL(driver, t, condition, qValue, i)
}
}

func otherSQL(driver string, searchTag *resolveSearchTag, condition Condition, qValue reflect.Value, i int) {
func parseSQL(driver string, searchTag *resolveSearchTag, condition Condition, qValue reflect.Value, i int) {
if driver == Dm {
searchTag.Table = strings.ToUpper(searchTag.Table)
searchTag.Column = strings.ToUpper(searchTag.Column)
}
iStr := ""
if driver == Postgres {
iStr = "i"
}
switch searchTag.Type {
case "left":
//左关联
Expand All @@ -128,8 +88,10 @@ func otherSQL(driver string, searchTag *resolveSearchTag, condition Condition, q
ResolveSearchQuery(driver, qValue.Field(i).Interface(), join)
case "exact", "iexact":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` = ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "contains", "icontains":
case "contains":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` like ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String() + "%"})
case "icontains":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` %slike ?", searchTag.Table, searchTag.Column, iStr), []interface{}{"%" + qValue.Field(i).String() + "%"})
case "gt":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` > ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "gte":
Expand All @@ -138,10 +100,14 @@ func otherSQL(driver string, searchTag *resolveSearchTag, condition Condition, q
condition.SetWhere(fmt.Sprintf("`%s`.`%s` < ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "lte":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` <= ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "startswith", "istartswith":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` like ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).String() + "%"})
case "endswith", "iendswith":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` like ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String()})
case "startswith":
condition.SetWhere(fmt.Sprintf("%s.%s like ?", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).String() + "%"})
case "istartswith":
condition.SetWhere(fmt.Sprintf("%s.%s %slike ?", searchTag.Table, searchTag.Column, iStr), []interface{}{qValue.Field(i).String() + "%"})
case "endswith":
condition.SetWhere(fmt.Sprintf("%s.%s like ?", searchTag.Table, searchTag.Column), []interface{}{"%" + qValue.Field(i).String()})
case "iendswith":
condition.SetWhere(fmt.Sprintf("%s.%s %slike ?", searchTag.Table, searchTag.Column, iStr), []interface{}{"%" + qValue.Field(i).String()})
case "in":
condition.SetWhere(fmt.Sprintf("`%s`.`%s` in (?)", searchTag.Table, searchTag.Column), []interface{}{qValue.Field(i).Interface()})
case "isnull":
Expand Down

0 comments on commit 45d5aa4

Please sign in to comment.