Compare commits
2 Commits
entgo/v1.1
...
v1.1.1
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
e9ea8fa536 | ||
|
|
6ed25e948c |
@@ -21,14 +21,14 @@ func QueryCommandToOrderConditions(orderBys []string) (error, func(s *sql.Select
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
s.OrderBy(sql.Desc(s.C(key)))
|
BuildOrderSelect(s, key, true)
|
||||||
} else {
|
} else {
|
||||||
// 升序
|
// 升序
|
||||||
if len(v) == 0 {
|
if len(v) == 0 {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
s.OrderBy(sql.Asc(s.C(v)))
|
BuildOrderSelect(s, v, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -37,9 +37,17 @@ func QueryCommandToOrderConditions(orderBys []string) (error, func(s *sql.Select
|
|||||||
func BuildOrderSelector(orderBys []string, defaultOrderField string) (error, func(s *sql.Selector)) {
|
func BuildOrderSelector(orderBys []string, defaultOrderField string) (error, func(s *sql.Selector)) {
|
||||||
if len(orderBys) == 0 {
|
if len(orderBys) == 0 {
|
||||||
return nil, func(s *sql.Selector) {
|
return nil, func(s *sql.Selector) {
|
||||||
s.OrderBy(sql.Desc(s.C(defaultOrderField)))
|
BuildOrderSelect(s, defaultOrderField, true)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
return QueryCommandToOrderConditions(orderBys)
|
return QueryCommandToOrderConditions(orderBys)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildOrderSelect(s *sql.Selector, field string, desc bool) {
|
||||||
|
if desc {
|
||||||
|
s.OrderBy(sql.Desc(s.C(field)))
|
||||||
|
} else {
|
||||||
|
s.OrderBy(sql.Asc(s.C(field)))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,18 +9,14 @@ import (
|
|||||||
func BuildPaginationSelector(page, pageSize int32, noPaging bool) func(*sql.Selector) {
|
func BuildPaginationSelector(page, pageSize int32, noPaging bool) func(*sql.Selector) {
|
||||||
if noPaging {
|
if noPaging {
|
||||||
return nil
|
return nil
|
||||||
}
|
} else {
|
||||||
|
return func(s *sql.Selector) {
|
||||||
if page == 0 {
|
BuildPaginationSelect(s, page, pageSize)
|
||||||
page = DefaultPage
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if pageSize == 0 {
|
|
||||||
pageSize = DefaultPageSize
|
|
||||||
}
|
|
||||||
|
|
||||||
return func(s *sql.Selector) {
|
|
||||||
s.Offset(paging.GetPageOffset(page, pageSize)).
|
|
||||||
Limit(int(pageSize))
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func BuildPaginationSelect(s *sql.Selector, page, pageSize int32) {
|
||||||
|
offset := paging.GetPageOffset(page, pageSize)
|
||||||
|
s.Offset(offset).Limit(int(pageSize))
|
||||||
|
}
|
||||||
|
|||||||
@@ -5,11 +5,6 @@ import (
|
|||||||
_ "github.com/go-kratos/kratos/v2/encoding/json"
|
_ "github.com/go-kratos/kratos/v2/encoding/json"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
DefaultPage = 1
|
|
||||||
DefaultPageSize = 10
|
|
||||||
)
|
|
||||||
|
|
||||||
// BuildQuerySelector 构建分页查询选择器
|
// BuildQuerySelector 构建分页查询选择器
|
||||||
func BuildQuerySelector(
|
func BuildQuerySelector(
|
||||||
dbDriverName string,
|
dbDriverName string,
|
||||||
|
|||||||
82
io/path.go
Normal file
82
io/path.go
Normal file
@@ -0,0 +1,82 @@
|
|||||||
|
package common
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
)
|
||||||
|
|
||||||
|
func GetWorkingDirPath() string {
|
||||||
|
dir, err := os.Getwd()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetExePath() string {
|
||||||
|
ex, err := os.Executable()
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
exePath := filepath.Dir(ex)
|
||||||
|
return exePath
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetAbsPath() string {
|
||||||
|
dir, err := filepath.Abs(filepath.Dir(os.Args[0]))
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
return dir
|
||||||
|
}
|
||||||
|
|
||||||
|
// PathExist 路径是否存在
|
||||||
|
func PathExist(path string) bool {
|
||||||
|
_, err := os.Stat(path)
|
||||||
|
if err == nil {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
if os.IsNotExist(err) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFileList 获取文件夹下面的所有文件的列表
|
||||||
|
func GetFileList(root string) []string {
|
||||||
|
var files []string
|
||||||
|
|
||||||
|
if err := filepath.Walk(root, func(path string, info os.FileInfo, err error) error {
|
||||||
|
if info.IsDir() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
files = append(files, path)
|
||||||
|
return nil
|
||||||
|
}); err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
return files
|
||||||
|
}
|
||||||
|
|
||||||
|
// GetFolderNameList 获取当前文件夹下面的所有文件夹名的列表
|
||||||
|
func GetFolderNameList(root string) []string {
|
||||||
|
var names []string
|
||||||
|
fs, _ := os.ReadDir(root)
|
||||||
|
for _, file := range fs {
|
||||||
|
if file.IsDir() {
|
||||||
|
names = append(names, file.Name())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return names
|
||||||
|
}
|
||||||
|
|
||||||
|
// ReadFile 读取文件
|
||||||
|
func ReadFile(path string) []byte {
|
||||||
|
content, err := os.ReadFile(path)
|
||||||
|
if err != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return content
|
||||||
|
}
|
||||||
@@ -1,5 +1,19 @@
|
|||||||
package pagination
|
package pagination
|
||||||
|
|
||||||
|
const (
|
||||||
|
DefaultPage = 1 // 默认页数
|
||||||
|
DefaultPageSize = 10 // 默认每页行数
|
||||||
|
)
|
||||||
|
|
||||||
|
// GetPageOffset 计算偏移量
|
||||||
func GetPageOffset(pageNum, pageSize int32) int {
|
func GetPageOffset(pageNum, pageSize int32) int {
|
||||||
|
if pageNum < 1 {
|
||||||
|
pageNum = DefaultPage
|
||||||
|
}
|
||||||
|
|
||||||
|
if pageSize < 1 {
|
||||||
|
pageSize = DefaultPageSize
|
||||||
|
}
|
||||||
|
|
||||||
return int((pageNum - 1) * pageSize)
|
return int((pageNum - 1) * pageSize)
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user