2020-06-05 05:08:22 +03:00
|
|
|
package gorm
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-03-20 14:47:20 +03:00
|
|
|
"database/sql/driver"
|
|
|
|
"errors"
|
2023-05-31 14:21:51 +03:00
|
|
|
"reflect"
|
2020-06-05 05:08:22 +03:00
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
2021-01-10 11:46:06 +03:00
|
|
|
type Stmt struct {
|
|
|
|
*sql.Stmt
|
|
|
|
Transaction bool
|
2022-09-30 13:13:36 +03:00
|
|
|
prepared chan struct{}
|
|
|
|
prepareErr error
|
2021-01-10 11:46:06 +03:00
|
|
|
}
|
|
|
|
|
2020-06-05 05:08:22 +03:00
|
|
|
type PreparedStmtDB struct {
|
2022-09-30 13:13:36 +03:00
|
|
|
Stmts map[string]*Stmt
|
2020-07-28 09:26:09 +03:00
|
|
|
PreparedSQL []string
|
2020-10-21 10:47:46 +03:00
|
|
|
Mux *sync.RWMutex
|
2020-06-05 05:08:22 +03:00
|
|
|
ConnPool
|
|
|
|
}
|
|
|
|
|
2023-06-05 11:23:17 +03:00
|
|
|
func NewPreparedStmtDB(connPool ConnPool) *PreparedStmtDB {
|
|
|
|
return &PreparedStmtDB{
|
|
|
|
ConnPool: connPool,
|
|
|
|
Stmts: make(map[string]*Stmt),
|
|
|
|
Mux: &sync.RWMutex{},
|
|
|
|
PreparedSQL: make([]string, 0, 100),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-19 16:33:31 +03:00
|
|
|
func (db *PreparedStmtDB) GetDBConn() (*sql.DB, error) {
|
2021-03-19 10:54:32 +03:00
|
|
|
if sqldb, ok := db.ConnPool.(*sql.DB); ok {
|
|
|
|
return sqldb, nil
|
|
|
|
}
|
|
|
|
|
2023-08-10 08:30:48 +03:00
|
|
|
if dbConnector, ok := db.ConnPool.(GetDBConnector); ok && dbConnector != nil {
|
|
|
|
return dbConnector.GetDBConn()
|
|
|
|
}
|
|
|
|
|
2021-04-19 16:03:39 +03:00
|
|
|
return nil, ErrInvalidDB
|
2021-03-19 10:54:32 +03:00
|
|
|
}
|
|
|
|
|
2020-06-21 13:01:50 +03:00
|
|
|
func (db *PreparedStmtDB) Close() {
|
2020-08-03 16:48:36 +03:00
|
|
|
db.Mux.Lock()
|
2021-10-08 06:16:58 +03:00
|
|
|
defer db.Mux.Unlock()
|
|
|
|
|
2020-07-28 09:26:09 +03:00
|
|
|
for _, query := range db.PreparedSQL {
|
|
|
|
if stmt, ok := db.Stmts[query]; ok {
|
|
|
|
delete(db.Stmts, query)
|
2021-07-14 13:31:50 +03:00
|
|
|
go stmt.Close()
|
2020-07-28 09:26:09 +03:00
|
|
|
}
|
2020-06-21 13:01:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-10 08:30:48 +03:00
|
|
|
func (sdb *PreparedStmtDB) Reset() {
|
|
|
|
sdb.Mux.Lock()
|
|
|
|
defer sdb.Mux.Unlock()
|
2022-11-13 06:12:09 +03:00
|
|
|
|
2023-08-10 08:30:48 +03:00
|
|
|
for _, stmt := range sdb.Stmts {
|
2022-10-19 09:46:59 +03:00
|
|
|
go stmt.Close()
|
|
|
|
}
|
2023-08-10 08:30:48 +03:00
|
|
|
sdb.PreparedSQL = make([]string, 0, 100)
|
|
|
|
sdb.Stmts = make(map[string]*Stmt)
|
2022-10-19 09:46:59 +03:00
|
|
|
}
|
|
|
|
|
2021-01-10 11:46:06 +03:00
|
|
|
func (db *PreparedStmtDB) prepare(ctx context.Context, conn ConnPool, isTransaction bool, query string) (Stmt, error) {
|
2020-08-03 16:48:36 +03:00
|
|
|
db.Mux.RLock()
|
2021-01-10 11:46:06 +03:00
|
|
|
if stmt, ok := db.Stmts[query]; ok && (!stmt.Transaction || isTransaction) {
|
2020-08-03 16:48:36 +03:00
|
|
|
db.Mux.RUnlock()
|
2022-09-30 13:13:36 +03:00
|
|
|
// wait for other goroutines prepared
|
|
|
|
<-stmt.prepared
|
|
|
|
if stmt.prepareErr != nil {
|
|
|
|
return Stmt{}, stmt.prepareErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return *stmt, nil
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
2020-08-03 16:48:36 +03:00
|
|
|
db.Mux.RUnlock()
|
2020-06-05 05:08:22 +03:00
|
|
|
|
2020-08-03 16:48:36 +03:00
|
|
|
db.Mux.Lock()
|
2020-06-21 13:01:50 +03:00
|
|
|
// double check
|
2021-01-10 11:46:06 +03:00
|
|
|
if stmt, ok := db.Stmts[query]; ok && (!stmt.Transaction || isTransaction) {
|
2022-09-30 13:13:36 +03:00
|
|
|
db.Mux.Unlock()
|
|
|
|
// wait for other goroutines prepared
|
|
|
|
<-stmt.prepared
|
|
|
|
if stmt.prepareErr != nil {
|
|
|
|
return Stmt{}, stmt.prepareErr
|
|
|
|
}
|
|
|
|
|
|
|
|
return *stmt, nil
|
2020-06-21 13:01:50 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:13:36 +03:00
|
|
|
// cache preparing stmt first
|
|
|
|
cacheStmt := Stmt{Transaction: isTransaction, prepared: make(chan struct{})}
|
|
|
|
db.Stmts[query] = &cacheStmt
|
|
|
|
db.Mux.Unlock()
|
|
|
|
|
|
|
|
// prepare completed
|
|
|
|
defer close(cacheStmt.prepared)
|
|
|
|
|
|
|
|
// Reason why cannot lock conn.PrepareContext
|
|
|
|
// suppose the maxopen is 1, g1 is creating record and g2 is querying record.
|
2022-11-13 06:12:09 +03:00
|
|
|
// 1. g1 begin tx, g1 is requeue because of waiting for the system call, now `db.ConnPool` db.numOpen == 1.
|
2022-09-30 13:13:36 +03:00
|
|
|
// 2. g2 select lock `conn.PrepareContext(ctx, query)`, now db.numOpen == db.maxOpen , wait for release.
|
|
|
|
// 3. g1 tx exec insert, wait for unlock `conn.PrepareContext(ctx, query)` to finish tx and release.
|
2020-12-14 13:30:43 +03:00
|
|
|
stmt, err := conn.PrepareContext(ctx, query)
|
2022-09-30 13:13:36 +03:00
|
|
|
if err != nil {
|
|
|
|
cacheStmt.prepareErr = err
|
|
|
|
db.Mux.Lock()
|
|
|
|
delete(db.Stmts, query)
|
|
|
|
db.Mux.Unlock()
|
|
|
|
return Stmt{}, err
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
|
|
|
|
2022-09-30 13:13:36 +03:00
|
|
|
db.Mux.Lock()
|
|
|
|
cacheStmt.Stmt = stmt
|
|
|
|
db.PreparedSQL = append(db.PreparedSQL, query)
|
|
|
|
db.Mux.Unlock()
|
|
|
|
|
|
|
|
return cacheStmt, nil
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-08-19 16:33:31 +03:00
|
|
|
|
|
|
|
beginner, ok := db.ConnPool.(ConnPoolBeginner)
|
|
|
|
if !ok {
|
|
|
|
return nil, ErrInvalidTransaction
|
|
|
|
}
|
|
|
|
|
|
|
|
connPool, err := beginner.BeginTx(ctx, opt)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if tx, ok := connPool.(Tx); ok {
|
|
|
|
return &PreparedStmtTX{PreparedStmtDB: db, Tx: tx}, nil
|
|
|
|
}
|
2020-06-05 05:08:22 +03:00
|
|
|
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) {
|
2021-01-10 11:46:06 +03:00
|
|
|
stmt, err := db.prepare(ctx, db.ConnPool, false, query)
|
2020-06-05 05:08:22 +03:00
|
|
|
if err == nil {
|
2020-07-03 05:26:18 +03:00
|
|
|
result, err = stmt.ExecContext(ctx, args...)
|
2024-03-20 14:47:20 +03:00
|
|
|
if errors.Is(err, driver.ErrBadConn) {
|
2020-08-03 16:48:36 +03:00
|
|
|
db.Mux.Lock()
|
2021-10-08 06:16:58 +03:00
|
|
|
defer db.Mux.Unlock()
|
2021-07-14 13:31:50 +03:00
|
|
|
go stmt.Close()
|
2020-07-03 05:26:18 +03:00
|
|
|
delete(db.Stmts, query)
|
|
|
|
}
|
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) {
|
2021-01-10 11:46:06 +03:00
|
|
|
stmt, err := db.prepare(ctx, db.ConnPool, false, query)
|
2020-06-05 05:08:22 +03:00
|
|
|
if err == nil {
|
2020-07-03 05:26:18 +03:00
|
|
|
rows, err = stmt.QueryContext(ctx, args...)
|
2024-03-20 14:47:20 +03:00
|
|
|
if errors.Is(err, driver.ErrBadConn) {
|
2020-08-03 16:48:36 +03:00
|
|
|
db.Mux.Lock()
|
2021-10-08 06:16:58 +03:00
|
|
|
defer db.Mux.Unlock()
|
|
|
|
|
2021-07-14 13:31:50 +03:00
|
|
|
go stmt.Close()
|
2020-07-03 05:26:18 +03:00
|
|
|
delete(db.Stmts, query)
|
|
|
|
}
|
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 {
|
2021-01-10 11:46:06 +03:00
|
|
|
stmt, err := db.prepare(ctx, db.ConnPool, false, query)
|
2020-06-05 05:08:22 +03:00
|
|
|
if err == nil {
|
|
|
|
return stmt.QueryRowContext(ctx, args...)
|
|
|
|
}
|
|
|
|
return &sql.Row{}
|
|
|
|
}
|
|
|
|
|
|
|
|
type PreparedStmtTX struct {
|
2022-02-28 12:12:09 +03:00
|
|
|
Tx
|
2020-06-05 05:08:22 +03:00
|
|
|
PreparedStmtDB *PreparedStmtDB
|
|
|
|
}
|
|
|
|
|
2023-08-19 16:33:31 +03:00
|
|
|
func (db *PreparedStmtTX) GetDBConn() (*sql.DB, error) {
|
|
|
|
return db.PreparedStmtDB.GetDBConn()
|
|
|
|
}
|
|
|
|
|
2020-09-03 14:16:55 +03:00
|
|
|
func (tx *PreparedStmtTX) Commit() error {
|
2023-05-31 14:21:51 +03:00
|
|
|
if tx.Tx != nil && !reflect.ValueOf(tx.Tx).IsNil() {
|
2020-09-03 14:16:55 +03:00
|
|
|
return tx.Tx.Commit()
|
|
|
|
}
|
|
|
|
return ErrInvalidTransaction
|
|
|
|
}
|
|
|
|
|
|
|
|
func (tx *PreparedStmtTX) Rollback() error {
|
2023-05-31 14:21:51 +03:00
|
|
|
if tx.Tx != nil && !reflect.ValueOf(tx.Tx).IsNil() {
|
2020-09-03 14:16:55 +03:00
|
|
|
return tx.Tx.Rollback()
|
|
|
|
}
|
|
|
|
return ErrInvalidTransaction
|
|
|
|
}
|
|
|
|
|
2020-07-03 05:26:18 +03:00
|
|
|
func (tx *PreparedStmtTX) ExecContext(ctx context.Context, query string, args ...interface{}) (result sql.Result, err error) {
|
2021-01-10 11:46:06 +03:00
|
|
|
stmt, err := tx.PreparedStmtDB.prepare(ctx, tx.Tx, true, query)
|
2020-06-05 05:08:22 +03:00
|
|
|
if err == nil {
|
2021-01-10 11:46:06 +03:00
|
|
|
result, err = tx.Tx.StmtContext(ctx, stmt.Stmt).ExecContext(ctx, args...)
|
2024-03-20 14:47:20 +03:00
|
|
|
if errors.Is(err, driver.ErrBadConn) {
|
2020-08-03 16:48:36 +03:00
|
|
|
tx.PreparedStmtDB.Mux.Lock()
|
2021-10-08 06:16:58 +03:00
|
|
|
defer tx.PreparedStmtDB.Mux.Unlock()
|
|
|
|
|
2021-07-14 13:31:50 +03:00
|
|
|
go stmt.Close()
|
2020-07-03 05:26:18 +03:00
|
|
|
delete(tx.PreparedStmtDB.Stmts, query)
|
|
|
|
}
|
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) {
|
2021-01-10 11:46:06 +03:00
|
|
|
stmt, err := tx.PreparedStmtDB.prepare(ctx, tx.Tx, true, query)
|
2020-06-05 05:08:22 +03:00
|
|
|
if err == nil {
|
2022-02-28 12:12:09 +03:00
|
|
|
rows, err = tx.Tx.StmtContext(ctx, stmt.Stmt).QueryContext(ctx, args...)
|
2024-03-20 14:47:20 +03:00
|
|
|
if errors.Is(err, driver.ErrBadConn) {
|
2020-08-03 16:48:36 +03:00
|
|
|
tx.PreparedStmtDB.Mux.Lock()
|
2021-10-08 06:16:58 +03:00
|
|
|
defer tx.PreparedStmtDB.Mux.Unlock()
|
|
|
|
|
2021-07-14 13:31:50 +03:00
|
|
|
go stmt.Close()
|
2020-07-03 05:26:18 +03:00
|
|
|
delete(tx.PreparedStmtDB.Stmts, query)
|
|
|
|
}
|
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 {
|
2021-01-10 11:46:06 +03:00
|
|
|
stmt, err := tx.PreparedStmtDB.prepare(ctx, tx.Tx, true, query)
|
2020-06-05 05:08:22 +03:00
|
|
|
if err == nil {
|
2021-01-10 11:46:06 +03:00
|
|
|
return tx.Tx.StmtContext(ctx, stmt.Stmt).QueryRowContext(ctx, args...)
|
2020-06-05 05:08:22 +03:00
|
|
|
}
|
|
|
|
return &sql.Row{}
|
|
|
|
}
|