1//===----------------------------------------------------------------------===//
2// DuckDB
3//
4// duckdb/parser/parser.hpp
5//
6//
7//===----------------------------------------------------------------------===//
8
9#pragma once
10
11#include "duckdb/parser/sql_statement.hpp"
12#include "duckdb/parser/parsed_expression.hpp"
13#include "duckdb/parser/query_node.hpp"
14#include "duckdb/parser/column_list.hpp"
15#include "duckdb/parser/simplified_token.hpp"
16#include "duckdb/parser/parser_options.hpp"
17
18namespace duckdb_libpgquery {
19struct PGNode;
20struct PGList;
21} // namespace duckdb_libpgquery
22
23namespace duckdb {
24
25//! The parser is responsible for parsing the query and converting it into a set
26//! of parsed statements. The parsed statements can then be converted into a
27//! plan and executed.
28class Parser {
29public:
30 Parser(ParserOptions options = ParserOptions());
31
32 //! The parsed SQL statements from an invocation to ParseQuery.
33 vector<unique_ptr<SQLStatement>> statements;
34
35public:
36 //! Attempts to parse a query into a series of SQL statements. Returns
37 //! whether or not the parsing was successful. If the parsing was
38 //! successful, the parsed statements will be stored in the statements
39 //! variable.
40 void ParseQuery(const string &query);
41
42 //! Tokenize a query, returning the raw tokens together with their locations
43 static vector<SimplifiedToken> Tokenize(const string &query);
44
45 //! Returns true if the given text matches a keyword of the parser
46 static bool IsKeyword(const string &text);
47 //! Returns a list of all keywords in the parser
48 static vector<ParserKeyword> KeywordList();
49
50 //! Parses a list of expressions (i.e. the list found in a SELECT clause)
51 DUCKDB_API static vector<unique_ptr<ParsedExpression>> ParseExpressionList(const string &select_list,
52 ParserOptions options = ParserOptions());
53 //! Parses a list as found in an ORDER BY expression (i.e. including optional ASCENDING/DESCENDING modifiers)
54 static vector<OrderByNode> ParseOrderList(const string &select_list, ParserOptions options = ParserOptions());
55 //! Parses an update list (i.e. the list found in the SET clause of an UPDATE statement)
56 static void ParseUpdateList(const string &update_list, vector<string> &update_columns,
57 vector<unique_ptr<ParsedExpression>> &expressions,
58 ParserOptions options = ParserOptions());
59 //! Parses a VALUES list (i.e. the list of expressions after a VALUES clause)
60 static vector<vector<unique_ptr<ParsedExpression>>> ParseValuesList(const string &value_list,
61 ParserOptions options = ParserOptions());
62 //! Parses a column list (i.e. as found in a CREATE TABLE statement)
63 static ColumnList ParseColumnList(const string &column_list, ParserOptions options = ParserOptions());
64
65 static bool StripUnicodeSpaces(const string &query_str, string &new_query);
66
67private:
68 ParserOptions options;
69};
70} // namespace duckdb
71