134 lines
3.2 KiB
Go
134 lines
3.2 KiB
Go
package db
|
|
|
|
import (
|
|
"base-go-v2/internal/utils/mapx"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// ---------------- 通用查询方法 ----------------
|
|
|
|
// 执行查询,返回多条记录
|
|
func queryMaps(query string, args []interface{}) ([]mapx.M, error) {
|
|
rows, err := DB.Queryx(query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
var result []mapx.M
|
|
for rows.Next() {
|
|
row := mapx.New()
|
|
if err := rows.MapScan(row); err != nil {
|
|
return nil, err
|
|
}
|
|
result = append(result, row)
|
|
}
|
|
|
|
return result, nil
|
|
}
|
|
|
|
// 执行查询,返回单条记录
|
|
func queryMap(query string, args []interface{}) (mapx.M, error) {
|
|
rows, err := DB.Queryx(query, args...)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
|
|
if rows.Next() {
|
|
row := mapx.New()
|
|
if err := rows.MapScan(row); err != nil {
|
|
return nil, err
|
|
}
|
|
return row, nil
|
|
}
|
|
|
|
return nil, nil
|
|
}
|
|
|
|
// 构建 WHERE 条件
|
|
func buildWhere(conditions map[string]interface{}) (string, []interface{}) {
|
|
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++
|
|
}
|
|
return strings.Join(whereParts, " AND "), values
|
|
}
|
|
|
|
// ---------------- 公共查询方法 ----------------
|
|
|
|
// GetOne 根据主键查询单条记录
|
|
func GetOne(table string, pkColumn string, pkValue interface{}) (mapx.M, error) {
|
|
query := fmt.Sprintf("SELECT * FROM %s WHERE %s=$1", table, pkColumn)
|
|
return queryMap(query, []interface{}{pkValue})
|
|
}
|
|
|
|
// GetBatch 根据主键批量查询
|
|
func GetBatch(table string, pkColumn string, pkValues []interface{}) ([]mapx.M, error) {
|
|
if len(pkValues) == 0 {
|
|
return nil, nil
|
|
}
|
|
|
|
placeholders := make([]string, len(pkValues))
|
|
for i := range pkValues {
|
|
placeholders[i] = fmt.Sprintf("$%d", i+1)
|
|
}
|
|
|
|
query := fmt.Sprintf("SELECT * FROM %s WHERE %s IN (%s)", table, pkColumn, strings.Join(placeholders, ","))
|
|
return queryMaps(query, pkValues)
|
|
}
|
|
|
|
// Find 根据条件查询多条记录,可选排序
|
|
func Find(table string, conditions map[string]interface{}, orderBy ...string) ([]mapx.M, error) {
|
|
if len(conditions) == 0 {
|
|
return nil, fmt.Errorf("查询条件不能为空")
|
|
}
|
|
|
|
where, values := buildWhere(conditions)
|
|
query := fmt.Sprintf("SELECT * FROM %s WHERE %s", table, where)
|
|
|
|
order := "id DESC"
|
|
if len(orderBy) > 0 && orderBy[0] != "" {
|
|
order = orderBy[0]
|
|
}
|
|
query += " ORDER BY " + order
|
|
|
|
return queryMaps(query, values)
|
|
}
|
|
|
|
// FindAll 查询整个表,可选排序
|
|
func FindAll(table string, orderBy ...string) ([]mapx.M, error) {
|
|
query := fmt.Sprintf("SELECT * FROM %s", table)
|
|
|
|
order := "id DESC"
|
|
if len(orderBy) > 0 && orderBy[0] != "" {
|
|
order = orderBy[0]
|
|
}
|
|
query += " ORDER BY " + order
|
|
|
|
return queryMaps(query, nil)
|
|
}
|
|
|
|
// FindOne 根据条件查询单条记录,可选排序
|
|
func FindOne(table string, conditions map[string]interface{}, orderBy ...string) (mapx.M, error) {
|
|
if len(conditions) == 0 {
|
|
return nil, fmt.Errorf("查询条件不能为空")
|
|
}
|
|
|
|
where, values := buildWhere(conditions)
|
|
query := fmt.Sprintf("SELECT * FROM %s WHERE %s", table, where)
|
|
|
|
order := "id DESC"
|
|
if len(orderBy) > 0 && orderBy[0] != "" {
|
|
order = orderBy[0]
|
|
}
|
|
query += " ORDER BY " + order + " LIMIT 1"
|
|
|
|
return queryMap(query, values)
|
|
}
|