~aritra1911/cylinder

19d7575000923e2cd1aee8b1c383e03f1bee9411 — Aritra Sarkar 3 years ago 11466e2
Begin serializing table creation
5 files changed, 54 insertions(+), 12 deletions(-)

M Makefile
M schema.cpp
M schema.hpp
M sqlparser.cpp
M sqlparser.hpp
M Makefile => Makefile +2 -2
@@ 1,7 1,7 @@
CXXFLAGS = -Wall -Wextra -pedantic -std=c++11
LIBS =
SRC = main.cpp schema.cpp sqlparser.cpp
OBJ = main.o schema.o sqlparser.o
SRC = main.cpp schema.cpp sqlparser.cpp field_type.cpp
OBJ = main.o schema.o sqlparser.o field_type.o

.PHONY: clean


M schema.cpp => schema.cpp +18 -1
@@ 3,6 3,7 @@
#include <cerrno>
#include <cstring>
#include "schema.hpp"
#include "field_type.hpp"

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


@@ 14,7 15,7 @@ Schema::Schema(const std::string& name) : name(name) {

    f.close();  /* Close if opened */

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

std::string Schema::get_name(void) {


@@ 68,6 69,22 @@ int Schema::drop(const std::string& name) {
    return 0;
}

void Schema::create_table(const std::string& name, AbstractField** fields, const size_t& num_fields) {
    /* Now we have to serialize the table header in the file. It should be something like this:
     *
     * Table_name\n
     * <DATA_TYPE><Field_name><DATA_TYPE><Field_name> ...num_field times... <DATA_TYPE><Field_name>\n
     * ...Records follow here...\n
     * \n
     * Table_name\n
     * ...same as above...
     */
    file << name << std::endl;
    for (size_t i=0; i<num_fields; i++)
        file << fields[i]->type << fields[i]->name;
    file << std::endl;
}

Schema::~Schema(void) {
    if (file.is_open())
        file.close();

M schema.hpp => schema.hpp +2 -0
@@ 2,6 2,7 @@
# define _SCHEMA_HPP

# include <fstream>
# include "field_type.hpp"

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


@@ 18,6 19,7 @@ class Schema {
    std::string get_name(void);
    static int create(const std::string&);
    static int drop(const std::string&);
    void create_table(const std::string&, AbstractField**, const size_t&);
    ~Schema(void);
};


M sqlparser.cpp => sqlparser.cpp +31 -3
@@ 3,6 3,7 @@
#include <vector>
#include "sqlparser.hpp"
#include "schema.hpp"
#include "field_type.hpp"

std::string head(const std::string& s) {
    size_t pos = s.find(' ');  // TODO: Include all whitespaces


@@ 146,14 147,41 @@ void SQL::execute(Schema*& schema) {  /* TODO: Should `schema' be mutable? */
                        std::cout << "Schema created.\n";
                    break;

                case TABLE:
                case TABLE: {
                    if (!schema) {
                        std::cerr << "No Schema selected!\n";
                        break;
                    }

                    //schema->create_table(name, columns);  /* TODO */
                    break;
                    /* Create an array of Fields that'll be passed to schema->create_table() */
                    AbstractField** fields = new AbstractField*[columns.size()];

                    /* Copy `columns' vector into `fields' array as we now already know the number of columns there are,
                     * so instead of passing a vector<Column>, we'll pass an array of `Field<int>'. Now arises two
                     * questions:
                     *
                     * * Why are we constructing an array again? Just pass the vector and call it done!
                     * > Well, if you see what kind of vector it is, you'll notice that it's a vector of `Column'
                     *   datatype, and `Column' is partial to this module. It helps in parsing and that's all. There's
                     *   no need of making it generic since we already have the `Field' data structure from
                     *   `field_type.hpp' which is generic enough.
                     *
                     * * Why Field<int>?
                     * > Now that's a hack since here we don't really care about what type of data the field is holding
                     *   since it'll be holding none. I could've just used objects of AbstractField, but it's abstract.
                     */
                    size_t i = 0;
                    for (Column& col : columns)
                        fields[i++] = new Field<int>(col.type, col.name);

                    schema->create_table(name, fields, i);

                    /* Clean up */
                    for (i=0; i<columns.size(); i++)
                        delete fields[i];
                    columns.clear();

                }   break;

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

M sqlparser.hpp => sqlparser.hpp +1 -6
@@ 26,14 26,9 @@ enum Clause {
    WHERE,
};

enum DataType {
    NUMBER,  /* Int, TODO: Include Float */
    VARCHAR,  /* std::string, Don't think(TODO) too much right now */
};

struct Column {
    std::string name;
    DataType type;
    FieldType type;
    /* TODO: Add constraints */

    Column(const std::string&);