2020-06-05 05:08:22 +03:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
type PreparedStmtDB struct {
|
2020-07-28 09:26:09 +03:00
|
|
|
Stmts map[string]*sql.Stmt
|
|
|
|
PreparedSQL []string
|
|
|
|
mux sync.RWMutex
|
2020-06-05 05:08:22 +03:00
|
|
|
ConnPool
|
|
|
|
}
|
|
|
|
|
2020-06-21 13:01:50 +03:00
|
|
|
func (db *PreparedStmtDB) Close() {
|
|
|
|
db.mux.Lock()
|
2020-07-28 09:26:09 +03:00
|
|
|
for _, query := range db.PreparedSQL {
|
|
|
|
if stmt, ok := db.Stmts[query]; ok {
|
|
|
|
delete(db.Stmts, query)
|
|
|
|
stmt.Close()
|
|
|
|
}
|
2020-06-21 13:01:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
db.mux.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:08:22 +03:00
|
|
|
func (db *PreparedStmtDB) prepare(query string) (*sql.Stmt, error) {
|
|
|
|
db.mux.RLock()
|
2020-06-21 13:01:50 +03:00
|
|
|
if stmt, ok := db.Stmts[query]; ok {
|
2020-06-05 05:08:22 +03:00
|
|
|
db.mux.RUnlock()
|
|
|
|
return stmt, nil
|
|
|
|
}
|
|
|
|
db.mux.RUnlock()
|
|
|
|
|
|
|
|
db.mux.Lock()
|
2020-06-21 13:01:50 +03:00
|
|
|
// double check
|
|
|
|
if stmt, ok := db.Stmts[query]; ok {
|
|
|
|
db.mux.Unlock()
|
|
|
|
return stmt, nil
|
|
|
|
}
|
|
|
|
|
2020-06-05 05:08:22 +03:00
|
|
|
stmt, err := db.ConnPool.PrepareContext(context.Background(), query)
|
|
|
|
if err == nil {
|
2020-06-21 13:01:50 +03:00
|
|
|
db.Stmts[query] = stmt
|
2020-07-28 09:26:09 +03:00
|
|
|
db.PreparedSQL = append(db.PreparedSQL, query)
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
|
|
|
db.mux.Unlock()
|
|
|
|
|
|
|
|
return stmt, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (db *PreparedStmtDB) BeginTx(ctx context.Context, opt *sql.TxOptions) (ConnPool, error) {
|
|
|
|
if beginner, ok := db.ConnPool.(TxBeginner); ok {
|
|
|
|
tx, err := beginner.BeginTx(ctx, opt)
|
|
|
|
return &PreparedStmtTX{PreparedStmtDB: db, Tx: tx}, err
|
|
|
|
}
|
|
|
|
return nil, ErrInvalidTransaction
|
|
|
|
}
|
|
|
|
|
2020-07-03 05:26:18 +03:00
|
|
|
func (db *PreparedStmtDB) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error) {
|
2020-06-05 05:08:22 +03:00
|
|
|
stmt, err := db.prepare(query)
|
|
|
|
if err == nil {
|
2020-07-03 05:26:18 +03:00
|
|
|
result, err = stmt.ExecContext(ctx, args...)
|
|
|
|
if err != nil {
|
|
|
|
db.mux.Lock()
|
|
|
|
stmt.Close()
|
|
|
|
delete(db.Stmts, query)
|
|
|
|
db.mux.Unlock()
|
|
|
|
}
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
2020-07-03 05:26:18 +03:00
|
|
|
return result, err
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 05:26:18 +03:00
|
|
|
func (db *PreparedStmtDB) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
|
2020-06-05 05:08:22 +03:00
|
|
|
stmt, err := db.prepare(query)
|
|
|
|
if err == nil {
|
2020-07-03 05:26:18 +03:00
|
|
|
rows, err = stmt.QueryContext(ctx, args...)
|
|
|
|
if err != nil {
|
|
|
|
db.mux.Lock()
|
|
|
|
stmt.Close()
|
|
|
|
delete(db.Stmts, query)
|
|
|
|
db.mux.Unlock()
|
|
|
|
}
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
2020-07-03 05:26:18 +03:00
|
|
|
return rows, err
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (db *PreparedStmtDB) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
|
|
|
|
stmt, err := db.prepare(query)
|
|
|
|
if err == nil {
|
|
|
|
return stmt.QueryRowContext(ctx, args...)
|
|
|
|
}
|
|
|
|
return &sql.Row{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PreparedStmtTX struct {
|
|
|
|
*sql.Tx
|
|
|
|
PreparedStmtDB *PreparedStmtDB
|
|
|
|
}
|
|
|
|
|
2020-07-03 05:26:18 +03:00
|
|
|
func (tx *PreparedStmtTX) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error) {
|
2020-06-05 05:08:22 +03:00
|
|
|
stmt, err := tx.PreparedStmtDB.prepare(query)
|
|
|
|
if err == nil {
|
2020-07-03 05:26:18 +03:00
|
|
|
result, err = tx.Tx.Stmt(stmt).ExecContext(ctx, args...)
|
|
|
|
if err != nil {
|
|
|
|
tx.PreparedStmtDB.mux.Lock()
|
|
|
|
stmt.Close()
|
|
|
|
delete(tx.PreparedStmtDB.Stmts, query)
|
|
|
|
tx.PreparedStmtDB.mux.Unlock()
|
|
|
|
}
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
2020-07-03 05:26:18 +03:00
|
|
|
return result, err
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
|
|
|
|
2020-07-03 05:26:18 +03:00
|
|
|
func (tx *PreparedStmtTX) QueryContext(ctx context.Context, query string, args ...interface{}) (rows *sql.Rows, err error) {
|
2020-06-05 05:08:22 +03:00
|
|
|
stmt, err := tx.PreparedStmtDB.prepare(query)
|
|
|
|
if err == nil {
|
2020-07-03 05:26:18 +03:00
|
|
|
rows, err = tx.Tx.Stmt(stmt).QueryContext(ctx, args...)
|
|
|
|
if err != nil {
|
|
|
|
tx.PreparedStmtDB.mux.Lock()
|
|
|
|
stmt.Close()
|
|
|
|
delete(tx.PreparedStmtDB.Stmts, query)
|
|
|
|
tx.PreparedStmtDB.mux.Unlock()
|
|
|
|
}
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
2020-07-03 05:26:18 +03:00
|
|
|
return rows, err
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *PreparedStmtTX) QueryRowContext(ctx context.Context, query string, args ...interface{}) *sql.Row {
|
|
|
|
stmt, err := tx.PreparedStmtDB.prepare(query)
|
|
|
|
if err == nil {
|
|
|
|
return tx.Tx.Stmt(stmt).QueryRowContext(ctx, args...)
|
|
|
|
}
|
|
|
|
return &sql.Row{}
|
|
|
|
}
|