reorganized sqlLogReplacements

This commit is contained in:
Gani Georgiev 2025-03-15 13:28:39 +02:00
parent 613af90fac
commit 44007a7c8c
1 changed files with 13 additions and 14 deletions

View File

@ -1139,26 +1139,25 @@ func (app *BaseApp) initDataDB() error {
return nil
}
var sqlLogReplacements = map[*regexp.Regexp]string{
regexp.MustCompile(`[^'"]\{\{`): "`",
regexp.MustCompile(`\}\}[^'"]`): "`",
regexp.MustCompile(`[^'"]\[\[`): "`",
regexp.MustCompile(`\]\][^'"]`): "`",
regexp.MustCompile(`<nil>`): "NULL",
var sqlLogReplacements = []struct {
pattern *regexp.Regexp
replacement string
}{
{regexp.MustCompile(`\[\[([^\[\]\{\}\.]+)\.([^\[\]\{\}\.]+)\]\]`), "`$1`.`$2`"},
{regexp.MustCompile(`\{\{([^\[\]\{\}\.]+)\.([^\[\]\{\}\.]+)\}\}`), "`$1`.`$2`"},
{regexp.MustCompile(`([^'"])\{\{`), "$1`"},
{regexp.MustCompile(`\}\}([^'"])`), "`$1"},
{regexp.MustCompile(`([^'"])\[\[`), "$1`"},
{regexp.MustCompile(`\]\]([^'"])`), "`$1"},
{regexp.MustCompile(`<nil>`), "NULL"},
}
var sqlLogPrefixedTableIdentifierPattern = regexp.MustCompile(`\[\[([^\[\]\{\}\.]+)\.([^\[\]\{\}\.]+)\]\]`)
var sqlLogPrefixedColumnIdentifierPattern = regexp.MustCompile(`\{\{([^\[\]\{\}\.]+)\.([^\[\]\{\}\.]+)\}\}`)
// normalizeSQLLog replaces common query builder charactes with their plain SQL version for easier debugging.
// The query is still not suitable for execution and should be used only for log and debug purposes
// (the normalization is done here to avoid breaking changes in dbx).
func normalizeSQLLog(sql string) string {
sql = sqlLogPrefixedTableIdentifierPattern.ReplaceAllString(sql, "`$1`.`$2`")
sql = sqlLogPrefixedColumnIdentifierPattern.ReplaceAllString(sql, "`$1`.`$2`")
for pattern, replacement := range sqlLogReplacements {
sql = pattern.ReplaceAllString(sql, replacement)
for _, item := range sqlLogReplacements {
sql = item.pattern.ReplaceAllString(sql, item.replacement)
}
return sql