~aritra1911/cylinder

f75756153be4acd57dc60e63e439eeb5a756dfa4 — Aritra Sarkar 3 years ago 360067e
Implement Exception for Schema selection

Also instantiating an object of schema, tries to select that schema
instead of having a separate select() function since that was
unnecessary. Also you cannot simply create() or drop() a schema through
an object. It'll call the static functions and that's how schemas are
created or dropped.
3 files changed, 26 insertions(+), 54 deletions(-)

M schema.cpp
M schema.hpp
M sqlparser.cpp
M schema.cpp => schema.cpp +11 -43
@@ 4,20 4,21 @@
#include <cstring>
#include "schema.hpp"

Schema::Schema(const std::string& name) : name(name) { }
Schema::Schema(const std::string& name) : name(name) {
    /* Instantiating a Schema, selects it. */

std::string Schema::get_name(void) {
    return this->name;
}
    /* Check if it exists */
    std::ifstream f(SCHEMA_FILE(this->name));
    if (!f.is_open())
        throw DoesntExistException();

int Schema::create(void) {
    /* Creates file for the schema (this->name) */
    f.close();  /* Close if opened */

    /* If schema has an open file, close it */
    if (file.is_open())
        file.close();
    file.open(SCHEMA_FILE(this->name), std::ios::in | std::ios::out | std::ios::app);
}

    return this->create(this->name);
std::string Schema::get_name(void) {
    return this->name;
}

int Schema::create(const std::string& name) {


@@ 46,39 47,6 @@ int Schema::create(const std::string& name) {
    return 0;
}

int Schema::select() {
    /* if already selected, don't repeat */
    if (file.is_open()) {
        std::cerr << "Schema is already selected!\n";
        return -1;
    }

    /* Check if it exists */
    std::ifstream f(SCHEMA_FILE(this->name));
    if (!f.is_open()) {
        std::cerr << "Schema doesn't exist!\n";
        return -1;
    }
    f.close();  /* Close if opened */

    file.open(SCHEMA_FILE(this->name), std::ios::in | std::ios::out | std::ios::app);

    return 0;
}

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

    /* If schema has open file, close it */
    if (file.is_open())
        file.close();

    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 */


M schema.hpp => schema.hpp +3 -3
@@ 11,11 11,11 @@ class Schema {
    std::fstream file;

    public:
    /* Exceptions */
    class DoesntExistException { };

    Schema(const std::string&);
    std::string get_name(void);
    int create(void);  // Create self
    int select(void);
    int drop(void);  // Drop self
    static int create(const std::string&);
    static int drop(const std::string&);
    ~Schema(void);

M sqlparser.cpp => sqlparser.cpp +12 -8
@@ 179,17 179,21 @@ void SQL::execute(Schema*& schema) {  /* TODO: Should `schema' be mutable? */
                case SCHEMA:
                    /* If we've selected another schema that's not pointed by `schema' */
                    if (schema && schema->get_name() != name) {
                        delete schema;
                        schema = nullptr;  /* This should happen automatically after a delete */
                        delete schema;  /* Schema's desctructor is called and it safely closes the file */
                        schema = nullptr;
                    }

                    if (!schema)
                        schema = new Schema(name);
                    if (!schema) {
                        try {
                            schema = new Schema(name);

                    if (schema->select() == -1)
                        std::cerr << "Couldn't select schema!\n";
                    else
                        std::cout << "Schema selected.\n";
                        } catch(Schema::DoesntExistException) {
                            std::cerr << "Schema doesn't exist!\n";
                            break;
                        }
                    }

                    std::cout << "Schema selected.\n";
                    break;

                default: