summaryrefslogtreecommitdiffstats
path: root/vendor/xorm.io/xorm/contexts
diff options
context:
space:
mode:
authorLunny Xiao <xiaolunwen@gmail.com>2020-06-16 04:46:01 +0800
committerGitHub <noreply@github.com>2020-06-15 16:46:01 -0400
commitcdef92b3ffc7dcbc0f4ee26f7f973ca2b28fea61 (patch)
tree0bf6d1d054affe3dd04daaa92e35fa1e50d8ade1 /vendor/xorm.io/xorm/contexts
parent492b7d63576cc18c5de70cb0fa061b97d67bf227 (diff)
downloadgitea-cdef92b3ffc7dcbc0f4ee26f7f973ca2b28fea61.tar.gz
gitea-cdef92b3ffc7dcbc0f4ee26f7f973ca2b28fea61.zip
Upgrade xorm to v1.0.2 (#11900)
Co-authored-by: techknowlogick <techknowlogick@gitea.io>
Diffstat (limited to 'vendor/xorm.io/xorm/contexts')
-rw-r--r--vendor/xorm.io/xorm/contexts/hook.go75
1 files changed, 75 insertions, 0 deletions
diff --git a/vendor/xorm.io/xorm/contexts/hook.go b/vendor/xorm.io/xorm/contexts/hook.go
new file mode 100644
index 0000000000..71ad8e8721
--- /dev/null
+++ b/vendor/xorm.io/xorm/contexts/hook.go
@@ -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
+}