Column Store
Table.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <string>
4 #include <unordered_map>
5 #include <vector>
6 
7 #include "Column.h"
8 #include "ForeignKey.h"
9 
10 namespace Parser {
11 using std::exception;
12 using std::string;
13 using std::vector;
14 using std::runtime_error;
15 
16 class TableNotFoundException : public exception {
17  std::string msg;
18 
19  public:
20  TableNotFoundException(std::string& t_name)
21  : msg("Table " + t_name + " not found") {}
22  virtual const char* what() const throw() { return msg.c_str(); }
23 };
24 
25 class Table {
26  std::string table_name;
27  std::unordered_map<std::string, int> column_map;
28  std::vector<Parser::Column> columns;
29  std::string primary_key;
30  std::vector<foreign_key> foreign_keys;
31 
32  public:
33  void set_table_name(std::string table_n) { table_name = table_n; }
34  void set_primary_key(std::string pk) { primary_key = pk; }
35  void add_column(std::string column_name, std::string data_type,
36  int size = 0);
37  void add_foreign_key(std::string from, std::string table, std::string to);
38  std::string get_table_name() { return table_name; }
39  std::string get_primary_key() { return primary_key; }
40  std::vector<foreign_key>& get_foreign_keys() { return foreign_keys; }
41  std::vector<Parser::Column>& get_columns() { return columns; }
43  if (i >= int(columns.size())) {
44  throw runtime_error("Index out of Bounds in Table:" + table_name);
45  }
46  return columns[i];
47  }
48  Parser::Column& get_column(std::string name) { return (*this)[name]; }
49  Parser::Column& operator[](std::string name) {
50  if (column_map.find(name) == column_map.end()) {
51  throw runtime_error("Table " + table_name +
52  " does not have column " + name);
53  }
54  return columns[column_map[name]];
55  }
56 };
57 } // namespace Parser
Table.h
Parser::TableNotFoundException::msg
std::string msg
Definition: Table.h:17
Parser::Table::set_table_name
void set_table_name(std::string table_n)
Definition: Table.h:33
ForeignKey.h
ColumnStore::DataType
DataType
Different datatypes supported by this project.
Definition: Column.h:16
Parser::TableNotFoundException::what
virtual const char * what() const
Definition: Table.h:22
Parser::Table::columns
std::vector< Parser::Column > columns
Definition: Table.h:28
Parser
Definition: Column.h:6
Parser::Table::operator[]
Parser::Column & operator[](int i)
Definition: Table.h:42
ColumnStore::DataType::STRING
@ STRING
Parser::Table::table_name
std::string table_name
Definition: Table.h:26
Parser::Table::get_table_name
std::string get_table_name()
Definition: Table.h:38
Parser::Table::get_foreign_keys
std::vector< foreign_key > & get_foreign_keys()
Definition: Table.h:40
Parser::Table::get_primary_key
std::string get_primary_key()
Definition: Table.h:39
Parser::DataType
Definition: Column.h:7
Parser::Table::add_column
void add_column(std::string column_name, std::string data_type, int size=0)
Definition: Table.cpp:5
Parser::Table::column_map
std::unordered_map< std::string, int > column_map
Definition: Table.h:27
Parser::Table::operator[]
Parser::Column & operator[](std::string name)
Definition: Table.h:49
Parser::TableNotFoundException
Definition: Table.h:16
Parser::Table::add_foreign_key
void add_foreign_key(std::string from, std::string table, std::string to)
Definition: Table.cpp:22
ColumnStore::DataType::INT
@ INT
Parser::Table::set_primary_key
void set_primary_key(std::string pk)
Definition: Table.h:34
Column.h
Parser::Table::foreign_keys
std::vector< foreign_key > foreign_keys
Definition: Table.h:30
Parser::Table
Definition: Table.h:25
Parser::TableNotFoundException::TableNotFoundException
TableNotFoundException(std::string &t_name)
Definition: Table.h:20
Parser::Table::get_column
Parser::Column & get_column(std::string name)
Definition: Table.h:48
Parser::Table::primary_key
std::string primary_key
Definition: Table.h:29
Parser::foreign_key
Definition: ForeignKey.h:6
Parser::Column
Definition: Column.h:38
Parser::DataType::size
int size
Definition: Column.h:9
ColumnStore::DataType::FLOAT
@ FLOAT
Parser::Table::get_columns
std::vector< Parser::Column > & get_columns()
Definition: Table.h:41