bun debug

This commit is contained in:
tsmethurst 2021-09-10 21:45:33 +02:00
commit 408170b754
17 changed files with 2349 additions and 0 deletions

24
vendor/github.com/uptrace/bun/extra/bundebug/LICENSE generated vendored Normal file
View file

@ -0,0 +1,24 @@
Copyright (c) 2021 Vladimir Mihailenco. All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above
copyright notice, this list of conditions and the following disclaimer
in the documentation and/or other materials provided with the
distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

109
vendor/github.com/uptrace/bun/extra/bundebug/debug.go generated vendored Normal file
View file

@ -0,0 +1,109 @@
package bundebug
import (
"context"
"database/sql"
"fmt"
"reflect"
"strings"
"time"
"github.com/fatih/color"
"github.com/uptrace/bun"
)
type ConfigOption func(*QueryHook)
func WithVerbose() ConfigOption {
return func(h *QueryHook) {
h.verbose = true
}
}
type QueryHook struct {
verbose bool
}
var _ bun.QueryHook = (*QueryHook)(nil)
func NewQueryHook(opts ...ConfigOption) *QueryHook {
h := new(QueryHook)
for _, opt := range opts {
opt(h)
}
return h
}
func (h *QueryHook) BeforeQuery(
ctx context.Context, event *bun.QueryEvent,
) context.Context {
return ctx
}
func (h *QueryHook) AfterQuery(ctx context.Context, event *bun.QueryEvent) {
if !h.verbose {
switch event.Err {
case nil, sql.ErrNoRows:
return
}
}
now := time.Now()
dur := now.Sub(event.StartTime)
args := []interface{}{
"[bun]",
now.Format(" 15:04:05.000 "),
formatOperation(event),
fmt.Sprintf(" %10s ", dur.Round(time.Microsecond)),
event.Query,
}
if event.Err != nil {
typ := reflect.TypeOf(event.Err).String()
args = append(args,
"\t",
color.New(color.BgRed).Sprintf(" %s ", typ+": "+event.Err.Error()),
)
}
fmt.Println(args...)
}
func formatOperation(event *bun.QueryEvent) string {
operation := event.Operation()
return operationColor(operation).Sprintf(" %-16s ", operation)
}
func eventOperation(event *bun.QueryEvent) string {
if event.QueryAppender != nil {
return event.QueryAppender.Operation()
}
return queryOperation(event.Query)
}
func queryOperation(name string) string {
if idx := strings.IndexByte(name, ' '); idx > 0 {
name = name[:idx]
}
if len(name) > 16 {
name = name[:16]
}
return name
}
func operationColor(operation string) *color.Color {
switch operation {
case "SELECT":
return color.New(color.BgGreen, color.FgHiWhite)
case "INSERT":
return color.New(color.BgBlue, color.FgHiWhite)
case "UPDATE":
return color.New(color.BgYellow, color.FgHiBlack)
case "DELETE":
return color.New(color.BgMagenta, color.FgHiWhite)
default:
return color.New(color.BgWhite, color.FgHiBlack)
}
}