HyriseSQLParser/src/sql/Expr.h

158 lines
3.4 KiB
C
Raw Normal View History

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>
#include <vector>
2014-10-20 23:19:27 +02:00
namespace hsql {
struct 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,
kExprParameter,
kExprColumnRef,
kExprFunctionRef,
kExprOperator,
2017-06-06 03:49:41 +02:00
kExprSelect,
kExprHint
};
// 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
// Ternary operators
kOpBetween,
kOpCase,
// Binary operators.
// Simple operators are identified by the opChar field (e.g. +, -, =, >, <).
kOpSimple,
kOpNotEquals,
kOpLessEq,
kOpGreaterEq,
kOpLike,
kOpNotLike,
kOpAnd,
kOpOr,
kOpIn,
2016-02-27 15:01:06 +01:00
// Unary operators.
kOpNot,
kOpMinus,
kOpIsNull,
kOpExists
};
2016-02-27 15:01:06 +01:00
typedef struct Expr Expr;
2017-02-08 01:55:59 +01: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
Expr(ExprType type);
virtual ~Expr();
2016-02-27 15:01:06 +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.
Expr* expr;
Expr* expr2;
std::vector<Expr*>* exprList;
SelectStatement* select;
char* name;
char* table;
char* alias;
float fval;
int64_t ival;
int64_t ival2;
2016-02-27 15:01:06 +01:00
OperatorType opType;
char opChar;
bool distinct;
2016-02-27 15:01:06 +01:00
// Convenience accessor methods.
2016-02-27 15:01:06 +01:00
bool isType(ExprType exprType) const;
2016-02-27 15:01:06 +01:00
bool isLiteral() const;
2016-02-27 15:01:06 +01:00
bool hasAlias() const;
2016-02-27 15:01:06 +01:00
bool hasTable() const;
2017-02-08 01:55:59 +01:00
const char* getName() const;
2017-02-08 01:55:59 +01:00
bool isSimpleOp() const;
2017-02-08 01:55:59 +01:00
bool isSimpleOp(char op) const;
2017-02-08 01:55:59 +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);
static Expr* makeOpUnary(OperatorType op, Expr* expr);
2017-02-08 01:55:59 +01:00
static Expr* makeOpBinary(Expr* expr1, char op, Expr* expr2);
2017-02-08 01:55:59 +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);
static Expr* makeLiteral(int64_t val);
2016-02-27 15:01:06 +01:00
static Expr* makeLiteral(double val);
2017-02-08 01:55:59 +01:00
static Expr* makeLiteral(char* val);
2017-02-08 01:55:59 +01:00
static Expr* makeColumnRef(char* name);
2016-02-27 15:01:06 +01:00
static Expr* makeColumnRef(char* table, char* name);
2017-02-08 01:55:59 +01:00
static Expr* makeFunctionRef(char* func_name, std::vector<Expr*>* exprList, bool distinct);
2017-02-08 01:55:59 +01:00
static Expr* makeParameter(int id);
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);
};
2014-10-09 04:09:47 +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
#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
} // namespace hsql
2014-10-20 23:19:27 +02:00
#endif