~aritra1911/cylinder

9e018ab7d4b9a3c33f8f9a8d379aabcdfbd0aab6 — Aritra Sarkar 3 years ago 9757b28
Make schema creation static
3 files changed, 27 insertions(+), 10 deletions(-)

M schema.cpp
M schema.hpp
M sqlparser.cpp
M schema.cpp => schema.cpp +16 -3
@@ 10,13 10,22 @@ 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) {
    /* Creates file for the schema (this->name) */

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

    return this->create(this->name);
}

int Schema::create(const std::string& name) {
    /* 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);
    f.open(SCHEMA_FILE(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";


@@ 25,7 34,7 @@ int Schema::create(void) {
    }

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


@@ 40,6 49,10 @@ int Schema::create(void) {
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

M schema.hpp => schema.hpp +4 -3
@@ 3,7 3,7 @@

# include <fstream>

# define DATABASE_DIR "db/"
# define DATABASE_DIR "db/"  /* TODO: Create if doesn't exist */
# define SCHEMA_FILE(x) (DATABASE_DIR + x)

class Schema {


@@ 13,8 13,9 @@ class Schema {
    public:
    Schema(const std::string&);
    std::string get_name(void);
    int create(void);
    int drop(void);
    int create(void);  // Create self
    int drop(void);  // Drop self
    static int create(const std::string&);
    static int drop(const std::string&);
    ~Schema(void);
};

M sqlparser.cpp => sqlparser.cpp +7 -4
@@ 66,11 66,14 @@ void SQL::execute(Schema*& schema) {  /* TODO: Should `schema' be mutable? */
        case CREATE:
            switch (substatement) {
                case SCHEMA:
                    if (schema)  /* Existing schema needs to be closed */
                        delete schema;  /* TODO: Was changes in current schema saved? */
                    /* TODO: Move this to SELECT SCHEMA */
                    //if (schema)  /* Existing schema needs to be closed */
                    //    delete schema;  /* TODO: Was changes in current schema saved? */
                    //schema = new Schema(name);

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