naming changes

This commit is contained in:
Pedro 2014-10-20 22:33:36 +02:00
parent caaa010160
commit 6876de5ca8
7 changed files with 75 additions and 72 deletions

View File

@ -20,8 +20,8 @@ int yyerror(Statement **expression, yyscan_t scanner, const char *msg) {
%code requires { %code requires {
#ifndef YY_TYPEDEF_YY_SCANNER_T #ifndef YYtypeDEF_YY_SCANNER_T
#define YY_TYPEDEF_YY_SCANNER_T #define YYtypeDEF_YY_SCANNER_T
typedef void* yyscan_t; typedef void* yyscan_t;
#endif #endif
@ -85,10 +85,10 @@ select_statement:
SELECT expr_list from_clause where_clause group_clause SELECT expr_list from_clause where_clause group_clause
{ {
SelectStatement* s = new SelectStatement(); SelectStatement* s = new SelectStatement();
s->_select_list = $2; s->select_list = $2;
s->_from_table = $3; s->from_table = $3;
s->_where_clause = $4; s->where_clause = $4;
s->_group_by = $5; s->group_by = $5;
$$ = s; $$ = s;
} }
; ;
@ -114,12 +114,12 @@ group_clause:
table_exp: table_exp:
table_ref_commalist { table_ref_commalist {
TableRef* t = new TableRef(eTableName); TableRef* t = new TableRef(eTableName);
t->_table_names = $1; t->table_names = $1;
$$ = t; $$ = t;
} }
| '(' select_statement ')' { | '(' select_statement ')' {
TableRef* t = new TableRef(eTableSelect); TableRef* t = new TableRef(eTableSelect);
t->_stmt = $2; t->stmt = $2;
$$ = t; $$ = t;
} }
; ;

View File

@ -6,7 +6,7 @@
%syntax_error { printf("Lemon syntax error\n"); } %syntax_error { printf("Lemon syntax error\n"); }
%extra_argument { Statement** result } %extra_argument { Statement** result }
%token_type {const char*} %tokentype {const char*}
%type expr {Statement*} %type expr {Statement*}
%left PLUS MINUS . %left PLUS MINUS .

View File

@ -8,8 +8,8 @@
#include <stdlib.h> #include <stdlib.h>
Statement::Statement(EStatementType type) : _type(type) {}; Statement::Statement(EStatementType type) : type(type) {};
SelectStatement::SelectStatement() : Statement(eSelect) {}; SelectStatement::SelectStatement() : Statement(eSelect) {};
TableRef::TableRef(ETableRefType type) : _type(type) {}; TableRef::TableRef(ETableRefType type) : type(type) {};

View File

@ -23,7 +23,7 @@ class Statement {
public: public:
Statement(EStatementType type); Statement(EStatementType type);
EStatementType _type; EStatementType type;
}; };
@ -31,10 +31,10 @@ class SelectStatement : public Statement {
public: public:
SelectStatement(); SelectStatement();
TableRef* _from_table; TableRef* from_table;
List<Expr*>* _select_list; List<Expr*>* select_list;
List<Expr*>* _group_by; List<Expr*>* group_by;
Expr* _where_clause; Expr* where_clause;
}; };
@ -51,10 +51,10 @@ class TableRef {
public: public:
TableRef(ETableRefType type); TableRef(ETableRefType type);
ETableRefType _type; ETableRefType type;
SelectStatement* _stmt; SelectStatement* stmt;
List<char*>* _table_names; List<char*>* table_names;
}; };

View File

