u
This commit is contained in:
1 parent
be8eaae7c5
commit
d6dd5b032d
11 files changed
+202
-112
No files matched your search
@@ -8,47 +8,47 @@ import (
|
||||
|
||||
// ---------------- 通用查询方法 ----------------
|
||||
|
||||
// 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 := mapx.New()
|
||||
err := DB.Get(&row, query, pkValue)
|
||||
// 执行查询,返回多条记录
|
||||
func queryMaps(query string, args []interface{}) ([]mapx.M, error) {
|
||||
rows, err := DB.Queryx(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return row, nil
|
||||
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
|
||||
}
|
||||
|
||||
// GetBatch 根据主键批量查询,返回 []mapx.M
|
||||
func GetBatch(table string, pkColumn string, pkValues []interface{}) ([]mapx.M, error) {
|
||||
if len(pkValues) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
var rows []mapx.M
|
||||
|
||||
// 构建 IN 占位符
|
||||
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, ","))
|
||||
err := DB.Select(&rows, query, pkValues...)
|
||||
// 执行查询,返回单条记录
|
||||
func queryMap(query string, args []interface{}) (mapx.M, error) {
|
||||
rows, err := DB.Queryx(query, args...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
return rows, nil
|
||||
}
|
||||
|
||||
// Find 根据条件查询多条记录,可选排序,返回 []mapx.M
|
||||
func Find(table string, conditions map[string]interface{}, orderBy ...string) ([]mapx.M, error) {
|
||||
if len(conditions) == 0 {
|
||||
return nil, fmt.Errorf("查询条件不能为空")
|
||||
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
|
||||
@@ -57,8 +57,40 @@ func Find(table string, conditions map[string]interface{}, orderBy ...string) ([
|
||||
values = append(values, v)
|
||||
i++
|
||||
}
|
||||
return strings.Join(whereParts, " AND "), values
|
||||
}
|
||||
|
||||
query := fmt.Sprintf("SELECT * FROM %s WHERE %s", table, strings.Join(whereParts, " AND "))
|
||||
// ---------------- 公共查询方法 ----------------
|
||||
|
||||
// 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] != "" {
|
||||
@@ -66,16 +98,10 @@ func Find(table string, conditions map[string]interface{}, orderBy ...string) ([
|
||||
}
|
||||
query += " ORDER BY " + order
|
||||
|
||||
var rows []mapx.M
|
||||
err := DB.Select(&rows, query, values...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
return queryMaps(query, values)
|
||||
}
|
||||
|
||||
// FindAll 查询整个表,可选排序,返回 []mapx.M
|
||||
// FindAll 查询整个表,可选排序
|
||||
func FindAll(table string, orderBy ...string) ([]mapx.M, error) {
|
||||
query := fmt.Sprintf("SELECT * FROM %s", table)
|
||||
|
||||
@@ -85,31 +111,17 @@ func FindAll(table string, orderBy ...string) ([]mapx.M, error) {
|
||||
}
|
||||
query += " ORDER BY " + order
|
||||
|
||||
var rows []mapx.M
|
||||
err := DB.Select(&rows, query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return rows, nil
|
||||
return queryMaps(query, nil)
|
||||
}
|
||||
|
||||
// FindOne 根据条件查询单条记录,返回 mapx.M
|
||||
// FindOne 根据条件查询单条记录,可选排序
|
||||
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 "))
|
||||
where, values := buildWhere(conditions)
|
||||
query := fmt.Sprintf("SELECT * FROM %s WHERE %s", table, where)
|
||||
|
||||
order := "id DESC"
|
||||
if len(orderBy) > 0 && orderBy[0] != "" {
|
||||
@@ -117,11 +129,5 @@ func FindOne(table string, conditions map[string]interface{}, orderBy ...string)
|
||||
}
|
||||
query += " ORDER BY " + order + " LIMIT 1"
|
||||
|
||||
row := mapx.New()
|
||||
err := DB.Get(&row, query, values...)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return row, nil
|
||||
return queryMap(query, values)
|
||||
}
|
||||
Reference in new issue
Block a user