Files
m2pool_payment/internal/db/sqlite.go
2025-10-27 16:27:33 +08:00

121 lines
2.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package db
import (
"database/sql"
"fmt"
_ "modernc.org/sqlite" // 导入驱动
)
type SQLite struct {
DB *sql.DB
}
// 初始化连接
func NewSQLite(path string) (*SQLite, error) {
db, err := sql.Open("sqlite", path)
if err != nil {
return nil, fmt.Errorf("open sqlite failed: %v", err)
}
return &SQLite{DB: db}, nil
}
// 关闭数据库
func (s *SQLite) Close() {
if s.DB != nil {
s.DB.Close()
}
}
// 通用新建表
func (s *SQLite) Exec_(sql string) error {
_, err := s.DB.Exec(sql)
if err != nil {
return fmt.Errorf("Exec DB error: %w", err)
}
return nil
}
// 通用查询方法(返回[]map[string]any
func (s *SQLite) Query_(query string, args ...any) ([]map[string]any, error) {
rows, err := s.DB.Query(query, args...)
if err != nil {
return nil, fmt.Errorf("query error: %v", err)
}
defer rows.Close()
columns, err := rows.Columns()
if err != nil {
return nil, err
}
var results []map[string]any
for rows.Next() {
// 创建一个与列数相同的 slice 用于 Scan
values := make([]any, len(columns))
valuePtrs := make([]any, len(columns))
for i := range values {
valuePtrs[i] = &values[i]
}
if err := rows.Scan(valuePtrs...); err != nil {
return nil, err
}
// 将行转换为 map
rowMap := make(map[string]any)
for i, col := range columns {
val := values[i]
if b, ok := val.([]byte); ok {
rowMap[col] = string(b)
} else {
rowMap[col] = val
}
}
results = append(results, rowMap)
}
return results, nil
}
// 插入数据(支持事务和批量)
func (s *SQLite) Insert(sqlStr string, args ...[]any) error {
tx, err := s.DB.Begin()
if err != nil {
return err
}
stmt, err := tx.Prepare(sqlStr)
if err != nil {
return err
}
defer stmt.Close()
for _, a := range args {
_, err = stmt.Exec(a...)
if err != nil {
tx.Rollback()
return fmt.Errorf("exec insert error: %v", err)
}
}
return tx.Commit()
}
// 更新数据
func (s *SQLite) Update(sqlStr string, args ...any) (int64, error) {
res, err := s.DB.Exec(sqlStr, args...)
if err != nil {
return 0, fmt.Errorf("update error: %v", err)
}
rows, _ := res.RowsAffected()
return rows, nil
}
// 删除数据
func (s *SQLite) Delete(sqlStr string, args ...any) (int64, error) {
res, err := s.DB.Exec(sqlStr, args...)
if err != nil {
return 0, fmt.Errorf("delete error: %v", err)
}
rows, _ := res.RowsAffected()
return rows, nil
}