remove inline functions from Expr

This commit is contained in:
Pedro 2017-02-08 01:55:59 +01:00
parent 0946624d54
commit 02b7b880ed
2 changed files with 77 additions and 66 deletions

View File

@ -5,16 +5,6 @@
namespace hsql { namespace hsql {
char* substr(const char* source, int from, int to) {
int len = to-from;
char* copy = new char[len+1];
strncpy(copy, source+from, len);
copy[len] = '\0';
return copy;
}
Expr::Expr(ExprType type) : Expr::Expr(ExprType type) :
type(type), type(type),
expr(NULL), expr(NULL),
@ -38,8 +28,6 @@ namespace hsql {
return e; return e;
} }
Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) { Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) {
Expr* e = new Expr(kExprOperator); Expr* e = new Expr(kExprOperator);
e->op_type = op; e->op_type = op;
@ -58,8 +46,6 @@ namespace hsql {
return e; return e;
} }
Expr* Expr::makeLiteral(int64_t val) { Expr* Expr::makeLiteral(int64_t val) {
Expr* e = new Expr(kExprLiteralInt); Expr* e = new Expr(kExprLiteralInt);
e->ival = val; e->ival = val;
@ -106,4 +92,40 @@ namespace hsql {
return e; return e;
} }
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() {
return op_type == SIMPLE_OP;
}
bool Expr::isSimpleOp(char op) {
return isSimpleOp() && op_char == op;
}
char* substr(const char* source, int from, int to) {
int len = to-from;
char* copy = new char[len+1];
strncpy(copy, source+from, len);
copy[len] = '\0';
return copy;
}
} // namespace hsql } // namespace hsql

View File

@ -6,12 +6,11 @@
namespace hsql { namespace hsql {
// Helper function // Helper function used by the lexer.
// TODO: move to more appropriate place.
char* substr(const char* source, int from, int to); char* substr(const char* source, int from, int to);
enum ExprType {
typedef enum {
kExprLiteralFloat, kExprLiteralFloat,
kExprLiteralString, kExprLiteralString,
kExprLiteralInt, kExprLiteralInt,
@ -20,27 +19,21 @@ namespace hsql {
kExprColumnRef, kExprColumnRef,
kExprFunctionRef, kExprFunctionRef,
kExprOperator kExprOperator
} ExprType; };
typedef struct Expr Expr; typedef struct Expr Expr;
/** // Represents SQL expressions (i.e. literals, operators, column_refs).
* Represents SQL expressions (i.e. literals, operators, column_refs) // TODO: When destructing a placeholder expression, we might need to alter the placeholder_list.
*
* TODO: When destructing a placeholder expression, we might need to alter the placeholder_list
*/
struct Expr { struct Expr {
/** // Operator types. These are important for expressions of type kExprOperator.
* Operator types. These are important for expressions of type kExprOperator // Trivial types are those that can be described by a single character e.g:
* Trivial types are those that can be described by a single character e.g: // + - * / < > = %
* + - * / < > = % // Non-trivial are: <> <= >= LIKE ISNULL NOT
* Non-trivial are: enum OperatorType {
* <> <= >= LIKE ISNULL NOT
*/
typedef enum {
SIMPLE_OP, SIMPLE_OP,
// Binary
// Binary operators.
NOT_EQUALS, NOT_EQUALS,
LESS_EQ, LESS_EQ,
GREATER_EQ, GREATER_EQ,
@ -48,18 +41,20 @@ namespace hsql {
NOT_LIKE, NOT_LIKE,
AND, AND,
OR, OR,
// Unary
// Unary operators.
NOT, NOT,
UMINUS, UMINUS,
ISNULL ISNULL
} OperatorType; };
Expr(ExprType type); Expr(ExprType type);
// Interesting side-effect: // Interesting side-effect:
// Making the destructor virtual used to cause segmentation faults // Making the destructor virtual used to cause segmentation faults.
// TODO: inspect.
~Expr(); ~Expr();
ExprType type; ExprType type;
@ -77,47 +72,41 @@ namespace hsql {
char op_char; char op_char;
bool distinct; bool distinct;
// Convenience accessor methods.
/** bool isType(ExprType e_type);
* Convenience accessor methods
*/ bool isLiteral();
inline bool isType(ExprType e_type) {
return e_type == type; bool hasAlias();
}
inline bool isLiteral() { bool hasTable();
return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprPlaceholder);
} char* getName();
inline bool hasAlias() {
return alias != NULL; bool isSimpleOp();
}
inline bool hasTable() { bool isSimpleOp(char op);
return table != NULL;
}
inline char* getName() {
if (alias != NULL) return alias;
else return name;
}
inline bool isSimpleOp() {
return op_type == SIMPLE_OP;
}
inline bool isSimpleOp(char op) {
return isSimpleOp() && op_char == op;
}
/** // Static constructors.
* Static expression constructors
*/
static Expr* makeOpUnary(OperatorType op, Expr* expr); static Expr* makeOpUnary(OperatorType op, Expr* expr);
static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2); static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2);
static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2); static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2);
static Expr* makeLiteral(int64_t val); static Expr* makeLiteral(int64_t val);
static Expr* makeLiteral(double val); static Expr* makeLiteral(double val);
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* makeColumnRef(char* table, char* name);
static Expr* makeFunctionRef(char* func_name, Expr* expr, bool distinct); static Expr* makeFunctionRef(char* func_name, Expr* expr, bool distinct);
static Expr* makePlaceholder(int id); static Expr* makePlaceholder(int id);