add astyle formatting

This commit is contained in:
Pedro 2016-02-27 15:01:06 +01:00
parent 57b8cdd31b
commit 7d1c56d0aa
27 changed files with 928 additions and 851 deletions

3
.gitignore vendored
View File

@ -31,3 +31,6 @@ lib-test/
*.exe
*.out
*.app
*.cpp.orig
*.h.orig

View File

@ -9,6 +9,9 @@ LIBCPP = $(shell find $(SRC) -name '*.cpp' -not -path "$(SRCPARSER)/*") $(S
LIBOBJ = $(LIBCPP:%.cpp=%.o)
TESTCPP = $(shell find test/lib/ -name '*.cpp')
ALLLIB = $(shell find $(SRC) -name '*.cpp' -not -path "$(SRCPARSER)/*") $(shell find $(SRC) -name '*.h' -not -path "$(SRCPARSER)/*")
ALLTEST = $(shell find test/lib/ -name '*.cpp') $(shell find test/lib/ -name '*.h')
# compile & link flages
CC = g++
CFLAGS = -std=c++11 -Wall -fPIC
@ -48,6 +51,10 @@ cleanall: clean cleanparser
install:
cp $(TARGET) $(INSTALL)/lib/$(TARGET)
format:
astyle --options=astyle.options $(ALLLIB)
astyle --options=astyle.options $(ALLTEST)
############
### Test ###
############
@ -63,5 +70,3 @@ $(BIN)/sql_tests: library
$(BIN)/sql_grammar_test: library
@mkdir -p $(BIN)/
$(CC) $(CTESTFLAGS) test/sql_grammar_test.cpp -o $(BIN)/sql_grammar_test -lsqlparser

8
astyle.options Normal file
View File

@ -0,0 +1,8 @@
# indentation
--indent=spaces=4
--indent-namespaces
--style=java
--style=attach
-A2

View File

@ -5,11 +5,11 @@
namespace hsql {
/**
/**
* @struct ColumnDefinition
* @brief Represents definition of a table column
*/
struct ColumnDefinition {
struct ColumnDefinition {
enum DataType {
TEXT,
INT,
@ -26,14 +26,14 @@ struct ColumnDefinition {
char* name;
DataType type;
};
};
/**
/**
* @struct CreateStatement
* @brief Represents "CREATE TABLE students (name TEXT, student_number INTEGER, city TEXT, grade DOUBLE)"
*/
struct CreateStatement : SQLStatement {
struct CreateStatement : SQLStatement {
enum CreateType {
kTable,
kTableFromTbl, // Hyrise file format
@ -60,7 +60,7 @@ struct CreateStatement : SQLStatement {
const char* file_path;
const char* table_name;
};
};

View File

@ -6,13 +6,13 @@
namespace hsql {
/**
/**
* @struct DeleteStatement
* @brief Represents "DELETE FROM students WHERE grade > 3.0"
*
* If expr == NULL => delete all rows (truncate)
*/
struct DeleteStatement : SQLStatement {
struct DeleteStatement : SQLStatement {
DeleteStatement() :
SQLStatement(kStmtDelete),
table_name(NULL),
@ -26,7 +26,7 @@ struct DeleteStatement : SQLStatement {
char* table_name;
Expr* expr;
};
};

View File

@ -6,11 +6,11 @@
namespace hsql {
/**
/**
* @struct DropStatement
* @brief Represents "DROP TABLE"
*/
struct DropStatement : SQLStatement {
struct DropStatement : SQLStatement {
enum EntityType {
kTable,
kSchema,
@ -32,7 +32,7 @@ struct DropStatement : SQLStatement {
EntityType type;
const char* name;
};
};

View File

@ -6,11 +6,11 @@
namespace hsql {
/**
/**
* @struct ExecuteStatement
* @brief Represents "EXECUTE ins_prep(100, "test", 2.3);"
*/
struct ExecuteStatement : SQLStatement {
struct ExecuteStatement : SQLStatement {
ExecuteStatement() :
SQLStatement(kStmtExecute),
name(NULL),
@ -23,7 +23,7 @@ struct ExecuteStatement : SQLStatement {
const char* name;
std::vector<Expr*>* parameters;
};
};

View File

@ -5,98 +5,98 @@
namespace hsql {
char* substr(const char* source, int from, int to) {
char* substr(const char* source, int from, int to) {
int len = to-from;
char* copy = new char[len+1];
strncpy(copy, source+from, len);
copy[len] = '\0';
return copy;
}
}
Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) {
Expr* Expr::makeOpUnary(OperatorType op, Expr* expr) {
Expr* e = new Expr(kExprOperator);
e->op_type = op;
e->expr = expr;
e->expr2 = NULL;
return e;
}
}
Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) {
Expr* Expr::makeOpBinary(Expr* expr1, OperatorType op, Expr* expr2) {
Expr* e = new Expr(kExprOperator);
e->op_type = op;
e->op_char = 0;
e->expr = expr1;
e->expr2 = expr2;
return e;
}
}
Expr* Expr::makeOpBinary(Expr* expr1, char op, Expr* expr2) {
Expr* Expr::makeOpBinary(Expr* expr1, char op, Expr* expr2) {
Expr* e = new Expr(kExprOperator);
e->op_type = SIMPLE_OP;
e->op_char = op;
e->expr = expr1;
e->expr2 = expr2;
return e;
}
}
Expr* Expr::makeLiteral(int64_t val) {
Expr* Expr::makeLiteral(int64_t val) {
Expr* e = new Expr(kExprLiteralInt);
e->ival = val;
return e;
}
}
Expr* Expr::makeLiteral(double value) {
Expr* Expr::makeLiteral(double value) {
Expr* e = new Expr(kExprLiteralFloat);
e->fval = value;
return e;
}
}
Expr* Expr::makeLiteral(char* string) {
Expr* Expr::makeLiteral(char* string) {
Expr* e = new Expr(kExprLiteralString);
e->name = string;
return e;
}
}
Expr* Expr::makeColumnRef(char* name) {
Expr* Expr::makeColumnRef(char* name) {
Expr* e = new Expr(kExprColumnRef);
e->name = name;
return e;
}
}
Expr* Expr::makeColumnRef(char* table, char* name) {
Expr* Expr::makeColumnRef(char* table, char* name) {
Expr* e = new Expr(kExprColumnRef);
e->name = name;
e->table = table;
return e;
}
}
Expr* Expr::makeFunctionRef(char* func_name, Expr* expr, bool distinct) {
Expr* Expr::makeFunctionRef(char* func_name, Expr* expr, bool distinct) {
Expr* e = new Expr(kExprFunctionRef);
e->name = func_name;
e->expr = expr;
e->distinct = distinct;
return e;
}
}
Expr* Expr::makePlaceholder(int id) {
Expr* Expr::makePlaceholder(int id) {
Expr* e = new Expr(kExprPlaceholder);
e->ival = id;
return e;
}
}
Expr::~Expr() {
Expr::~Expr() {
delete expr;
delete expr2;
delete name;
delete table;
}
}
} // namespace hsql

View File

@ -7,11 +7,11 @@
namespace hsql {
// Helper function
char* substr(const char* source, int from, int to);
char* substr(const char* source, int from, int to);
typedef enum {
typedef enum {
kExprLiteralFloat,
kExprLiteralString,
kExprLiteralInt,
@ -20,17 +20,17 @@ typedef enum {
kExprColumnRef,
kExprFunctionRef,
kExprOperator
} ExprType;
} ExprType;
typedef struct Expr Expr;
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 {
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:
@ -87,16 +87,28 @@ struct Expr {
/**
* Convenience accessor methods
*/
inline bool isType(ExprType e_type) { return e_type == type; }
inline bool isLiteral() { return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprPlaceholder); }
inline bool hasAlias() { return alias != NULL; }
inline bool hasTable() { return table != NULL; }
inline bool isType(ExprType e_type) {
return e_type == type;
}
inline bool isLiteral() {
return isType(kExprLiteralInt) || isType(kExprLiteralFloat) || isType(kExprLiteralString) || isType(kExprPlaceholder);
}
inline bool hasAlias() {
return alias != NULL;
}
inline bool hasTable() {
return table != NULL;
}
inline char* getName() {
if (alias != NULL) return alias;
else return name;
}
inline bool isSimpleOp() { return op_type == SIMPLE_OP; }
inline bool isSimpleOp(char op) { return isSimpleOp() && op_char == op; }
inline bool isSimpleOp() {
return op_type == SIMPLE_OP;
}
inline bool isSimpleOp(char op) {
return isSimpleOp() && op_char == op;
}
/**
@ -115,7 +127,7 @@ struct Expr {
static Expr* makeFunctionRef(char* func_name, Expr* expr, bool distinct);
static Expr* makePlaceholder(int id);
};
};
// 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

View File

@ -8,11 +8,11 @@ namespace hsql {
/**
/**
* @struct ImportStatement
* @brief Represents "IMPORT"
*/
struct ImportStatement : SQLStatement {
struct ImportStatement : SQLStatement {
enum ImportType {
kImportCSV,
kImportTbl, // Hyrise file format
@ -34,7 +34,7 @@ struct ImportStatement : SQLStatement {
ImportType type;
const char* file_path;
const char* table_name;
};
};

View File

@ -7,11 +7,11 @@
namespace hsql {
/**
/**
* @struct InsertStatement
* @brief Represents "INSERT INTO students VALUES ('Max', 1112233, 'Musterhausen', 2.3)"
*/
struct InsertStatement : SQLStatement {
struct InsertStatement : SQLStatement {
enum InsertType {
kInsertValues,
kInsertSelect
@ -37,7 +37,7 @@ struct InsertStatement : SQLStatement {
std::vector<char*>* columns;
std::vector<Expr*>* values;
SelectStatement* select;
};
};

View File

@ -9,11 +9,11 @@
namespace hsql {
/**
/**
* @struct PrepareStatement
* @brief Represents "PREPARE ins_prep: SELECT * FROM t1 WHERE c1 = ? AND c2 = ?"
*/
struct PrepareStatement : SQLStatement {
struct PrepareStatement : SQLStatement {
PrepareStatement() :
SQLStatement(kStmtPrepare),
name(NULL),
@ -45,7 +45,7 @@ struct PrepareStatement : SQLStatement {
const char* name;
SQLParserResult* query;
std::vector<Expr*> placeholders;
};
};

View File

@ -32,7 +32,9 @@ namespace hsql {
virtual ~SQLStatement() {}
virtual StatementType type() { return _type; }
virtual StatementType type() {
return _type;
}
private:
StatementType _type;

View File

@ -9,18 +9,18 @@ namespace hsql {
/**
/**
* @struct OrderDescription
* @brief Description of the order by clause within a select statement
*
* TODO: hold multiple expressions to be sorted by
*/
typedef enum {
typedef enum {
kOrderAsc,
kOrderDesc
} OrderType;
} OrderType;
struct OrderDescription {
struct OrderDescription {
OrderDescription(OrderType type, Expr* expr) :
type(type),
expr(expr) {}
@ -31,27 +31,27 @@ struct OrderDescription {
OrderType type;
Expr* expr;
};
};
/**
/**
* @struct LimitDescription
* @brief Description of the limit clause within a select statement
*/
const int64_t kNoLimit = -1;
const int64_t kNoOffset = -1;
struct LimitDescription {
const int64_t kNoLimit = -1;
const int64_t kNoOffset = -1;
struct LimitDescription {
LimitDescription(int64_t limit, int64_t offset) :
limit(limit),
offset(offset) {}
int64_t limit;
int64_t offset;
};
};
/**
/**
* @struct GroupByDescription
*/
struct GroupByDescription {
struct GroupByDescription {
GroupByDescription() :
columns(NULL),
having(NULL) {}
@ -63,15 +63,15 @@ struct GroupByDescription {
std::vector<Expr*>* columns;
Expr* having;
};
};
/**
/**
* @struct SelectStatement
* @brief Representation of a full select statement.
*
* TODO: add union_order and union_limit
*/
struct SelectStatement : SQLStatement {
struct SelectStatement : SQLStatement {
SelectStatement() :
SQLStatement(kStmtSelect),
from_table(NULL),
@ -100,7 +100,7 @@ struct SelectStatement : SQLStatement {
SelectStatement* union_select;
OrderDescription* order;
LimitDescription* limit;
};
};
} // namespace hsql

View File

@ -7,28 +7,28 @@
namespace hsql {
struct SelectStatement;
struct JoinDefinition;
struct TableRef;
struct SelectStatement;
struct JoinDefinition;
struct TableRef;
/**
/**
* @enum TableRefType
* Types table references
*/
typedef enum {
typedef enum {
kTableName,
kTableSelect,
kTableJoin,
kTableCrossProduct
} TableRefType;
} TableRefType;
/**
/**
* @struct TableRef
* @brief Holds reference to tables. Can be either table names or a select statement.
*/
struct TableRef {
struct TableRef {
TableRef(TableRefType type) :
type(type),
schema(NULL),
@ -54,32 +54,34 @@ struct TableRef {
/**
* Convenience accessor methods
*/
inline bool hasSchema() { return schema != NULL; }
inline bool hasSchema() {
return schema != NULL;
}
inline char* getName() {
if (alias != NULL) return alias;
else return name;
}
};
};
/**
/**
* @enum JoinType
* Types of joins
*/
typedef enum {
typedef enum {
kJoinInner,
kJoinOuter,
kJoinLeft,
kJoinRight,
} JoinType;
} JoinType;
/**
/**
* @struct JoinDefinition
* @brief Definition of a join table
*/
struct JoinDefinition {
struct JoinDefinition {
JoinDefinition() :
left(NULL),
right(NULL),
@ -97,7 +99,7 @@ struct JoinDefinition {
Expr* condition;
JoinType type;
};
};

View File

@ -6,21 +6,21 @@
namespace hsql {
/**
/**
* @struct UpdateClause
* @brief Represents "column = value" expressions
*/
struct UpdateClause {
struct UpdateClause {
char* column;
Expr* value;
};
};
/**
/**
* @struct UpdateStatement
* @brief Represents "UPDATE"
*/
struct UpdateStatement : SQLStatement {
struct UpdateStatement : SQLStatement {
UpdateStatement() :
SQLStatement(kStmtUpdate),
table(NULL),
@ -37,7 +37,7 @@ struct UpdateStatement : SQLStatement {
TableRef* table;
std::vector<UpdateClause*>* updates;
Expr* where;
};
};

View File

@ -5,12 +5,12 @@
namespace hsql {
TableRef::~TableRef() {
TableRef::~TableRef() {
delete name;
delete alias;
delete select;
delete list;
}
}
} // namespace hsql

View File

@ -5,17 +5,31 @@
namespace hsql {
void printOperatorExpression(Expr* expr, uint num_indent);
void printOperatorExpression(Expr* expr, uint num_indent);
std::string indent(uint num_indent) { return std::string(num_indent, '\t'); }
void inprint(int64_t val, uint num_indent) { printf("%s%ld \n", indent(num_indent).c_str(), val); }
void inprint(float val, uint num_indent) { printf("%s%f\n", indent(num_indent).c_str(), val); }
void inprint(const char* val, uint num_indent) { printf("%s%s\n", indent(num_indent).c_str(), val); }
void inprint(const char* val, const char* val2, uint num_indent) { printf("%s%s->%s\n", indent(num_indent).c_str(), val, val2); }
void inprintC(char val, uint num_indent) { printf("%s%c\n", indent(num_indent).c_str(), val); }
void inprintU(uint64_t val, uint num_indent) { printf("%s%lu\n", indent(num_indent).c_str(), val); }
std::string indent(uint num_indent) {
return std::string(num_indent, '\t');
}
void inprint(int64_t val, uint num_indent) {
printf("%s%ld \n", indent(num_indent).c_str(), val);
}
void inprint(float val, uint num_indent) {
printf("%s%f\n", indent(num_indent).c_str(), val);
}
void inprint(const char* val, uint num_indent) {
printf("%s%s\n", indent(num_indent).c_str(), val);
}
void inprint(const char* val, const char* val2, uint num_indent) {
printf("%s%s->%s\n", indent(num_indent).c_str(), val, val2);
}
void inprintC(char val, uint num_indent) {
printf("%s%c\n", indent(num_indent).c_str(), val);
}
void inprintU(uint64_t val, uint num_indent) {
printf("%s%lu\n", indent(num_indent).c_str(), val);
}
void printTableRefInfo(TableRef* table, uint num_indent) {
void printTableRefInfo(TableRef* table, uint num_indent) {
switch (table->type) {
case kTableName:
inprint(table->name, num_indent);
@ -40,40 +54,71 @@ void printTableRefInfo(TableRef* table, uint num_indent) {
inprint("Alias", num_indent+1);
inprint(table->alias, num_indent+2);
}
}
}
void printOperatorExpression(Expr* expr, uint num_indent) {
if (expr == NULL) { inprint("null", num_indent); return; }
void printOperatorExpression(Expr* expr, uint num_indent) {
if (expr == NULL) {
inprint("null", num_indent);
return;
}
switch (expr->op_type) {
case Expr::SIMPLE_OP: inprintC(expr->op_char, num_indent); break;
case Expr::AND: inprint("AND", num_indent); break;
case Expr::OR: inprint("OR", num_indent); break;
case Expr::NOT: inprint("NOT", num_indent); break;
default: inprintU(expr->op_type, num_indent); break;
case Expr::SIMPLE_OP:
inprintC(expr->op_char, num_indent);
break;
case Expr::AND:
inprint("AND", num_indent);
break;
case Expr::OR:
inprint("OR", num_indent);
break;
case Expr::NOT:
inprint("NOT", num_indent);
break;
default:
inprintU(expr->op_type, num_indent);
break;
}
printExpression(expr->expr, num_indent+1);
if (expr->expr2 != NULL) printExpression(expr->expr2, num_indent+1);
}
}
void printExpression(Expr* expr, uint num_indent) {
void printExpression(Expr* expr, uint num_indent) {
switch (expr->type) {
case kExprStar: inprint("*", num_indent); break;
case kExprColumnRef: inprint(expr->name, num_indent); break;
case kExprStar:
inprint("*", num_indent);
break;
case kExprColumnRef:
inprint(expr->name, num_indent);
break;
// case kExprTableColumnRef: inprint(expr->table, expr->name, num_indent); break;
case kExprLiteralFloat: inprint(expr->fval, num_indent); break;
case kExprLiteralInt: inprint(expr->ival, num_indent); break;
case kExprLiteralString: inprint(expr->name, num_indent); break;
case kExprFunctionRef: inprint(expr->name, num_indent); inprint(expr->expr->name, num_indent+1); break;
case kExprOperator: printOperatorExpression(expr, num_indent); break;
default: fprintf(stderr, "Unrecognized expression type %d\n", expr->type); return;
case kExprLiteralFloat:
inprint(expr->fval, num_indent);
break;
case kExprLiteralInt:
inprint(expr->ival, num_indent);
break;
case kExprLiteralString:
inprint(expr->name, num_indent);
break;
case kExprFunctionRef:
inprint(expr->name, num_indent);
inprint(expr->expr->name, num_indent+1);
break;
case kExprOperator:
printOperatorExpression(expr, num_indent);
break;
default:
fprintf(stderr, "Unrecognized expression type %d\n", expr->type);
return;
}
if (expr->alias != NULL) {
inprint("Alias", num_indent+1); inprint(expr->alias, num_indent+2);
inprint("Alias", num_indent+1);
inprint(expr->alias, num_indent+2);
}
}
}
void printSelectStatementInfo(SelectStatement* stmt, uint num_indent) {
void printSelectStatementInfo(SelectStatement* stmt, uint num_indent) {
inprint("SelectStatement", num_indent);
inprint("Fields:", num_indent+1);
for (Expr* expr : *stmt->select_list) printExpression(expr, num_indent+2);
@ -103,23 +148,23 @@ void printSelectStatementInfo(SelectStatement* stmt, uint num_indent) {
inprint("Limit:", num_indent+1);
inprint(stmt->limit->limit, num_indent+2);
}
}
}
void printImportStatementInfo(ImportStatement* stmt, uint num_indent) {
void printImportStatementInfo(ImportStatement* stmt, uint num_indent) {
inprint("ImportStatment", num_indent);
inprint(stmt->file_path, num_indent+1);
inprint(stmt->table_name, num_indent+1);
}
}
void printCreateStatementInfo(CreateStatement* stmt, uint num_indent) {
void printCreateStatementInfo(CreateStatement* stmt, uint num_indent) {
inprint("CreateStatment", num_indent);
inprint(stmt->table_name, num_indent+1);
inprint(stmt->file_path, num_indent+1);
}
}
void printInsertStatementInfo(InsertStatement* stmt, uint num_indent) {
void printInsertStatementInfo(InsertStatement* stmt, uint num_indent) {
inprint("InsertStatment", num_indent);
inprint(stmt->table_name, num_indent+1);
if (stmt->columns != NULL) {
@ -139,9 +184,9 @@ void printInsertStatementInfo(InsertStatement* stmt, uint num_indent) {
printSelectStatementInfo(stmt->select, num_indent+1);
break;
}
}
}
void printStatementInfo(SQLStatement* stmt) {
void printStatementInfo(SQLStatement* stmt) {
switch (stmt->type()) {
case kStmtSelect:
printSelectStatementInfo((SelectStatement*) stmt, 0);
@ -158,6 +203,6 @@ void printStatementInfo(SQLStatement* stmt) {
default:
break;
}
}
}
} // namespace hsql