mirror of https://github.com/go-gitea/gitea
Upgrade xorm to v1.0.2 (#11900)
Co-authored-by: techknowlogick <techknowlogick@gitea.io>pull/11887/head^2
parent
492b7d6357
commit
cdef92b3ff
@ -0,0 +1,75 @@ |
|||||||
|
// Copyright 2020 The Xorm Authors. All rights reserved.
|
||||||
|
// Use of this source code is governed by a BSD-style
|
||||||
|
// license that can be found in the LICENSE file.
|
||||||
|
|
||||||
|
package contexts |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
"database/sql" |
||||||
|
"time" |
||||||
|
) |
||||||
|
|
||||||
|
// ContextHook represents a hook context
|
||||||
|
type ContextHook struct { |
||||||
|
start time.Time |
||||||
|
Ctx context.Context |
||||||
|
SQL string // log content or SQL
|
||||||
|
Args []interface{} // if it's a SQL, it's the arguments
|
||||||
|
Result sql.Result |
||||||
|
ExecuteTime time.Duration |
||||||
|
Err error // SQL executed error
|
||||||
|
} |
||||||
|
|
||||||
|
// NewContextHook return context for hook
|
||||||
|
func NewContextHook(ctx context.Context, sql string, args []interface{}) *ContextHook { |
||||||
|
return &ContextHook{ |
||||||
|
start: time.Now(), |
||||||
|
Ctx: ctx, |
||||||
|
SQL: sql, |
||||||
|
Args: args, |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func (c *ContextHook) End(ctx context.Context, result sql.Result, err error) { |
||||||
|
c.Ctx = ctx |
||||||
|
c.Result = result |
||||||
|
c.Err = err |
||||||
|
c.ExecuteTime = time.Now().Sub(c.start) |
||||||
|
} |
||||||
|
|
||||||
|
type Hook interface { |
||||||
|
BeforeProcess(c *ContextHook) (context.Context, error) |
||||||
|
AfterProcess(c *ContextHook) error |
||||||
|
} |
||||||
|
|
||||||
|
type Hooks struct { |
||||||
|
hooks []Hook |
||||||
|
} |
||||||
|
|
||||||
|
func (h *Hooks) AddHook(hooks ...Hook) { |
||||||
|
h.hooks = append(h.hooks, hooks...) |
||||||
|
} |
||||||
|
|
||||||
|
func (h *Hooks) BeforeProcess(c *ContextHook) (context.Context, error) { |
||||||
|
ctx := c.Ctx |
||||||
|
for _, h := range h.hooks { |
||||||
|
var err error |
||||||
|
ctx, err = h.BeforeProcess(c) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
} |
||||||
|
return ctx, nil |
||||||
|
} |
||||||
|
|
||||||
|
func (h *Hooks) AfterProcess(c *ContextHook) error { |
||||||
|
firstErr := c.Err |
||||||
|
for _, h := range h.hooks { |
||||||
|
err := h.AfterProcess(c) |
||||||
|
if err != nil && firstErr == nil { |
||||||
|
firstErr = err |
||||||
|
} |
||||||
|
} |
||||||
|
return firstErr |
||||||
|
} |
@ -1,81 +0,0 @@ |
|||||||
// Copyright 2015 The Xorm Authors. All rights reserved.
|
|
||||||
// Use of this source code is governed by a BSD-style
|
|
||||||
// license that can be found in the LICENSE file.
|
|
||||||
|
|
||||||
// +build go1.11
|
|
||||||
|
|
||||||
package xorm |
|
||||||
|
|
||||||
import ( |
|
||||||
"context" |
|
||||||
"os" |
|
||||||
"runtime" |
|
||||||
"time" |
|
||||||
|
|
||||||
"xorm.io/xorm/caches" |
|
||||||
"xorm.io/xorm/core" |
|
||||||
"xorm.io/xorm/dialects" |
|
||||||
"xorm.io/xorm/log" |
|
||||||
"xorm.io/xorm/names" |
|
||||||
"xorm.io/xorm/schemas" |
|
||||||
"xorm.io/xorm/tags" |
|
||||||
) |
|
||||||
|
|
||||||
func close(engine *Engine) { |
|
||||||
engine.Close() |
|
||||||
} |
|
||||||
|
|
||||||
// NewEngine new a db manager according to the parameter. Currently support four
|
|
||||||
// drivers
|
|
||||||
func NewEngine(driverName string, dataSourceName string) (*Engine, error) { |
|
||||||
dialect, err := dialects.OpenDialect(driverName, dataSourceName) |
|
||||||
if err != nil { |
|
||||||
return nil, err |
|
||||||
} |
|
||||||
|
|
||||||
db, err := core.Open(driverName, dataSourceName) |
|
||||||
if err != nil { |
|
||||||
return nil, err |
|
||||||
} |
|
||||||
|
|
||||||
cacherMgr := caches.NewManager() |
|
||||||
mapper := names.NewCacheMapper(new(names.SnakeMapper)) |
|
||||||
tagParser := tags.NewParser("xorm", dialect, mapper, mapper, cacherMgr) |
|
||||||
|
|
||||||
engine := &Engine{ |
|
||||||
dialect: dialect, |
|
||||||
TZLocation: time.Local, |
|
||||||
defaultContext: context.Background(), |
|
||||||
cacherMgr: cacherMgr, |
|
||||||
tagParser: tagParser, |
|
||||||
driverName: driverName, |
|
||||||
dataSourceName: dataSourceName, |
|
||||||
db: db, |
|
||||||
} |
|
||||||
|
|
||||||
if dialect.URI().DBType == schemas.SQLITE { |
|
||||||
engine.DatabaseTZ = time.UTC |
|
||||||
} else { |
|
||||||
engine.DatabaseTZ = time.Local |
|
||||||
} |
|
||||||
|
|
||||||
logger := log.NewSimpleLogger(os.Stdout) |
|
||||||
logger.SetLevel(log.LOG_INFO) |
|
||||||
engine.SetLogger(log.NewLoggerAdapter(logger)) |
|
||||||
|
|
||||||
runtime.SetFinalizer(engine, close) |
|
||||||
|
|
||||||
return engine, nil |
|
||||||
} |
|
||||||
|
|
||||||
// NewEngineWithParams new a db manager with params. The params will be passed to dialects.
|
|
||||||
func NewEngineWithParams(driverName string, dataSourceName string, params map[string]string) (*Engine, error) { |
|
||||||
engine, err := NewEngine(driverName, dataSourceName) |
|
||||||
engine.dialect.SetParams(params) |
|
||||||
return engine, err |
|
||||||
} |
|
||||||
|
|
||||||
// Clone clone an engine
|
|
||||||
func (engine *Engine) Clone() (*Engine, error) { |
|
||||||
return NewEngine(engine.DriverName(), engine.DataSourceName()) |
|
||||||
} |
|
Loading…
Reference in new issue