MCPcopy
hub / github.com/go-gorm/gorm / Session

Method Session

gorm.go:261–360  ·  view source on GitHub ↗

Session create new db session

(config *Session)

Source from the content-addressed store, hash-verified

259
260// Session create new db session
261func (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,

Callers 15

WithContextMethod · 0.95
ToSQLMethod · 0.95
FirstOrCreateMethod · 0.95
TransactionMethod · 0.95
AddVarMethod · 0.80
MigratorMethod · 0.80
DebugMethod · 0.80
CreateInBatchesMethod · 0.80
SaveMethod · 0.80
FindInBatchesMethod · 0.80
BeginMethod · 0.80
applyMethod · 0.80

Calls 3

NewPreparedStmtDBFunction · 0.85
cloneMethod · 0.80
getInstanceMethod · 0.45

Tested by 15

TestTransactionFunction · 0.64
TestTransactionWithHooksFunction · 0.64
TestTableFunction · 0.64
TestTableWithAllFieldsFunction · 0.64