parseSelectBody parses a SQL string expected to be a single SELECT statement and returns the *ast.SelectStmt. Rejects empty input, multiple statements, and non-SELECT top-level nodes. Unwraps a RawStmt wrapper if ParsePg returns one.
(sql string)
| 179 | // statements, and non-SELECT top-level nodes. Unwraps a RawStmt wrapper if |
| 180 | // ParsePg returns one. |
| 181 | func parseSelectBody(sql string) (*ast.SelectStmt, error) { |
| 182 | stmts, err := ParsePg(sql) |
| 183 | if err != nil { |
| 184 | return nil, errors.Wrap(err, "parse") |
| 185 | } |
| 186 | if len(stmts) != 1 { |
| 187 | return nil, errors.Errorf("expected 1 statement, got %d", len(stmts)) |
| 188 | } |
| 189 | node := stmts[0].AST |
| 190 | if raw, ok := node.(*ast.RawStmt); ok { |
| 191 | node = raw.Stmt |
| 192 | } |
| 193 | sel, ok := node.(*ast.SelectStmt) |
| 194 | if !ok { |
| 195 | return nil, errors.Errorf("expected SelectStmt, got %T", node) |
| 196 | } |
| 197 | return sel, nil |
| 198 | } |
| 199 | |
| 200 | // parseFunctionSignatureArgTypes extracts the comma-separated argument types |
| 201 | // from a signature like "fn(integer, text)" or "fn(numeric(10,2), boolean)". |