2014-10-09 04:26:38 +02:00
|
|
|
|
|
|
|
#include "Expr.h"
|
2014-10-09 04:46:25 +02:00
|
|
|
#include <stdio.h>
|
2014-10-24 16:10:38 +02:00
|
|
|
#include <string.h>
|
2017-03-07 15:09:39 +01:00
|
|
|
#include "SelectStatement.h"
|
2014-10-24 16:10:38 +02:00
|
|
|
|
2014-10-31 18:36:02 +01:00
|
|
|
namespace hsql {
|
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
Expr::Expr(ExprType type) :
|
|
|
|
type(type),
|
|
|
|
expr(NULL),
|
|
|
|
expr2(NULL),
|
2017-03-07 15:09:39 +01:00
|
|
|
exprList(NULL),
|
|
|
|
select(NULL),
|
2017-02-08 02:06:15 +01:00
|
|
|
name(NULL),
|
|
|
|
table(NULL),
|
|
|
|
alias(NULL) {};
|
|
|
|
|
|
|
|
Expr::~Expr() {
|
|
|
|
delete expr;
|
|
|
|
delete expr2;
|
2017-03-07 15:09:39 +01:00
|
|
|
delete select;
|
2017-02-08 03:54:38 +01:00
|
|
|
free(name);
|
|
|
|
free(table);
|
|
|
|
free(alias);
|
2017-03-07 15:09:39 +01:00
|
|
|
|
|
|
|
if (exprList != NULL) {
|
|
|
|
for (Expr* e : *exprList) {
|
|
|
|
delete e;
|
|
|
|
}
|
|
|
|
delete exprList;
|
|
|
|
}
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) {
|
|
|
|
Expr* e = new Expr(kExprOperator);
|
2017-02-08 03:07:51 +01:00
|
|
|
e->opType = op;
|
2017-02-08 02:06:15 +01:00
|
|
|
e->expr = expr;
|
|
|
|
e->expr2 = NULL;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) {
|
|
|
|
Expr* e = new Expr(kExprOperator);
|
2017-02-08 03:07:51 +01:00
|
|
|
e->opType = op;
|
|
|
|
e->opChar = 0;
|
2017-02-08 02:06:15 +01:00
|
|
|
e->expr = expr1;
|
|
|
|
e->expr2 = expr2;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr* Expr::makeOpBinary(Expr* expr1, char op, Expr* expr2) {
|
|
|
|
Expr* e = new Expr(kExprOperator);
|
2017-02-08 03:07:51 +01:00
|
|
|
e->opType = SIMPLE_OP;
|
|
|
|
e->opChar = op;
|
2017-02-08 02:06:15 +01:00
|
|
|
e->expr = expr1;
|
|
|
|
e->expr2 = expr2;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-03-07 13:49:56 +01:00
|
|
|
Expr* Expr::makeBetween(Expr* expr, Expr* left, Expr* right) {
|
|
|
|
Expr* e = new Expr(kExprOperator);
|
|
|
|
e->expr = expr;
|
2017-03-07 14:37:05 +01:00
|
|
|
e->opType = BETWEEN;
|
2017-03-07 13:49:56 +01:00
|
|
|
e->exprList = new std::vector<Expr*>();
|
|
|
|
e->exprList->push_back(left);
|
|
|
|
e->exprList->push_back(right);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:55:51 +01:00
|
|
|
Expr* Expr::makeCase(Expr* expr, Expr* then, Expr* other) {
|
|
|
|
Expr* e = new Expr(kExprOperator);
|
|
|
|
e->expr = expr;
|
|
|
|
e->opType = CASE;
|
|
|
|
e->exprList = new std::vector<Expr*>();
|
|
|
|
e->exprList->push_back(then);
|
|
|
|
e->exprList->push_back(other);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
Expr* Expr::makeLiteral(int64_t val) {
|
|
|
|
Expr* e = new Expr(kExprLiteralInt);
|
|
|
|
e->ival = val;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr* Expr::makeLiteral(double value) {
|
|
|
|
Expr* e = new Expr(kExprLiteralFloat);
|
|
|
|
e->fval = value;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr* Expr::makeLiteral(char* string) {
|
|
|
|
Expr* e = new Expr(kExprLiteralString);
|
|
|
|
e->name = string;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
Expr* Expr::makeColumnRef(char* name) {
|
|
|
|
Expr* e = new Expr(kExprColumnRef);
|
|
|
|
e->name = name;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr* Expr::makeColumnRef(char* table, char* name) {
|
|
|
|
Expr* e = new Expr(kExprColumnRef);
|
|
|
|
e->name = name;
|
|
|
|
e->table = table;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-03-07 02:49:29 +01:00
|
|
|
Expr* Expr::makeFunctionRef(char* func_name, std::vector<Expr*>* exprList, bool distinct) {
|
2017-02-08 02:06:15 +01:00
|
|
|
Expr* e = new Expr(kExprFunctionRef);
|
|
|
|
e->name = func_name;
|
2017-03-07 02:49:29 +01:00
|
|
|
e->exprList = exprList;
|
2017-02-08 02:06:15 +01:00
|
|
|
e->distinct = distinct;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr* Expr::makePlaceholder(int id) {
|
|
|
|
Expr* e = new Expr(kExprPlaceholder);
|
|
|
|
e->ival = id;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:21:45 +01:00
|
|
|
Expr* Expr::makeSelect(SelectStatement* select) {
|
|
|
|
Expr* e = new Expr(kExprSelect);
|
|
|
|
e->select = select;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:37:05 +01:00
|
|
|
Expr* Expr::makeExists(SelectStatement* select) {
|
|
|
|
Expr* e = new Expr(kExprOperator);
|
|
|
|
e->opType = EXISTS;
|
|
|
|
e->select = select;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-03-07 15:44:44 +01:00
|
|
|
Expr* Expr::makeInOperator(Expr* expr, std::vector<Expr*>* exprList, bool notIn) {
|
|
|
|
Expr* e = new Expr(kExprOperator);
|
|
|
|
e->opType = IN;
|
|
|
|
e->expr = expr;
|
|
|
|
e->exprList = exprList;
|
|
|
|
|
|
|
|
// If it is NOT IN, wrap the IN in a NOT operator.
|
|
|
|
if (notIn) {
|
|
|
|
Expr* neg = new Expr(kExprOperator);
|
|
|
|
neg->opType = NOT;
|
|
|
|
neg->expr = e;
|
|
|
|
return neg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
|
|
|
Expr* Expr::makeInOperator(Expr* expr, SelectStatement* select, bool notIn) {
|
|
|
|
Expr* e = new Expr(kExprOperator);
|
|
|
|
e->opType = IN;
|
|
|
|
e->expr = expr;
|
|
|
|
e->select = select;
|
|
|
|
|
|
|
|
// If it is NOT IN, wrap the IN in a NOT operator.
|
|
|
|
if (notIn) {
|
|
|
|
Expr* neg = new Expr(kExprOperator);
|
|
|
|
neg->opType = NOT;
|
|
|
|
neg->expr = e;
|
|
|
|
return neg;
|
|
|
|
}
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
bool Expr::isType(ExprType e_type) {
|
|
|
|
return e_type == type;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr::isLiteral() {
|
|
|
|
return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprPlaceholder);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr::hasAlias() {
|
|
|
|
return alias != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr::hasTable() {
|
|
|
|
return table != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
char* Expr::getName() {
|
|
|
|
if (alias != NULL) return alias;
|
|
|
|
else return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr::isSimpleOp() {
|
2017-02-08 03:07:51 +01:00
|
|
|
return opType == SIMPLE_OP;
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Expr::isSimpleOp(char op) {
|
2017-02-08 03:07:51 +01:00
|
|
|
return isSimpleOp() && opChar == op;
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
char* substr(const char* source, int from, int to) {
|
|
|
|
int len = to - from;
|
2017-02-08 04:10:26 +01:00
|
|
|
char* copy = (char*) malloc(len + 1);;
|
2017-02-08 02:06:15 +01:00
|
|
|
strncpy(copy, source + from, len);
|
|
|
|
copy[len] = '\0';
|
|
|
|
return copy;
|
|
|
|
}
|
2017-02-08 01:55:59 +01:00
|
|
|
} // namespace hsql
|