-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathExpr.h
More file actions
157 lines (110 loc) · 3.33 KB
/
Copy pathExpr.h
File metadata and controls
157 lines (110 loc) · 3.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#ifndef __EXPRESSION_H__
#define __EXPRESSION_H__
#include <stdlib.h>
#include <memory>
#include <vector>
namespace hsql {
class SelectStatement;
// Helper function used by the lexer.
// TODO: move to more appropriate place.
char* substr(const char* source, int from, int to);
enum ExprType {
kExprLiteralFloat,
kExprLiteralString,
kExprLiteralInt,
kExprStar,
kExprPlaceholder,
kExprColumnRef,
kExprFunctionRef,
kExprOperator,
kExprSelect
};
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.
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
enum OperatorType {
NONE,
// Ternary operators
BETWEEN,
CASE,
// Binary operators.
SIMPLE_OP,
NOT_EQUALS,
LESS_EQ,
GREATER_EQ,
LIKE,
NOT_LIKE,
AND,
OR,
IN,
// Unary operators.
NOT,
UMINUS,
ISNULL,
EXISTS
};
Expr(ExprType type);
// Interesting side-effect:
// Making the destructor virtual used to cause segmentation faults.
// TODO: inspect.
~Expr();
ExprType type;
// TODO: Replace expressions by list.
Expr* expr;
Expr* expr2;
std::vector<Expr*>* exprList;
SelectStatement* select;
char* name;
char* table;
char* alias;
float fval;
int64_t ival;
int64_t ival2;
OperatorType opType;
char opChar;
bool distinct;
// Convenience accessor methods.
bool isType(ExprType e_type);
bool isLiteral();
bool hasAlias();
bool hasTable();
char* getName();
bool isSimpleOp();
bool isSimpleOp(char op);
// 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* makeBetween(Expr* expr, Expr* left, Expr* right);
static Expr* makeCase(Expr* expr, Expr* then, Expr* other);
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, std::vector<Expr*>* exprList, bool distinct);
static Expr* makePlaceholder(int id);
static Expr* makeSelect(SelectStatement* select);
static Expr* makeExists(SelectStatement* select);
static Expr* makeInOperator(Expr* expr, std::vector<Expr*>* exprList);
static Expr* makeInOperator(Expr* expr, SelectStatement* select);
};
// Zero initializes an Expr object and assigns it to a space in the heap
// For Hyrise we still had to put in the explicit NULL constructor
// http://www.ex-parrot.com/~chris/random/initialise.html
// Unused
#define ALLOC_EXPR(var, type) \
Expr* var; \
do { \
Expr zero = {type}; \
var = (Expr*)malloc(sizeof *var); \
*var = zero; \
} while(0);
#undef ALLOC_EXPR
} // namespace hsql
#endif