~aritra1911/cylinder

26e6d181c4f3b1583e88f3a7cf1c18d6d5495295 — Aritra Sarkar 3 years ago 5903a56
Prepare for parsing CREATE TABLE
2 files changed, 61 insertions(+), 2 deletions(-)

M sqlparser.cpp
M sqlparser.hpp
M sqlparser.cpp => sqlparser.cpp +44 -2
@@ 1,4 1,6 @@
#include <iostream>
#include <sstream>
#include <vector>
#include "sqlparser.hpp"
#include "schema.hpp"



@@ 22,6 24,41 @@ std::string tail(const std::string& s) {
    return "";
}

std::string strip_parenthesis(const std::string& s) {
    /* Gets text within parenthesis */

    size_t pos = s.find('(');  // We're not assuming that `s' starts with '(', but it should!
    size_t qos = s.find(')');  // We're not assuming that `s' ends with ')', but it should!

    if (pos == std::string::npos && qos == std::string::npos)
        /* We didn't find any begining or ending parenthesis */
        return s;

    if (pos == std::string::npos)
        /* We found an ending parenthesis */
        return s.substr(0, qos);

    if (qos == std::string::npos)
        /* We found a starting parenthesis */
        return s.substr(pos + 1);

    /* We found both starting and ending parentheses */
    return s.substr(pos + 1, qos - pos - 1);
}

std::vector<std::string> split(const std::string& str, const char& delim) {
    /* Splits and vectorizes a list of `delim' separated string of items */

    std::istringstream input(str);
    std::vector<std::string> retvec;
    std::string item;

    while (getline(input, item, delim))
        retvec.push_back(item);

    return retvec;
}

int SQL::parse(const std::string& _query) {
    std::string query = _query;  // Create a copy so we don't modify the original one



@@ 35,8 72,13 @@ int SQL::parse(const std::string& _query) {
            this->name = tail(query);
                /* TODO: SQL statements may end with a semi-colon which is not a part of the name itself. */

        //} else if (head(query) == "TABLE") {
        //    /* TODO */
        } else if (head(query) == "TABLE") {
            query = tail(query);  // Chop off head, we won't need that anymore!
            this->substatement = TABLE;
            this->name = head(query);
            query = tail(query);  // Chop off head, we won't need that anymore!
            /* Here query is now a list of columns and their datatypes separated by comma and enclosed in parenthsis */
            /* TODO: We need a function that interprets next the list of columns and datatypes enclosed in parenthesis */
        } else {
            std::cerr << "What's " << head(query) << "? - rest of the line ignored!\n";
            return -1;

M sqlparser.hpp => sqlparser.hpp +17 -0
@@ 1,6 1,8 @@
#ifndef _SQLPARSER_HPP
# define _SQLPARSER_HPP

# include <string>
# include <vector>
# include "schema.hpp"

enum Clause {


@@ 24,10 26,22 @@ enum Clause {
    WHERE,
};

enum DataType {
    NUMBER,  /* Int, TODO: Include Float */
    VARCHAR,  /* std::string, Don't think(TODO) too much right now */
};

struct Column {
    std::string name;
    DataType type;
    /* TODO: Add constraints */
};

struct SQL {
    Clause statement;
    Clause substatement;
    std::string name;
    std::vector<Column> table_columns;

    int parse(const std::string&);
    void execute(Schema*&);


@@ 38,4 52,7 @@ struct SQL {
std::string head(const std::string&);
std::string tail(const std::string&);

std::string strip_parenthesis(const std::string&);
std::vector<std::string> split(const std::string&);

#endif  /* _SQLPARSER_HPP */