1#include "duckdb/parser/statement/pragma_statement.hpp"
2#include "duckdb/parser/transformer.hpp"
3#include "duckdb/parser/expression/constant_expression.hpp"
4
5using namespace duckdb;
6using namespace std;
7
8unique_ptr<PragmaStatement> Transformer::TransformPragma(PGNode *node) {
9 auto stmt = reinterpret_cast<PGPragmaStmt *>(node);
10
11 auto result = make_unique<PragmaStatement>();
12 auto &info = *result->info;
13
14 info.name = stmt->name;
15 // parse the arguments, if any
16 if (stmt->args) {
17 for (auto cell = stmt->args->head; cell != nullptr; cell = cell->next) {
18 auto node = reinterpret_cast<PGNode *>(cell->data.ptr_value);
19 if (node->type != T_PGAConst) {
20 throw ParserException("Unsupported PRAGMA parameter: can only accept constants!");
21 }
22 auto constant = TransformConstant((PGAConst *)node);
23 info.parameters.push_back(((ConstantExpression &)*constant).value);
24 }
25 }
26 // now parse the pragma type
27 switch (stmt->kind) {
28 case PG_PRAGMA_TYPE_NOTHING:
29 if (info.parameters.size() > 0) {
30 throw ParserException("PRAGMA statement that is not a call or assignment cannot contain parameters");
31 }
32 info.pragma_type = PragmaType::NOTHING;
33 break;
34 case PG_PRAGMA_TYPE_ASSIGNMENT:
35 if (info.parameters.size() != 1) {
36 throw ParserException("PRAGMA statement with assignment should contain exactly one parameter");
37 }
38 info.pragma_type = PragmaType::ASSIGNMENT;
39 break;
40 case PG_PRAGMA_TYPE_CALL:
41 info.pragma_type = PragmaType::CALL;
42 break;
43 default:
44 throw ParserException("Unknown pragma type");
45 }
46
47 return result;
48}
49