Session create new db session
(config *Session)
| 259 | |
| 260 | // Session create new db session |
| 261 | func (db *DB) Session(config *Session) *DB { |
| 262 | var ( |
| 263 | txConfig = *db.Config |
| 264 | tx = &DB{ |
| 265 | Config: &txConfig, |
| 266 | Statement: db.Statement, |
| 267 | Error: db.Error, |
| 268 | clone: 1, |
| 269 | } |
| 270 | ) |
| 271 | if config.CreateBatchSize > 0 { |
| 272 | tx.Config.CreateBatchSize = config.CreateBatchSize |
| 273 | } |
| 274 | |
| 275 | if config.SkipDefaultTransaction { |
| 276 | tx.Config.SkipDefaultTransaction = true |
| 277 | } |
| 278 | |
| 279 | if config.AllowGlobalUpdate { |
| 280 | txConfig.AllowGlobalUpdate = true |
| 281 | } |
| 282 | |
| 283 | if config.FullSaveAssociations { |
| 284 | txConfig.FullSaveAssociations = true |
| 285 | } |
| 286 | |
| 287 | if config.PropagateUnscoped { |
| 288 | txConfig.PropagateUnscoped = true |
| 289 | } |
| 290 | |
| 291 | if config.Context != nil || config.PrepareStmt || config.SkipHooks { |
| 292 | tx.Statement = tx.Statement.clone() |
| 293 | tx.Statement.DB = tx |
| 294 | } |
| 295 | |
| 296 | if config.Context != nil { |
| 297 | tx.Statement.Context = config.Context |
| 298 | } |
| 299 | |
| 300 | if config.PrepareStmt { |
| 301 | var preparedStmt *PreparedStmtDB |
| 302 | |
| 303 | if v, ok := db.cacheStore.Load(preparedStmtDBKey); ok { |
| 304 | preparedStmt = v.(*PreparedStmtDB) |
| 305 | } else { |
| 306 | preparedStmt = NewPreparedStmtDB(db.ConnPool, db.PrepareStmtMaxSize, db.PrepareStmtTTL) |
| 307 | db.cacheStore.Store(preparedStmtDBKey, preparedStmt) |
| 308 | } |
| 309 | |
| 310 | switch t := tx.Statement.ConnPool.(type) { |
| 311 | case Tx: |
| 312 | tx.Statement.ConnPool = &PreparedStmtTX{ |
| 313 | Tx: t, |
| 314 | PreparedStmtDB: preparedStmt, |
| 315 | } |
| 316 | default: |
| 317 | tx.Statement.ConnPool = &PreparedStmtDB{ |
| 318 | ConnPool: db.Config.ConnPool, |