~aritra1911/cylinder

99a786fe1d35fc6c01cc411b9b0426b7c6df2459 — Aritra Sarkar 3 years ago c3f7b1e
Test describing a table
3 files changed, 63 insertions(+), 5 deletions(-)

M schema.cpp
M schema.hpp
M sqlparser.cpp
M schema.cpp => schema.cpp +54 -5
@@ 13,7 13,8 @@ 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::binary );//| std::ios::app);
    //file.open( SCHEMA_FILE(this->name), std::ios::in | std::ios::out | std::ios::binary | std::ios::trunc );//| std::ios::app);
}

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


@@ 55,7 56,7 @@ void Schema::drop( const std::string& name ) {
        throw DoesntExistException();
}

void Schema::create_table(const std::string& name, AbstractField** fields, const size_t& num_fields) {
void Schema::create_table(const std::string& table_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


@@ 65,12 66,60 @@ void Schema::create_table(const std::string& name, AbstractField** fields, const
     * 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;

    /* Write the table name on the first line */
    file << table_name << std::endl;

    /* Then print slowly i.e. one by one, the field details i.e. field types and field names */
    for ( size_t i = 0; i < num_fields; i++ ) {
        file.write( reinterpret_cast< char* >( &fields[i]->type ), 1 );
        file << fields[i]->name << ' ';
    }

    /* Add an empty line which will serve as tables separatator */
    file << std::endl;
}

void Schema::describe( const std::string& table_name ) {
    /* TODO: Needs to be implemented actually under a Table class */

    /* Get to begining 'cause that's where table header is */
    file.seekg(0, std::ios::beg);

    /* The first line contains the table name */
    std::string name;
    file >> name;

    /* TODO: Check if this is the table the user wants to be described, i.e. check if ( table_name == name )
     *       Currently, we are testing only 1 table, so it's okay to ignore the `table_name' argument. */

    std::cout << table_name << std::endl;
    file.seekg(1, std::ios::cur);  /* Skip reading the newline character */

    /* The second line contains the field details. It goes something like this:
     * <DATA_TYPE><Field_name> <DATA_TYPE><Field_name> ...num_field times... <DATA_TYPE><Field_name> \n
     */

    FieldType type = NUMBER;

    char ch;
    while ( !file.eof() ) {
        /* Read the datatype, followed by the name of the field */
        file.read( reinterpret_cast< char* >( &type ), 1 );
        file >> name;

        /* Show them (nicely) TODO: Be nicer */
        std::cout << name << '\t' << ( type ? "VARCHAR" : "NUMBER" ) << std::endl;

        file.seekg( 1, std::ios::cur );  /* Skip reading the space character */

        /* Is this the end of the field details of this table? */
        file.get(ch);
        if ( ch == '\n' ) break;  /* If so, get out of here */
        file.putback( ch );  /* If not, put things back into place and continue */
    }
}

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

M schema.hpp => schema.hpp +1 -0
@@ 20,6 20,7 @@ class Schema {
    static int create(const std::string&);
    static void drop( const std::string& );
    void create_table(const std::string&, AbstractField**, const size_t&);
    void describe( const std::string& );
    ~Schema(void);
};


M sqlparser.cpp => sqlparser.cpp +8 -0
@@ 128,6 128,10 @@ 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).substr(0, 4) == "DESC") {
        this->name = tail(query);
        this->statement = DESCRIBE;

    } else {
        std::cerr << "Couldn't parse query\n";
        return -1;


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

        case DESCRIBE:
            schema->describe(this->name);
            break;

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