mirror of https://github.com/go-gorm/gorm.git
refactor: remove unnecessary prepared statement allocation (#6374)
This commit is contained in:
parent
c1ea730367
commit
7a76c042e6
20
gorm.go
20
gorm.go
|
@ -187,15 +187,9 @@ func Open(dialector Dialector, opts ...Option) (db *DB, err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
preparedStmt := &PreparedStmtDB{
|
|
||||||
ConnPool: db.ConnPool,
|
|
||||||
Stmts: make(map[string]*Stmt),
|
|
||||||
Mux: &sync.RWMutex{},
|
|
||||||
PreparedSQL: make([]string, 0, 100),
|
|
||||||
}
|
|
||||||
db.cacheStore.Store(preparedStmtDBKey, preparedStmt)
|
|
||||||
|
|
||||||
if config.PrepareStmt {
|
if config.PrepareStmt {
|
||||||
|
preparedStmt := NewPreparedStmtDB(db.ConnPool)
|
||||||
|
db.cacheStore.Store(preparedStmtDBKey, preparedStmt)
|
||||||
db.ConnPool = preparedStmt
|
db.ConnPool = preparedStmt
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -256,8 +250,15 @@ func (db *DB) Session(config *Session) *DB {
|
||||||
}
|
}
|
||||||
|
|
||||||
if config.PrepareStmt {
|
if config.PrepareStmt {
|
||||||
|
var preparedStmt *PreparedStmtDB
|
||||||
|
|
||||||
if v, ok := db.cacheStore.Load(preparedStmtDBKey); ok {
|
if v, ok := db.cacheStore.Load(preparedStmtDBKey); ok {
|
||||||
preparedStmt := v.(*PreparedStmtDB)
|
preparedStmt = v.(*PreparedStmtDB)
|
||||||
|
} else {
|
||||||
|
preparedStmt = NewPreparedStmtDB(db.ConnPool)
|
||||||
|
db.cacheStore.Store(preparedStmtDBKey, preparedStmt)
|
||||||
|
}
|
||||||
|
|
||||||
switch t := tx.Statement.ConnPool.(type) {
|
switch t := tx.Statement.ConnPool.(type) {
|
||||||
case Tx:
|
case Tx:
|
||||||
tx.Statement.ConnPool = &PreparedStmtTX{
|
tx.Statement.ConnPool = &PreparedStmtTX{
|
||||||
|
@ -274,7 +275,6 @@ func (db *DB) Session(config *Session) *DB {
|
||||||
txConfig.ConnPool = tx.Statement.ConnPool
|
txConfig.ConnPool = tx.Statement.ConnPool
|
||||||
txConfig.PrepareStmt = true
|
txConfig.PrepareStmt = true
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if config.SkipHooks {
|
if config.SkipHooks {
|
||||||
tx.Statement.SkipHooks = true
|
tx.Statement.SkipHooks = true
|
||||||
|
|
|
@ -21,6 +21,15 @@ type PreparedStmtDB struct {
|
||||||
ConnPool
|
ConnPool
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func NewPreparedStmtDB(connPool ConnPool) *PreparedStmtDB {
|
||||||
|
return &PreparedStmtDB{
|
||||||
|
ConnPool: connPool,
|
||||||
|
Stmts: make(map[string]*Stmt),
|
||||||
|
Mux: &sync.RWMutex{},
|
||||||
|
PreparedSQL: make([]string, 0, 100),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (db *PreparedStmtDB) GetDBConn() (*sql.DB, error) {
|
func (db *PreparedStmtDB) GetDBConn() (*sql.DB, error) {
|
||||||
if dbConnector, ok := db.ConnPool.(GetDBConnector); ok && dbConnector != nil {
|
if dbConnector, ok := db.ConnPool.(GetDBConnector); ok && dbConnector != nil {
|
||||||
return dbConnector.GetDBConn()
|
return dbConnector.GetDBConn()
|
||||||
|
|
Loading…
Reference in New Issue