diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-09-19 19:49:59 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-09-19 19:49:59 +0800 |
commit | a4bfef265d9e512830350635a0489c2cdcd6508f (patch) | |
tree | 1e3c2ec94276dfcb2f8ba73a2ac075ba39c4a34a /models/db/context.go | |
parent | 462306e263db5a809dbe2cdf62e99307aeff28de (diff) | |
download | gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.tar.gz gitea-a4bfef265d9e512830350635a0489c2cdcd6508f.zip |
Move db related basic functions to models/db (#17075)
* Move db related basic functions to models/db
* Fix lint
* Fix lint
* Fix test
* Fix lint
* Fix lint
* revert unnecessary change
* Fix test
* Fix wrong replace string
* Use *Context
* Correct committer spelling and fix wrong replaced words
Co-authored-by: zeripath <art27@cantab.net>
Diffstat (limited to 'models/db/context.go')
-rw-r--r-- | models/db/context.go | 86 |
1 files changed, 86 insertions, 0 deletions
diff --git a/models/db/context.go b/models/db/context.go new file mode 100644 index 0000000000..9b1a3010b6 --- /dev/null +++ b/models/db/context.go @@ -0,0 +1,86 @@ +// Copyright 2019 The Gitea Authors. All rights reserved. +// Use of this source code is governed by a MIT-style +// license that can be found in the LICENSE file. + +package db + +import ( + "code.gitea.io/gitea/modules/setting" + + "xorm.io/builder" + "xorm.io/xorm" +) + +// Context represents a db context +type Context struct { + e Engine +} + +// Engine returns db engine +func (ctx *Context) Engine() Engine { + return ctx.e +} + +// NewSession returns a new session +func (ctx *Context) NewSession() *xorm.Session { + e, ok := ctx.e.(*xorm.Engine) + if ok { + return e.NewSession() + } + return nil +} + +// DefaultContext represents a Context with default Engine +func DefaultContext() *Context { + return &Context{x} +} + +// Committer represents an interface to Commit or Close the Context +type Committer interface { + Commit() error + Close() error +} + +// TxContext represents a transaction Context +func TxContext() (*Context, Committer, error) { + sess := x.NewSession() + if err := sess.Begin(); err != nil { + sess.Close() + return nil, nil, err + } + + return &Context{sess}, sess, nil +} + +// WithContext represents executing database operations +func WithContext(f func(ctx *Context) error) error { + return f(&Context{x}) +} + +// WithTx represents executing database operations on a transaction +func WithTx(f func(ctx *Context) error) error { + sess := x.NewSession() + defer sess.Close() + if err := sess.Begin(); err != nil { + return err + } + + if err := f(&Context{sess}); err != nil { + return err + } + + return sess.Commit() +} + +// Iterate iterates the databases and doing something +func Iterate(ctx *Context, tableBean interface{}, cond builder.Cond, fun func(idx int, bean interface{}) error) error { + return ctx.e.Where(cond). + BufferSize(setting.Database.IterateBufferSize). + Iterate(tableBean, fun) +} + +// Insert inserts records into database +func Insert(ctx *Context, beans ...interface{}) error { + _, err := ctx.e.Insert(beans...) + return err +} |