-
Notifications
You must be signed in to change notification settings - Fork 0
/
SQLExec.h
96 lines (77 loc) · 3.34 KB
/
SQLExec.h
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
/**
* @file SQLExec.h - SQLExec class
* @author Kevin Lundeen
* @see "Seattle University, CPSC5300, Summer 2018"
*/
#pragma once
#include <exception>
#include <string>
#include "SQLParser.h"
#include "schema_tables.h"
/**
* @class SQLExecError - exception for SQLExec methods
*/
class SQLExecError : public std::runtime_error {
public:
explicit SQLExecError(std::string s) : runtime_error(s) {}
};
/**
* @class QueryResult - data structure to hold all the returned data for a query execution
*/
class QueryResult {
public:
QueryResult() : column_names(nullptr), column_attributes(nullptr), rows(nullptr), message("") {}
QueryResult(std::string message) : column_names(nullptr), column_attributes(nullptr), rows(nullptr),
message(message) {}
QueryResult(ColumnNames *column_names, ColumnAttributes *column_attributes, ValueDicts *rows, std::string message)
: column_names(column_names), column_attributes(column_attributes), rows(rows), message(message) {}
virtual ~QueryResult();
ColumnNames *get_column_names() const { return column_names; }
ColumnAttributes *get_column_attributes() const { return column_attributes; }
ValueDicts *get_rows() const { return rows; }
const std::string &get_message() const { return message; }
friend std::ostream &operator<<(std::ostream &stream, const QueryResult &qres);
protected:
ColumnNames *column_names;
ColumnAttributes *column_attributes;
ValueDicts *rows;
std::string message;
};
/**
* @class SQLExec - execution engine
*/
class SQLExec {
public:
/**
* Execute the given SQL statement.
* @param statement the Hyrise AST of the SQL statement to execute
* @returns the query result (freed by caller)
*/
static QueryResult *execute(const hsql::SQLStatement *statement) throw(SQLExecError);
protected:
// the one place in the system that holds the _tables table and _indices table
static Tables *tables;
static Indices *indices;
// recursive decent into the AST
static QueryResult *create(const hsql::CreateStatement *statement);
static QueryResult *create_table(const hsql::CreateStatement *statement);
static QueryResult *create_index(const hsql::CreateStatement *statement);
static QueryResult *drop(const hsql::DropStatement *statement);
static QueryResult *drop_table(const hsql::DropStatement *statement);
static QueryResult *drop_index(const hsql::DropStatement *statement);
static QueryResult *show(const hsql::ShowStatement *statement);
static QueryResult *show_tables();
static QueryResult *show_columns(const hsql::ShowStatement *statement);
static QueryResult *show_index(const hsql::ShowStatement *statement);
static QueryResult *insert(const hsql::InsertStatement *statement);
static QueryResult *del(const hsql::DeleteStatement *statement);
static QueryResult *select(const hsql::SelectStatement *statement);
/**
* Pull out column name and attributes from AST's column definition clause
* @param col AST column definition
* @param column_name returned by reference
* @param column_attributes returned by reference
*/
static ValueDict* get_where_conjunction(const hsql::Expr *expr);
static void column_definition(const hsql::ColumnDefinition *col, Identifier &column_name, ColumnAttribute &column_attribute);
};