(db *gorm.DB)
| 84 | } |
| 85 | |
| 86 | func (p *Plugin) afterStatement(db *gorm.DB) { |
| 87 | // Guard against nil Statement — this can happen during initialization callbacks. |
| 88 | if db.Statement == nil { |
| 89 | return |
| 90 | } |
| 91 | sql := db.Statement.SQL.String() |
| 92 | if sql == "" { |
| 93 | return |
| 94 | } |
| 95 | |
| 96 | rec := QueryRecord{SQL: sql} |
| 97 | |
| 98 | // Normalize GORM-generated SQL for GoSQLX compatibility: |
| 99 | // 1. Replace backtick-quoted identifiers with double-quoted identifiers |
| 100 | // (GORM SQLite/MySQL driver uses backticks; GoSQLX standard mode uses double-quotes). |
| 101 | // 2. Replace ? positional placeholders with $N (PostgreSQL style). |
| 102 | normalized := normalizeSQLForParsing(sql) |
| 103 | |
| 104 | // Try PostgreSQL dialect (handles double-quoted identifiers and $N placeholders), |
| 105 | // then fall back to standard SQL parsing. |
| 106 | tree, err := gosqlx.ParseWithDialect(normalized, keywords.DialectPostgreSQL) |
| 107 | if err != nil { |
| 108 | tree, err = gosqlx.Parse(normalized) |
| 109 | } |
| 110 | if err != nil { |
| 111 | rec.ParseOK = false |
| 112 | if p.onParseError != nil { |
| 113 | p.onParseError(sql, err) |
| 114 | } |
| 115 | } else { |
| 116 | rec.ParseOK = true |
| 117 | rec.Tables = gosqlx.ExtractTables(tree) |
| 118 | rec.Columns = gosqlx.ExtractColumns(tree) |
| 119 | if tree != nil && len(tree.Statements) > 0 { |
| 120 | rec.Type = stmtTypeName(tree.Statements[0]) |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | p.mu.Lock() |
| 125 | p.queries = append(p.queries, rec) |
| 126 | if len(p.queries) > p.maxHistory { |
| 127 | // Trim oldest entries to stay within the limit. |
| 128 | excess := len(p.queries) - p.maxHistory |
| 129 | copy(p.queries, p.queries[excess:]) |
| 130 | p.queries = p.queries[:p.maxHistory] |
| 131 | } |
| 132 | p.mu.Unlock() |
| 133 | } |
| 134 | |
| 135 | // normalizeSQLForParsing converts GORM-generated SQL into a form that GoSQLX |
| 136 | // can parse: backtick identifiers become double-quoted, and ? placeholders |
nothing calls this directly
no test coverage detected