sillyql / functions.h
functions.h
Raw
// Project Identifier: C0F4DFE8B340D81183C208F70F9D2D797908754Ds
#include <queue>
#include <stack>
#include <deque>
#include <list>
#include <getopt.h>
#include <sstream>
#include <unistd.h>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <ctype.h>
#include <algorithm>
#include <map>
#include <unordered_map>
#include "TableEntry.h"

using namespace std;

// Quiet mode:
// Print only numerical summaries of the rows affected by the commnand

// SILLY CLASS
class Silly {
    public:
    bool quiet;
    Silly(): quiet(false){}
};

// COLUMN CLASS
class Column{
    public:
    // COLTYPE: d = double, i = int, b = bool, s = string
    char coltype;
    std::string colname;
    std::unordered_map<TableEntry, size_t> hash_map;
    std::map<TableEntry, size_t> bst_map;
    std::vector<TableEntry> col;
    bool hash_index;
    bool bst_index;
    Column(char type):
        coltype(type), colname(""), hash_index(false), bst_index(false){}
};

// TABLE CLASS
class Table{
public:
    std::string tablename;
    int num_cols;
    std::vector<Column> cols;
    
    Table(std::string t, int cols):
        tablename(t), num_cols(cols){}
};


// UTILITY ////////////////////////////////////////////////////////////////////////////////////////////////////////////
// process command line
void process_cmd_line(int argc, char** argv, Silly &silly);

// set command
char set_command(string &cmd);

// create table entry
TableEntry create_entry(Column &column);

// if column in table, returns column index. else, returns -1
size_t get_col_index(string &colname, Table &table);

// FUNCTIONS ////////////////////////////////////////////////////////////////////////////////////////////////////////////
// CREATE
void create_table(string &argument, unordered_map<string, Table> &table_map);

// INSERT
void insert_into(string &argument, unordered_map<string, Table> &table_map);

// takes coltype, operation and value, adjusts value based on coltype, and returns true if row should be deleted
bool delete_check(char &coltype, TableEntry &col_entry, string &operation, string &value);

// for each column in table, delete entry at col_index
void execute_delete(Table &table, size_t col_index);

// DELETE
void delete_rows(string &argument, unordered_map<std::string, Table> &table_map);

// GENERATE
void generate_index(string &colname, unordered_map<std::string, Table> &table_map);

// takes coltype, operation and value, adjusts value based on coltype, and returns true if row should be printed
bool print_check(Table &table_map, size_t &index, string &colname, string &operation, string &value);

// PRINT
void print_rows(string &argument, unordered_map<std::string, Table> &table_map, Silly &silly);

// print join row
void print_j_row(Table &table1, Table &table2, vector<string> &colnames, vector<int> &table_specs, size_t &index_one, size_t &index_two);

// JOIN
void join_tables(string &argument, unordered_map<string, Table> &table_map, Silly &silly);

// REMOVE
void remove_table(string &argument, unordered_map<string, Table> &table_map);

// ERRORS //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// check table already exists
bool check_table_exists(string &tablename, unordered_map<string, Table> &table_map);
// print already exists
void print_table_exists(string &tablename, string command);
// print table does not exist
void print_table_n_exists(string &tablename, string command);

// returns true if column is in table
bool check_column(string &colname, Table &table);
// print column not in table
void print_check_column(string &colname, string command, Table &table);