2017-04-06 18:27:47 +02:00
|
|
|
#ifndef __SQLPARSER__EXPR_H__
|
|
|
|
#define __SQLPARSER__EXPR_H__
|
2014-10-09 04:09:47 +02:00
|
|
|
|
2014-10-20 23:19:27 +02:00
|
|
|
#include <stdlib.h>
|
2014-11-07 15:21:54 +01:00
|
|
|
#include <memory>
|
2017-03-07 02:49:29 +01:00
|
|
|
#include <vector>
|
2014-10-20 23:19:27 +02:00
|
|
|
|
2014-10-31 18:36:02 +01:00
|
|
|
namespace hsql {
|
2017-04-21 16:15:07 +02:00
|
|
|
struct SelectStatement;
|
2014-10-31 18:36:02 +01:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
// 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,
|
2017-05-29 16:22:13 +02:00
|
|
|
kExprParameter,
|
2017-02-08 02:06:15 +01:00
|
|
|
kExprColumnRef,
|
|
|
|
kExprFunctionRef,
|
2017-03-07 14:21:45 +01:00
|
|
|
kExprOperator,
|
2017-06-06 03:49:41 +02:00
|
|
|
kExprSelect,
|
|
|
|
kExprHint
|
2017-02-08 02:06:15 +01:00
|
|
|
};
|
|
|
|
|
2017-04-06 17:25:47 +02:00
|
|
|
// 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 {
|
|
|
|
kOpNone,
|
2017-03-07 14:37:05 +01:00
|
|
|
|
2017-04-06 17:25:47 +02:00
|
|
|
// Ternary operators
|
|
|
|
kOpBetween,
|
|
|
|
kOpCase,
|
2017-02-08 02:06:15 +01:00
|
|
|
|
2017-04-06 17:25:47 +02:00
|
|
|
// Binary operators.
|
2017-04-06 17:42:46 +02:00
|
|
|
// Simple operators are identified by the opChar field (e.g. +, -, =, >, <).
|
2017-04-06 17:25:47 +02:00
|
|
|
kOpSimple,
|
2017-02-08 02:06:15 +01:00
|
|
|
|
2017-04-06 17:25:47 +02:00
|
|
|
kOpNotEquals,
|
|
|
|
kOpLessEq,
|
|
|
|
kOpGreaterEq,
|
|
|
|
kOpLike,
|
|
|
|
kOpNotLike,
|
|
|
|
kOpAnd,
|
|
|
|
kOpOr,
|
|
|
|
kOpIn,
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-04-06 17:25:47 +02:00
|
|
|
// Unary operators.
|
|
|
|
kOpNot,
|
|
|
|
kOpMinus,
|
|
|
|
kOpIsNull,
|
|
|
|
kOpExists
|
|
|
|
};
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-04-06 17:25:47 +02:00
|
|
|
typedef struct Expr Expr;
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-04-06 17:25:47 +02:00
|
|
|
// 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 {
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
Expr(ExprType type);
|
|
|
|
virtual ~Expr();
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
ExprType type;
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-03-07 13:49:56 +01:00
|
|
|
// TODO: Replace expressions by list.
|
2017-02-08 02:06:15 +01:00
|
|
|
Expr* expr;
|
|
|
|
Expr* expr2;
|
2017-03-07 02:49:29 +01:00
|
|
|
std::vector<Expr*>* exprList;
|
2017-03-07 14:21:45 +01:00
|
|
|
SelectStatement* select;
|
2017-02-08 02:06:15 +01:00
|
|
|
char* name;
|
|
|
|
char* table;
|
|
|
|
char* alias;
|
|
|
|
float fval;
|
|
|
|
int64_t ival;
|
|
|
|
int64_t ival2;
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-02-08 03:07:51 +01:00
|
|
|
OperatorType opType;
|
|
|
|
char opChar;
|
2017-02-08 02:06:15 +01:00
|
|
|
bool distinct;
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-03-07 14:21:45 +01:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
// Convenience accessor methods.
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool isType(ExprType exprType) const;
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool isLiteral() const;
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool hasAlias() const;
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool hasTable() const;
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
const char* getName() const;
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool isSimpleOp() const;
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-04-06 17:42:46 +02:00
|
|
|
bool isSimpleOp(char op) const;
|
2017-02-08 01:55:59 +01:00
|
|
|
|
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
// Static constructors.
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-06-06 03:49:41 +02:00
|
|
|
static Expr* make(ExprType type);
|
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
static Expr* makeOpUnary(OperatorType op, Expr* expr);
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2);
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2);
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-03-07 13:49:56 +01:00
|
|
|
static Expr* makeBetween(Expr* expr, Expr* left, Expr* right);
|
|
|
|
|
2017-03-07 14:55:51 +01:00
|
|
|
static Expr* makeCase(Expr* expr, Expr* then, Expr* other);
|
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
static Expr* makeLiteral(int64_t val);
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
static Expr* makeLiteral(double val);
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
static Expr* makeLiteral(char* val);
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
static Expr* makeColumnRef(char* name);
|
2016-02-27 15:01:06 +01:00
|
|
|
|
2017-02-08 02:06:15 +01:00
|
|
|
static Expr* makeColumnRef(char* table, char* name);
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-03-07 02:49:29 +01:00
|
|
|
static Expr* makeFunctionRef(char* func_name, std::vector<Expr*>* exprList, bool distinct);
|
2017-02-08 01:55:59 +01:00
|
|
|
|
2017-05-29 16:22:13 +02:00
|
|
|
static Expr* makeParameter(int id);
|
2017-03-07 14:21:45 +01:00
|
|
|
|
|
|
|
static Expr* makeSelect(SelectStatement* select);
|
2017-03-07 14:37:05 +01:00
|
|
|
|
|
|
|
static Expr* makeExists(SelectStatement* select);
|
2017-03-07 15:44:44 +01:00
|
|
|
|
2017-03-07 15:53:22 +01:00
|
|
|
static Expr* makeInOperator(Expr* expr, std::vector<Expr*>* exprList);
|
2017-03-07 15:44:44 +01:00
|
|
|
|
2017-03-07 15:53:22 +01:00
|
|
|
static Expr* makeInOperator(Expr* expr, SelectStatement* select);
|
2017-02-08 02:06:15 +01:00
|
|
|
};
|
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-11-13 17:54:24 +01:00
|
|
|
// Unused
|
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-11-13 17:54:24 +01:00
|
|
|
#undef ALLOC_EXPR
|
2014-11-12 11:18:43 +01:00
|
|
|
|
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
|