This commit is contained in:
oneao committed 2025-11-15 17:31:36 +08:00
1 parent 33113f279c
commit c2c5cc92af
16 files changed
+864 -352

No files matched your search

@@ -24,9 +24,11 @@ var defaultPoolConfig = struct {
func InitDb() error {
d := config.App.Db
// 构造 DSN
dsn := fmt.Sprintf("host=%s port=%d user=%s password=%s dbname=%s sslmode=disable",
d.Host, d.Port, d.User, d.Password, d.Dbname)
// 构造 DSN,指定时区为 Asia/Shanghai
dsn := fmt.Sprintf(
"host=%s port=%d user=%s password=%s dbname=%s sslmode=disable TimeZone=Asia/Shanghai",
d.Host, d.Port, d.User, d.Password, d.Dbname,
)
db, err := sqlx.Connect("postgres", dsn)
if err != nil {
@@ -8,7 +8,6 @@ import (
// ---------------- 通用查询方法 ----------------
// 执行查询,返回多条记录
func queryMaps(query string, args []interface{}) ([]mapx.M, error) {
rows, err := DB.Queryx(query, args...)
if err != nil {
@@ -22,13 +21,20 @@ func queryMaps(query string, args []interface{}) ([]mapx.M, error) {
if err := rows.MapScan(row); err != nil {
return nil, err
}
// 统一处理 []byte 类型,直接转字符串,保留原始小数点
for k, v := range row {
if b, ok := v.([]byte); ok {
row[k] = string(b)
}
}
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 {
@@ -41,93 +47,73 @@ func queryMap(query string, args []interface{}) (mapx.M, error) {
if err := rows.MapScan(row); err != nil {
return nil, err
}
// 统一处理 []byte 类型,直接转字符串
for k, v := range row {
if b, ok := v.([]byte); ok {
row[k] = string(b)
}
}
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++
// BuildWhereString 将 map 条件转换为 SQL WHERE 字符串
func BuildWhereString(conds mapx.M) string {
if len(conds) == 0 {
return ""
}
return strings.Join(whereParts, " AND "), values
var parts []string
for k, v := range conds {
parts = append(parts, fmt.Sprintf("%s='%v'", k, v))
}
return strings.Join(parts, " AND ")
}
// ---------------- 公共查询方法 ----------------
// ---------------- 统一 select 系列方法 ----------------
// GetOne 根据主键查询单条记录
func GetOne(table string, pkColumn string, pkValue interface{}) (mapx.M, error) {
query := fmt.Sprintf("SELECT * FROM %s WHERE %s=$1", table, pkColumn)
// SelectById 根据主键查询单条记录
func SelectById(tableName, pkColumn string, pkValue interface{}) (mapx.M, error) {
query := fmt.Sprintf("SELECT * FROM %s WHERE %s=$1", tableName, 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)
// SelectBySql 根据任意 SQL 查询多条记录
func SelectBySql(sql string, args ...interface{}) ([]mapx.M, error) {
return queryMaps(sql, args)
}
// Find 根据条件查询多条记录,可选排序
func Find(table string, conditions map[string]interface{}, orderBy ...string) ([]mapx.M, error) {
if len(conditions) == 0 {
return nil, fmt.Errorf("查询条件不能为空")
// SelectList 查询列表,可传 where 和 order,where 可以为空
func SelectList(tableName string, where string, order string) ([]mapx.M, error) {
query := fmt.Sprintf("SELECT * FROM %s", tableName)
if strings.TrimSpace(where) != "" {
query += " WHERE " + where
}
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]
if strings.TrimSpace(order) == "" {
order = "id DESC"
}
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("查询条件不能为空")
// SelectOne 查询单条记录,可传 where 和 order(order 可为空)
func SelectOne(tableName string, where string, order string) (mapx.M, error) {
query := fmt.Sprintf("SELECT * FROM %s", tableName)
if strings.TrimSpace(where) != "" {
query += " WHERE " + where
}
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]
if strings.TrimSpace(order) == "" {
order = "id DESC" // 默认排序
}
query += " ORDER BY " + order + " LIMIT 1"
return queryMap(query, values)
return queryMap(query, nil)
}