~aritra1911/cylinder

d8cf7135d4f31bb2d4973cd763828360635b2bd5 — Aritra Sarkar 3 years ago f6ee4f1
Complete Schema creation
6 files changed, 63 insertions(+), 7 deletions(-)

M .gitignore
M main.cpp
M schema.cpp
M schema.hpp
M sqlparser.cpp
M sqlparser.hpp
M .gitignore => .gitignore +1 -0
@@ 1,2 1,3 @@
*.o
db/
cylinder

M main.cpp => main.cpp +6 -2
@@ 6,6 6,7 @@ const std::string PROMPT = "SQL> ";

int main(void) {
    std::string query;
    Schema* schema = nullptr;
    SQL sql;

    while (1) {


@@ 26,8 27,11 @@ int main(void) {
        if (sql.parse(query) == -1)
            continue;  /* Bad query */

        /* TODO: This will work on a particular schema, but should we mutate a global schema? */
        sql.execute();
        /* TODO: This works on a particular schema, but should we mutate a global schema? */
        sql.execute(schema);

        if (schema)
            std::cout << schema->get_name() << std::endl;
    }

    return EXIT_SUCCESS;

M schema.cpp => schema.cpp +35 -1
@@ 1,4 1,4 @@
#include <string>
#include <iostream>
#include "schema.hpp"

Schema::Schema(const std::string& name) : name(name) { }


@@ 6,3 6,37 @@ Schema::Schema(const std::string& name) : name(name) { }
std::string Schema::get_name(void) {
    return this->name;
}

int Schema::create(void) {
    /* Note: This function only creates the schema, but doesn't load it. */
    std::fstream f;

    /* Check if schema already exists */
    f.open(DATABASE_DIR + this->name, std::ios::in);
    if (f.is_open()) {
        /* We were able to open the schema, therefore it exists. So return error. */
        std::cerr << "Schema already exists!\n";
        f.close();
        return -1;
    }

    /* Now that we know for sure that it doesn't exist, create it */
    f.open(DATABASE_DIR + this->name, std::ios::out);
    if (!f.is_open()) {
        /* Something went wrong, maybe file create permissions? */
        std::cerr << "Couldn't create schema\n";
        return -1;
    }

    /* Things went as intended. Now close the open output file and return. */
    f.close();
    return 0;
}

Schema::~Schema(void) {
    std::cout << "Schema destructor called\n";
    if (file.is_open()) {
        std::cout << "File closed\n";
        file.close();
    }
}

M schema.hpp => schema.hpp +7 -0
@@ 1,12 1,19 @@
#ifndef _SCHEMA_HPP
# define _SCHEMA_HPP

# include <fstream>

# define DATABASE_DIR "db/"

class Schema {
    std::string name;
    std::fstream file;

    public:
    Schema(const std::string&);
    std::string get_name(void);
    int create(void);
    ~Schema(void);
};

#endif  /* _SCHEMA_HPP */

M sqlparser.cpp => sqlparser.cpp +13 -3
@@ 35,6 35,8 @@ 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 {
            std::cerr << "What's " << head(query) << "? - rest of the line ignored!\n";
            return -1;


@@ 47,13 49,21 @@ int SQL::parse(const std::string& _query) {
    return 0;
}

void SQL::execute(void) {
    /* TODO: Quit bluffing */
void SQL::execute(Schema*& schema) {  /* TODO: Should `schema' be mutable? */
    switch (statement) {
        case CREATE:
            switch (substatement) {
                case SCHEMA:
                    std::cout << "Schema Name : \"" << name << "\"\n";
                    if (schema)  /* Existing schema needs to be closed */
                        delete schema;  /* TODO: Was changes in current schema saved? */

                    schema = new Schema(name);
                    if (schema->create() != -1)
                        std::cout << "Schema created.\n";
                    break;

                case TABLE:
                    /* TODO */
                    break;

                default:

M sqlparser.hpp => sqlparser.hpp +1 -1
@@ 30,7 30,7 @@ struct SQL {
    std::string name;

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

/* After splitting the given string using the first encountered whitespace as a delimeter, the head() and tail()