summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Flynn <trflynn89@pm.me>2021-04-23 12:39:44 -0400
committerAndreas Kling <kling@serenityos.org>2021-04-23 22:36:07 +0200
commit27685bc7993425e2ca02cf9497b648b9d9a2a453 (patch)
tree7f7c104449f9eb135cbc4d9113db604698911724
parent418884ab64e7dabe539b629ec284529798309a43 (diff)
downloadserenity-27685bc7993425e2ca02cf9497b648b9d9a2a453.zip
LibSQL: Add Parser::parse_schema_and_table_name helper
Another common semantic is parsing an identifier of the form "schema_name.table_name" / "table_name". Add a helper to do this work. This helper does not parse any optional alias after the table name. some syntaxes specify an alias using the AS keyword, some let the AS keyword be optional, and others just parse it as an identifier. So callers to this helper will just continue parsing the alias however they require.
-rw-r--r--Userland/Libraries/LibSQL/Parser.cpp57
-rw-r--r--Userland/Libraries/LibSQL/Parser.h1
2 files changed, 18 insertions, 40 deletions
diff --git a/Userland/Libraries/LibSQL/Parser.cpp b/Userland/Libraries/LibSQL/Parser.cpp
index 7752a62e6a..4e8e813eb6 100644
--- a/Userland/Libraries/LibSQL/Parser.cpp
+++ b/Userland/Libraries/LibSQL/Parser.cpp
@@ -72,16 +72,9 @@ NonnullRefPtr<CreateTable> Parser::parse_create_table_statement()
is_error_if_table_exists = false;
}
- String schema_or_table_name = consume(TokenType::Identifier).value();
String schema_name;
String table_name;
-
- if (consume_if(TokenType::Period)) {
- schema_name = move(schema_or_table_name);
- table_name = consume(TokenType::Identifier).value();
- } else {
- table_name = move(schema_or_table_name);
- }
+ parse_schema_and_table_name(schema_name, table_name);
// FIXME: Parse "AS select-stmt".
@@ -107,16 +100,9 @@ NonnullRefPtr<DropTable> Parser::parse_drop_table_statement()
is_error_if_table_does_not_exist = false;
}
- String schema_or_table_name = consume(TokenType::Identifier).value();
String schema_name;
String table_name;
-
- if (consume_if(TokenType::Period)) {
- schema_name = move(schema_or_table_name);
- table_name = consume(TokenType::Identifier).value();
- } else {
- table_name = move(schema_or_table_name);
- }
+ parse_schema_and_table_name(schema_name, table_name);
consume(TokenType::SemiColon);
@@ -633,16 +619,9 @@ Optional<NonnullRefPtr<Expression>> Parser::parse_in_expression(NonnullRefPtr<Ex
return create_ast_node<InChainedExpression>(move(expression), move(chain), invert_expression);
}
- String schema_or_table_name = consume(TokenType::Identifier).value();
String schema_name;
String table_name;
-
- if (consume_if(TokenType::Period)) {
- schema_name = move(schema_or_table_name);
- table_name = consume(TokenType::Identifier).value();
- } else {
- table_name = move(schema_or_table_name);
- }
+ parse_schema_and_table_name(schema_name, table_name);
if (match(TokenType::ParenOpen)) {
// FIXME: Parse "table-function".
@@ -724,16 +703,9 @@ NonnullRefPtr<CommonTableExpression> Parser::parse_common_table_expression()
NonnullRefPtr<QualifiedTableName> Parser::parse_qualified_table_name()
{
// https://sqlite.org/syntax/qualified-table-name.html
- String schema_or_table_name = consume(TokenType::Identifier).value();
String schema_name;
String table_name;
-
- if (consume_if(TokenType::Period)) {
- schema_name = move(schema_or_table_name);
- table_name = consume(TokenType::Identifier).value();
- } else {
- table_name = move(schema_or_table_name);
- }
+ parse_schema_and_table_name(schema_name, table_name);
String alias;
if (consume_if(TokenType::As))
@@ -803,16 +775,9 @@ NonnullRefPtr<TableOrSubquery> Parser::parse_table_or_subquery()
{
// https://sqlite.org/syntax/table-or-subquery.html
if (match(TokenType::Identifier)) {
- String schema_or_table_name = consume().value();
String schema_name;
String table_name;
-
- if (consume_if(TokenType::Period)) {
- schema_name = move(schema_or_table_name);
- table_name = consume(TokenType::Identifier).value();
- } else {
- table_name = move(schema_or_table_name);
- }
+ parse_schema_and_table_name(schema_name, table_name);
String table_alias;
if (consume_if(TokenType::As) || match(TokenType::Identifier))
@@ -859,6 +824,18 @@ NonnullRefPtr<OrderingTerm> Parser::parse_ordering_term()
return create_ast_node<OrderingTerm>(move(expression), move(collation_name), order, nulls);
}
+void Parser::parse_schema_and_table_name(String& schema_name, String& table_name)
+{
+ String schema_or_table_name = consume(TokenType::Identifier).value();
+
+ if (consume_if(TokenType::Period)) {
+ schema_name = move(schema_or_table_name);
+ table_name = consume(TokenType::Identifier).value();
+ } else {
+ table_name = move(schema_or_table_name);
+ }
+}
+
Token Parser::consume()
{
auto old_token = m_parser_state.m_token;
diff --git a/Userland/Libraries/LibSQL/Parser.h b/Userland/Libraries/LibSQL/Parser.h
index 827d60c37c..6d6cf91591 100644
--- a/Userland/Libraries/LibSQL/Parser.h
+++ b/Userland/Libraries/LibSQL/Parser.h
@@ -84,6 +84,7 @@ private:
NonnullRefPtr<ResultColumn> parse_result_column();
NonnullRefPtr<TableOrSubquery> parse_table_or_subquery();
NonnullRefPtr<OrderingTerm> parse_ordering_term();
+ void parse_schema_and_table_name(String& schema_name, String& table_name);
template<typename ParseCallback>
void parse_comma_separated_list(bool surrounded_by_parentheses, ParseCallback&& parse_callback)