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 return nil
} }
var sqlLogReplacements = map[*regexp.Regexp]string{ var sqlLogReplacements = []struct {
regexp.MustCompile(`[^'"]\{\{`): "`", pattern *regexp.Regexp
regexp.MustCompile(`\}\}[^'"]`): "`", replacement string
regexp.MustCompile(`[^'"]\[\[`): "`", }{
regexp.MustCompile(`\]\][^'"]`): "`", {regexp.MustCompile(`\[\[([^\[\]\{\}\.]+)\.([^\[\]\{\}\.]+)\]\]`), "`$1`.`$2`"},
regexp.MustCompile(`<nil>`): "NULL", {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. // 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 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). // (the normalization is done here to avoid breaking changes in dbx).
func normalizeSQLLog(sql string) string { func normalizeSQLLog(sql string) string {
sql = sqlLogPrefixedTableIdentifierPattern.ReplaceAllString(sql, "`$1`.`$2`") for _, item := range sqlLogReplacements {
sql = item.pattern.ReplaceAllString(sql, item.replacement)
sql = sqlLogPrefixedColumnIdentifierPattern.ReplaceAllString(sql, "`$1`.`$2`")
for pattern, replacement := range sqlLogReplacements {
sql = pattern.ReplaceAllString(sql, replacement)
} }
return sql return sql