1#include "duckdb.hpp"
2#include "duckdb/parser/parser.hpp"
3#include "duckdb/common/enums/statement_type.hpp"
4#include "catch.hpp"
5
6using namespace duckdb;
7
8TEST_CASE("Test parser", "[parser]") {
9 Parser parser;
10
11 SECTION("Query with several statements") {
12 parser.ParseQuery("CREATE TABLE nums (num INTEGER);"
13 "BEGIN TRANSACTION;"
14 " INSERT INTO nums VALUES(1);"
15 " INSERT INTO nums VALUES(2);"
16 " INSERT INTO nums VALUES(3);"
17 " INSERT INTO nums VALUES(4);"
18 "COMMIT;");
19
20 REQUIRE(parser.statements.size() == 7);
21 REQUIRE(parser.statements[0]->type == StatementType::CREATE_STATEMENT);
22 REQUIRE(parser.statements[1]->type == StatementType::TRANSACTION_STATEMENT);
23 REQUIRE(parser.statements[2]->type == StatementType::INSERT_STATEMENT);
24 REQUIRE(parser.statements[3]->type == StatementType::INSERT_STATEMENT);
25 REQUIRE(parser.statements[4]->type == StatementType::INSERT_STATEMENT);
26 REQUIRE(parser.statements[5]->type == StatementType::INSERT_STATEMENT);
27 REQUIRE(parser.statements[6]->type == StatementType::TRANSACTION_STATEMENT);
28 }
29
30 SECTION("Wrong query") {
31 REQUIRE_THROWS(parser.ParseQuery("TABLE"));
32 }
33
34 SECTION("Empty query") {
35 parser.ParseQuery("");
36 REQUIRE(parser.statements.size() == 0);
37 }
38
39 SECTION("Pragma query") {
40 parser.ParseQuery("PRAGMA table_info('nums');");
41 REQUIRE(parser.statements.size() == 1);
42 }
43
44 SECTION("Wrong pragma query") {
45 parser.ParseQuery("PRAGMA table_info;");
46 REQUIRE(parser.statements.size() == 1);
47 }
48}
49