allowing column ref with table specified

This commit is contained in:
Pedro 2014-11-04 00:40:30 +01:00
parent 33e04efcab
commit b06ea85ff5
6 changed files with 19 additions and 2 deletions

View File

@ -21,7 +21,7 @@ echo "\n\n"
# ./bin/analysis "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3" # ./bin/analysis "SELECT a FROM foo WHERE a > 12 OR b > 3 AND c = 3"
./bin/analysis "SELECT col1, col2, 'test' FROM table, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1 ORDER BY col2 DESC LIMIT 100;" ./bin/analysis "SELECT col1, col2, 'test' FROM table, foo WHERE age > 12 AND zipcode = 12345 GROUP BY col1 ORDER BY col2 DESC LIMIT 100;"
./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON a = b" ./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON t1.a = t2.b"
# ./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON a = b" # ./bin/analysis "SELECT * from table WHERE (b OR NOT a) AND a = 12.5 JOIN table2 ON a = b"
echo "\n\n" echo "\n\n"

View File

@ -72,6 +72,13 @@ Expr* Expr::makeColumnRef(char* name) {
return e; return e;
} }
Expr* Expr::makeColumnRef(char* table, char* name) {
ALLOC_EXPR(e, kExprTableColumnRef);
e->name = name;
e->table = table;
return e;
}
Expr* Expr::makeFunctionRef(char* func_name, Expr* expr) { Expr* Expr::makeFunctionRef(char* func_name, Expr* expr) {
ALLOC_EXPR(e, kExprFunctionRef); ALLOC_EXPR(e, kExprFunctionRef);
e->name = func_name; e->name = func_name;

View File

@ -11,6 +11,7 @@ typedef enum {
kExprLiteralInt, kExprLiteralInt,
kExprStar, kExprStar,
kExprColumnRef, kExprColumnRef,
kExprTableColumnRef,
kExprFunctionRef, kExprFunctionRef,
kExprOperator kExprOperator
} ExprType; } ExprType;
@ -49,6 +50,7 @@ struct Expr {
Expr* expr; Expr* expr;
Expr* expr2; Expr* expr2;
char* name; char* name;
char* table;
float fval; float fval;
int64_t ival; int64_t ival;
@ -64,6 +66,7 @@ struct Expr {
static Expr* makeLiteral(char* val); static Expr* makeLiteral(char* val);
static Expr* makeColumnRef(char* name); static Expr* makeColumnRef(char* name);
static Expr* makeColumnRef(char* table, char* name);
static Expr* makeFunctionRef(char* func_name, Expr* expr); static Expr* makeFunctionRef(char* func_name, Expr* expr);
}; };

View File

@ -12,6 +12,7 @@ const char* indent(uint num_indent) { return std::string(num_indent, '\t').c_str
void inprint(int64_t val, uint num_indent) { printf("%s%ld \n", indent(num_indent), val); } void inprint(int64_t val, uint num_indent) { printf("%s%ld \n", indent(num_indent), val); }
void inprint(float val, uint num_indent) { printf("%s%f\n", indent(num_indent), val); } void inprint(float val, uint num_indent) { printf("%s%f\n", indent(num_indent), val); }
void inprint(const char* val, uint num_indent) { printf("%s%s\n", indent(num_indent), val); } void inprint(const char* val, uint num_indent) { printf("%s%s\n", indent(num_indent), val); }
void inprint(const char* val, const char* val2, uint num_indent) { printf("%s%s->%s\n", indent(num_indent), val, val2); }
void inprintC(char val, uint num_indent) { printf("%s%c\n", indent(num_indent), val); } void inprintC(char val, uint num_indent) { printf("%s%c\n", indent(num_indent), val); }
void inprintU(uint64_t val, uint num_indent) { printf("%s%lu\n", indent(num_indent), val); } void inprintU(uint64_t val, uint num_indent) { printf("%s%lu\n", indent(num_indent), val); }
@ -47,6 +48,7 @@ void printExpression(Expr* expr, uint num_indent) {
switch (expr->type) { switch (expr->type) {
case kExprStar: inprint("*", num_indent); break; case kExprStar: inprint("*", num_indent); break;
case kExprColumnRef: inprint(expr->name, num_indent); break; case kExprColumnRef: inprint(expr->name, num_indent); break;
case kExprTableColumnRef: inprint(expr->table, expr->name, num_indent); break;
case kExprLiteralFloat: inprint(expr->fval, num_indent); break; case kExprLiteralFloat: inprint(expr->fval, num_indent); break;
case kExprLiteralInt: inprint(expr->ival, num_indent); break; case kExprLiteralInt: inprint(expr->ival, num_indent); break;
case kExprLiteralString: inprint(expr->name, num_indent); break; case kExprLiteralString: inprint(expr->name, num_indent); break;

View File

@ -291,6 +291,7 @@ function_expr:
column_name: column_name:
NAME { $$ = Expr::makeColumnRef($1); } NAME { $$ = Expr::makeColumnRef($1); }
| NAME '.' NAME { $$ = Expr::makeColumnRef($1, $3); }
; ;
literal: literal:

View File

@ -27,10 +27,14 @@ int main(int argc, char *argv[]) {
printSelectStatementInfo((SelectStatement*) stmt, 0); printSelectStatementInfo((SelectStatement*) stmt, 0);
} else if (stmt->type == kStmtJoin) { } else if (stmt->type == kStmtJoin) {
printJoinStatementInfo((JoinStatement*) stmt, 0); printJoinStatementInfo((JoinStatement*) stmt, 0);
} else {
if (stmt->type == kStmtError) {
fprintf(stderr, "%s!\n", stmt->parser_msg);
} else { } else {
fprintf(stderr, "Unsupported Statement Type %u!\n", stmt->type); fprintf(stderr, "Unsupported Statement Type %u!\n", stmt->type);
} }
} }
}
return 0; return 0;
} }