parseCTEPrefix parses the SQL text before stmtLoc as a WITH clause. Returns the CTE list if the prefix contains a valid WITH clause, nil otherwise.
(fullSQL string, stmtLoc ast.Loc)
| 300 | // parseCTEPrefix parses the SQL text before stmtLoc as a WITH clause. |
| 301 | // Returns the CTE list if the prefix contains a valid WITH clause, nil otherwise. |
| 302 | func parseCTEPrefix(fullSQL string, stmtLoc ast.Loc) []*ast.CommonTableExpr { |
| 303 | if stmtLoc.Start <= 0 { |
| 304 | return nil |
| 305 | } |
| 306 | prefix := strings.TrimSpace(fullSQL[:stmtLoc.Start]) |
| 307 | if prefix == "" { |
| 308 | return nil |
| 309 | } |
| 310 | // Wrap the prefix with a dummy SELECT to make it parseable. |
| 311 | parsed, err := ParseMySQLOmni(prefix + " SELECT 1") |
| 312 | if err != nil || parsed == nil { |
| 313 | return nil |
| 314 | } |
| 315 | for _, item := range parsed.Items { |
| 316 | if sel, ok := item.(*ast.SelectStmt); ok && len(sel.CTEs) > 0 { |
| 317 | return sel.CTEs |
| 318 | } |
| 319 | } |
| 320 | return nil |
| 321 | } |
| 322 | |
| 323 | func extractStatementText(fullSQL string, loc ast.Loc) string { |
| 324 | end := loc.End |
no test coverage detected