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),
|
2017-06-06 03:49:41 +02:00
|
|
|
expr(nullptr),
|
|
|
|
expr2(nullptr),
|
|
|
|
exprList(nullptr),
|
|
|
|
select(nullptr),
|
|
|
|
name(nullptr),
|
|
|
|
table(nullptr),
|
|
|
|
alias(nullptr) {};
|
2017-02-08 02:06:15 +01:00
|
|
|
|
|
|
|
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
|
|
|
|
2017-06-06 03:49:41 +02:00
|
|
|
if (exprList != nullptr) {
|
2017-03-07 15:09:39 +01:00
|
|
|
for (Expr* e : *exprList) {
|
|
|
|
delete e;
|
|
|
|
}
|
|
|
|
delete exprList;
|
|
|
|
}
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
|
|
|
|
2017-06-06 03:49:41 +02:00
|
|
|
Expr* Expr::make(ExprType type) {
|
|
|
|
Expr* e = new Expr(type);
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
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;
|
2017-06-06 03:49:41 +02:00
|
|
|
e->expr2 = nullptr;
|
2017-02-08 02:06:15 +01:00
|
|
|
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;
|
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-04-06 17:25:47 +02:00
|
|
|
e->opType = kOpBetween;
|
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;
|
2017-04-06 17:25:47 +02:00
|
|
|
e->opType = kOpCase;
|
2017-03-07 14:55:51 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
Expr* Expr::makeParameter(int id) {
|
|
|
|
Expr* e = new Expr(kExprParameter);
|
2017-02-08 02:06:15 +01:00
|
|
|
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);
|
2017-04-06 17:25:47 +02:00
|
|
|
e->opType = kOpExists;
|
2017-03-07 14:37:05 +01:00
|
|
|
e->select = select;
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-03-07 15:53:22 +01:00
|
|
|
Expr* Expr::makeInOperator(Expr* expr, std::vector<Expr*>* exprList) {
|
2017-03-07 15:44:44 +01:00
|
|
|
Expr* e = new Expr(kExprOperator);
|
2017-04-06 17:25:47 +02:00
|
|
|
e->opType = kOpIn;
|
2017-03-07 15:44:44 +01:00
|
|
|
e->expr = expr;
|
|
|
|
e->exprList = exprList;
|
|
|
|
|
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-03-07 15:53:22 +01:00
|
|
|
Expr* Expr::makeInOperator(Expr* expr, SelectStatement* select) {
|
2017-03-07 15:44:44 +01:00
|
|
|
Expr* e = new Expr(kExprOperator);
|
2017-04-06 17:25:47 +02:00
|
|
|
e->opType = kOpIn;
|
2017-03-07 15:44:44 +01:00
|
|
|
e->expr = expr;
|
|
|
|
e->select = select;
|
2017-03-08 16:55:29 +01:00
|
|
|
|
2017-03-07 15:44:44 +01:00
|
|
|
return e;
|
|
|
|
}
|
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool Expr::isType(ExprType exprType) const {
|
|
|
|
return exprType == type;
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool Expr::isLiteral() const {
|
2017-05-29 16:22:13 +02:00
|
|
|
return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprParameter);
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool Expr::hasAlias() const {
|
2017-06-06 03:49:41 +02:00
|
|
|
return alias != nullptr;
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool Expr::hasTable() const {
|
2017-06-06 03:49:41 +02:00
|
|
|
return table != nullptr;
|
2017-02-08 02:06:15 +01:00
|
|
|
}
|
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
const char* Expr::getName() const {
|
2017-06-06 03:49:41 +02:00
|
|
|
if (alias != nullptr) return alias;
|
2017-02-08 02:06:15 +01:00
|
|
|
else return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|