Wrapper around the generated SQL parser. Translates output to the expected class and exceptions to an ParseException.
| 28 | * the expected class and exceptions to an ParseException. |
| 29 | */ |
| 30 | public class Parser { |
| 31 | /** |
| 32 | * Parse the statement using default options. Used for testing and for |
| 33 | * parsing internally-generated statements. See the full version for details. |
| 34 | */ |
| 35 | public static StatementBase parse(String query) throws AnalysisException { |
| 36 | return parse(query, new TQueryOptions()); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Run the parser with the given input and options, returning an |
| 41 | * abstract syntax tree (AST) for the statement, ready for semantic analysis. |
| 42 | * |
| 43 | * @param query the query: a valid SQL statement |
| 44 | * @param options query options |
| 45 | * @return the AST for the statement |
| 46 | * @throws AnalysisException for errors which are of two kinds: |
| 47 | * {@link ParseException} for syntactic errors, or the generic |
| 48 | * AnalysisException for semantic errors (such as overflow of a |
| 49 | * numeric literal) |
| 50 | */ |
| 51 | public static StatementBase parse(String query, TQueryOptions options) |
| 52 | throws AnalysisException { |
| 53 | SqlScanner input = new SqlScanner(new StringReader(query)); |
| 54 | SqlParser parser = new SqlParser(input); |
| 55 | parser.setQueryOptions(options); |
| 56 | try { |
| 57 | return (StatementBase) parser.parse().value; |
| 58 | } catch (AnalysisException e) { |
| 59 | // Pass along any analysis exceptions as they are. Since AnalysisException |
| 60 | // is a kind of Exception, we have to special-case it here |
| 61 | throw e; |
| 62 | } catch (Exception e) { |
| 63 | // Annoyingly, the parser uses a generic Exception to report |
| 64 | // syntax errors. Translate it to something more helpful. |
| 65 | throw new ParseException(parser.getErrorMsg(query), e); |
| 66 | } |
| 67 | } |
| 68 | } |
no outgoing calls