GORM provides Session
method, which is a New Session Method
, it allows create a new session mode with configuration:
// Session Configuration |
DryRun
Generate SQL
without executing, can be used to prepare or test generated SQL, for example:
// session mode |
PrepareStmt
PreparedStmt
creates prepared statement when executing any SQL and caches them to speed up future calls, for example:
// globally mode, all DB operations will create prepared stmt and cache them |
WithConditions
Share *gorm.DB
conditions with option WithConditions
, for example:
tx := db.Where("name = ?", "jinzhu").Session(&gorm.Session{WithConditions: true}) |
AllowGlobalUpdate
GORM doesn’t allow global update/delete by default, will return ErrMissingWhereClause
error, you can set this option to true to enable it, for example:
DB.Session(&gorm.Session{ |
Context
With the Context
option, you can set the Context
for following SQL operations, for example:
timeoutCtx, _ := context.WithTimeout(context.Background(), time.Second) |
GORM also provides shortcut method WithContext
, here is the definition:
func (db *DB) WithContext(ctx context.Context) *DB { |
Logger
Gorm allows customize built-in logger with the Logger
option, for example:
newLogger := logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), |
Checkout Logger for more details
NowFunc
NowFunc
allows change the function to get current time of GORM, for example:
db.Session(&Session{ |
Debug
Debug
is a shortcut method to change session’s Logger
to debug mode, here is the definition:
func (db *DB) Debug() (tx *DB) { |