Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
X
xpkg
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
屠思豪
xpkg
Commits
73eac9db
提交
73eac9db
authored
12月 09, 2025
作者:
mooncake
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
增加gormutils用于构建复杂where条件,适应多表联查场景
上级
dc0f9fb2
隐藏空白字符变更
内嵌
并排
正在显示
8 个修改的文件
包含
475 行增加
和
54 行删除
+475
-54
cmd.go
cmd/pagination/cmd.go
+1
-1
http_error.go
pkg/errcode/http_error.go
+4
-0
http_system_code.go
pkg/errcode/http_system_code.go
+4
-4
odao.go
xcommon/odao/odao.go
+0
-37
query.go
xcommon/odao/query.go
+13
-9
cron_job.go
xcron/service/cron_job.go
+2
-3
gormutils.go
xutils/gormutils/gormutils.go
+372
-0
gormutils_test.go
xutils/gormutils/gormutils_test.go
+79
-0
没有找到文件。
cmd/pagination/cmd.go
浏览文件 @
73eac9db
...
@@ -84,7 +84,7 @@ func main1() {
...
@@ -84,7 +84,7 @@ func main1() {
SiteID
:
1320976731124793344
,
SiteID
:
1320976731124793344
,
Name
:
"优盟店"
,
Name
:
"优盟店"
,
}
}
dsn
:=
"
qitu_test:By1rGembg6@tcp(rm-bp15831972934dz2c.mysql.rds.aliyuncs.com:3306)/qitu_athena_test
?charset=utf8mb4&parseTime=True&loc=Local"
dsn
:=
"
root:123456@tcp(localhost:30006)/qitu_athena
?charset=utf8mb4&parseTime=True&loc=Local"
// 方法2:全局DryRun配置
// 方法2:全局DryRun配置
dryRunDB
,
_
:=
gorm
.
Open
(
mysql
.
Open
(
dsn
),
&
gorm
.
Config
{
Logger
:
newLogger
})
dryRunDB
,
_
:=
gorm
.
Open
(
mysql
.
Open
(
dsn
),
&
gorm
.
Config
{
Logger
:
newLogger
})
db
=
dryRunDB
db
=
dryRunDB
...
...
pkg/errcode/http_error.go
浏览文件 @
73eac9db
...
@@ -88,6 +88,10 @@ func (e *Error) ToXerror() (int, string) {
...
@@ -88,6 +88,10 @@ func (e *Error) ToXerror() (int, string) {
return
e
.
code
,
e
.
msg
return
e
.
code
,
e
.
msg
}
}
func
(
e
*
Error
)
ToError
()
(
int
,
string
)
{
return
e
.
code
,
e
.
msg
}
// NeedHTTPCode need to convert to standard http code
// NeedHTTPCode need to convert to standard http code
func
(
e
*
Error
)
NeedHTTPCode
()
bool
{
func
(
e
*
Error
)
NeedHTTPCode
()
bool
{
return
e
.
needHTTPCode
return
e
.
needHTTPCode
...
...
pkg/errcode/http_system_code.go
浏览文件 @
73eac9db
...
@@ -32,8 +32,8 @@ var (
...
@@ -32,8 +32,8 @@ var (
TooEarly
=
NewError
(
100025
,
"Too Early"
)
TooEarly
=
NewError
(
100025
,
"Too Early"
)
// 已下错误不会返给前端
// 已下错误不会返给前端
DBError
=
NewError
(
1
6
0001
,
"DB Error"
)
DBError
=
NewError
(
1
1
0001
,
"DB Error"
)
CacheError
=
NewError
(
1
6
0002
,
"Cache Error"
)
CacheError
=
NewError
(
1
1
0002
,
"Cache Error"
)
NetError
=
NewError
(
1
6
0003
,
"Net Error"
)
NetError
=
NewError
(
1
1
0003
,
"Net Error"
)
FileError
=
NewError
(
1
6
0004
,
"File Error"
)
FileError
=
NewError
(
1
1
0004
,
"File Error"
)
)
)
xcommon/odao/odao.go
浏览文件 @
73eac9db
...
@@ -4,7 +4,6 @@ import (
...
@@ -4,7 +4,6 @@ import (
"context"
"context"
"database/sql"
"database/sql"
"fmt"
"fmt"
"strings"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"gitlab.wanzhuangkj.com/tush/xpkg/database"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/goredis"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/goredis"
...
@@ -317,42 +316,6 @@ func (x *ODao[T]) Page(ctx context.Context, where IPageParams) (rs []*T, total i
...
@@ -317,42 +316,6 @@ func (x *ODao[T]) Page(ctx context.Context, where IPageParams) (rs []*T, total i
return
rs
,
total
,
nil
return
rs
,
total
,
nil
}
}
func
(
x
*
ODao
[
T
])
JPage
(
ctx
context
.
Context
,
where
IPageParams
,
result
any
,
resultType
any
)
(
total
int64
,
err
error
)
{
condition
:=
x
.
DB
()
.
WithContext
(
ctx
)
.
Model
(
new
(
T
))
.
Scopes
(
x
.
MakeCondition
(
where
))
if
where
.
Unscoped
()
{
condition
=
condition
.
Unscoped
()
}
if
err
=
condition
.
Limit
(
-
1
)
.
Offset
(
-
1
)
.
Count
(
&
total
)
.
Error
;
err
!=
nil
{
return
total
,
xerror
.
NewDBError
(
err
.
Error
())
}
if
total
==
0
{
return
total
,
nil
}
fields
:=
GetGormTags
(
resultType
)
if
err
=
condition
.
Select
(
strings
.
Join
(
fields
,
","
))
.
Limit
(
where
.
GetLimit
())
.
Offset
(
where
.
GetOffset
())
.
Scan
(
result
)
.
Error
;
err
!=
nil
{
return
total
,
xerror
.
NewDBError
(
err
.
Error
())
}
return
total
,
nil
}
func
(
x
*
ODao
[
T
])
JList
(
ctx
context
.
Context
,
where
IListParams
,
result
any
,
resultType
any
)
(
total
int64
,
err
error
)
{
condition
:=
x
.
DB
()
.
WithContext
(
ctx
)
.
Model
(
new
(
T
))
.
Scopes
(
x
.
MakeCondition
(
where
))
if
where
.
Unscoped
()
{
condition
=
condition
.
Unscoped
()
}
if
err
=
condition
.
Limit
(
-
1
)
.
Offset
(
-
1
)
.
Count
(
&
total
)
.
Error
;
err
!=
nil
{
return
total
,
xerror
.
NewDBError
(
err
.
Error
())
}
if
total
==
0
{
return
total
,
nil
}
fields
:=
GetGormTags
(
resultType
)
if
err
=
condition
.
Select
(
strings
.
Join
(
fields
,
","
))
.
Scan
(
result
)
.
Error
;
err
!=
nil
{
return
total
,
xerror
.
NewDBError
(
err
.
Error
())
}
return
total
,
nil
}
func
(
x
*
ODao
[
T
])
GetPageByColumns
(
ctx
context
.
Context
,
params
*
query
.
Params
)
(
rs
[]
*
T
,
total
int64
,
err
error
)
{
func
(
x
*
ODao
[
T
])
GetPageByColumns
(
ctx
context
.
Context
,
params
*
query
.
Params
)
(
rs
[]
*
T
,
total
int64
,
err
error
)
{
queryStr
,
args
,
err
:=
params
.
ConvertToGormConditions
()
queryStr
,
args
,
err
:=
params
.
ConvertToGormConditions
()
if
err
!=
nil
{
if
err
!=
nil
{
...
...
xcommon/odao/query.go
浏览文件 @
73eac9db
...
@@ -40,7 +40,8 @@ type Query interface {
...
@@ -40,7 +40,8 @@ type Query interface {
const
(
const
(
// FromQueryTag tag标记
// FromQueryTag tag标记
FromQueryTag
=
"query"
FromQueryTag
=
"query"
FromQueryTag2
=
"gq"
// Mysql 数据库标识
// Mysql 数据库标识
Mysql
=
"mysql"
Mysql
=
"mysql"
// Postgres 数据库标识
// Postgres 数据库标识
...
@@ -89,15 +90,21 @@ func ResolveSearchQuery(driver string, q any, condition Condition, pTName string
...
@@ -89,15 +90,21 @@ func ResolveSearchQuery(driver string, q any, condition Condition, pTName string
fieldName
:=
field
.
Name
fieldName
:=
field
.
Name
fieldValue
:=
qValue
.
Field
(
i
)
fieldValue
:=
qValue
.
Field
(
i
)
kind
:=
field
.
Type
.
Kind
()
kind
:=
field
.
Type
.
Kind
()
tag
,
ok
=
field
.
Tag
.
Lookup
(
FromQueryTag
)
tag
,
ok
=
field
.
Tag
.
Lookup
(
FromQueryTag2
)
if
!
ok
{
if
!
ok
{
//递归调用
tag
,
ok
=
field
.
Tag
.
Lookup
(
FromQueryTag
)
ResolveSearchQuery
(
driver
,
fieldValue
.
Interface
(),
condition
,
tname
)
if
!
ok
{
continue
//递归调用
ResolveSearchQuery
(
driver
,
fieldValue
.
Interface
(),
condition
,
tname
)
continue
}
}
}
if
tag
==
"-"
{
if
tag
==
"-"
{
continue
continue
}
}
t
=
parseTag
(
tag
)
t
=
parseTag
(
tag
)
if
t
.
Join
==
""
{
if
t
.
Join
==
""
{
if
!
fieldValue
.
IsValid
()
{
if
!
fieldValue
.
IsValid
()
{
...
@@ -224,10 +231,7 @@ func pgSql(driver string, t *resolveSearchTag, condition Condition, qValue refle
...
@@ -224,10 +231,7 @@ func pgSql(driver string, t *resolveSearchTag, condition Condition, qValue refle
// join := condition.SetJoinOn(t.Type, fmt.Sprintf(
// join := condition.SetJoinOn(t.Type, fmt.Sprintf(
// "left join %s on `%s`.`%s` = `%s`.`%s`", t.Join, t.Join, t.On[0], t.Table, t.On[1],
// "left join %s on `%s`.`%s` = `%s`.`%s`", t.Join, t.Join, t.On[0], t.Table, t.On[1],
// ))
// ))
join
:=
condition
.
SetJoinOn
(
t
.
Type
,
fmt
.
Sprintf
(
join
:=
condition
.
SetJoinOn
(
t
.
Type
,
fmt
.
Sprintf
(
"%s"
,
t
.
Join
))
"%s"
,
t
.
Join
,
))
ResolveSearchQuery
(
driver
,
qValue
.
Field
(
i
)
.
Interface
(),
join
,
tname
)
ResolveSearchQuery
(
driver
,
qValue
.
Field
(
i
)
.
Interface
(),
join
,
tname
)
return
return
default
:
default
:
...
...
xcron/service/cron_job.go
浏览文件 @
73eac9db
...
@@ -2,7 +2,6 @@ package service
...
@@ -2,7 +2,6 @@ package service
import
(
import
(
"context"
"context"
"errors"
"fmt"
"fmt"
"gitlab.wanzhuangkj.com/tush/xpkg/xcommon"
"gitlab.wanzhuangkj.com/tush/xpkg/xcommon"
...
@@ -61,7 +60,7 @@ func (x *cronJobService) DeleteByID(ctx context.Context, id xsf.ID) error {
...
@@ -61,7 +60,7 @@ func (x *cronJobService) DeleteByID(ctx context.Context, id xsf.ID) error {
return
err
return
err
}
}
if
cronJob
==
nil
{
if
cronJob
==
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"未查询到%s[id:%d]"
,
models
.
CronJobTool
.
TableName
(),
id
)
)
return
fmt
.
Errorf
(
"未查询到%s[id:%d]"
,
models
.
CronJobTool
.
TableName
(),
id
)
}
}
return
dao
.
CronJobDao
.
DeleteByID
(
ctx
,
id
)
return
dao
.
CronJobDao
.
DeleteByID
(
ctx
,
id
)
}
}
...
@@ -106,7 +105,7 @@ func (x *cronJobService) UpdateByID(ctx context.Context, req *types.CronJobUpdat
...
@@ -106,7 +105,7 @@ func (x *cronJobService) UpdateByID(ctx context.Context, req *types.CronJobUpdat
return
err
return
err
}
}
if
cronJob
==
nil
{
if
cronJob
==
nil
{
return
errors
.
New
(
fmt
.
Sprintf
(
"未查询到%s[id:%d]"
,
models
.
CronJobTool
.
TableName
(),
req
.
ID
)
)
return
fmt
.
Errorf
(
"未查询到%s[id:%d]"
,
models
.
CronJobTool
.
TableName
(),
req
.
ID
)
}
}
cronJobUpd
:=
&
models
.
CronJob
{}
cronJobUpd
:=
&
models
.
CronJob
{}
_
=
copier
.
Copy
(
cronJobUpd
,
req
)
_
=
copier
.
Copy
(
cronJobUpd
,
req
)
...
...
xutils/gormutils/gormutils.go
0 → 100644
浏览文件 @
73eac9db
package
gormutils
import
(
"fmt"
"reflect"
"regexp"
"strings"
"gorm.io/gorm"
)
const
(
Tag1
=
"gq"
Tag2
=
"query"
)
func
Scopes
(
where
any
)
func
(
db
*
gorm
.
DB
)
*
gorm
.
DB
{
return
func
(
db
*
gorm
.
DB
)
*
gorm
.
DB
{
return
makeScopes
(
db
,
where
)
}
}
func
makeScopes
(
db
*
gorm
.
DB
,
where
any
)
*
gorm
.
DB
{
condition
:=
&
GormCondition
{}
ResolveSearchQuery
(
where
,
condition
)
for
k
,
v
:=
range
condition
.
Where
{
db
=
db
.
Where
(
k
,
v
...
)
}
for
k
,
v
:=
range
condition
.
Or
{
db
=
db
.
Or
(
k
,
v
...
)
}
for
_
,
o
:=
range
condition
.
Order
{
db
=
db
.
Order
(
o
)
}
return
db
}
type
GormCondition
struct
{
Where
map
[
string
][]
any
Order
[]
string
Or
map
[
string
][]
any
}
type
Condition
interface
{
SetWhere
(
k
string
,
v
[]
any
)
SetOr
(
k
string
,
v
[]
any
)
SetOrder
(
k
string
)
SetJoinOn
(
t
,
on
string
)
Condition
}
func
(
e
*
GormCondition
)
SetJoinOn
(
t
,
on
string
)
Condition
{
return
nil
}
func
(
e
*
GormCondition
)
SetWhere
(
k
string
,
v
[]
any
)
{
if
e
.
Where
==
nil
{
e
.
Where
=
make
(
map
[
string
][]
any
)
}
e
.
Where
[
k
]
=
v
}
func
(
e
*
GormCondition
)
SetOr
(
k
string
,
v
[]
any
)
{
if
e
.
Or
==
nil
{
e
.
Or
=
make
(
map
[
string
][]
any
)
}
e
.
Or
[
k
]
=
v
}
func
(
e
*
GormCondition
)
SetOrder
(
k
string
)
{
if
e
.
Order
==
nil
{
e
.
Order
=
make
([]
string
,
0
)
}
e
.
Order
=
append
(
e
.
Order
,
k
)
}
type
JoinCondition
struct
{
Type
string
JoinOn
string
GormCondition
}
// ResolveSearchQuery 解析
/**
* eq 等于(默认不填都可以)
* like 包含
* gt / gte 大于 / 大于等于
* lt / lte 小于 / 小于等于
* left / ileft :like xxx%
* right / iright : like %xxx
* in
* isnull
* order 排序 e.g. order[key]=desc order[key]=asc
*/
func
ResolveSearchQuery
(
q
any
,
condition
Condition
)
{
qType
:=
reflect
.
TypeOf
(
q
)
qValue
:=
reflect
.
ValueOf
(
q
)
var
tag
string
var
ok
bool
var
t
*
resolveSearchTag
var
tname
string
if
qType
.
Kind
()
==
reflect
.
Ptr
{
qType
=
qType
.
Elem
()
qValue
=
qValue
.
Elem
()
}
if
qType
.
Kind
()
!=
reflect
.
Struct
{
return
}
for
i
:=
0
;
i
<
qType
.
NumField
();
i
++
{
field
:=
qType
.
Field
(
i
)
fieldName
:=
field
.
Name
fieldValue
:=
qValue
.
Field
(
i
)
kind
:=
field
.
Type
.
Kind
()
tag
,
ok
=
field
.
Tag
.
Lookup
(
Tag1
)
if
!
ok
{
tag
,
ok
=
field
.
Tag
.
Lookup
(
Tag2
)
if
!
ok
{
ResolveSearchQuery
(
fieldValue
.
Interface
(),
condition
)
continue
}
}
if
tag
==
"-"
{
continue
}
t
=
parseTag
(
tag
)
if
t
.
Join
==
""
{
if
!
fieldValue
.
IsValid
()
{
continue
}
switch
fieldValue
.
Kind
()
{
case
reflect
.
Ptr
,
reflect
.
Slice
,
reflect
.
Map
,
reflect
.
Chan
,
reflect
.
Interface
:
if
fieldValue
.
IsNil
()
{
continue
}
default
:
if
fieldValue
.
IsZero
()
{
continue
}
}
}
if
t
.
Column
==
""
{
if
kind
==
reflect
.
Array
||
kind
==
reflect
.
Slice
{
if
strings
.
HasSuffix
(
fieldName
,
"s"
)
{
t
.
Column
=
snakeCase
(
strings
.
TrimSuffix
(
fieldName
,
"s"
),
false
)
}
else
{
t
.
Column
=
snakeCase
(
fieldName
,
false
)
}
}
else
{
t
.
Column
=
snakeCase
(
fieldName
,
false
)
}
}
if
t
.
Table
==
""
{
t
.
Table
=
tname
}
//解析 Postgres `语法不支持,单独适配
otherSql
(
t
,
condition
,
qValue
,
i
)
}
}
func
otherSql
(
t
*
resolveSearchTag
,
condition
Condition
,
qValue
reflect
.
Value
,
i
int
)
{
if
t
.
Type
==
""
{
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` = ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
qValue
.
Field
(
i
)
.
Interface
()})
return
}
qtag
:=
QueryTag
(
t
.
Type
)
switch
qtag
{
case
EQ
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` = ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
qValue
.
Field
(
i
)
.
Interface
()})
return
case
GT
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` > ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
qValue
.
Field
(
i
)
.
Interface
()})
return
case
GTE
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` >= ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
qValue
.
Field
(
i
)
.
Interface
()})
return
case
LT
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` < ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
qValue
.
Field
(
i
)
.
Interface
()})
return
case
LTE
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` <= ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
qValue
.
Field
(
i
)
.
Interface
()})
return
case
LEFT
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` like ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
qValue
.
Field
(
i
)
.
String
()
+
"%"
})
return
case
LIKE
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` like ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
"%"
+
qValue
.
Field
(
i
)
.
String
()
+
"%"
})
return
case
RIGHT
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` like ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
"%"
+
qValue
.
Field
(
i
)
.
String
()})
return
case
IN
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` in (?)"
,
t
.
Table
,
t
.
Column
),
[]
any
{
qValue
.
Field
(
i
)
.
Interface
()})
return
case
ISNULL
:
if
!
(
qValue
.
Field
(
i
)
.
IsZero
()
&&
qValue
.
Field
(
i
)
.
IsNil
())
{
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` is null"
,
t
.
Table
,
t
.
Column
),
make
([]
any
,
0
))
}
return
case
ISNOTNULL
:
if
!
(
qValue
.
Field
(
i
)
.
IsZero
()
&&
qValue
.
Field
(
i
)
.
IsNil
())
{
condition
.
SetWhere
(
fmt
.
Sprintf
(
"%s.%s is not null"
,
t
.
Table
,
t
.
Column
),
make
([]
any
,
0
))
}
return
case
ORDER
:
val
:=
strings
.
TrimSpace
(
qValue
.
Field
(
i
)
.
String
())
if
val
!=
""
{
order
,
success
:=
parseOrder
(
val
)
if
success
{
if
!
strings
.
Contains
(
order
,
","
)
&&
!
strings
.
Contains
(
order
,
"."
)
{
condition
.
SetOrder
(
fmt
.
Sprintf
(
"%s.%s"
,
t
.
Table
,
order
))
}
else
{
condition
.
SetOrder
(
order
)
}
}
else
{
switch
strings
.
ToLower
(
qValue
.
Field
(
i
)
.
String
())
{
case
"desc"
,
"asc"
:
condition
.
SetOrder
(
fmt
.
Sprintf
(
"`%s`.`%s` %s"
,
t
.
Table
,
t
.
Column
,
qValue
.
Field
(
i
)
.
String
()))
}
}
}
else
{
switch
strings
.
ToLower
(
qValue
.
Field
(
i
)
.
String
())
{
case
"desc"
,
"asc"
:
condition
.
SetOrder
(
fmt
.
Sprintf
(
"`%s`.`%s` %s"
,
t
.
Table
,
t
.
Column
,
qValue
.
Field
(
i
)
.
String
()))
}
}
return
default
:
condition
.
SetWhere
(
fmt
.
Sprintf
(
"`%s`.`%s` = ?"
,
t
.
Table
,
t
.
Column
),
[]
any
{
qValue
.
Field
(
i
)
.
Interface
()})
}
}
type
resolveSearchTag
struct
{
Type
string
Column
string
Table
string
On
string
Join
string
}
// parseTag 解析search的tag标签
func
parseTag
(
tag
string
)
*
resolveSearchTag
{
r
:=
&
resolveSearchTag
{}
tags
:=
strings
.
Split
(
tag
,
";"
)
var
ts
[]
string
for
_
,
t
:=
range
tags
{
ts
=
strings
.
Split
(
t
,
":"
)
if
len
(
ts
)
==
0
{
continue
}
switch
ts
[
0
]
{
case
"type"
:
if
len
(
ts
)
>
1
{
r
.
Type
=
ts
[
1
]
}
case
"column"
:
if
len
(
ts
)
>
1
{
r
.
Column
=
ts
[
1
]
}
case
"table"
:
if
len
(
ts
)
>
1
{
r
.
Table
=
ts
[
1
]
}
case
"on"
:
if
len
(
ts
)
>
1
{
r
.
On
=
ts
[
1
]
}
case
"join"
:
if
len
(
ts
)
>
1
{
r
.
Join
=
ts
[
1
]
}
}
}
return
r
}
type
QueryTag
string
const
(
EQ
QueryTag
=
"eq"
LIKE
QueryTag
=
"like"
ILIKE
QueryTag
=
"ilike"
LEFT
QueryTag
=
"left"
ILEFT
QueryTag
=
"ileft"
RIGHT
QueryTag
=
"right"
IRIGHT
QueryTag
=
"iright"
GT
QueryTag
=
"gt"
GTE
QueryTag
=
"gte"
LT
QueryTag
=
"lt"
LTE
QueryTag
=
"lte"
IN
QueryTag
=
"in"
ISNULL
QueryTag
=
"isnull"
ISNOTNULL
QueryTag
=
"isnotnull"
ORDER
QueryTag
=
"order"
)
/**
* 驼峰转蛇形 snake string
**/
func
snakeCase
(
s
string
,
allMode
bool
)
string
{
num
:=
len
(
s
)
data
:=
make
([]
byte
,
0
,
num
*
2
)
for
i
:=
0
;
i
<
num
;
i
++
{
d
:=
s
[
i
]
// or通过ASCII码进行大小写的转化
// 65-90(A-Z),97-122(a-z)
//判断如果字母为大写的A-Z就在前面拼接一个_
if
d
>=
'A'
&&
d
<=
'Z'
{
if
i
>
0
{
if
allMode
{
data
=
append
(
data
,
'_'
,
d
+
32
)
}
else
{
if
s
[
i
-
1
]
>=
'A'
&&
s
[
i
-
1
]
<=
'Z'
{
data
=
append
(
data
,
d
+
32
)
}
else
{
data
=
append
(
data
,
'_'
,
d
+
32
)
}
}
}
else
{
data
=
append
(
data
,
d
+
32
)
}
}
else
{
data
=
append
(
data
,
d
)
}
}
//ToLower把大写字母统一转小写
return
string
(
data
[
:
])
}
func
parseOrder
(
order
string
)
(
string
,
bool
)
{
if
detectSQLInjection
(
order
)
{
return
""
,
false
}
columns
:=
strings
.
Split
(
order
,
","
)
orderColumns
:=
make
([]
string
,
0
,
len
(
columns
))
for
_
,
column
:=
range
columns
{
column
=
strings
.
TrimSpace
(
column
)
if
len
(
column
)
==
0
{
continue
}
order
:=
"asc"
if
column
[
:
1
]
==
"-"
{
order
=
"desc"
column
=
column
[
1
:
]
}
orderColumns
=
append
(
orderColumns
,
column
+
" "
+
order
)
}
if
len
(
orderColumns
)
==
0
{
return
""
,
false
}
return
strings
.
Join
(
orderColumns
,
","
),
true
}
func
detectSQLInjection
(
input
string
)
bool
{
return
detectSQLInjectionRe
.
MatchString
(
input
)
}
var
(
orderReg
=
regexp
.
MustCompile
(
`order\[([^\]]+)\]=([^=]+)`
)
detectSQLInjectionRe
=
regexp
.
MustCompile
(
`['";]+|UNION|SELECT|INSERT|UPDATE|DELETE|DROP|GRANT|EXEC|CREATE|ALTER|TRUNCATE|COUNT|\*|--|\/\*|;|\+|\/`
)
)
xutils/gormutils/gormutils_test.go
0 → 100644
浏览文件 @
73eac9db
package
gormutils_test
import
(
"context"
"fmt"
"log"
"os"
"testing"
"time"
"gitlab.wanzhuangkj.com/tush/xpkg/pkg/sgorm/query"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/gormutils"
"gitlab.wanzhuangkj.com/tush/xpkg/xutils/xsf"
"gorm.io/driver/mysql"
"gorm.io/gorm"
"gorm.io/gorm/logger"
)
func
TestMakeWhere
(
t
*
testing
.
T
)
{
// in := PageReq{}
in
:=
PageReq2
{}
in
.
PageIndex
=
1
in
.
PageSize
=
50
in
.
IDs
=
[]
xsf
.
ID
{
10
,
2
,
3
}
in
.
UserID
=
10
in
.
SiteName
=
"优盟"
scopes
:=
gormutils
.
Scopes
(
in
)
conn
:=
getConn
()
var
rs
[]
*
User
if
e
:=
conn
.
WithContext
(
context
.
TODO
())
.
Table
(
"operator as op"
)
.
Select
(
"op.id as id ,su.site_id as site_id,s.name as site_name"
)
.
Joins
(
" left join site_user su on su.user_type = 2 and su.user_id = op.id"
)
.
Joins
(
" left join site s on su.site_id = s.id"
)
.
Scopes
(
scopes
)
.
Limit
(
in
.
GetLimit
())
.
Offset
(
in
.
GetOffset
())
.
Find
(
&
rs
)
.
Error
;
e
!=
nil
{
fmt
.
Println
(
e
.
Error
())
}
for
_
,
r
:=
range
rs
{
fmt
.
Println
(
r
)
}
}
func
getConn
()
*
gorm
.
DB
{
newLogger
:=
logger
.
New
(
log
.
New
(
os
.
Stdout
,
"
\r\n
"
,
log
.
LstdFlags
),
logger
.
Config
{
SlowThreshold
:
time
.
Second
,
LogLevel
:
logger
.
Info
,
// 设置为Info级别打印所有SQL
Colorful
:
true
,
},
)
dsn
:=
"root:123456@tcp(localhost:30006)/qitu_operator?charset=utf8mb4&parseTime=True&loc=Local"
// 方法2:全局DryRun配置
con
,
_
:=
gorm
.
Open
(
mysql
.
Open
(
dsn
),
&
gorm
.
Config
{
Logger
:
newLogger
})
return
con
}
type
PageReq
struct
{
query
.
Pagination
IDs
[]
xsf
.
ID
`json:"ids" gq:"type:in;table:op;column:id" example:"10"`
UserID
xsf
.
ID
`json:"userID" gq:"table:su;column:user_id" example:"10"`
SiteName
string
`json:"siteName" gq:"type:left;table:s;column:name" example:"优盟"`
}
type
PageReq2
struct
{
query
.
Pagination
IDs
[]
xsf
.
ID
`json:"ids" query:"type:in;table:op;column:id" example:"10"`
UserID
xsf
.
ID
`json:"userID" query:"table:su;column:user_id" example:"10"`
SiteName
string
`json:"siteName" query:"type:left;table:s;column:name" example:"优盟"`
}
type
User
struct
{
ID
xsf
.
ID
`json:"id" gorm:"id"`
SiteID
xsf
.
ID
`json:"siteID" gorm:"site_id"`
SiteName
string
`json:"siteName" gorm:"site_name"`
}
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论