~aritra1911/cylinder

a582069f162d532ce06b1a603e8233c55279ed0d — Aritra Sarkar 3 years ago 9e018ab
Select schema
4 files changed, 62 insertions(+), 4 deletions(-)

M main.cpp
M schema.cpp
M schema.hpp
M sqlparser.cpp
M main.cpp => main.cpp +3 -0
@@ 31,5 31,8 @@ int main(void) {
        sql.execute(schema);
    }

    if (schema)  /* Make sure we're closing schema files */
        delete schema;

    return EXIT_SUCCESS;
}

M schema.cpp => schema.cpp +21 -4
@@ 46,6 46,26 @@ 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) */



@@ 81,9 101,6 @@ int Schema::drop(const std::string& name) {
}

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

M schema.hpp => schema.hpp +1 -0
@@ 14,6 14,7 @@ class Schema {
    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&);

M sqlparser.cpp => sqlparser.cpp +37 -0
@@ 53,6 53,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) == "SELECT") {
        query = tail(query);  // Chop off head, we won't need that anymore!
        this->statement = SELECT;

        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;


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

        case SELECT:
            switch (substatement) {
                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 */
                    }

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

                    if (schema->select() == -1)
                        std::cerr << "Couldn't select schema!\n";
                    else
                        std::cout << "Schema selected.\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. */