MCPcopy Create free account
hub / github.com/ajitpratap0/GoSQLX / afterStatement

Method afterStatement

integrations/gorm/plugin.go:86–133  ·  view source on GitHub ↗
(db *gorm.DB)

Source from the content-addressed store, hash-verified

84}
85
86func (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

Callers

nothing calls this directly

Calls 8

ParseWithDialectFunction · 0.92
ParseFunction · 0.92
ExtractTablesFunction · 0.92
ExtractColumnsFunction · 0.92
normalizeSQLForParsingFunction · 0.85
copyFunction · 0.85
stmtTypeNameFunction · 0.70
StringMethod · 0.45

Tested by

no test coverage detected