~aritra1911/cylinder

9757b281159dc4cd1a71721766abd408a39ebaef — Aritra Sarkar 3 years ago eb5cb82
Drop Schemas
4 files changed, 66 insertions(+), 4 deletions(-)

M main.cpp
M schema.cpp
M schema.hpp
M sqlparser.cpp
M main.cpp => main.cpp +0 -3
@@ 29,9 29,6 @@ int main(void) {

        /* 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,7 @@
#include <iostream>
#include <cstdio>
#include <cerrno>
#include <cstring>
#include "schema.hpp"

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


@@ 7,6 10,7 @@ std::string Schema::get_name(void) {
    return this->name;
}

/* TODO: We should not have to instantiate in order to create a schema. It's just a file. */
int Schema::create(void) {
    /* Note: This function only creates the schema, but doesn't load it. */
    std::fstream f;


@@ 20,7 24,7 @@ int Schema::create(void) {
        return -1;
    }

    /* Now that we know for sure that it doesn't exist, create it */
    /* Now that we know for sure that it doesn't exist, so create it */
    f.open(DATABASE_DIR + this->name, std::ios::out);
    if (!f.is_open()) {
        /* Something went wrong, maybe file create permissions? */


@@ 33,6 37,36 @@ int Schema::create(void) {
    return 0;
}

int Schema::drop(void) {
    /* Deletes the file for the schema (this->name) */

    return this->drop(this->name);

    /* if object is pointed, it's a good idea to delete that pointer now, thereby calling the destructor. Also a
     * pointer is easier to check if the Schema is open or not. A null pointer signifies that it's not. */
}

int Schema::drop(const std::string& name) {
    /* Is able to delete file for any schema given a name */

    /* Try to open schema file and if we're not successful, then it doesn't exist. */
    std::ifstream f(SCHEMA_FILE(name));
    if (!f.is_open()) {
        std::cerr << "Schema doesn't exist\n";
        return -1;
    }
    f.close();  /* It opened, so close it */

    errno = 0;  /* Make sure `errno' is reset */
    if (std::remove(SCHEMA_FILE(name).c_str()) == -1) {
        std::cerr << SCHEMA_FILE(name) << ": " << std::strerror(errno);
        errno = 0;
        return -1;
    }

    return 0;
}

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

M schema.hpp => schema.hpp +3 -0
@@ 4,6 4,7 @@
# include <fstream>

# define DATABASE_DIR "db/"
# define SCHEMA_FILE(x) (DATABASE_DIR + x)

class Schema {
    std::string name;


@@ 13,6 14,8 @@ class Schema {
    Schema(const std::string&);
    std::string get_name(void);
    int create(void);
    int drop(void);
    static int drop(const std::string&);
    ~Schema(void);
};


M sqlparser.cpp => sqlparser.cpp +28 -0
@@ 41,6 41,18 @@ int SQL::parse(const std::string& _query) {
            std::cerr << "What's " << head(query) << "? - rest of the line ignored!\n";
            return -1;
        }
    } else if (head(query) == "DROP") {
        query = tail(query);  // Chop off head, we won't need that anymore!
        this->statement = DROP;

        if (head(query) == "SCHEMA") {
            this->substatement = SCHEMA;
            this->name = tail(query);

        } else {
            std::cerr << "What's " << head(query) << "? - rest of the line ignored!\n";
            return -1;
        }
    } else {
        std::cerr << "Couldn't parse query\n";
        return -1;


@@ 74,6 86,22 @@ void SQL::execute(Schema*& schema) {  /* TODO: Should `schema' be mutable? */
            }
            break;

        case DROP:
            switch (substatement) {
                case SCHEMA:
                    if (Schema::drop(name) == -1)
                        std::cerr << "Couldn't drop schema!\n";
                    else
                        std::cout << "Schema dropped!\n";
                    break;

                default:
                    /* If the parse() works correctly, and provided no break statements were missed above, this case
                     * should never be reached. */
                    std::cerr << "You shouldn't be seeing this!\n";
            }
            break;

        default:
            /* If the parse() works correctly, and provided no break statements were missed above, this case should
             * never be reached. */