diff options
author | Lunny Xiao <xiaolunwen@gmail.com> | 2021-11-21 23:41:00 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-21 23:41:00 +0800 |
commit | d710af6669654f27f02b69d7ef1ba563e7d58a90 (patch) | |
tree | 9727f468a570106293dc90beb70035180bbb7e8e /models/db | |
parent | 0add627182388ac63fd04b94cdf912fb87fd0326 (diff) | |
download | gitea-d710af6669654f27f02b69d7ef1ba563e7d58a90.tar.gz gitea-d710af6669654f27f02b69d7ef1ba563e7d58a90.zip |
Remove NewSession method from db.Engine interface (#17577)
* Remove NewSession method from db.Engine interface
* Fix bug
* Some improvements
* Fix bug
* Fix test
* Use XXXBean instead of XXXExample
Diffstat (limited to 'models/db')
-rw-r--r-- | models/db/context.go | 55 | ||||
-rwxr-xr-x | models/db/engine.go | 1 | ||||
-rw-r--r-- | models/db/list_options.go | 2 |
3 files changed, 28 insertions, 30 deletions
diff --git a/models/db/context.go b/models/db/context.go index 62b77bc72f..55e38ba7e5 100644 --- a/models/db/context.go +++ b/models/db/context.go @@ -6,11 +6,11 @@ package db import ( "context" + "database/sql" "code.gitea.io/gitea/modules/setting" "xorm.io/builder" - "xorm.io/xorm" ) // DefaultContext is the default context to run xorm queries in @@ -44,15 +44,6 @@ 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 -} - // Value shadows Value for context.Context but allows us to get ourselves and an Engined object func (ctx *Context) Value(key interface{}) interface{} { if key == EnginedContextKey { @@ -64,7 +55,6 @@ func (ctx *Context) Value(key interface{}) interface{} { // Engined structs provide an Engine type Engined interface { Engine() Engine - NewSession() *xorm.Session } // GetEngine will get a db Engine from this context or return an Engine restricted to this context @@ -79,24 +69,6 @@ func GetEngine(ctx context.Context) Engine { return x.Context(ctx) } -// NewSession will get a db Session from this context or return a session restricted to this context -func NewSession(ctx context.Context) *xorm.Session { - if engined, ok := ctx.(Engined); ok { - return engined.NewSession() - } - - enginedInterface := ctx.Value(EnginedContextKey) - if enginedInterface != nil { - sess := enginedInterface.(Engined).NewSession() - if sess != nil { - return sess.Context(ctx) - } - return nil - } - - return x.NewSession().Context(ctx) -} - // Committer represents an interface to Commit or Close the Context type Committer interface { Commit() error @@ -155,3 +127,28 @@ func Insert(ctx context.Context, beans ...interface{}) error { _, err := GetEngine(ctx).Insert(beans...) return err } + +// Exec executes a sql with args +func Exec(ctx context.Context, sqlAndArgs ...interface{}) (sql.Result, error) { + return GetEngine(ctx).Exec(sqlAndArgs...) +} + +// GetByBean filled empty fields of the bean according non-empty fields to query in database. +func GetByBean(ctx context.Context, bean interface{}) (bool, error) { + return GetEngine(ctx).Get(bean) +} + +// DeleteByBean deletes all records according non-empty fields of the bean as conditions. +func DeleteByBean(ctx context.Context, bean interface{}) (int64, error) { + return GetEngine(ctx).Delete(bean) +} + +// CountByBean counts the number of database records according non-empty fields of the bean as conditions. +func CountByBean(ctx context.Context, bean interface{}) (int64, error) { + return GetEngine(ctx).Count(bean) +} + +// TableName returns the table name according a bean object +func TableName(bean interface{}) string { + return x.TableName(bean) +} diff --git a/models/db/engine.go b/models/db/engine.go index b97e954cc3..0f744d027e 100755 --- a/models/db/engine.go +++ b/models/db/engine.go @@ -55,6 +55,7 @@ type Engine interface { Asc(colNames ...string) *xorm.Session Desc(colNames ...string) *xorm.Session Limit(limit int, start ...int) *xorm.Session + NoAutoTime() *xorm.Session SumInt(bean interface{}, columnName string) (res int64, err error) Sync2(...interface{}) error Select(string) *xorm.Session diff --git a/models/db/list_options.go b/models/db/list_options.go index f31febfe25..843e73c8ae 100644 --- a/models/db/list_options.go +++ b/models/db/list_options.go @@ -24,7 +24,7 @@ func GetPaginatedSession(p Paginator) *xorm.Session { } // SetSessionPagination sets pagination for a database session -func SetSessionPagination(sess *xorm.Session, p Paginator) *xorm.Session { +func SetSessionPagination(sess Engine, p Paginator) *xorm.Session { skip, take := p.GetSkipTake() return sess.Limit(take, skip) |