mirror of
https://github.com/jackc/pgx.git
synced 2025-05-31 11:42:24 +00:00
Improve logging some more
This commit is contained in:
parent
ad88123f9c
commit
b982aeb102
92
conn.go
92
conn.go
@ -338,14 +338,23 @@ func (c *Conn) selectFunc(sql string, onDataRow func(*DataRowReader) error, argu
|
|||||||
// sql can be either a prepared statement name or an SQL string. arguments will be
|
// sql can be either a prepared statement name or an SQL string. arguments will be
|
||||||
// sanitized before being interpolated into sql strings. arguments should be referenced
|
// sanitized before being interpolated into sql strings. arguments should be referenced
|
||||||
// positionally from the sql string as $1, $2, etc.
|
// positionally from the sql string as $1, $2, etc.
|
||||||
func (c *Conn) SelectRows(sql string, arguments ...interface{}) (rows []map[string]interface{}, err error) {
|
func (c *Conn) SelectRows(sql string, arguments ...interface{}) ([]map[string]interface{}, error) {
|
||||||
rows = make([]map[string]interface{}, 0, 8)
|
startTime := time.Now()
|
||||||
|
|
||||||
|
rows := make([]map[string]interface{}, 0, 8)
|
||||||
onDataRow := func(r *DataRowReader) error {
|
onDataRow := func(r *DataRowReader) error {
|
||||||
rows = append(rows, c.rxDataRow(r))
|
rows = append(rows, c.rxDataRow(r))
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = c.SelectFunc(sql, onDataRow, arguments...)
|
err := c.selectFunc(sql, onDataRow, arguments...)
|
||||||
return
|
if err != nil {
|
||||||
|
c.logger.Error("SelectRows", "sql", sql, "args", arguments, "error", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
endTime := time.Now()
|
||||||
|
c.logger.Info("SelectRows", "sql", sql, "args", arguments, "rowsFound", len(rows), "time", endTime.Sub(startTime))
|
||||||
|
return rows, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectRow executes sql and returns a map representing the found row.
|
// SelectRow executes sql and returns a map representing the found row.
|
||||||
@ -354,19 +363,31 @@ func (c *Conn) SelectRows(sql string, arguments ...interface{}) (rows []map[stri
|
|||||||
// positionally from the sql string as $1, $2, etc.
|
// positionally from the sql string as $1, $2, etc.
|
||||||
//
|
//
|
||||||
// Returns a NotSingleRowError if exactly one row is not found
|
// Returns a NotSingleRowError if exactly one row is not found
|
||||||
func (c *Conn) SelectRow(sql string, arguments ...interface{}) (row map[string]interface{}, err error) {
|
func (c *Conn) SelectRow(sql string, arguments ...interface{}) (map[string]interface{}, error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
var numRowsFound int64
|
var numRowsFound int64
|
||||||
|
var row map[string]interface{}
|
||||||
|
|
||||||
onDataRow := func(r *DataRowReader) error {
|
onDataRow := func(r *DataRowReader) error {
|
||||||
numRowsFound++
|
numRowsFound++
|
||||||
row = c.rxDataRow(r)
|
row = c.rxDataRow(r)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = c.SelectFunc(sql, onDataRow, arguments...)
|
err := c.selectFunc(sql, onDataRow, arguments...)
|
||||||
if err == nil && numRowsFound != 1 {
|
if err != nil {
|
||||||
err = NotSingleRowError{RowCount: numRowsFound}
|
c.logger.Error("SelectRow", "sql", sql, "args", arguments, "error", err)
|
||||||
|
return nil, err
|
||||||
}
|
}
|
||||||
return
|
if numRowsFound != 1 {
|
||||||
|
err = NotSingleRowError{RowCount: numRowsFound}
|
||||||
|
row = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
endTime := time.Now()
|
||||||
|
c.logger.Info("SelectRow", "sql", sql, "args", arguments, "rowsFound", numRowsFound, "time", endTime.Sub(startTime))
|
||||||
|
|
||||||
|
return row, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectValue executes sql and returns a single value. sql can be either a prepared
|
// SelectValue executes sql and returns a single value. sql can be either a prepared
|
||||||
@ -376,8 +397,11 @@ func (c *Conn) SelectRow(sql string, arguments ...interface{}) (row map[string]i
|
|||||||
//
|
//
|
||||||
// Returns a UnexpectedColumnCountError if exactly one column is not found
|
// Returns a UnexpectedColumnCountError if exactly one column is not found
|
||||||
// Returns a NotSingleRowError if exactly one row is not found
|
// Returns a NotSingleRowError if exactly one row is not found
|
||||||
func (c *Conn) SelectValue(sql string, arguments ...interface{}) (v interface{}, err error) {
|
func (c *Conn) SelectValue(sql string, arguments ...interface{}) (interface{}, error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
var numRowsFound int64
|
var numRowsFound int64
|
||||||
|
var v interface{}
|
||||||
|
|
||||||
onDataRow := func(r *DataRowReader) error {
|
onDataRow := func(r *DataRowReader) error {
|
||||||
if len(r.fields) != 1 {
|
if len(r.fields) != 1 {
|
||||||
@ -388,13 +412,20 @@ func (c *Conn) SelectValue(sql string, arguments ...interface{}) (v interface{},
|
|||||||
v = r.ReadValue()
|
v = r.ReadValue()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = c.SelectFunc(sql, onDataRow, arguments...)
|
err := c.selectFunc(sql, onDataRow, arguments...)
|
||||||
if err == nil {
|
if err != nil {
|
||||||
if numRowsFound != 1 {
|
c.logger.Error("SelectValue", "sql", sql, "args", arguments, "error", err)
|
||||||
err = NotSingleRowError{RowCount: numRowsFound}
|
return nil, err
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return
|
if numRowsFound != 1 {
|
||||||
|
err = NotSingleRowError{RowCount: numRowsFound}
|
||||||
|
v = nil
|
||||||
|
}
|
||||||
|
|
||||||
|
endTime := time.Now()
|
||||||
|
c.logger.Info("SelectValue", "sql", sql, "args", arguments, "rowsFound", numRowsFound, "time", endTime.Sub(startTime))
|
||||||
|
|
||||||
|
return v, err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SelectValueTo executes sql that returns a single value and writes that value to w.
|
// SelectValueTo executes sql that returns a single value and writes that value to w.
|
||||||
@ -408,14 +439,20 @@ func (c *Conn) SelectValue(sql string, arguments ...interface{}) (v interface{},
|
|||||||
// Returns a UnexpectedColumnCountError if exactly one column is not found
|
// Returns a UnexpectedColumnCountError if exactly one column is not found
|
||||||
// Returns a NotSingleRowError if exactly one row is not found
|
// Returns a NotSingleRowError if exactly one row is not found
|
||||||
func (c *Conn) SelectValueTo(w io.Writer, sql string, arguments ...interface{}) (err error) {
|
func (c *Conn) SelectValueTo(w io.Writer, sql string, arguments ...interface{}) (err error) {
|
||||||
|
startTime := time.Now()
|
||||||
|
|
||||||
defer func() {
|
defer func() {
|
||||||
if err != nil {
|
if err == nil {
|
||||||
|
endTime := time.Now()
|
||||||
|
c.logger.Info("SelectValueTo", "sql", sql, "args", arguments, "time", endTime.Sub(startTime))
|
||||||
|
} else {
|
||||||
c.logger.Error(fmt.Sprintf("SelectValueTo `%s` with %v failed: %v", sql, arguments, err))
|
c.logger.Error(fmt.Sprintf("SelectValueTo `%s` with %v failed: %v", sql, arguments, err))
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
if err = c.sendQuery(sql, arguments...); err != nil {
|
err = c.sendQuery(sql, arguments...)
|
||||||
return
|
if err != nil {
|
||||||
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
var numRowsFound int64
|
var numRowsFound int64
|
||||||
@ -506,8 +543,10 @@ func (c *Conn) rxDataRowValueTo(w io.Writer, bodySize int32) (err error) {
|
|||||||
// the sql string as $1, $2, etc.
|
// the sql string as $1, $2, etc.
|
||||||
//
|
//
|
||||||
// Returns a UnexpectedColumnCountError if exactly one column is not found
|
// Returns a UnexpectedColumnCountError if exactly one column is not found
|
||||||
func (c *Conn) SelectValues(sql string, arguments ...interface{}) (values []interface{}, err error) {
|
func (c *Conn) SelectValues(sql string, arguments ...interface{}) ([]interface{}, error) {
|
||||||
values = make([]interface{}, 0, 8)
|
startTime := time.Now()
|
||||||
|
|
||||||
|
values := make([]interface{}, 0, 8)
|
||||||
onDataRow := func(r *DataRowReader) error {
|
onDataRow := func(r *DataRowReader) error {
|
||||||
if len(r.fields) != 1 {
|
if len(r.fields) != 1 {
|
||||||
return UnexpectedColumnCountError{ExpectedCount: 1, ActualCount: int16(len(r.fields))}
|
return UnexpectedColumnCountError{ExpectedCount: 1, ActualCount: int16(len(r.fields))}
|
||||||
@ -516,8 +555,15 @@ func (c *Conn) SelectValues(sql string, arguments ...interface{}) (values []inte
|
|||||||
values = append(values, r.ReadValue())
|
values = append(values, r.ReadValue())
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
err = c.SelectFunc(sql, onDataRow, arguments...)
|
err := c.selectFunc(sql, onDataRow, arguments...)
|
||||||
return
|
if err != nil {
|
||||||
|
c.logger.Error("SelectValues", "sql", sql, "args", arguments, "error", err)
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
endTime := time.Now()
|
||||||
|
c.logger.Info("SelectValues", "sql", sql, "args", arguments, "valuesFound", len(values), "time", endTime.Sub(startTime))
|
||||||
|
return values, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Prepare creates a prepared statement with name and sql. sql can contain placeholders
|
// Prepare creates a prepared statement with name and sql. sql can contain placeholders
|
||||||
|
Loading…
x
Reference in New Issue
Block a user