@ -26,7 +26,7 @@ int main(int argc, char *argv[]) {
continue; continue;
} }
if (stmt->_type == eSelect) { if (stmt->type == eSelect) {
executeSelect((SelectStatement*)stmt); executeSelect((SelectStatement*)stmt);
} else { } else {
fprintf(stderr, "Only Supporting Select Statements!\n"); fprintf(stderr, "Only Supporting Select Statements!\n");
@ -64,16 +64,16 @@ Table executeSelect(SelectStatement* stmt) {
// Step 1: // Step 1:
// Determine source table // Determine source table
TableRef* from_table = stmt->_from_table; TableRef* from_table = stmt->from_table;
Table source; Table source;
if (from_table->_type == eTableSelect) { if (from_table->type == eTableSelect) {
// Nested Select Statements // Nested Select Statements
source = executeSelect(from_table->_stmt); source = executeSelect(from_table->stmt);
} else if (from_table->_type == eTableName) { } else if (from_table->type == eTableName) {
if (from_table->_table_names->size() == 1) { if (from_table->table_names->size() == 1) {
if (std::string(from_table->_table_names->at(0)).compare("table") == 0) { if (std::string(from_table->table_names->at(0)).compare("table") == 0) {
source = TABLE; source = TABLE;
} else { } else {
fprintf(stderr, "Couldn't find table!\n"); fprintf(stderr, "Couldn't find table!\n");
@ -88,7 +88,7 @@ Table executeSelect(SelectStatement* stmt) {
// Step 2 // Step 2
// Check if group by // Check if group by
if (stmt->_group_by != NULL) { if (stmt->group_by != NULL) {
return executeGroupBySelect(stmt, source); return executeGroupBySelect(stmt, source);
} }

View File

@ -39,7 +39,7 @@ void evaluate_statement(Statement* stmt) {
printf("Statement at %p\n", stmt); printf("Statement at %p\n", stmt);
if (stmt == NULL) return; if (stmt == NULL) return;
switch (stmt->_type) { switch (stmt->type) {
case eSelect: case eSelect:
evaluate_select_statement((SelectStatement*) stmt); evaluate_select_statement((SelectStatement*) stmt);
break; break;

View File

@ -21,26 +21,26 @@ void SelectTest1() {
const char* sql = "SELECT age, name, address from table WHERE age > 12.5;"; const char* sql = "SELECT age, name, address from table WHERE age > 12.5;";
Statement* sqlStatement = SQLParser::parseSQL(sql); Statement* sqlStatement = SQLParser::parseSQL(sql);
ASSERT(sqlStatement != NULL); ASSERT(sqlStatement != NULL);
ASSERT(sqlStatement->_type == eSelect); ASSERT(sqlStatement->type == eSelect);
SelectStatement* stmt = (SelectStatement*) sqlStatement; SelectStatement* stmt = (SelectStatement*) sqlStatement;
ASSERT(stmt->_select_list->size() == 3); ASSERT(stmt->select_list->size() == 3);
ASSERT_STR(stmt->_select_list->at(0)->name, "age"); ASSERT_STR(stmt->select_list->at(0)->name, "age");
ASSERT_STR(stmt->_select_list->at(1)->name, "name"); ASSERT_STR(stmt->select_list->at(1)->name, "name");
ASSERT_STR(stmt->_select_list->at(2)->name, "address"); ASSERT_STR(stmt->select_list->at(2)->name, "address");
ASSERT(stmt->_from_table != NULL); ASSERT(stmt->from_table != NULL);
ASSERT(stmt->_from_table->_type == eTableName); ASSERT(stmt->from_table->type == eTableName);
ASSERT_STR(stmt->_from_table->_table_names->at(0), "table"); ASSERT_STR(stmt->from_table->table_names->at(0), "table");
// WHERE // WHERE
ASSERT(stmt->_where_clause != NULL); ASSERT(stmt->where_clause != NULL);
ASSERT(stmt->_where_clause->expr->type == eExprColumnRef); ASSERT(stmt->where_clause->expr->type == eExprColumnRef);
ASSERT_STR(stmt->_where_clause->expr->name, "age"); ASSERT_STR(stmt->where_clause->expr->name, "age");
ASSERT_STR(stmt->_where_clause->name, ">"); ASSERT_STR(stmt->where_clause->name, ">");
ASSERT(stmt->_where_clause->expr2->type == eExprLiteralFloat); ASSERT(stmt->where_clause->expr2->type == eExprLiteralFloat);
ASSERT(stmt->_where_clause->expr2->float_literal == 12.5); ASSERT(stmt->where_clause->expr2->float_literal == 12.5);
printf("passed!\n"); printf("passed!\n");
} }
@ -52,26 +52,27 @@ void SelectTest2() {
const char* sql = "SELECT age, name, address FROM (SELECT age FROM table, table2);"; const char* sql = "SELECT age, name, address FROM (SELECT age FROM table, table2);";
Statement* stmt = SQLParser::parseSQL(sql); Statement* stmt = SQLParser::parseSQL(sql);
ASSERT(stmt != NULL); ASSERT(stmt != NULL);
ASSERT(stmt->_type == eSelect); ASSERT(stmt->type == eSelect);
SelectStatement* select = (SelectStatement*) stmt; SelectStatement* select = (SelectStatement*) stmt;
ASSERT(select->_select_list->size() == 3); ASSERT(select->select_list->size() == 3);
ASSERT_STR(select->_select_list->at(0)->name, "age"); ASSERT_STR(select->select_list->at(0)->name, "age");
ASSERT_STR(select->_select_list->at(1)->name, "name"); ASSERT_STR(select->select_list->at(1)->name, "name");
ASSERT_STR(select->_select_list->at(2)->name, "address"); ASSERT_STR(select->select_list->at(2)->name, "address");
ASSERT(select->_from_table != NULL); ASSERT(select->from_table != NULL);
ASSERT(select->_from_table->_type == eTableSelect); ASSERT(select->from_table->type == eTableSelect);
ASSERT(select->_from_table->_stmt != NULL); ASSERT(select->from_table->stmt != NULL);
ASSERT(select->_from_table->_stmt->_select_list->size() == 1); ASSERT(select->from_table->stmt->select_list->size() == 1);
ASSERT_STR(select->_from_table->_stmt->_from_table->_table_names->at(0), "table"); ASSERT_STR(select->from_table->stmt->from_table->table_names->at(0), "table");
ASSERT_STR(select->_from_table->_stmt->_from_table->_table_names->at(1), "table2"); ASSERT_STR(select->from_table->stmt->from_table->table_names->at(1), "table2");
printf("passed!\n"); printf("passed!\n");
} }
int parse_count = 0; uint parse_count = 0;
uint conflicts = 0;
void SelectTest3(bool print) { void SelectTest3(bool print) {
if (print) printf("Test: SelectTest3... "); if (print) printf("Test: SelectTest3... ");
fflush(stdout); fflush(stdout);
@ -81,24 +82,24 @@ void SelectTest3(bool print) {
Statement* stmt = SQLParser::parseSQL(sql); Statement* stmt = SQLParser::parseSQL(sql);
if (parse_count != 1) printf("+"); if (parse_count != 1) conflicts++;
parse_count--; parse_count--;
ASSERT(stmt != NULL); ASSERT(stmt != NULL);
ASSERT(stmt->_type == eSelect); ASSERT(stmt->type == eSelect);
SelectStatement* select = (SelectStatement*) stmt; SelectStatement* select = (SelectStatement*) stmt;
ASSERT(select->_select_list->size() == 2); ASSERT(select->select_list->size() == 2);
ASSERT(select->_select_list->at(0)->type == eExprColumnRef); ASSERT(select->select_list->at(0)->type == eExprColumnRef);
ASSERT(select->_select_list->at(1)->type == eExprFunctionRef); ASSERT(select->select_list->at(1)->type == eExprFunctionRef);
ASSERT_STR("name", select->_select_list->at(0)->name); ASSERT_STR("name", select->select_list->at(0)->name);
ASSERT(select->_group_by != NULL); ASSERT(select->group_by != NULL);
ASSERT(select->_group_by->size() == 1); ASSERT(select->group_by->size() == 1);
ASSERT_STR("name", select->_group_by->at(0)->name); ASSERT_STR("name", select->group_by->at(0)->name);
if (print) printf("passed!\n"); if (print) printf("passed!\n");
} }
@ -112,6 +113,7 @@ void multithreadTest(int numberOfRuns, int id) {
} }
void ThreadSafeTest(uint numThreads, uint runsPerThread) { void ThreadSafeTest(uint numThreads, uint runsPerThread) {
printf("Starting multithread-test... "); printf("Starting multithread-test... ");
conflicts = 0;
std::thread* threads = new std::thread[numThreads]; std::thread* threads = new std::thread[numThreads];
for (int n = 0; n < numThreads; ++n) { for (int n = 0; n < numThreads; ++n) {
threads[n] = std::thread(multithreadTest, runsPerThread, n); threads[n] = std::thread(multithreadTest, runsPerThread, n);
@ -119,6 +121,7 @@ void ThreadSafeTest(uint numThreads, uint runsPerThread) {
for (int n = 0; n < numThreads; ++n) { for (int n = 0; n < numThreads; ++n) {
threads[n].join(); threads[n].join();
} }
printf("there were %u concurrent parses... ", conflicts);
printf("finished!\n"); printf("finished!\n");
} }