Struct sql_parser::parser::Parser [−][src]
struct Parser<'a> {
sql: &'a str,
tokens: Vec<(Token, usize)>,
index: usize,
recursion_guard: RecursionGuard,
}Expand description
SQL Parser
Fields
sql: &'a strtokens: Vec<(Token, usize)>index: usizeThe index of the first unprocessed token in self.tokens
recursion_guard: RecursionGuardImplementations
Parse a single top-level statement (such as SELECT, INSERT, CREATE, etc.), stopping before the statement separator, if any.
Parse a new expression
Parse tokens until the precedence changes
fn parse_subexpr_seeded(
&mut self,
precedence: Precedence,
expr: Expr<Raw>
) -> Result<Expr<Raw>, ParserError>
Parse an expression prefix
Parses an expression that appears in parentheses, like (1 + 1) or
(SELECT 1). Assumes that the opening parenthesis has already been
parsed. Parses up to the closing parenthesis without consuming it.
Parse CURRENT ROW or { <positive number> | UNBOUNDED } { PRECEDING | FOLLOWING }
Parse a SQL CAST function e.g. CAST(expr AS FLOAT)
Parse a SQL EXISTS expression e.g. WHERE EXISTS(SELECT ...).
fn parse_homogenizing_function(
&mut self,
function: HomogenizingFunction
) -> Result<Expr<Raw>, ParserError>
Parse an INTERVAL literal.
Some syntactically valid intervals:
INTERVAL '1' DAYINTERVAL '1-1' YEAR TO MONTHINTERVAL '1' SECOND- `INTERVAL ‘1:1’ MINUTE TO SECOND
INTERVAL '1:1:1.1' HOUR TO SECOND (5)INTERVAL '1.111' SECOND (2)
fn parse_infix(
&mut self,
expr: Expr<Raw>,
precedence: Precedence
) -> Result<Expr<Raw>, ParserError>
fn parse_infix(
&mut self,
expr: Expr<Raw>,
precedence: Precedence
) -> Result<Expr<Raw>, ParserError>
Parse an operator following an expression
Parse subscript expression, i.e. either an index value or slice range.
Parse a possibly qualified, possibly quoted operator, e.g.
+, pg_catalog.+, or "pg_catalog".+
Parses the parens following the [ NOT ] IN operator
Parses BETWEEN <low> AND <high>, assuming the BETWEEN keyword was already consumed
Parse a postgresql casting style which is in the form of expr::datatype
Get the precedence of the next token
Return the first non-whitespace token that has not yet been processed (or None if reached end-of-file)
Return the nth token that has not yet been processed.
Return the next token that has not yet been processed, or None if reached end-of-file, and mark it as processed. OK to call repeatedly after reaching EOF.
Push back the last one non-whitespace token. Must be called after
next_token(), otherwise might panic. OK to call after
next_token() indicates an EOF.
Return the byte position within the query string at which the next token starts.
Return the byte position within the query string at which the previous token starts.
Must be called after next_token(), otherwise might panic.
OK to call after next_token() indicates an EOF.
Report unexpected token
Look for an expected keyword and consume it if it exists
Look for an expected sequence of keywords and consume them if they exist
fn parse_at_most_one_keyword(
&mut self,
keywords: &[Keyword],
location: &str
) -> Result<Option<Keyword>, ParserError>
Look for one of the given keywords and return the one that matches.
Bail out if the current token is not one of the expected keywords, or consume it if it is
Bail out if the current token is not an expected keyword, or consume it if it is
Bail out if the following tokens are not the expected sequence of keywords, or consume them if they are.
Consume the next token if it matches the expected token, otherwise return false
Bail out if the current token is not an expected keyword, or consume it if it is
fn parse_comma_separated<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError> where
F: FnMut(&mut Self) -> Result<T, ParserError>,
fn parse_comma_separated<T, F>(&mut self, f: F) -> Result<Vec<T>, ParserError> where
F: FnMut(&mut Self) -> Result<T, ParserError>,
Parse a comma-separated list of 1+ items accepted by F
fn maybe_parse<T, F>(&mut self, f: F) -> Option<T> where
F: FnMut(&mut Self) -> Result<T, ParserError>,
Parse a SQL CREATE statement
fn parse_source_columns(
&mut self
) -> Result<(Vec<Ident>, Option<KeyConstraint>), ParserError>
fn parse_source_columns(
&mut self
) -> Result<(Vec<Ident>, Option<KeyConstraint>), ParserError>
Parses the column section of a CREATE SOURCE statement which can be empty or a comma-separated list of column identifiers and a single key constraint, e.g.
(col_0, col_i, …, col_n, key_constraint)
Parses a key constraint.
fn parse_source_include_metadata(
&mut self
) -> Result<Vec<SourceIncludeMetadata>, ParserError>
fn parse_columns(
&mut self,
optional: IsOptional
) -> Result<(Vec<ColumnDef<Raw>>, Vec<TableConstraint<Raw>>), ParserError>
fn parse_optional_table_constraint(
&mut self
) -> Result<Option<TableConstraint<Raw>>, ParserError>
fn parse_with_options(
&mut self,
require_equals: bool
) -> Result<Vec<WithOption>, ParserError>
If require_equals is true, parse options of the form KEY = VALUE or just
KEY. If require_equals is false, additionally support KEY VALUE (but still the others).
Parse a copy statement
Parse a literal value (numbers, strings, date/time, booleans)
fn parse_sequence<F>(&mut self, f: F) -> Result<Vec<Expr<Raw>>, ParserError> where
F: FnMut(&mut Self) -> Result<Expr<Raw>, ParserError>,
Parse an unsigned literal integer/long
Parse a literal string
Parse a SQL datatype (in the context of a CREATE TABLE statement for example)
fn parse_optional_alias<F>(
&mut self,
is_reserved: F
) -> Result<Option<Ident>, ParserError> where
F: FnOnce(Keyword) -> bool,
fn parse_optional_alias<F>(
&mut self,
is_reserved: F
) -> Result<Option<Ident>, ParserError> where
F: FnOnce(Keyword) -> bool,
Parse AS identifier (or simply identifier if it’s not a reserved keyword)
Some examples with aliases: SELECT 1 foo, SELECT COUNT(*) AS cnt,
SELECT ... FROM t1 foo, t2 bar, SELECT ... FROM (...) AS bar
Parse AS identifier when the AS is describing a table-valued object,
like in ... FROM generate_series(1, 10) AS t (col). In this case
the alias is allowed to optionally name the columns in the table, in
addition to the table itself.
Parse a possibly qualified, possibly quoted identifier, e.g.
foo or myschema."table"
Parse a simple one-word identifier (possibly quoted, possibly a keyword)
fn parse_parenthesized_column_list(
&mut self,
optional: IsOptional
) -> Result<Vec<Ident>, ParserError>
fn parse_parenthesized_column_list(
&mut self,
optional: IsOptional
) -> Result<Vec<Ident>, ParserError>
Parse a parenthesized comma-separated list of unqualified, possibly quoted identifiers
Parse a query expression, i.e. a SELECT statement optionally
preceeded with some WITH CTE declarations and optionally followed
by ORDER BY. Unlike some other parse_… methods, this one doesn’t
expect the initial keyword to be already consumed
fn parse_query_tail(
&mut self,
ctes: Vec<Cte<Raw>>,
body: SetExpr<Raw>
) -> Result<Query<Raw>, ParserError>
Parse a CTE (alias [( col1, col2, ... )] AS (subquery))
fn parse_query_body(
&mut self,
precedence: SetPrecedence
) -> Result<SetExpr<Raw>, ParserError>
fn parse_query_body(
&mut self,
precedence: SetPrecedence
) -> Result<SetExpr<Raw>, ParserError>
Parse a “query body”, which is an expression with roughly the following grammar:
query_body ::= restricted_select | '(' subquery ')' | set_operation
restricted_select ::= 'SELECT' [expr_list] [ from ] [ where ] [ groupby_having ]
subquery ::= query_body [ order_by_limit ]
set_operation ::= query_body { 'UNION' | 'EXCEPT' | 'INTERSECT' } [ 'ALL' ] query_bodyfn parse_query_body_seeded(
&mut self,
precedence: SetPrecedence,
expr: SetExpr<Raw>
) -> Result<SetExpr<Raw>, ParserError>
Parse a restricted SELECT statement (no CTEs / UNION / ORDER BY),
assuming the initial SELECT was already consumed
fn parse_show_columns(
&mut self,
extended: bool,
full: bool
) -> Result<Statement<Raw>, ParserError>
fn parse_show_statement_filter(
&mut self
) -> Result<Option<ShowStatementFilter<Raw>>, ParserError>
A table name or a parenthesized subquery, followed by optional [AS] alias
fn parse_derived_table_factor(
&mut self,
lateral: IsLateral
) -> Result<TableFactor<Raw>, ParserError>
fn parse_join_constraint(
&mut self,
natural: bool
) -> Result<JoinConstraint<Raw>, ParserError>
Parse an INSERT statement
Parse a var = expr assignment, used in an UPDATE statement
fn parse_optional_args(
&mut self,
allow_order_by: bool
) -> Result<FunctionArgs<Raw>, ParserError>
Parse AS OF, if present.
Parse a comma-delimited list of projections after SELECT
Parse an expression, optionally followed by ASC or DESC (used in ORDER BY)
Parse an EXPLAIN statement, assuming that the EXPLAIN token
has already been consumed.
Parse a DECLARE statement, assuming that the DECLARE token
has already been consumed.
Parse a CLOSE statement, assuming that the CLOSE token
has already been consumed.
Parse a PREPARE statement, assuming that the PREPARE token
has already been consumed.
Parse a EXECUTE statement, assuming that the EXECUTE token
has already been consumed.
Parse a DEALLOCATE statement, assuming that the DEALLOCATE token
has already been consumed.
Parse a FETCH statement, assuming that the FETCH token
has already been consumed.
Trait Implementations
Extracts a reference to the recursion guard embedded within the type.
fn checked_recur<F, T, E>(&self, f: F) -> Result<T, E> where
F: FnOnce(&Self) -> Result<T, E>,
E: From<RecursionLimitError>,
fn checked_recur<F, T, E>(&self, f: F) -> Result<T, E> where
F: FnOnce(&Self) -> Result<T, E>,
E: From<RecursionLimitError>,
Checks whether it is safe to recur and calls f if so. Read more
fn checked_recur_mut<F, T, E>(&mut self, f: F) -> Result<T, E> where
F: FnOnce(&mut Self) -> Result<T, E>,
E: From<RecursionLimitError>,
fn checked_recur_mut<F, T, E>(&mut self, f: F) -> Result<T, E> where
F: FnOnce(&mut Self) -> Result<T, E>,
E: From<RecursionLimitError>,
Like CheckedRecursion::checked_recur, but operates on a mutable
reference to Self. Read more
Auto Trait Implementations
impl<'a> !RefUnwindSafe for Parser<'a>
impl<'a> UnwindSafe for Parser<'a>
Blanket Implementations
Mutably borrows from an owned value. Read more
Attaches the provided Subscriber to this type, returning a
WithDispatch wrapper. Read more
Attaches the current default Subscriber to this type, returning a
WithDispatch wrapper. Read more