Skip to content

Commit

Permalink
Improve SQL QUERY logging
Browse files Browse the repository at this point in the history
[]BYTEA was panicing for nil or empty array, which then gets trapped and
ignored... making it very difficult to figure out which query the error
is coming from.

While fixing that bug, updated formating of []BYTEA and TEXT to be
closer an actual SQL query you can run by cutting and pasting from the log
  • Loading branch information
reductionista committed Feb 13, 2024
1 parent f3b6b22 commit e8fb883
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 9 deletions.
17 changes: 14 additions & 3 deletions core/services/pg/q.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,14 +296,25 @@ func sprintQ(query string, args []interface{}) string {
case common.Hash:
pairs = append(pairs, fmt.Sprintf("$%d", i+1), fmt.Sprintf("'\\x%x'", v.Bytes()))
case pq.ByteaArray:
pairs = append(pairs, fmt.Sprintf("$%d", i+1))
if v == nil {
pairs = append(pairs, "NULL")
continue
}
if len(v) == 0 {
pairs = append(pairs, "ARRAY[]")
continue
}
var s strings.Builder
fmt.Fprintf(&s, "('\\x%x'", v[0])
fmt.Fprintf(&s, "ARRAY['\\x%x'", v[0])
for j := 1; j < len(v); j++ {
fmt.Fprintf(&s, ",'\\x%x'", v[j])
}
pairs = append(pairs, fmt.Sprintf("$%d", i+1), fmt.Sprintf("%s)", s.String()))
pairs = append(pairs, fmt.Sprintf("%s]", s.String()))
case string:
pairs = append(pairs, fmt.Sprintf("$%d", i+1), fmt.Sprintf("'%s'", v))
default:
pairs = append(pairs, fmt.Sprintf("$%d", i+1), fmt.Sprintf("%v", arg))
pairs = append(pairs, fmt.Sprintf("$%d", i+1), fmt.Sprintf("%v", v))
}
}
replacer := strings.NewReplacer(pairs...)
Expand Down
12 changes: 6 additions & 6 deletions core/services/pg/q_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,27 +21,27 @@ func Test_sprintQ(t *testing.T) {
{"one",
"SELECT $1 FROM table;",
[]interface{}{"foo"},
"SELECT foo FROM table;"},
"SELECT 'foo' FROM table;"},
{"two",
"SELECT $1 FROM table WHERE bar = $2;",
[]interface{}{"foo", 1},
"SELECT foo FROM table WHERE bar = 1;"},
"SELECT 'foo' FROM table WHERE bar = 1;"},
{"limit",
"SELECT $1 FROM table LIMIT $2;",
[]interface{}{"foo", Limit(10)},
"SELECT foo FROM table LIMIT 10;"},
"SELECT 'foo' FROM table LIMIT 10;"},
{"limit-all",
"SELECT $1 FROM table LIMIT $2;",
[]interface{}{"foo", Limit(-1)},
"SELECT foo FROM table LIMIT NULL;"},
"SELECT 'foo' FROM table LIMIT NULL;"},
{"bytea",
"SELECT $1 FROM table WHERE b = $2;",
[]interface{}{"foo", []byte{0x0a}},
"SELECT foo FROM table WHERE b = '\\x0a';"},
"SELECT 'foo' FROM table WHERE b = '\\x0a';"},
{"bytea[]",
"SELECT $1 FROM table WHERE b = $2;",
[]interface{}{"foo", pq.ByteaArray([][]byte{{0xa}, {0xb}})},
"SELECT foo FROM table WHERE b = ('\\x0a','\\x0b');"},
"SELECT 'foo' FROM table WHERE b = ARRAY['\\x0a','\\x0b'];"},
} {
t.Run(tt.name, func(t *testing.T) {
got := sprintQ(tt.query, tt.args)
Expand Down

0 comments on commit e8fb883

Please sign in to comment.