preparing for functions
This commit is contained in:
parent
eebfaacde4
commit
24d4fb2567
|
|
@ -0,0 +1,12 @@
|
|||
|
||||
### TODO
|
||||
- unify using of float and double keywords to double
|
||||
- use long data type for int
|
||||
- stoi -> stol, stof -> stod
|
||||
- add exceptions
|
||||
- class members should have prefix m_
|
||||
- add pipe | token
|
||||
- add to_date a to_number functions
|
||||
- add min and max functions
|
||||
- add logging
|
||||
- add const wherever should be
|
||||
|
|
@ -8,4 +8,5 @@ namespace usql {
|
|||
|
||||
|
||||
const char *Exception::what() const noexcept { return cause.c_str(); }
|
||||
|
||||
}
|
||||
2
main.cpp
2
main.cpp
|
|
@ -32,7 +32,7 @@ int main(int argc, char *argv[]) {
|
|||
};
|
||||
|
||||
|
||||
usql::uSQL uSql{};
|
||||
usql::USql uSql{};
|
||||
|
||||
for (auto command : sql_commands) {
|
||||
std::cout << command << std::endl;
|
||||
|
|
|
|||
38
parser.cpp
38
parser.cpp
|
|
@ -82,8 +82,7 @@ namespace usql {
|
|||
lexer.nextToken();
|
||||
}
|
||||
|
||||
cols_def.push_back(
|
||||
ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
|
||||
cols_def.push_back( ColDefNode(column_name, column_type, column_order++, column_len, column_nullable));
|
||||
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
|
||||
|
|
@ -99,7 +98,7 @@ namespace usql {
|
|||
std::unique_ptr<Node> Parser::parse_insert_into_table() {
|
||||
std::vector<Node> exec_code{};
|
||||
std::vector<ColNameNode> cols_names{};
|
||||
std::vector<ColValueNode> cols_values{};
|
||||
std::vector<std::unique_ptr<Node>> cols_values{};
|
||||
|
||||
lexer.skipToken(TokenType::keyword_insert);
|
||||
lexer.skipToken(TokenType::keyword_into);
|
||||
|
|
@ -123,13 +122,42 @@ namespace usql {
|
|||
// column values
|
||||
lexer.skipToken(TokenType::open_paren);
|
||||
do {
|
||||
cols_values.push_back(lexer.consumeCurrentToken().token_string);
|
||||
// cols_values.push_back(lexer.consumeCurrentToken().token_string);
|
||||
auto col_value = parse_value();
|
||||
cols_values.push_back(std::move(col_value));
|
||||
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
} while (lexer.tokenType() != TokenType::close_paren);
|
||||
lexer.skipToken(TokenType::close_paren);
|
||||
|
||||
return std::make_unique<InsertIntoTableNode>(table_name, cols_names, cols_values);
|
||||
return std::make_unique<InsertIntoTableNode>(table_name, cols_names, std::move(cols_values));
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_value() {
|
||||
if (lexer.tokenType() == TokenType::int_number) {
|
||||
return std::make_unique<IntValueNode>(std::stoi(lexer.consumeCurrentToken().token_string));
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::double_number) {
|
||||
return std::make_unique<FloatValueNode>(std::stof(lexer.consumeCurrentToken().token_string));
|
||||
}
|
||||
if (lexer.tokenType() == TokenType::string_literal) {
|
||||
if (lexer.nextTokenType() != TokenType::open_paren) {
|
||||
return std::make_unique<StringValueNode>(lexer.consumeCurrentToken().token_string);
|
||||
} else {
|
||||
// function
|
||||
std::string func_name = lexer.consumeCurrentToken().token_string;
|
||||
std::vector<std::unique_ptr<Node>> pars;
|
||||
|
||||
lexer.skipToken(TokenType::open_paren);
|
||||
while (lexer.tokenType() != TokenType::close_paren) { // TODO handle errors
|
||||
auto par = parse_value();
|
||||
lexer.skipTokenOptional(TokenType::comma);
|
||||
}
|
||||
lexer.skipToken(TokenType::close_paren);
|
||||
return std::make_unique<FunctionNode>(func_name, std::move(pars));
|
||||
}
|
||||
}
|
||||
throw Exception("Syntax error");
|
||||
}
|
||||
|
||||
std::unique_ptr<Node> Parser::parse_select_from_table() {
|
||||
|
|
|
|||
30
parser.h
30
parser.h
|
|
@ -32,6 +32,7 @@ namespace usql {
|
|||
load_table,
|
||||
column_name,
|
||||
column_value,
|
||||
function,
|
||||
column_def,
|
||||
error
|
||||
};
|
||||
|
|
@ -49,13 +50,6 @@ namespace usql {
|
|||
Node(NodeType::column_name), name(col_name) {}
|
||||
};
|
||||
|
||||
struct ColValueNode : Node {
|
||||
std::string value;
|
||||
|
||||
ColValueNode(const std::string col_value) :
|
||||
Node(NodeType::column_value), value(col_value) {}
|
||||
};
|
||||
|
||||
// TODO add order in row
|
||||
struct ColDefNode : Node {
|
||||
std::string name;
|
||||
|
|
@ -69,6 +63,20 @@ namespace usql {
|
|||
null(nullable) {}
|
||||
};
|
||||
|
||||
struct ColValueNode : Node {
|
||||
std::string value;
|
||||
|
||||
ColValueNode(const std::string col_value) :
|
||||
Node(NodeType::column_value), value(col_value) {}
|
||||
};
|
||||
|
||||
struct FunctionNode : Node {
|
||||
std::string function;
|
||||
std::vector<std::unique_ptr<Node>> params;
|
||||
|
||||
FunctionNode(const std::string func_name, std::vector<std::unique_ptr<Node>> pars) :
|
||||
Node(NodeType::function), function(func_name), params(std::move(pars)) {}
|
||||
};
|
||||
|
||||
struct TrueNode : Node {
|
||||
TrueNode() : Node(NodeType::true_node) {}
|
||||
|
|
@ -193,10 +201,10 @@ namespace usql {
|
|||
struct InsertIntoTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::vector<ColValueNode> cols_values;
|
||||
std::vector<std::unique_ptr<Node>> cols_values;
|
||||
|
||||
InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<ColValueNode> values) :
|
||||
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(values) {}
|
||||
InsertIntoTableNode(const std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> values) :
|
||||
Node(NodeType::insert_into), table_name(name), cols_names(names), cols_values(std::move(values)) {}
|
||||
};
|
||||
|
||||
struct SelectFromTableNode : Node {
|
||||
|
|
@ -252,6 +260,8 @@ namespace usql {
|
|||
|
||||
std::unique_ptr<Node> parse_insert_into_table();
|
||||
|
||||
std::unique_ptr<Node> parse_value();
|
||||
|
||||
std::unique_ptr<Node> parse_select_from_table();
|
||||
|
||||
std::unique_ptr<Node> parse_delete_from_table();
|
||||
|
|
|
|||
2
table.h
2
table.h
|
|
@ -15,7 +15,7 @@ namespace usql {
|
|||
|
||||
ColDefNode get_column_def(const std::string &col_name);
|
||||
|
||||
int columns_count() { return m_col_defs.size(); };
|
||||
int columns_count() const { return m_col_defs.size(); };
|
||||
|
||||
Row createEmptyRow(); // TODO this means unnecessary copying
|
||||
void addRow(const Row &row);
|
||||
|
|
|
|||
49
usql.cpp
49
usql.cpp
|
|
@ -7,14 +7,13 @@
|
|||
|
||||
namespace usql {
|
||||
|
||||
std::unique_ptr<Table> uSQL::execute(const std::string &command) {
|
||||
std::unique_ptr<Table> USql::execute(const std::string &command) {
|
||||
auto node = m_parser.parse(command);
|
||||
|
||||
return execute(*node);
|
||||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> uSQL::execute(Node &node) {
|
||||
std::unique_ptr<Table> USql::execute(Node &node) {
|
||||
// TODO optimize execution nodes here
|
||||
switch (node.node_type) {
|
||||
case NodeType::create_table:
|
||||
|
|
@ -35,7 +34,7 @@ namespace usql {
|
|||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> uSQL::execute_create_table(CreateTableNode &node) {
|
||||
std::unique_ptr<Table> USql::execute_create_table(CreateTableNode &node) {
|
||||
// TODO check table does not exists
|
||||
Table table{node.table_name, node.cols_defs};
|
||||
m_tables.push_back(table);
|
||||
|
|
@ -44,7 +43,7 @@ namespace usql {
|
|||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> uSQL::execute_insert_into_table(InsertIntoTableNode &node) {
|
||||
std::unique_ptr<Table> USql::execute_insert_into_table(InsertIntoTableNode &node) {
|
||||
// TODO check column names.size = values.size
|
||||
|
||||
// find table
|
||||
|
|
@ -58,13 +57,14 @@ namespace usql {
|
|||
ColDefNode col_def = table_def->get_column_def(node.cols_names[i].name);
|
||||
|
||||
// TODO validate value
|
||||
auto value = evalValueNode(node.cols_values[i].get());
|
||||
|
||||
if (col_def.type == ColumnType::integer_type) {
|
||||
new_row.setColumnValue(col_def.order, std::stoi(node.cols_values[i].value));
|
||||
new_row.setColumnValue(col_def.order, value->getIntValue());
|
||||
} else if (col_def.type == ColumnType::float_type) {
|
||||
new_row.setColumnValue(col_def.order, std::stof(node.cols_values[i].value));
|
||||
new_row.setColumnValue(col_def.order, value->getDoubleValue());
|
||||
} else {
|
||||
new_row.setColumnValue(col_def.order, node.cols_values[i].value);
|
||||
new_row.setColumnValue(col_def.order, value->getStringValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -75,7 +75,7 @@ namespace usql {
|
|||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> uSQL::execute_select(SelectFromTableNode &node) {
|
||||
std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
|
||||
// TODO create plan for accessing rows
|
||||
|
||||
// find source table
|
||||
|
|
@ -125,7 +125,7 @@ namespace usql {
|
|||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> uSQL::execute_delete(DeleteFromTableNode &node) {
|
||||
std::unique_ptr<Table> USql::execute_delete(DeleteFromTableNode &node) {
|
||||
// TODO create plan for accessing rows
|
||||
|
||||
// find source table
|
||||
|
|
@ -146,7 +146,7 @@ namespace usql {
|
|||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> uSQL::execute_update(UpdateTableNode &node) {
|
||||
std::unique_ptr<Table> USql::execute_update(UpdateTableNode &node) {
|
||||
// TODO create plan for accessing rows
|
||||
|
||||
// find source table
|
||||
|
|
@ -183,7 +183,7 @@ namespace usql {
|
|||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> uSQL::execute_load(LoadIntoTableNode &node) {
|
||||
std::unique_ptr<Table> USql::execute_load(LoadIntoTableNode &node) {
|
||||
// find source table
|
||||
Table *table_def = find_table(node.table_name);
|
||||
|
||||
|
|
@ -225,7 +225,7 @@ namespace usql {
|
|||
}
|
||||
|
||||
|
||||
bool uSQL::evalWhere(Node *where, Table *table,
|
||||
bool USql::evalWhere(Node *where, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
switch (where->node_type) { // no where clause
|
||||
case NodeType::true_node:
|
||||
|
|
@ -242,7 +242,7 @@ namespace usql {
|
|||
}
|
||||
|
||||
|
||||
bool uSQL::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
|
||||
bool USql::evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
std::unique_ptr<ValueNode> left_value = evalNode(table, row, filter.left.get());
|
||||
std::unique_ptr<ValueNode> right_value = evalNode(table, row, filter.right.get());
|
||||
|
|
@ -287,7 +287,7 @@ namespace usql {
|
|||
|
||||
|
||||
std::unique_ptr<ValueNode>
|
||||
uSQL::evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, Node *node) const {
|
||||
USql::evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row, Node *node) const {
|
||||
if (node->node_type == NodeType::database_value) {
|
||||
DatabaseValueNode *dvl = static_cast<DatabaseValueNode *>(node);
|
||||
ColDefNode col_def = table->get_column_def(
|
||||
|
|
@ -303,8 +303,14 @@ namespace usql {
|
|||
if (col_def.type == ColumnType::varchar_type) {
|
||||
return std::make_unique<StringValueNode>(db_value->stringValue());
|
||||
}
|
||||
} else {
|
||||
return evalValueNode(node);
|
||||
}
|
||||
}
|
||||
|
||||
} else if (node->node_type == NodeType::int_value) {
|
||||
|
||||
std::unique_ptr<ValueNode> USql::evalValueNode(Node *node) const {
|
||||
if (node->node_type == NodeType::int_value) {
|
||||
IntValueNode *ivl = static_cast<IntValueNode *>(node);
|
||||
return std::make_unique<IntValueNode>(ivl->value);
|
||||
|
||||
|
|
@ -315,13 +321,14 @@ namespace usql {
|
|||
} else if (node->node_type == NodeType::string_value) {
|
||||
StringValueNode *ivl = static_cast<StringValueNode *>(node);
|
||||
return std::make_unique<StringValueNode>(ivl->value);
|
||||
} else if ("function eval" == "xxx") {
|
||||
|
||||
}
|
||||
|
||||
throw Exception("invalid type");
|
||||
}
|
||||
|
||||
|
||||
bool uSQL::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
|
||||
bool USql::evalLogicalOperator(LogicalOperatorNode &node, Table *pTable,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &iter) const {
|
||||
bool left = evalRelationalOperator(static_cast<const RelationalOperatorNode &>(*node.left), pTable, iter);
|
||||
|
||||
|
|
@ -335,7 +342,7 @@ namespace usql {
|
|||
|
||||
|
||||
std::unique_ptr<ValueNode>
|
||||
uSQL::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
|
||||
USql::evalArithmetic(ColumnType outType, ArithmeticalOperatorNode &node, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const {
|
||||
if (node.op == ArithmeticalOperatorType::copy_value) {
|
||||
return evalNode(table, row, node.left.get());
|
||||
|
|
@ -392,7 +399,7 @@ namespace usql {
|
|||
|
||||
|
||||
|
||||
Table *uSQL::find_table(const std::string name) {
|
||||
Table *USql::find_table(const std::string name) {
|
||||
auto name_cmp = [name](const Table& t) { return t.m_name == name; };
|
||||
auto table_def = std::find_if(begin(m_tables), end(m_tables), name_cmp);
|
||||
if (table_def != std::end(m_tables)) {
|
||||
|
|
@ -403,7 +410,7 @@ namespace usql {
|
|||
}
|
||||
|
||||
|
||||
std::unique_ptr<Table> uSQL::create_stmt_result_table(int code, std::string text) {
|
||||
std::unique_ptr<Table> USql::create_stmt_result_table(int code, std::string text) {
|
||||
std::vector<ColDefNode> result_tbl_col_defs{};
|
||||
result_tbl_col_defs.push_back(ColDefNode("code", ColumnType::integer_type, 0, 1, false));
|
||||
result_tbl_col_defs.push_back(ColDefNode("desc", ColumnType::varchar_type, 1, 255, false));
|
||||
|
|
|
|||
12
usql.h
12
usql.h
|
|
@ -7,22 +7,30 @@
|
|||
|
||||
namespace usql {
|
||||
|
||||
class uSQL {
|
||||
class USql {
|
||||
|
||||
public:
|
||||
USql() {};
|
||||
|
||||
std::unique_ptr<Table> execute(const std::string &command);
|
||||
|
||||
private:
|
||||
std::unique_ptr<Table> execute(Node &node);
|
||||
|
||||
std::unique_ptr<Table> execute_create_table(CreateTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_insert_into_table(InsertIntoTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_select(SelectFromTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_delete(DeleteFromTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_update(UpdateTableNode &node);
|
||||
|
||||
std::unique_ptr<Table> execute_load(LoadIntoTableNode &node);
|
||||
|
||||
Table *find_table(const std::string name);
|
||||
|
||||
std::unique_ptr<Table> create_stmt_result_table(int code, std::string text);
|
||||
|
||||
|
||||
|
|
@ -33,6 +41,8 @@ namespace usql {
|
|||
std::unique_ptr<ValueNode> evalNode(Table *table, std::vector<Row, std::allocator<Row>>::iterator &row,
|
||||
Node *node) const;
|
||||
|
||||
std::unique_ptr<ValueNode> evalValueNode(Node *node) const;
|
||||
|
||||
bool evalRelationalOperator(const RelationalOperatorNode &filter, Table *table,
|
||||
std::vector<Row, std::allocator<Row>>::iterator &row) const;
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue