| 1 | #include "postgres_parser.hpp" |
|---|---|
| 2 | |
| 3 | #include "pg_functions.hpp" |
| 4 | #include "parser/parser.hpp" |
| 5 | #include "parser/scansup.hpp" |
| 6 | #include "common/keywords.hpp" |
| 7 | |
| 8 | namespace duckdb { |
| 9 | |
| 10 | PostgresParser::PostgresParser() : success(false), parse_tree(nullptr), error_message(""), error_location(0) {} |
| 11 | |
| 12 | void PostgresParser::Parse(const std::string &query) { |
| 13 | duckdb_libpgquery::pg_parser_init(); |
| 14 | duckdb_libpgquery::parse_result res; |
| 15 | pg_parser_parse(query: query.c_str(), res: &res); |
| 16 | success = res.success; |
| 17 | |
| 18 | if (success) { |
| 19 | parse_tree = res.parse_tree; |
| 20 | } else { |
| 21 | error_message = std::string(res.error_message); |
| 22 | error_location = res.error_location; |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | vector<duckdb_libpgquery::PGSimplifiedToken> PostgresParser::Tokenize(const std::string &query) { |
| 27 | duckdb_libpgquery::pg_parser_init(); |
| 28 | auto tokens = duckdb_libpgquery::tokenize(str: query.c_str()); |
| 29 | duckdb_libpgquery::pg_parser_cleanup(); |
| 30 | return std::move(tokens); |
| 31 | } |
| 32 | |
| 33 | PostgresParser::~PostgresParser() { |
| 34 | duckdb_libpgquery::pg_parser_cleanup(); |
| 35 | } |
| 36 | |
| 37 | bool PostgresParser::IsKeyword(const std::string &text) { |
| 38 | return duckdb_libpgquery::is_keyword(str: text.c_str()); |
| 39 | } |
| 40 | |
| 41 | vector<duckdb_libpgquery::PGKeyword> PostgresParser::KeywordList() { |
| 42 | // FIXME: because of this, we might need to change the libpg_query library to use duckdb::vector |
| 43 | return std::forward<vector<duckdb_libpgquery::PGKeyword> >(t: duckdb_libpgquery::keyword_list()); |
| 44 | } |
| 45 | |
| 46 | void PostgresParser::SetPreserveIdentifierCase(bool preserve) { |
| 47 | duckdb_libpgquery::set_preserve_identifier_case(preserve); |
| 48 | } |
| 49 | |
| 50 | } |
| 51 |