2014-10-09 04:09:47 +02:00
|
|
|
#ifndef __EXPRESSION_H__
|
|
|
|
#define __EXPRESSION_H__
|
|
|
|
|
2014-10-20 23:19:27 +02:00
|
|
|
#include <stdlib.h>
|
2014-11-07 15:21:54 +01:00
|
|
|
#include <memory>
|
2014-10-20 23:19:27 +02:00
|
|
|
|
2014-10-31 18:36:02 +01:00
|
|
|
namespace hsql {
|
|
|
|
|
2014-11-07 16:29:46 +01:00
|
|
|
// Helper function
|
|
|
|
char* substr(const char* source, int from, int to);
|
|
|
|
|
|
|
|
|
|
|
|
|
2014-10-09 04:26:38 +02:00
|
|
|
typedef enum {
|
2014-10-27 11:23:31 +01:00
|
|
|
kExprLiteralFloat,
|
|
|
|
kExprLiteralString,
|
2014-10-27 13:54:16 +01:00
|
|
|
kExprLiteralInt,
|
2014-10-27 11:23:31 +01:00
|
|
|
kExprStar,
|
|
|
|
kExprColumnRef,
|
2014-11-04 00:40:30 +01:00
|
|
|
kExprTableColumnRef,
|
2014-10-27 11:23:31 +01:00
|
|
|
kExprFunctionRef,
|
|
|
|
kExprOperator
|
2014-10-24 16:10:38 +02:00
|
|
|
} ExprType;
|
|
|
|
|
2014-10-31 18:05:08 +01:00
|
|
|
typedef struct Expr Expr;
|
2014-10-24 16:10:38 +02:00
|
|
|
|
2014-10-31 18:05:08 +01:00
|
|
|
struct Expr {
|
|
|
|
/**
|
|
|
|
* Operator types. These are important for expressions of type kExprOperator
|
|
|
|
* Trivial types are those that can be descriped by a sigle character e.g:
|
|
|
|
* + - * / < > = %
|
|
|
|
* Non-trivial are:
|
|
|
|
* <> <= >= LIKE ISNULL NOT
|
|
|
|
*/
|
|
|
|
typedef enum {
|
|
|
|
SIMPLE_OP,
|
|
|
|
// Binary
|
|
|
|
NOT_EQUALS,
|
|
|
|
LESS_EQ,
|
|
|
|
GREATER_EQ,
|
|
|
|
LIKE,
|
2014-11-05 15:54:41 +01:00
|
|
|
NOT_LIKE,
|
2014-10-31 18:05:08 +01:00
|
|
|
AND,
|
|
|
|
OR,
|
|
|
|
// Unary
|
|
|
|
NOT,
|
|
|
|
UMINUS,
|
|
|
|
ISNULL
|
|
|
|
} OperatorType;
|
2014-10-09 04:09:47 +02:00
|
|
|
|
2014-10-09 04:26:38 +02:00
|
|
|
|
2014-10-24 16:10:38 +02:00
|
|
|
|
2014-11-13 01:27:47 +01:00
|
|
|
Expr(ExprType type) :
|
|
|
|
type(type),
|
|
|
|
expr(NULL),
|
|
|
|
expr2(NULL),
|
|
|
|
name(NULL),
|
|
|
|
table(NULL),
|
|
|
|
alias(NULL) {};
|
2014-11-07 15:21:54 +01:00
|
|
|
|
|
|
|
// Interesting side-effect:
|
|
|
|
// Making the destructor virtual causes segmentation faults
|
|
|
|
~Expr();
|
2014-10-22 17:46:52 +02:00
|
|
|
|
2014-10-24 16:10:38 +02:00
|
|
|
ExprType type;
|
2014-10-09 04:09:47 +02:00
|
|
|
|
2014-10-09 04:26:38 +02:00
|
|
|
Expr* expr;
|
2014-10-09 04:46:25 +02:00
|
|
|
Expr* expr2;
|
2014-10-22 16:01:25 +02:00
|
|
|
char* name;
|
2014-11-04 00:40:30 +01:00
|
|
|
char* table;
|
2014-11-12 10:43:10 +01:00
|
|
|
char* alias;
|
2014-10-31 18:05:08 +01:00
|
|
|
float fval;
|
2014-10-27 13:54:16 +01:00
|
|
|
int64_t ival;
|
2014-10-24 16:10:38 +02:00
|
|
|
|
|
|
|
OperatorType op_type;
|
|
|
|
char op_char;
|
|
|
|
|
|
|
|
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);
|
2014-10-27 13:54:16 +01:00
|
|
|
|
|
|
|
static Expr* makeLiteral(int64_t val);
|
|
|
|
static Expr* makeLiteral(double val);
|
|
|
|
static Expr* makeLiteral(char* val);
|
2014-10-31 18:05:08 +01:00
|
|
|
|
|
|
|
static Expr* makeColumnRef(char* name);
|
2014-11-04 00:40:30 +01:00
|
|
|
static Expr* makeColumnRef(char* table, char* name);
|
2014-10-31 18:05:08 +01:00
|
|
|
static Expr* makeFunctionRef(char* func_name, Expr* expr);
|
2014-10-09 04:09:47 +02:00
|
|
|
};
|
|
|
|
|
2014-10-09 04:26:38 +02:00
|
|
|
// Zero initializes an Expr object and assigns it to a space in the heap
|
2014-11-12 11:18:43 +01:00
|
|
|
// For Hyrise we still had to put in the explicit NULL constructor
|
2014-10-09 04:46:25 +02:00
|
|
|
// http://www.ex-parrot.com/~chris/random/initialise.html
|
2014-10-09 04:26:38 +02:00
|
|
|
#define ALLOC_EXPR(var, type) \
|
|
|
|
Expr* var; \
|
|
|
|
do { \
|
|
|
|
Expr zero = {type}; \
|
|
|
|
var = (Expr*)malloc(sizeof *var); \
|
|
|
|
*var = zero; \
|
2014-11-12 11:18:43 +01:00
|
|
|
} while(0);
|
|
|
|
|
2014-10-09 04:26:38 +02:00
|
|
|
|
2014-10-31 18:36:02 +01:00
|
|
|
} // namespace hsql
|
|
|
|
|
2014-10-20 23:19:27 +02:00
|
|
|
#endif
|