mirror of
https://github.com/TeaOSLab/EdgeNode.git
synced 2026-04-24 12:35:28 +08:00
增加对数据库操作的统计命令:edge-node dbstat/减少几个不必要的查询操作
This commit is contained in:
69
internal/utils/dbs/db.go
Normal file
69
internal/utils/dbs/db.go
Normal file
@@ -0,0 +1,69 @@
|
||||
// Copyright 2022 Liuxiangchao iwind.liu@gmail.com. All rights reserved.
|
||||
|
||||
package dbs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
)
|
||||
|
||||
type DB struct {
|
||||
rawDB *sql.DB
|
||||
|
||||
enableStat bool
|
||||
}
|
||||
|
||||
func NewDB(rawDB *sql.DB) *DB {
|
||||
return &DB{
|
||||
rawDB: rawDB,
|
||||
}
|
||||
}
|
||||
|
||||
func (this *DB) EnableStat(b bool) {
|
||||
this.enableStat = b
|
||||
}
|
||||
|
||||
func (this *DB) Prepare(query string) (*Stmt, error) {
|
||||
stmt, err := this.rawDB.Prepare(query)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
var s = NewStmt(stmt, query)
|
||||
if this.enableStat {
|
||||
s.EnableStat()
|
||||
}
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (this *DB) ExecContext(ctx context.Context, query string, args ...interface{}) (sql.Result, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.ExecContext(ctx, query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) Exec(query string, args ...interface{}) (sql.Result, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.Exec(query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.Query(query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) QueryRow(query string, args ...interface{}) *sql.Row {
|
||||
if this.enableStat {
|
||||
defer SharedQueryStatManager.AddQuery(query).End()
|
||||
}
|
||||
return this.rawDB.QueryRow(query, args...)
|
||||
}
|
||||
|
||||
func (this *DB) Close() error {
|
||||
return this.rawDB.Close()
|
||||
}
|
||||
Reference in New Issue
Block a user