fix(SQLParser): move to Data dir; add extradirs, remove vs 140,150 build scripts generation

This commit is contained in:
Alex Fabijanic
2024-02-13 14:04:23 +01:00
parent 3261ebbd42
commit ee39b611f2
167 changed files with 780 additions and 792 deletions

View File

@@ -0,0 +1,40 @@
#ifndef SQLPARSER_ALTER_STATEMENT_H
#define SQLPARSER_ALTER_STATEMENT_H
#include "SQLStatement.h"
// Note: Implementations of constructors and destructors can be found in statements.cpp.
namespace hsql {
enum ActionType {
DropColumn,
};
struct AlterAction {
AlterAction(ActionType type);
ActionType type;
virtual ~AlterAction();
};
struct DropColumnAction : AlterAction {
DropColumnAction(char* column_name);
char* columnName;
bool ifExists;
~DropColumnAction() override;
};
// Represents SQL Alter Table statements.
// Example "ALTER TABLE students DROP COLUMN name;"
struct SQLParser_API AlterStatement : SQLStatement {
AlterStatement(char* name, AlterAction* action);
~AlterStatement() override;
char* schema;
bool ifTableExists;
char* name;
AlterAction* action;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,43 @@
#ifndef SQLPARSER_COLUMN_TYPE_H
#define SQLPARSER_COLUMN_TYPE_H
#include"sqlparser_win.h"
#include <ostream>
namespace hsql {
enum class DataType {
UNKNOWN,
BIGINT,
BOOLEAN,
CHAR,
DATE,
DATETIME,
DECIMAL,
DOUBLE,
FLOAT,
INT,
LONG,
REAL,
SMALLINT,
TEXT,
TIME,
VARCHAR,
};
// Represents the type of a column, e.g., FLOAT or VARCHAR(10)
struct SQLParser_API ColumnType {
ColumnType() = default;
ColumnType(DataType data_type, int64_t length = 0, int64_t precision = 0, int64_t scale = 0);
DataType data_type;
int64_t length; // Used for, e.g., VARCHAR(10)
int64_t precision; // Used for, e.g., DECIMAL (6, 4) or TIME (5)
int64_t scale; // Used for DECIMAL (6, 4)
};
bool operator==(const ColumnType& lhs, const ColumnType& rhs);
bool operator!=(const ColumnType& lhs, const ColumnType& rhs);
std::ostream& operator<<(std::ostream&, const ColumnType&);
} // namespace hsql
#endif

View File

@@ -0,0 +1,70 @@
#include "CreateStatement.h"
#include "SelectStatement.h"
namespace hsql {
// CreateStatemnet
CreateStatement::CreateStatement(CreateType type)
: SQLStatement(kStmtCreate),
type(type),
ifNotExists(false),
filePath(nullptr),
schema(nullptr),
tableName(nullptr),
indexName(nullptr),
indexColumns(nullptr),
columns(nullptr),
tableConstraints(nullptr),
viewColumns(nullptr),
select(nullptr) {}
CreateStatement::~CreateStatement() {
free(filePath);
free(schema);
free(tableName);
free(indexName);
delete select;
if (columns) {
for (ColumnDefinition* def : *columns) {
delete def;
}
delete columns;
}
if (tableConstraints) {
for (TableConstraint* def : *tableConstraints) {
delete def;
}
delete tableConstraints;
}
if (indexColumns) {
for (char* column : *indexColumns) {
free(column);
}
delete indexColumns;
}
if (viewColumns) {
for (char* column : *viewColumns) {
free(column);
}
delete viewColumns;
}
}
void CreateStatement::setColumnDefsAndConstraints(std::vector<TableElement*>* tableElements) {
columns = new std::vector<ColumnDefinition*>();
tableConstraints = new std::vector<TableConstraint*>();
for (auto tableElem : *tableElements) {
if (auto* colDef = dynamic_cast<ColumnDefinition*>(tableElem)) {
columns->emplace_back(colDef);
} else if (auto* tableConstraint = dynamic_cast<TableConstraint*>(tableElem)) {
tableConstraints->emplace_back(tableConstraint);
}
}
}
} // namespace hsql

View File

@@ -0,0 +1,86 @@
#ifndef SQLPARSER_CREATE_STATEMENT_H
#define SQLPARSER_CREATE_STATEMENT_H
#include "ColumnType.h"
#include "SQLStatement.h"
#include <ostream>
#include <unordered_set>
// Note: Implementations of constructors and destructors can be found in statements.cpp.
namespace hsql {
struct SQLParser_API SelectStatement;
enum struct ConstraintType { None, NotNull, Null, PrimaryKey, Unique };
// Superclass for both TableConstraint and Column Definition
struct SQLParser_API TableElement {
virtual ~TableElement() {}
};
// Represents definition of a table constraint
struct SQLParser_API TableConstraint : TableElement {
TableConstraint(ConstraintType keyType, std::vector<char*>* columnNames);
~TableConstraint() override;
ConstraintType type;
std::vector<char*>* columnNames;
};
// Represents definition of a table column
struct SQLParser_API ColumnDefinition : TableElement {
ColumnDefinition(char* name, ColumnType type, std::unordered_set<ConstraintType>* column_constraints);
~ColumnDefinition() override;
// By default, columns are nullable. However, we track if a column is explicitly requested to be nullable to
// notice conflicts with PRIMARY KEY table constraints.
bool trySetNullableExplicit() {
if (column_constraints->count(ConstraintType::NotNull) || column_constraints->count(ConstraintType::PrimaryKey)) {
if (column_constraints->count(ConstraintType::Null)) {
return false;
}
nullable = false;
}
return true;
}
std::unordered_set<ConstraintType>* column_constraints;
char* name;
ColumnType type;
bool nullable;
};
enum CreateType {
kCreateTable,
kCreateTableFromTbl, // Hyrise file format
kCreateView,
kCreateIndex
};
// Represents SQL Create statements.
// Example: "CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)"
struct CreateStatement : SQLStatement {
CreateStatement(CreateType type);
~CreateStatement() override;
void setColumnDefsAndConstraints(std::vector<TableElement*>* tableElements);
CreateType type;
bool ifNotExists; // default: false
char* filePath; // default: nullptr
char* schema; // default: nullptr
char* tableName; // default: nullptr
char* indexName; // default: nullptr
std::vector<char*>* indexColumns; // default: nullptr
std::vector<ColumnDefinition*>* columns; // default: nullptr
std::vector<TableConstraint*>* tableConstraints; // default: nullptr
std::vector<char*>* viewColumns;
SelectStatement* select;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,23 @@
#ifndef SQLPARSER_DELETE_STATEMENT_H
#define SQLPARSER_DELETE_STATEMENT_H
#include "SQLStatement.h"
// Note: Implementations of constructors and destructors can be found in statements.cpp.
namespace hsql {
// Represents SQL Delete statements.
// Example: "DELETE FROM students WHERE grade > 3.0"
// Note: if (expr == nullptr) => delete all rows (truncate)
struct SQLParser_API DeleteStatement : SQLStatement {
DeleteStatement();
~DeleteStatement() override;
char* schema;
char* tableName;
Expr* expr;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,25 @@
#ifndef SQLPARSER_DROP_STATEMENT_H
#define SQLPARSER_DROP_STATEMENT_H
#include "SQLStatement.h"
// Note: Implementations of constructors and destructors can be found in statements.cpp.
namespace hsql {
enum DropType { kDropTable, kDropSchema, kDropIndex, kDropView, kDropPreparedStatement };
// Represents SQL Delete statements.
// Example "DROP TABLE students;"
struct SQLParser_API DropStatement : SQLStatement {
DropStatement(DropType type);
~DropStatement() override;
DropType type;
bool ifExists;
char* schema;
char* name;
char* indexName;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,20 @@
#ifndef SQLPARSER_EXECUTE_STATEMENT_H
#define SQLPARSER_EXECUTE_STATEMENT_H
#include "SQLStatement.h"
namespace hsql {
// Represents SQL Execute statements.
// Example: "EXECUTE ins_prep(100, "test", 2.3);"
struct SQLParser_API ExecuteStatement : SQLStatement {
ExecuteStatement();
~ExecuteStatement() override;
char* name;
std::vector<Expr*>* parameters;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,24 @@
#ifndef SQLPARSER_EXPORT_STATEMENT_H
#define SQLPARSER_EXPORT_STATEMENT_H
#include "ImportStatement.h"
#include "SQLStatement.h"
#include "SelectStatement.h"
namespace hsql {
// Represents SQL Export statements.
struct SQLParser_API ExportStatement : SQLStatement {
ExportStatement(ImportType type);
~ExportStatement() override;
// ImportType is used for compatibility reasons
ImportType type;
char* filePath;
char* schema;
char* tableName;
SelectStatement* select;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,315 @@
#include "Expr.h"
#include <stdio.h>
#include <string.h>
#include "SelectStatement.h"
namespace hsql {
FrameBound::FrameBound(int64_t offset, FrameBoundType type, bool unbounded)
: offset{offset}, type{type}, unbounded{unbounded} {}
FrameDescription::FrameDescription(FrameType type, FrameBound* start, FrameBound* end)
: type{type}, start{start}, end{end} {}
FrameDescription::~FrameDescription() {
delete start;
delete end;
}
WindowDescription::WindowDescription(std::vector<Expr*>* partitionList, std::vector<OrderDescription*>* orderList,
FrameDescription* frameDescription)
: partitionList{partitionList}, orderList{orderList}, frameDescription{frameDescription} {}
WindowDescription::~WindowDescription() {
if (partitionList) {
for (Expr* e : *partitionList) {
delete e;
}
delete partitionList;
}
if (orderList) {
for (OrderDescription* orderDescription : *orderList) {
delete orderDescription;
}
delete orderList;
}
delete frameDescription;
}
Expr::Expr(ExprType type)
: type(type),
expr(nullptr),
expr2(nullptr),
exprList(nullptr),
select(nullptr),
name(nullptr),
table(nullptr),
alias(nullptr),
fval(0),
ival(0),
ival2(0),
datetimeField(kDatetimeNone),
columnType(DataType::UNKNOWN, 0),
isBoolLiteral(false),
opType(kOpNone),
distinct(false),
windowDescription(nullptr) {}
Expr::~Expr() {
delete expr;
delete expr2;
delete select;
delete windowDescription;
free(name);
free(table);
free(alias);
if (exprList) {
for (Expr* e : *exprList) {
delete e;
}
delete exprList;
}
}
Expr* Expr::make(ExprType type) {
Expr* e = new Expr(type);
return e;
}
Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) {
Expr* e = new Expr(kExprOperator);
e->opType = op;
e->expr = expr;
e->expr2 = nullptr;
return e;
}
Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) {
Expr* e = new Expr(kExprOperator);
e->opType = op;
e->expr = expr1;
e->expr2 = expr2;
return e;
}
Expr* Expr::makeBetween(Expr* expr, Expr* left, Expr* right) {
Expr* e = new Expr(kExprOperator);
e->expr = expr;
e->opType = kOpBetween;
e->exprList = new std::vector<Expr*>();
e->exprList->push_back(left);
e->exprList->push_back(right);
return e;
}
Expr* Expr::makeCaseList(Expr* caseListElement) {
Expr* e = new Expr(kExprOperator);
// Case list expressions are temporary and will be integrated into the case
// expressions exprList - thus assign operator type kOpNone
e->opType = kOpNone;
e->exprList = new std::vector<Expr*>();
e->exprList->push_back(caseListElement);
return e;
}
Expr* Expr::makeCaseListElement(Expr* when, Expr* then) {
Expr* e = new Expr(kExprOperator);
e->opType = kOpCaseListElement;
e->expr = when;
e->expr2 = then;
return e;
}
Expr* Expr::caseListAppend(Expr* caseList, Expr* caseListElement) {
caseList->exprList->push_back(caseListElement);
return caseList;
}
Expr* Expr::makeCase(Expr* expr, Expr* caseList, Expr* elseExpr) {
Expr* e = new Expr(kExprOperator);
e->opType = kOpCase;
e->expr = expr;
e->expr2 = elseExpr;
e->exprList = caseList->exprList;
caseList->exprList = nullptr;
delete caseList;
return e;
}
Expr* Expr::makeLiteral(int64_t val) {
Expr* e = new Expr(kExprLiteralInt);
e->ival = val;
return e;
}
Expr* Expr::makeLiteral(double value) {
Expr* e = new Expr(kExprLiteralFloat);
e->fval = value;
return e;
}
Expr* Expr::makeLiteral(char* string) {
Expr* e = new Expr(kExprLiteralString);
e->name = string;
return e;
}
Expr* Expr::makeLiteral(bool val) {
Expr* e = new Expr(kExprLiteralInt);
e->ival = (int)val;
e->isBoolLiteral = true;
return e;
}
Expr* Expr::makeNullLiteral() {
Expr* e = new Expr(kExprLiteralNull);
return e;
}
Expr* Expr::makeDateLiteral(char* string) {
Expr* e = new Expr(kExprLiteralDate);
e->name = string;
return e;
}
Expr* Expr::makeIntervalLiteral(int64_t duration, DatetimeField unit) {
Expr* e = new Expr(kExprLiteralInterval);
e->ival = duration;
e->datetimeField = unit;
return e;
}
Expr* Expr::makeColumnRef(char* name) {
Expr* e = new Expr(kExprColumnRef);
e->name = name;
return e;
}
Expr* Expr::makeColumnRef(char* table, char* name) {
Expr* e = new Expr(kExprColumnRef);
e->name = name;
e->table = table;
return e;
}
Expr* Expr::makeStar(void) {
Expr* e = new Expr(kExprStar);
return e;
}
Expr* Expr::makeStar(char* table) {
Expr* e = new Expr(kExprStar);
e->table = table;
return e;
}
Expr* Expr::makeFunctionRef(char* func_name, std::vector<Expr*>* exprList, bool distinct, WindowDescription* window) {
Expr* e = new Expr(kExprFunctionRef);
e->name = func_name;
e->exprList = exprList;
e->distinct = distinct;
e->windowDescription = window;
return e;
}
Expr* Expr::makeArray(std::vector<Expr*>* exprList) {
Expr* e = new Expr(kExprArray);
e->exprList = exprList;
return e;
}
Expr* Expr::makeArrayIndex(Expr* expr, int64_t index) {
Expr* e = new Expr(kExprArrayIndex);
e->expr = expr;
e->ival = index;
return e;
}
Expr* Expr::makeParameter(int id) {
Expr* e = new Expr(kExprParameter);
e->ival = id;
return e;
}
Expr* Expr::makeSelect(SelectStatement* select) {
Expr* e = new Expr(kExprSelect);
e->select = select;
return e;
}
Expr* Expr::makeExists(SelectStatement* select) {
Expr* e = new Expr(kExprOperator);
e->opType = kOpExists;
e->select = select;
return e;
}
Expr* Expr::makeInOperator(Expr* expr, std::vector<Expr*>* exprList) {
Expr* e = new Expr(kExprOperator);
e->opType = kOpIn;
e->expr = expr;
e->exprList = exprList;
return e;
}
Expr* Expr::makeInOperator(Expr* expr, SelectStatement* select) {
Expr* e = new Expr(kExprOperator);
e->opType = kOpIn;
e->expr = expr;
e->select = select;
return e;
}
Expr* Expr::makeExtract(DatetimeField datetimeField, Expr* expr) {
Expr* e = new Expr(kExprExtract);
e->datetimeField = datetimeField;
e->expr = expr;
return e;
}
Expr* Expr::makeCast(Expr* expr, ColumnType columnType) {
Expr* e = new Expr(kExprCast);
e->columnType = columnType;
e->expr = expr;
return e;
}
bool Expr::isType(ExprType exprType) const { return exprType == type; }
bool Expr::isLiteral() const {
return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprParameter) ||
isType(kExprLiteralNull) || isType(kExprLiteralDate) || isType(kExprLiteralInterval);
}
bool Expr::hasAlias() const { return alias != nullptr; }
bool Expr::hasTable() const { return table != nullptr; }
const char* Expr::getName() const {
if (alias)
return alias;
else
return name;
}
char* substr(const char* source, int from, int to) {
int len = to - from;
char* copy = (char*)malloc(len + 1);
;
#if defined(_WIN32) || defined(_WIN64)
#pragma warning(disable : 4996)
#endif
strncpy(copy, source + from, len);
copy[len] = '\0';
return copy;
#if defined(_WIN32) || defined(_WIN64)
#pragma warning(default : 4996)
#endif
}
} // namespace hsql

View File

@@ -0,0 +1,237 @@
#ifndef SQLPARSER_EXPR_H
#define SQLPARSER_EXPR_H
#include <stdlib.h>
#include <memory>
#include <vector>
#include "ColumnType.h"
namespace hsql {
struct SelectStatement;
struct OrderDescription;
// 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,
kExprLiteralNull,
kExprLiteralDate,
kExprLiteralInterval,
kExprStar,
kExprParameter,
kExprColumnRef,
kExprFunctionRef,
kExprOperator,
kExprSelect,
kExprHint,
kExprArray,
kExprArrayIndex,
kExprExtract,
kExprCast
};
// Operator types. These are important for expressions of type kExprOperator.
enum OperatorType {
kOpNone,
// Ternary operator
kOpBetween,
// n-nary special case
kOpCase,
kOpCaseListElement, // `WHEN expr THEN expr`
// Binary operators.
kOpPlus,
kOpMinus,
kOpAsterisk,
kOpSlash,
kOpPercentage,
kOpCaret,
kOpEquals,
kOpNotEquals,
kOpLess,
kOpLessEq,
kOpGreater,
kOpGreaterEq,
kOpLike,
kOpNotLike,
kOpILike,
kOpAnd,
kOpOr,
kOpIn,
kOpConcat,
// Unary operators.
kOpNot,
kOpUnaryMinus,
kOpIsNull,
kOpExists
};
enum DatetimeField {
kDatetimeNone,
kDatetimeSecond,
kDatetimeMinute,
kDatetimeHour,
kDatetimeDay,
kDatetimeMonth,
kDatetimeYear,
};
// Description of the frame clause within a window expression.
enum FrameBoundType { kFollowing, kPreceding, kCurrentRow };
struct FrameBound {
FrameBound(int64_t offset, FrameBoundType type, bool unbounded);
int64_t offset;
FrameBoundType type;
bool unbounded;
};
enum FrameType { kRange, kRows, kGroups };
struct SQLParser_API FrameDescription {
FrameDescription(FrameType type, FrameBound* start, FrameBound* end);
virtual ~FrameDescription();
FrameType type;
FrameBound* start;
FrameBound* end;
};
typedef struct Expr Expr;
// Description of additional fields for a window expression.
struct SQLParser_API WindowDescription {
WindowDescription(std::vector<Expr*>* partitionList, std::vector<OrderDescription*>* orderList,
FrameDescription* frameDescription);
virtual ~WindowDescription();
std::vector<Expr*>* partitionList;
std::vector<OrderDescription*>* orderList;
FrameDescription* frameDescription;
};
// Represents SQL expressions (i.e. literals, operators, column_refs).
// TODO: When destructing a placeholder expression, we might need to alter the
// placeholder_list.
struct SQLParser_API Expr {
Expr(ExprType type);
virtual ~Expr();
ExprType type;
// TODO: Replace expressions by list.
Expr* expr;
Expr* expr2;
std::vector<Expr*>* exprList;
SelectStatement* select;
char* name;
char* table;
char* alias;
double fval;
int64_t ival;
int64_t ival2;
DatetimeField datetimeField;
ColumnType columnType;
bool isBoolLiteral;
OperatorType opType;
bool distinct;
WindowDescription* windowDescription;
// Convenience accessor methods.
bool isType(ExprType exprType) const;
bool isLiteral() const;
bool hasAlias() const;
bool hasTable() const;
const char* getName() const;
// Static constructors.
static Expr* make(ExprType type);
static Expr* makeOpUnary(OperatorType op, Expr* expr);
static Expr* makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2);
static Expr* makeBetween(Expr* expr, Expr* left, Expr* right);
static Expr* makeCaseList(Expr* caseListElement);
static Expr* makeCaseListElement(Expr* when, Expr* then);
static Expr* caseListAppend(Expr* caseList, Expr* caseListElement);
static Expr* makeCase(Expr* expr, Expr* when, Expr* elseExpr);
static Expr* makeLiteral(int64_t val);
static Expr* makeLiteral(double val);
static Expr* makeLiteral(char* val);
static Expr* makeLiteral(bool val);
static Expr* makeNullLiteral();
static Expr* makeDateLiteral(char* val);
static Expr* makeIntervalLiteral(int64_t duration, DatetimeField unit);
static Expr* makeColumnRef(char* name);
static Expr* makeColumnRef(char* table, char* name);
static Expr* makeStar(void);
static Expr* makeStar(char* table);
static Expr* makeFunctionRef(char* func_name, std::vector<Expr*>* exprList, bool distinct, WindowDescription* window);
static Expr* makeArray(std::vector<Expr*>* exprList);
static Expr* makeArrayIndex(Expr* expr, int64_t index);
static Expr* makeParameter(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);
static Expr* makeExtract(DatetimeField datetimeField1, Expr* expr);
static Expr* makeCast(Expr* expr, ColumnType columnType);
};
// 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

View File

@@ -0,0 +1,28 @@
#ifndef SQLPARSER_IMPORT_STATEMENT_H
#define SQLPARSER_IMPORT_STATEMENT_H
#include "SQLStatement.h"
namespace hsql {
enum ImportType {
kImportCSV,
kImportTbl, // Hyrise file format
kImportBinary,
kImportAuto
};
// Represents SQL Import statements.
struct SQLParser_API ImportStatement : SQLStatement {
ImportStatement(ImportType type);
~ImportStatement() override;
ImportType type;
char* filePath;
char* schema;
char* tableName;
Expr* whereClause;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,26 @@
#ifndef SQLPARSER_INSERT_STATEMENT_H
#define SQLPARSER_INSERT_STATEMENT_H
#include "SQLStatement.h"
#include "SelectStatement.h"
namespace hsql {
enum InsertType { kInsertValues, kInsertSelect };
// Represents SQL Insert statements.
// Example: "INSERT INTO students VALUES ('Max', 1112233, 'Musterhausen', 2.3)"
struct SQLParser_API InsertStatement : SQLStatement {
InsertStatement(InsertType type);
~InsertStatement() override;
InsertType type;
char* schema;
char* tableName;
std::vector<char*>* columns;
std::vector<Expr*>* values;
SelectStatement* select;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,12 @@
#include "PrepareStatement.h"
namespace hsql {
// PrepareStatement
PrepareStatement::PrepareStatement() : SQLStatement(kStmtPrepare), name(nullptr), query(nullptr) {}
PrepareStatement::~PrepareStatement() {
free(name);
free(query);
}
} // namespace hsql

View File

@@ -0,0 +1,22 @@
#ifndef SQLPARSER_PREPARE_STATEMENT_H
#define SQLPARSER_PREPARE_STATEMENT_H
#include "SQLStatement.h"
namespace hsql {
// Represents SQL Prepare statements.
// Example: PREPARE test FROM 'SELECT * FROM test WHERE a = ?;'
struct SQLParser_API PrepareStatement : SQLStatement {
PrepareStatement();
~PrepareStatement() override;
char* name;
// The query that is supposed to be prepared.
char* query;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,24 @@
#include "SQLStatement.h"
namespace hsql {
// SQLStatement
SQLStatement::SQLStatement(StatementType type) : hints(nullptr), type_(type) {}
SQLStatement::~SQLStatement() {
if (hints) {
for (Expr* hint : *hints) {
delete hint;
}
}
delete hints;
}
StatementType SQLStatement::type() const { return type_; }
bool SQLStatement::isType(StatementType type) const { return (type_ == type); }
bool SQLStatement::is(StatementType type) const { return isType(type); }
} // namespace hsql

View File

@@ -0,0 +1,51 @@
#ifndef SQLPARSER_SQLSTATEMENT_H
#define SQLPARSER_SQLSTATEMENT_H
#include <vector>
#include "sqlparser_win.h"
#include "Expr.h"
namespace hsql {
enum StatementType {
kStmtError, // unused
kStmtSelect,
kStmtImport,
kStmtInsert,
kStmtUpdate,
kStmtDelete,
kStmtCreate,
kStmtDrop,
kStmtPrepare,
kStmtExecute,
kStmtExport,
kStmtRename,
kStmtAlter,
kStmtShow,
kStmtTransaction
};
// Base struct for every SQL statement
struct SQLParser_API SQLStatement {
SQLStatement(StatementType type);
virtual ~SQLStatement();
StatementType type() const;
bool isType(StatementType type) const;
// Shorthand for isType(type).
bool is(StatementType type) const;
// Length of the string in the SQL query string
size_t stringLength;
std::vector<Expr*>* hints;
private:
StatementType type_;
};
} // namespace hsql
#endif // SQLPARSER_SQLSTATEMENT_H

View File

@@ -0,0 +1,113 @@
#ifndef SQLPARSER_SELECT_STATEMENT_H
#define SQLPARSER_SELECT_STATEMENT_H
#include "Expr.h"
#include "SQLStatement.h"
#include "Table.h"
namespace hsql {
enum OrderType { kOrderAsc, kOrderDesc };
enum SetType { kSetUnion, kSetIntersect, kSetExcept };
enum RowLockMode { ForUpdate, ForNoKeyUpdate, ForShare, ForKeyShare };
enum RowLockWaitPolicy { NoWait, SkipLocked, None };
// Description of the order by clause within a select statement.
struct SQLParser_API OrderDescription {
OrderDescription(OrderType type, Expr* expr);
virtual ~OrderDescription();
OrderType type;
Expr* expr;
};
// Description of the limit clause within a select statement.
struct SQLParser_API LimitDescription {
LimitDescription(Expr* limit, Expr* offset);
virtual ~LimitDescription();
Expr* limit;
Expr* offset;
};
// Description of the group-by clause within a select statement.
struct SQLParser_API GroupByDescription {
GroupByDescription();
virtual ~GroupByDescription();
std::vector<Expr*>* columns;
Expr* having;
};
struct SQLParser_API WithDescription {
~WithDescription();
char* alias;
SelectStatement* select;
};
struct SQLParser_API SetOperation {
SetOperation();
virtual ~SetOperation();
SetType setType;
bool isAll;
SelectStatement* nestedSelectStatement;
std::vector<OrderDescription*>* resultOrder;
LimitDescription* resultLimit;
};
struct SQLParser_API LockingClause {
RowLockMode rowLockMode;
RowLockWaitPolicy rowLockWaitPolicy;
std::vector<char*>* tables;
};
// Representation of a full SQL select statement.
struct SQLParser_API SelectStatement : SQLStatement {
SelectStatement();
~SelectStatement() override;
TableRef* fromTable;
bool selectDistinct;
std::vector<Expr*>* selectList;
Expr* whereClause;
GroupByDescription* groupBy;
// Note that a SetOperation is always connected to a
// different SelectStatement. This statement can itself
// have SetOperation connections to other SelectStatements.
// To evaluate the operations in the correct order:
// Iterate over the setOperations vector:
// 1. Fully evaluate the nestedSelectStatement within the SetOperation
// 2. Connect the original statement with the
// evaluated nestedSelectStatement
// 3. Apply the resultOrder and the resultLimit
// 4. The result now functions as the the original statement
// for the next iteration
//
// Example:
//
// (SELECT * FROM students INTERSECT SELECT * FROM students_2) UNION SELECT * FROM students_3 ORDER BY grade ASC;
//
// 1. We evaluate `Select * FROM students`
// 2. Then we iterate over the setOperations vector
// 3. We evalute the nestedSelectStatement of the first entry, which is: `SELECT * FROM students_2`
// 4. We connect the result of 1. with the results of 3. using the setType, which is INTERSECT
// 5. We continue the iteration of the setOperations vector
// 6. We evaluate the new nestedSelectStatement which is: `SELECT * FROM students_3`
// 7. We apply a Union-Operation to connect the results of 4. and 6.
// 8. Finally, we apply the resultOrder of the last SetOperation (ORDER BY grade ASC)
std::vector<SetOperation*>* setOperations;
std::vector<OrderDescription*>* order;
std::vector<WithDescription*>* withDescriptions;
LimitDescription* limit;
std::vector<LockingClause*>* lockings;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,23 @@
#ifndef SQLPARSER_SHOW_STATEMENT_H
#define SQLPARSER_SHOW_STATEMENT_H
#include "SQLStatement.h"
// Note: Implementations of constructors and destructors can be found in statements.cpp.
namespace hsql {
enum ShowType { kShowColumns, kShowTables };
// Represents SQL SHOW statements.
// Example "SHOW TABLES;"
struct SQLParser_API ShowStatement : SQLStatement {
ShowStatement(ShowType type);
~ShowStatement() override;
ShowType type;
char* schema;
char* name;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,68 @@
#ifndef SQLPARSER_TABLEREF_H
#define SQLPARSER_TABLEREF_H
#include <stdio.h>
#include <vector>
#include "Expr.h"
namespace hsql {
struct SelectStatement;
struct JoinDefinition;
struct TableRef;
// Possible table reference types.
enum TableRefType { kTableName, kTableSelect, kTableJoin, kTableCrossProduct };
struct SQLParser_API TableName {
char* schema;
char* name;
};
struct SQLParser_API Alias {
Alias(char* name, std::vector<char*>* columns = nullptr);
~Alias();
char* name;
std::vector<char*>* columns;
};
// Holds reference to tables. Can be either table names or a select statement.
struct SQLParser_API TableRef {
TableRef(TableRefType type);
virtual ~TableRef();
TableRefType type;
char* schema;
char* name;
Alias* alias;
SelectStatement* select;
std::vector<TableRef*>* list;
JoinDefinition* join;
// Returns true if a schema is set.
bool hasSchema() const;
// Returns the alias, if it is set. Otherwise the name.
const char* getName() const;
};
// Possible types of joins.
enum JoinType { kJoinInner, kJoinFull, kJoinLeft, kJoinRight, kJoinCross, kJoinNatural };
// Definition of a join construct.
struct SQLParser_API JoinDefinition {
JoinDefinition();
virtual ~JoinDefinition();
TableRef* left;
TableRef* right;
Expr* condition;
JoinType type;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,21 @@
#ifndef HYRISE_TRANSACTIONSTATEMENT_H
#define HYRISE_TRANSACTIONSTATEMENT_H
#include "SQLStatement.h"
namespace hsql {
// Represents SQL Transaction statements.
// Example: BEGIN TRANSACTION;
enum TransactionCommand { kBeginTransaction, kCommitTransaction, kRollbackTransaction };
struct SQLParser_API TransactionStatement : SQLStatement {
TransactionStatement(TransactionCommand command);
~TransactionStatement() override;
TransactionCommand command;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,27 @@
#ifndef SQLPARSER_UPDATE_STATEMENT_H
#define SQLPARSER_UPDATE_STATEMENT_H
#include "SQLStatement.h"
namespace hsql {
// Represents "column = value" expressions.
struct UpdateClause {
char* column;
Expr* value;
};
// Represents SQL Update statements.
struct SQLParser_API UpdateStatement : SQLStatement {
UpdateStatement();
~UpdateStatement() override;
// TODO: switch to char* instead of TableRef
TableRef* table;
std::vector<UpdateClause*>* updates;
Expr* where;
};
} // namespace hsql
#endif

View File

@@ -0,0 +1,393 @@
#include "statements.h"
#include "AlterStatement.h"
namespace hsql {
// KeyConstraints
TableConstraint::TableConstraint(ConstraintType type, std::vector<char*>* columnNames)
: type(type), columnNames(columnNames) {}
TableConstraint::~TableConstraint() {
for (char* def : *columnNames) {
free(def);
}
delete columnNames;
}
// ColumnDefinition
ColumnDefinition::ColumnDefinition(char* name, ColumnType type, std::unordered_set<ConstraintType>* column_constraints)
: column_constraints(column_constraints), name(name), type(type), nullable(true) {}
ColumnDefinition::~ColumnDefinition() {
free(name);
delete column_constraints;
}
ColumnType::ColumnType(DataType data_type, int64_t length, int64_t precision, int64_t scale)
: data_type(data_type), length(length), precision(precision), scale(scale) {}
bool operator==(const ColumnType& lhs, const ColumnType& rhs) {
if (lhs.data_type != rhs.data_type) return false;
return lhs.length == rhs.length && lhs.precision == rhs.precision && lhs.scale == rhs.scale;
}
bool operator!=(const ColumnType& lhs, const ColumnType& rhs) { return !(lhs == rhs); }
std::ostream& operator<<(std::ostream& stream, const ColumnType& column_type) {
switch (column_type.data_type) {
case DataType::UNKNOWN:
stream << "UNKNOWN";
break;
case DataType::INT:
stream << "INT";
break;
case DataType::BIGINT:
stream << "BIGINT";
break;
case DataType::LONG:
stream << "LONG";
break;
case DataType::FLOAT:
stream << "FLOAT";
break;
case DataType::DOUBLE:
stream << "DOUBLE";
break;
case DataType::REAL:
stream << "REAL";
break;
case DataType::CHAR:
stream << "CHAR(" << column_type.length << ")";
break;
case DataType::VARCHAR:
stream << "VARCHAR(" << column_type.length << ")";
break;
case DataType::DECIMAL:
stream << "DECIMAL";
break;
case DataType::TEXT:
stream << "TEXT";
break;
case DataType::DATETIME:
stream << "DATETIME";
break;
case DataType::DATE:
stream << "DATE";
break;
case DataType::TIME:
stream << "TIME";
break;
case DataType::SMALLINT:
stream << "SMALLINT";
break;
case DataType::BOOLEAN:
stream << "BOOLEAN";
break;
}
return stream;
}
// DeleteStatement
DeleteStatement::DeleteStatement() : SQLStatement(kStmtDelete), schema(nullptr), tableName(nullptr), expr(nullptr) {}
DeleteStatement::~DeleteStatement() {
free(schema);
free(tableName);
delete expr;
}
// DropStatement
DropStatement::DropStatement(DropType type)
: SQLStatement(kStmtDrop), type(type), schema(nullptr), name(nullptr), indexName(nullptr) {}
DropStatement::~DropStatement() {
free(schema);
free(name);
free(indexName);
}
// AlterStatement and supportive classes
AlterAction::AlterAction(ActionType type) : type(type) {}
AlterAction::~AlterAction() = default;
DropColumnAction::DropColumnAction(char* column_name)
: AlterAction(ActionType::DropColumn), columnName(column_name), ifExists(false) {}
DropColumnAction::~DropColumnAction() { free(columnName); }
AlterStatement::AlterStatement(char* name, AlterAction* action)
: SQLStatement(kStmtAlter), schema(nullptr), ifTableExists(false), name(name), action(action) {}
AlterStatement::~AlterStatement() {
free(schema);
free(name);
delete action;
}
// TransactionStatement
TransactionStatement::TransactionStatement(TransactionCommand command)
: SQLStatement(kStmtTransaction), command(command) {}
TransactionStatement::~TransactionStatement() {}
// ExecuteStatement
ExecuteStatement::ExecuteStatement() : SQLStatement(kStmtExecute), name(nullptr), parameters(nullptr) {}
ExecuteStatement::~ExecuteStatement() {
free(name);
if (parameters) {
for (Expr* param : *parameters) {
delete param;
}
delete parameters;
}
}
// ExportStatement
ExportStatement::ExportStatement(ImportType type)
: SQLStatement(kStmtExport), type(type), filePath(nullptr), schema(nullptr), tableName(nullptr), select(nullptr) {}
ExportStatement::~ExportStatement() {
free(filePath);
free(schema);
free(tableName);
delete select;
}
// ImportStatement
ImportStatement::ImportStatement(ImportType type)
: SQLStatement(kStmtImport),
type(type),
filePath(nullptr),
schema(nullptr),
tableName(nullptr),
whereClause(nullptr) {}
ImportStatement::~ImportStatement() {
free(filePath);
free(schema);
free(tableName);
delete whereClause;
}
// InsertStatement
InsertStatement::InsertStatement(InsertType type)
: SQLStatement(kStmtInsert),
type(type),
schema(nullptr),
tableName(nullptr),
columns(nullptr),
values(nullptr),
select(nullptr) {}
InsertStatement::~InsertStatement() {
free(schema);
free(tableName);
delete select;
if (columns) {
for (char* column : *columns) {
free(column);
}
delete columns;
}
if (values) {
for (Expr* expr : *values) {
delete expr;
}
delete values;
}
}
// ShowStatament
ShowStatement::ShowStatement(ShowType type) : SQLStatement(kStmtShow), type(type), schema(nullptr), name(nullptr) {}
ShowStatement::~ShowStatement() {
free(schema);
free(name);
}
// SelectStatement.h
// OrderDescription
OrderDescription::OrderDescription(OrderType type, Expr* expr) : type(type), expr(expr) {}
OrderDescription::~OrderDescription() { delete expr; }
// LimitDescription
LimitDescription::LimitDescription(Expr* limit, Expr* offset) : limit(limit), offset(offset) {}
LimitDescription::~LimitDescription() {
delete limit;
delete offset;
}
// GroypByDescription
GroupByDescription::GroupByDescription() : columns(nullptr), having(nullptr) {}
GroupByDescription::~GroupByDescription() {
delete having;
if (columns) {
for (Expr* expr : *columns) {
delete expr;
}
delete columns;
}
}
WithDescription::~WithDescription() {
free(alias);
delete select;
}
// SelectStatement
SelectStatement::SelectStatement()
: SQLStatement(kStmtSelect),
fromTable(nullptr),
selectDistinct(false),
selectList(nullptr),
whereClause(nullptr),
groupBy(nullptr),
setOperations(nullptr),
order(nullptr),
withDescriptions(nullptr),
limit(nullptr),
lockings(nullptr) {}
SelectStatement::~SelectStatement() {
delete fromTable;
delete whereClause;
delete groupBy;
delete limit;
// Delete each element in the select list.
if (selectList) {
for (Expr* expr : *selectList) {
delete expr;
}
delete selectList;
}
if (order) {
for (OrderDescription* desc : *order) {
delete desc;
}
delete order;
}
if (withDescriptions) {
for (WithDescription* desc : *withDescriptions) {
delete desc;
}
delete withDescriptions;
}
if (setOperations) {
for (SetOperation* setOperation : *setOperations) {
delete setOperation;
}
delete setOperations;
}
if (lockings) {
for (LockingClause* lockingClause : *lockings) {
if (lockingClause->tables) {
for (char* dtable : *lockingClause->tables) {
free(dtable);
}
delete lockingClause->tables;
}
delete lockingClause;
}
delete lockings;
}
}
// UpdateStatement
UpdateStatement::UpdateStatement() : SQLStatement(kStmtUpdate), table(nullptr), updates(nullptr), where(nullptr) {}
UpdateStatement::~UpdateStatement() {
delete table;
delete where;
if (updates) {
for (UpdateClause* update : *updates) {
free(update->column);
delete update->value;
delete update;
}
delete updates;
}
}
// Alias
Alias::Alias(char* name, std::vector<char*>* columns) : name(name), columns(columns) {}
Alias::~Alias() {
free(name);
if (columns) {
for (char* column : *columns) {
free(column);
}
delete columns;
}
}
// TableRef
TableRef::TableRef(TableRefType type)
: type(type), schema(nullptr), name(nullptr), alias(nullptr), select(nullptr), list(nullptr), join(nullptr) {}
TableRef::~TableRef() {
free(schema);
free(name);
delete select;
delete join;
delete alias;
if (list) {
for (TableRef* table : *list) {
delete table;
}
delete list;
}
}
bool TableRef::hasSchema() const { return schema != nullptr; }
const char* TableRef::getName() const {
if (alias)
return alias->name;
else
return name;
}
// JoinDefinition
JoinDefinition::JoinDefinition() : left(nullptr), right(nullptr), condition(nullptr), type(kJoinInner) {}
JoinDefinition::~JoinDefinition() {
delete left;
delete right;
delete condition;
}
SetOperation::SetOperation() : nestedSelectStatement(nullptr), resultOrder(nullptr), resultLimit(nullptr) {}
SetOperation::~SetOperation() {
delete nestedSelectStatement;
delete resultLimit;
if (resultOrder) {
for (OrderDescription* desc : *resultOrder) {
delete desc;
}
delete resultOrder;
}
}
} // namespace hsql

View File

@@ -0,0 +1,18 @@
#ifndef SQLPARSER_STATEMENTS_H
#define SQLPARSER_STATEMENTS_H
#include "AlterStatement.h"
#include "CreateStatement.h"
#include "DeleteStatement.h"
#include "DropStatement.h"
#include "ExecuteStatement.h"
#include "ExportStatement.h"
#include "ImportStatement.h"
#include "InsertStatement.h"
#include "PrepareStatement.h"
#include "SelectStatement.h"
#include "ShowStatement.h"
#include "TransactionStatement.h"
#include "UpdateStatement.h"
#endif // SQLPARSER_STATEMENTS_H