github.com/blend/go-sdk@v1.20220411.3/db/query_start_event.go (about) 1 /* 2 3 Copyright (c) 2022 - Present. Blend Labs, Inc. All rights reserved 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file. 5 6 */ 7 8 package db 9 10 import ( 11 "context" 12 "fmt" 13 "io" 14 15 "github.com/blend/go-sdk/ansi" 16 "github.com/blend/go-sdk/logger" 17 "github.com/blend/go-sdk/stringutil" 18 ) 19 20 // Logger flags 21 const ( 22 QueryStartFlag = "db.query.start" 23 ) 24 25 // these are compile time assertions 26 var ( 27 _ logger.Event = (*QueryEvent)(nil) 28 _ logger.TextWritable = (*QueryEvent)(nil) 29 _ logger.JSONWritable = (*QueryEvent)(nil) 30 ) 31 32 // these are compile time assertions 33 var ( 34 _ logger.Event = (*QueryEvent)(nil) 35 _ logger.TextWritable = (*QueryEvent)(nil) 36 _ logger.JSONWritable = (*QueryEvent)(nil) 37 ) 38 39 // NewQueryStartEvent creates a new query start event. 40 func NewQueryStartEvent(body string, options ...QueryStartEventOption) QueryStartEvent { 41 qse := QueryStartEvent{ 42 Body: body, 43 } 44 for _, opt := range options { 45 opt(&qse) 46 } 47 return qse 48 } 49 50 // NewQueryStartEventListener returns a new listener for query events. 51 func NewQueryStartEventListener(listener func(context.Context, QueryStartEvent)) logger.Listener { 52 return func(ctx context.Context, e logger.Event) { 53 if typed, isTyped := e.(QueryStartEvent); isTyped { 54 listener(ctx, typed) 55 } 56 } 57 } 58 59 // NewQueryStartEventFilter returns a new query event filter. 60 func NewQueryStartEventFilter(filter func(context.Context, QueryStartEvent) (QueryStartEvent, bool)) logger.Filter { 61 return func(ctx context.Context, e logger.Event) (logger.Event, bool) { 62 if typed, isTyped := e.(QueryStartEvent); isTyped { 63 return filter(ctx, typed) 64 } 65 return e, false 66 } 67 } 68 69 // QueryStartEventOption mutates a query start event. 70 type QueryStartEventOption func(*QueryStartEvent) 71 72 // OptQueryStartEventBody sets a field on the query event. 73 func OptQueryStartEventBody(value string) QueryStartEventOption { 74 return func(e *QueryStartEvent) { e.Body = value } 75 } 76 77 // OptQueryStartEventDatabase sets a field on the query event. 78 func OptQueryStartEventDatabase(value string) QueryStartEventOption { 79 return func(e *QueryStartEvent) { e.Database = value } 80 } 81 82 // OptQueryStartEventEngine sets a field on the query event. 83 func OptQueryStartEventEngine(value string) QueryStartEventOption { 84 return func(e *QueryStartEvent) { e.Engine = value } 85 } 86 87 // OptQueryStartEventUsername sets a field on the query event. 88 func OptQueryStartEventUsername(value string) QueryStartEventOption { 89 return func(e *QueryStartEvent) { e.Username = value } 90 } 91 92 // OptQueryStartEventLabel sets a field on the query event. 93 func OptQueryStartEventLabel(label string) QueryStartEventOption { 94 return func(e *QueryStartEvent) { e.Label = label } 95 } 96 97 // QueryStartEvent represents the start of a database query. 98 type QueryStartEvent struct { 99 Database string 100 Engine string 101 Username string 102 Label string 103 Body string 104 } 105 106 // GetFlag implements Event. 107 func (e QueryStartEvent) GetFlag() string { return QueryStartFlag } 108 109 // WriteText writes the event text to the output. 110 func (e QueryStartEvent) WriteText(tf logger.TextFormatter, wr io.Writer) { 111 fmt.Fprint(wr, "[") 112 if len(e.Engine) > 0 { 113 fmt.Fprint(wr, tf.Colorize(e.Engine, ansi.ColorLightWhite)) 114 fmt.Fprint(wr, logger.Space) 115 } 116 if len(e.Username) > 0 { 117 fmt.Fprint(wr, tf.Colorize(e.Username, ansi.ColorLightWhite)) 118 fmt.Fprint(wr, "@") 119 } 120 fmt.Fprint(wr, tf.Colorize(e.Database, ansi.ColorLightWhite)) 121 fmt.Fprint(wr, "]") 122 123 if len(e.Label) > 0 { 124 fmt.Fprint(wr, logger.Space) 125 fmt.Fprintf(wr, "[%s]", tf.Colorize(e.Label, ansi.ColorLightWhite)) 126 } 127 128 if len(e.Body) > 0 { 129 fmt.Fprint(wr, logger.Space) 130 fmt.Fprint(wr, stringutil.CompressSpace(e.Body)) 131 } 132 } 133 134 // Decompose implements JSONWritable. 135 func (e QueryStartEvent) Decompose() map[string]interface{} { 136 return map[string]interface{}{ 137 "engine": e.Engine, 138 "database": e.Database, 139 "username": e.Username, 140 "label": e.Label, 141 "body": e.Body, 142 } 143 }