go.charczuk.com@v0.0.0-20240327042549-bc490516bd1a/sdk/db/query_event.go (about) 1 /* 2 3 Copyright (c) 2023 - Present. Will Charczuk. All rights reserved. 4 Use of this source code is governed by a MIT license that can be found in the LICENSE file at the root of the repository. 5 6 */ 7 8 package db 9 10 import ( 11 "strconv" 12 "strings" 13 "time" 14 15 "go.charczuk.com/sdk/stringutil" 16 ) 17 18 // QueryEvent represents a database query. 19 type QueryEvent struct { 20 Database string `json:"database,omitempty"` 21 Engine string `json:"engine,omitempty"` 22 Username string `json:"username,omitempty"` 23 Label string `json:"label,omitempty"` 24 Body string `json:"body,omitempty"` 25 Elapsed time.Duration `json:"elapsed,omitempty"` 26 Err error `json:"err,omitempty"` 27 RowsAffected int64 `json:"rowsAffected,omitempty"` 28 } 29 30 // WriteText writes the event text to the output. 31 func (e QueryEvent) String() string { 32 wr := new(strings.Builder) 33 34 wr.WriteString("[") 35 if len(e.Engine) > 0 { 36 wr.WriteString(e.Engine + " ") 37 } 38 if len(e.Username) > 0 { 39 wr.WriteString(e.Username + "@") 40 } 41 wr.WriteString(e.Database + "]") 42 43 if len(e.Label) > 0 { 44 wr.WriteString(" [" + e.Label + "]") 45 } 46 47 if len(e.Body) > 0 { 48 wr.WriteString(" " + stringutil.CompressSpace(e.Body)) 49 } 50 51 wr.WriteString(" " + e.Elapsed.String()) 52 53 if e.RowsAffected > 0 { 54 wr.WriteString(" " + strconv.FormatInt(e.RowsAffected, 10) + " rows") 55 } 56 57 if e.Err != nil { 58 wr.WriteString(" failed") 59 } 60 return wr.String() 61 }