This repository was archived by the owner on Sep 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 618
/
Copy pathstatement.cpp
131 lines (103 loc) · 3.8 KB
/
statement.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
//===----------------------------------------------------------------------===//
//
// Peloton
//
// statement.cpp
//
// Identification: src/common/statement.cpp
//
// Copyright (c) 2015-16, Carnegie Mellon University Database Group
//
//===----------------------------------------------------------------------===//
#include <cstdio>
#include <sstream>
#include "common/logger.h"
#include "common/statement.h"
#include "parser/postgresparser.h"
#include "planner/abstract_plan.h"
namespace peloton {
Statement::Statement(const std::string& statement_name,
const std::string& query_string)
: statement_name_(statement_name), query_string_(query_string) {
try {
auto &peloton_parser = parser::PostgresParser::GetInstance();
sql_stmt_list_ = peloton_parser.BuildParseTree(query_string);
if (sql_stmt_list_.get() != nullptr && !sql_stmt_list_->is_valid) {
throw ParserException("Error Parsing SQL statement");
}
} // If the statement is invalid or not supported yet
catch (Exception &e) {
LOG_ERROR("%s", e.what());
}
if (sql_stmt_list_ == nullptr || sql_stmt_list_->GetNumStatements() == 0) {
// This should not happen. We only initialize Statement with query string in tests.
LOG_ERROR("Empty statement");
}
LOG_INFO("create statement: %s", query_string.c_str());
query_type_ = StatementTypeToQueryType(sql_stmt_list_->GetStatement(0)->GetType(), sql_stmt_list_->GetStatement(0));
}
Statement::Statement(const std::string &stmt_name, QueryType query_type,
std::string query_string, std::unique_ptr<parser::SQLStatementList> sql_stmt_list)
: statement_name_(stmt_name), query_type_(query_type),
query_string_(query_string), sql_stmt_list_(std::move(sql_stmt_list)),
query_type_string_(QueryTypeToString(query_type)){};
Statement::~Statement() {}
std::vector<FieldInfo> Statement::GetTupleDescriptor() const {
return tuple_descriptor_;
}
void Statement::SetStatementName(const std::string& statement_name) {
statement_name_ = statement_name;
}
std::string Statement::GetStatementName() const { return statement_name_; }
void Statement::SetQueryString(const std::string& query_string) {
query_string_ = query_string;
}
std::string Statement::GetQueryString() const { return query_string_; }
std::string Statement::GetQueryTypeString() const { return query_type_string_; }
QueryType Statement::GetQueryType() const { return query_type_; }
void Statement::SetParamTypes(const std::vector<PostgresValueType>& param_types) {
param_types_ = param_types;
}
std::vector<PostgresValueType> Statement::GetParamTypes() const { return param_types_; }
void Statement::SetTupleDescriptor(
const std::vector<FieldInfo>& tuple_descriptor) {
tuple_descriptor_ = tuple_descriptor;
}
void Statement::SetPlanTree(std::shared_ptr<planner::AbstractPlan> plan_tree) {
plan_tree_ = std::move(plan_tree);
}
void Statement::SetReferencedTables(const std::set<oid_t> table_ids) {
table_ids_.insert(table_ids.begin(), table_ids.end());
}
const std::set<oid_t> Statement::GetReferencedTables() const {
return (table_ids_);
}
const std::shared_ptr<planner::AbstractPlan>& Statement::GetPlanTree() const {
return plan_tree_;
}
const std::string Statement::GetInfo() const {
std::ostringstream os;
os << "Statement[";
if (statement_name_.empty()) {
os << "**UNNAMED**";
} else {
os << statement_name_;
}
os << "] -> " << query_string_ << " (";
// Tables Oids Referenced
os << "TablesRef={";
bool first = true;
for (auto t_id : table_ids_) {
if (first == false) os << ",";
os << t_id;
first = false;
}
os << "}";
// Replan Flag
os << ", ReplanNeeded=" << needs_replan_;
// Query Type
os << ", QueryType=" << query_type_string_;
os << ")";
return os.str();
}
} // namespace peloton