~aritra1911/cylinder

a0d28f8447a3289d286598d57b043802c8290481 — Aritra Sarkar 3 years ago 98dd10b improv_parse
Trim query
2 files changed, 51 insertions(+), 12 deletions(-)

M sqlparser.cpp
M sqlparser.hpp
M sqlparser.cpp => sqlparser.cpp +46 -12
@@ 45,6 45,34 @@ std::string tail(const std::string& s) {
    return "";
}

std::string trim_left( const std::string& s ) {
    /* Removes any space characters preceding a string */

    size_t pos;
    if ( (pos = s.find_first_not_of( ' ' )) != std::string::npos ) {
        return s.substr( pos );
    }

    return s;
}

std::string trim_right( const std::string& s ) {
    /* Removes any space characters following a string */

    size_t pos;
    if ( (pos = s.find_last_not_of( ' ' )) != std::string::npos ) {
        return s.substr( 0, pos + 1 );
    }

    return s;
}

std::string trim( const std::string& s ) {
    /* Removes any space characters surrounding a string */

    return trim_right( trim_left( s ) );
}

std::string strip_parenthesis(const std::string& s) {
    /* Gets text within parenthesis */



@@ 74,8 102,10 @@ std::vector<std::string> split(const std::string& str, const char& delim) {
    std::vector<std::string> retvec;
    std::string item;

    while (getline(input, item, delim))
    while ( getline( input, item, delim ) ) {
        if ( !item.size() ) continue;
        retvec.push_back(item);
    }

    return retvec;
}


@@ 94,20 124,20 @@ Column::Column(const std::string& col_str) {
}

int SQL::parse(const std::string& _query) {
    std::string query = _query;  // Create a copy so we don't modify the original one
    std::string query = trim_left( _query );  // Create a copy so we don't modify the original one

    /* TODO: SQL is case-insensitive */
    if (head(query) == "CREATE") {
        query = tail(query);  // Chop off head, we won't need that anymore!
        query = trim_left( tail( query ) );  // Chop off head, we won't need that anymore!
        this->statement = CREATE;

        if (head(query) == "SCHEMA") {
            this->substatement = SCHEMA;
            this->name = tail(query);
            this->name = trim( tail( query ) );
                /* TODO: SQL statements may end with a semi-colon which is not a part of the name itself. */

        } else if (head(query) == "TABLE") {
            query = tail(query);  // Chop off head, we won't need that anymore!
            query = trim_left( tail( query ) );  // Chop off head, we won't need that anymore!
            this->substatement = TABLE;
            this->name = head(query);



@@ 115,8 145,12 @@ int SQL::parse(const std::string& _query) {
             * parenthesis */

            columns.clear();
            for (std::string& col_str : split(strip_parenthesis(tail(query)), ','))
                columns.push_back(Column(col_str));
            /* This is for debugging, TODO: Remove */
            std::vector< std::string > col_strs = split(strip_parenthesis(tail(query)), ',');

            //for (std::string& col_str : split(strip_parenthesis(tail(query)), ','))
            for (std::string& col_str : col_strs )
                columns.push_back( Column( trim( col_str ) ) );

            /* And that's boyz & gals is how you parse a CREATE TABLE query */



@@ 125,31 159,31 @@ int SQL::parse(const std::string& _query) {
            return -1;
        }
    } else if (head(query) == "DROP") {
        query = tail(query);  // Chop off head, we won't need that anymore!
        query = trim_left(tail(query));  // Chop off head, we won't need that anymore!
        this->statement = DROP;

        if (head(query) == "SCHEMA") {
            this->substatement = SCHEMA;
            this->name = tail(query);
            this->name = trim( tail( query ) );

        } else {
            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!
        query = trim_left( 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);
            this->name = trim( tail(query) );

        } else {
            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->name = trim( tail(query) );
        this->statement = DESCRIBE;

    } else {

M sqlparser.hpp => sqlparser.hpp +5 -0
@@ 49,6 49,11 @@ struct SQL {
std::string head(const std::string&);
std::string tail(const std::string&);

/* The trim() functions remove spaces either left or right of a string or both */
std::string trim_left( const std::string& );
std::string trim_right( const std::string& );
std::string trim( const std::string& );

std::string strip_parenthesis(const std::string&);
std::vector<std::string> split(const std::string&);