1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 07:58:11 +00:00

LibSQL: Parse EXISTS expressions

The EXISTS expression is a bit of an odd-man-out because it can appear
as any of the following forms:

    EXISTS (select-stmt)
    NOT EXISTS (select-stmt)
    (select-stmt)

Which makes it the only keyword expression that doesn't require its
keyword to actually be used. The consequence is that we might come
across an EXISTS expression while parsing another expression type;
NOT would have triggered a unary operator expression, and an opening
parentheses would have triggered an expression chain.
This commit is contained in:
Timothy Flynn 2021-04-23 15:29:28 -04:00 committed by Andreas Kling
parent e62e76ca1a
commit 99b38aa3fa
4 changed files with 80 additions and 5 deletions

View file

@ -338,6 +338,39 @@ TEST_CASE(case_expression)
validate("CASE 15 WHEN 16 THEN 17 WHEN 18 THEN 19 ELSE 20 END", true, 2, true);
}
TEST_CASE(exists_expression)
{
EXPECT(parse("EXISTS").is_error());
EXPECT(parse("EXISTS (").is_error());
EXPECT(parse("EXISTS (SELECT").is_error());
EXPECT(parse("EXISTS (SELECT)").is_error());
EXPECT(parse("EXISTS (SELECT * FROM table").is_error());
EXPECT(parse("NOT EXISTS").is_error());
EXPECT(parse("NOT EXISTS (").is_error());
EXPECT(parse("NOT EXISTS (SELECT").is_error());
EXPECT(parse("NOT EXISTS (SELECT)").is_error());
EXPECT(parse("NOT EXISTS (SELECT * FROM table").is_error());
EXPECT(parse("(").is_error());
EXPECT(parse("(SELECT").is_error());
EXPECT(parse("(SELECT)").is_error());
EXPECT(parse("(SELECT * FROM table").is_error());
auto validate = [](StringView sql, bool expected_invert_expression) {
auto result = parse(sql);
EXPECT(!result.is_error());
auto expression = result.release_value();
EXPECT(is<SQL::ExistsExpression>(*expression));
const auto& exists = static_cast<const SQL::ExistsExpression&>(*expression);
EXPECT_EQ(exists.invert_expression(), expected_invert_expression);
};
validate("EXISTS (SELECT * FROM table)", false);
validate("NOT EXISTS (SELECT * FROM table)", true);
validate("(SELECT * FROM table)", false);
}
TEST_CASE(collate_expression)
{
EXPECT(parse("COLLATE").is_error());