2024-05-17 18:30:33 +08:00
|
|
|
// Copyright 2022 GoEdge goedge.cdn@gmail.com. All rights reserved.
|
2022-03-13 19:27:38 +08:00
|
|
|
|
|
|
|
|
package dbs
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"database/sql"
|
2023-10-06 20:56:27 +08:00
|
|
|
fsutils "github.com/TeaOSLab/EdgeNode/internal/utils/fs"
|
2022-03-13 19:27:38 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type Stmt struct {
|
2023-06-23 21:32:38 +08:00
|
|
|
db *DB
|
2022-03-13 19:27:38 +08:00
|
|
|
rawStmt *sql.Stmt
|
|
|
|
|
query string
|
|
|
|
|
|
|
|
|
|
enableStat bool
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-23 21:32:38 +08:00
|
|
|
func NewStmt(db *DB, rawStmt *sql.Stmt, query string) *Stmt {
|
2022-03-13 19:27:38 +08:00
|
|
|
return &Stmt{
|
2023-06-23 21:32:38 +08:00
|
|
|
db: db,
|
2022-03-13 19:27:38 +08:00
|
|
|
rawStmt: rawStmt,
|
|
|
|
|
query: query,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *Stmt) EnableStat() {
|
|
|
|
|
this.enableStat = true
|
|
|
|
|
}
|
|
|
|
|
|
2023-10-06 20:56:27 +08:00
|
|
|
func (this *Stmt) ExecContext(ctx context.Context, args ...any) (result sql.Result, err error) {
|
2023-06-23 21:32:38 +08:00
|
|
|
// check database status
|
|
|
|
|
if this.db.BeginUpdating() {
|
|
|
|
|
defer this.db.EndUpdating()
|
|
|
|
|
} else {
|
|
|
|
|
return nil, errDBIsClosed
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 19:27:38 +08:00
|
|
|
if this.enableStat {
|
|
|
|
|
defer SharedQueryStatManager.AddQuery(this.query).End()
|
|
|
|
|
}
|
2024-04-29 22:36:26 +08:00
|
|
|
fsutils.WriterLimiter.Ack()
|
2023-10-06 20:56:27 +08:00
|
|
|
result, err = this.rawStmt.ExecContext(ctx, args...)
|
2024-04-29 22:36:26 +08:00
|
|
|
fsutils.WriterLimiter.Release()
|
2023-10-06 20:56:27 +08:00
|
|
|
return
|
2022-03-13 19:27:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-10-06 20:56:27 +08:00
|
|
|
func (this *Stmt) Exec(args ...any) (result sql.Result, err error) {
|
2023-06-23 21:32:38 +08:00
|
|
|
// check database status
|
|
|
|
|
if this.db.BeginUpdating() {
|
|
|
|
|
defer this.db.EndUpdating()
|
|
|
|
|
} else {
|
|
|
|
|
return nil, errDBIsClosed
|
|
|
|
|
}
|
|
|
|
|
|
2022-03-13 19:27:38 +08:00
|
|
|
if this.enableStat {
|
|
|
|
|
defer SharedQueryStatManager.AddQuery(this.query).End()
|
|
|
|
|
}
|
2023-10-06 20:56:27 +08:00
|
|
|
|
2024-04-29 22:36:26 +08:00
|
|
|
fsutils.WriterLimiter.Ack()
|
2023-10-06 20:56:27 +08:00
|
|
|
result, err = this.rawStmt.Exec(args...)
|
2024-04-29 22:36:26 +08:00
|
|
|
fsutils.WriterLimiter.Release()
|
2023-10-06 20:56:27 +08:00
|
|
|
return
|
2022-03-13 19:27:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 11:17:13 +08:00
|
|
|
func (this *Stmt) QueryContext(ctx context.Context, args ...any) (*sql.Rows, error) {
|
2022-03-13 19:27:38 +08:00
|
|
|
if this.enableStat {
|
|
|
|
|
defer SharedQueryStatManager.AddQuery(this.query).End()
|
|
|
|
|
}
|
|
|
|
|
return this.rawStmt.QueryContext(ctx, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 11:17:13 +08:00
|
|
|
func (this *Stmt) Query(args ...any) (*sql.Rows, error) {
|
2022-03-13 19:27:38 +08:00
|
|
|
if this.enableStat {
|
|
|
|
|
defer SharedQueryStatManager.AddQuery(this.query).End()
|
|
|
|
|
}
|
2023-08-09 11:17:13 +08:00
|
|
|
rows, err := this.rawStmt.Query(args...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
|
|
|
|
var rowsErr = rows.Err()
|
|
|
|
|
if rowsErr != nil {
|
|
|
|
|
_ = rows.Close()
|
|
|
|
|
return nil, rowsErr
|
|
|
|
|
}
|
|
|
|
|
return rows, nil
|
2022-03-13 19:27:38 +08:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 11:17:13 +08:00
|
|
|
func (this *Stmt) QueryRowContext(ctx context.Context, args ...any) *sql.Row {
|
2022-03-13 19:27:38 +08:00
|
|
|
if this.enableStat {
|
|
|
|
|
defer SharedQueryStatManager.AddQuery(this.query).End()
|
|
|
|
|
}
|
|
|
|
|
return this.rawStmt.QueryRowContext(ctx, args...)
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-09 11:17:13 +08:00
|
|
|
func (this *Stmt) QueryRow(args ...any) *sql.Row {
|
2022-03-13 19:27:38 +08:00
|
|
|
if this.enableStat {
|
|
|
|
|
defer SharedQueryStatManager.AddQuery(this.query).End()
|
|
|
|
|
}
|
|
|
|
|
return this.rawStmt.QueryRow(args...)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (this *Stmt) Close() error {
|
|
|
|
|
return this.rawStmt.Close()
|
|
|
|
|
}
|