From 45d5aa426b711ab4363c2763563b12c0d82cc02d Mon Sep 17 00:00:00 2001 From: lwnmengjing Date: Tue, 14 Nov 2023 23:51:22 +0800 Subject: [PATCH] :sparkles: support dm search (#61) --- pkg/config/gormdb/open.go | 2 +- pkg/search/gorms/query.go | 80 +++++++++++---------------------------- 2 files changed, 24 insertions(+), 58 deletions(-) diff --git a/pkg/config/gormdb/open.go b/pkg/config/gormdb/open.go index 1a16cb1..6dd3735 100644 --- a/pkg/config/gormdb/open.go +++ b/pkg/config/gormdb/open.go @@ -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" diff --git a/pkg/search/gorms/query.go b/pkg/search/gorms/query.go index 3ec7ecd..216605b 100644 --- a/pkg/search/gorms/query.go +++ b/pkg/search/gorms/query.go @@ -13,6 +13,8 @@ const ( Mysql = "mysql" // Postgres 数据库标识 Postgres = "postgres" + // Dm 数据库标识 + Dm = "dm" ) // ResolveSearchQuery 解析 @@ -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": //左关联 @@ -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": @@ -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":