u
This commit is contained in:
1 parent
c7a1f83c67
commit
6849579652
19 files changed
+600
-249
No files matched your search
@@ -1,17 +1,18 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"base-go-v2/internal/utils/mapx"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ---------------- 通用查询方法 ----------------
|
||||
|
||||
// GetOne 根据主键查询单条记录,返回 map[string]interface{}
|
||||
func GetOne(table string, pkColumn string, pkValue interface{}) (map[string]interface{}, error) {
|
||||
// GetOne 根据主键查询单条记录,返回 mapx.M
|
||||
func GetOne(table string, pkColumn string, pkValue interface{}) (mapx.M, error) {
|
||||
query := fmt.Sprintf("SELECT * FROM %s WHERE %s=$1", table, pkColumn)
|
||||
|
||||
row := make(map[string]interface{})
|
||||
row := mapx.New()
|
||||
err := DB.Get(&row, query, pkValue)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -19,13 +20,13 @@ func GetOne(table string, pkColumn string, pkValue interface{}) (map[string]inte
|
||||
return row, nil
|
||||
}
|
||||
|
||||
// GetBatch 根据主键批量查询,返回 []map[string]interface{}
|
||||
func GetBatch(table string, pkColumn string, pkValues []interface{}) ([]map[string]interface{}, error) {
|
||||
// GetBatch 根据主键批量查询,返回 []mapx.M
|
||||
func GetBatch(table string, pkColumn string, pkValues []interface{}) ([]mapx.M, error) {
|
||||
if len(pkValues) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var rows []map[string]interface{}
|
||||
var rows []mapx.M
|
||||
|
||||
// 构建 IN 占位符
|
||||
placeholders := make([]string, len(pkValues))
|
||||
@@ -42,10 +43,8 @@ func GetBatch(table string, pkColumn string, pkValues []interface{}) ([]map[stri
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// Find 根据条件查询多条记录,可选排序
|
||||
// conditions: map[column]value, 将以 AND 拼接
|
||||
// orderBy: 可选排序字段,如 "created_at ASC",为空默认 "b_id DESC"
|
||||
func Find(table string, conditions map[string]interface{}, orderBy ...string) ([]map[string]interface{}, error) {
|
||||
// Find 根据条件查询多条记录,可选排序,返回 []mapx.M
|
||||
func Find(table string, conditions map[string]interface{}, orderBy ...string) ([]mapx.M, error) {
|
||||
if len(conditions) == 0 {
|
||||
return nil, fmt.Errorf("查询条件不能为空")
|
||||
}
|
||||
@@ -67,7 +66,7 @@ func Find(table string, conditions map[string]interface{}, orderBy ...string) ([
|
||||
}
|
||||
query += " ORDER BY " + order
|
||||
|
||||
var rows []map[string]interface{}
|
||||
var rows []mapx.M
|
||||
err := DB.Select(&rows, query, values...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -76,8 +75,8 @@ func Find(table string, conditions map[string]interface{}, orderBy ...string) ([
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// FindAll 查询整个表,可选排序
|
||||
func FindAll(table string, orderBy ...string) ([]map[string]interface{}, error) {
|
||||
// FindAll 查询整个表,可选排序,返回 []mapx.M
|
||||
func FindAll(table string, orderBy ...string) ([]mapx.M, error) {
|
||||
query := fmt.Sprintf("SELECT * FROM %s", table)
|
||||
|
||||
order := "b_id DESC"
|
||||
@@ -86,7 +85,7 @@ func FindAll(table string, orderBy ...string) ([]map[string]interface{}, error)
|
||||
}
|
||||
query += " ORDER BY " + order
|
||||
|
||||
var rows []map[string]interface{}
|
||||
var rows []mapx.M
|
||||
err := DB.Select(&rows, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -94,3 +93,35 @@ func FindAll(table string, orderBy ...string) ([]map[string]interface{}, error)
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// FindOne 根据条件查询单条记录,返回 mapx.M
|
||||
func FindOne(table string, conditions map[string]interface{}, orderBy ...string) (mapx.M, error) {
|
||||
if len(conditions) == 0 {
|
||||
return nil, fmt.Errorf("查询条件不能为空")
|
||||
}
|
||||
|
||||
var whereParts []string
|
||||
var values []interface{}
|
||||
i := 1
|
||||
for k, v := range conditions {
|
||||
whereParts = append(whereParts, fmt.Sprintf("%s=$%d", k, i))
|
||||
values = append(values, v)
|
||||
i++
|
||||
}
|
||||
|
||||
query := fmt.Sprintf("SELECT * FROM %s WHERE %s", table, strings.Join(whereParts, " AND "))
|
||||
|
||||
order := "b_id DESC"
|
||||
if len(orderBy) > 0 && orderBy[0] != "" {
|
||||
order = orderBy[0]
|
||||
}
|
||||
query += " ORDER BY " + order + " LIMIT 1" // 只取一条记录
|
||||
|
||||
row := mapx.New()
|
||||
err := DB.Get(&row, query, values...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return row, nil
|
||||
}
|
||||
Reference in new issue
Block a user