MCPcopy Create free account
hub / github.com/bytebase/bytebase / parseCTEPrefix

Function parseCTEPrefix

backend/plugin/parser/mysql/backup.go:302–321  ·  view source on GitHub ↗

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)

Source from the content-addressed store, hash-verified

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.
302func 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
323func extractStatementText(fullSQL string, loc ast.Loc) string {
324 end := loc.End

Callers 2

collectCTENamesFunction · 0.70
extractCTEFunction · 0.70

Calls 1

ParseMySQLOmniFunction · 0.85

Tested by

no test coverage detected