Security News
Opengrep Emerges as Open Source Alternative Amid Semgrep Licensing Controversy
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
gitee.com/ostaer/gin-pagination
1.引入
import "gitee.com/ostaer/gin-pagination"
2.使用中间件
app := gin.New()
paginationHandler := pagination.New(&pagination.CustomeParser{})
app.Get("/test", paginationHandler)
3.gorm引用查询数据
测试请求如middleware_test.go
110-115行
GET /test3?page=1&&size=3&&order=desc&&order_by=id&&is_child=true&&child_relation=1&&search.1.key=age&&search.1.value=5&&search.1.exactly=true&&search.2.key=email&&search.2.value=user-1,user-2&&search.2.exactly=false
查询处理如middleware_test.go
211-214行
// SELECT count(*) FROM `users` WHERE ((`age` = "5") or ((`email` like "%user-1%") or (`email` like "%user-2%")))
DB.Model(&User{}).Scopes(pagination.PaginationScope(query, ip.Count)).Count(&count)
// SELECT * FROM `users` WHERE ((`age` = "5") or ((`email` like "%user-1%") or (`email` like "%user-2%"))) ORDER BY id desc LIMIT 3 OFFSET 0
DB.Model(&User{}).Scopes(pagination.PaginationScope(query)).Find(&users)
4.拼接query
var query = pagination.PaginationQuery{
PageNum: 1, PageSize: 10,
}
query.Search = append(query.Search, pagination.SearchGroup{
SearchTerm: pagination.SearchTerm{
Raw: &pagination.RawQuery{
Query: "user_name = ? or nick_name = ?",
Args: []interface{}{
"张三", "四",
},
},
},
})
自己组装gorm的Scope
var scope = func(db *gorm.DB) *gorm.DB {
d := &pagination.PaginationQueryDB{DB: db, Query: query}
d.ParseLimit().ParsePagination().ParseOrder()
return d.DB
}
a.conn.Model(&model.Account{}).Scopes(scope).Where("id in ?", ids).Find(&accounts)
更多例子 参考middleware_test.go文件
文件 | 说明 |
---|---|
model.go | 分页查询相关struct的定义, 如PaginationQuery |
customParser.go | 负责解析查询参数到PaginationQuery |
resolver.go | 负责解析PaginationQuery 到gorm.DB Scopes |
middleware.go | 实现兼容gin的中间件 |
FAQs
Unknown package
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Opengrep forks Semgrep to preserve open source SAST in response to controversial licensing changes.
Security News
Critics call the Node.js EOL CVE a misuse of the system, sparking debate over CVE standards and the growing noise in vulnerability databases.
Security News
cURL and Go security teams are publicly rejecting CVSS as flawed for assessing vulnerabilities and are calling for more accurate, context-aware approaches.