diff --git a/src/sql/Expr.cpp b/src/sql/Expr.cpp index df8a259..f4c36e8 100644 --- a/src/sql/Expr.cpp +++ b/src/sql/Expr.cpp @@ -5,16 +5,6 @@ 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) : type(type), expr(NULL), @@ -38,8 +28,6 @@ namespace hsql { return e; } - - Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) { Expr* e = new Expr(kExprOperator); e->op_type = op; @@ -58,8 +46,6 @@ namespace hsql { return e; } - - Expr* Expr::makeLiteral(int64_t val) { Expr* e = new Expr(kExprLiteralInt); e->ival = val; @@ -106,4 +92,40 @@ namespace hsql { return e; } -} // namespace hsql \ No newline at end of file + 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 diff --git a/src/sql/Expr.h b/src/sql/Expr.h index 84a1d6f..d594e0e 100644 --- a/src/sql/Expr.h +++ b/src/sql/Expr.h @@ -6,12 +6,11 @@ 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); - - - typedef enum { + enum ExprType { kExprLiteralFloat, kExprLiteralString, kExprLiteralInt, @@ -20,27 +19,21 @@ namespace hsql { kExprColumnRef, kExprFunctionRef, kExprOperator - } ExprType; - + }; typedef struct Expr Expr; - /** - * Represents SQL expressions (i.e. literals, operators, column_refs) - * - * TODO: When destructing a placeholder expression, we might need to alter the placeholder_list - */ + // Represents SQL expressions (i.e. literals, operators, column_refs). + // TODO: When destructing a placeholder expression, we might need to alter the placeholder_list. struct Expr { - /** - * Operator types. These are important for expressions of type kExprOperator - * Trivial types are those that can be described by a single character e.g: - * + - * / < > = % - * Non-trivial are: - * <> <= >= LIKE ISNULL NOT - */ - typedef enum { + // Operator types. These are important for expressions of type kExprOperator. + // Trivial types are those that can be described by a single character e.g: + // + - * / < > = % + // Non-trivial are: <> <= >= LIKE ISNULL NOT + enum OperatorType { SIMPLE_OP, - // Binary + + // Binary operators. NOT_EQUALS, LESS_EQ, GREATER_EQ, @@ -48,18 +41,20 @@ namespace hsql { NOT_LIKE, AND, OR, - // Unary + + // Unary operators. NOT, UMINUS, ISNULL - } OperatorType; + }; Expr(ExprType type); // Interesting side-effect: - // Making the destructor virtual used to cause segmentation faults + // Making the destructor virtual used to cause segmentation faults. + // TODO: inspect. ~Expr(); ExprType type; @@ -77,47 +72,41 @@ namespace hsql { char op_char; bool distinct; + // Convenience accessor methods. - /** - * Convenience accessor methods - */ - inline bool isType(ExprType e_type) { - return e_type == type; - } - inline bool isLiteral() { - return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprPlaceholder); - } - inline bool hasAlias() { - return alias != NULL; - } - inline bool hasTable() { - 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; - } + bool isType(ExprType e_type); + + bool isLiteral(); + + bool hasAlias(); + + bool hasTable(); + + char* getName(); + + bool isSimpleOp(); + + bool isSimpleOp(char op); - /** - * Static expression constructors - */ + // Static constructors. + static Expr* makeOpUnary(OperatorType op, Expr* expr); + static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2); + static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2); static Expr* makeLiteral(int64_t val); + static Expr* makeLiteral(double val); + static Expr* makeLiteral(char* val); static Expr* makeColumnRef(char* name); + static Expr* makeColumnRef(char* table, char* name); + static Expr* makeFunctionRef(char* func_name, Expr* expr, bool distinct); static Expr* makePlaceholder(int id);