usql update
This commit is contained in:
156
usql/parser.h
156
usql/parser.h
@@ -2,16 +2,22 @@
|
||||
|
||||
#include "lexer.h"
|
||||
#include "exception.h"
|
||||
#include <string>
|
||||
#include "ml_date.h"
|
||||
#include "settings.h"
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
static const int FUNCTION_CALL = -1;
|
||||
|
||||
namespace usql {
|
||||
|
||||
enum class ColumnType {
|
||||
integer_type,
|
||||
float_type,
|
||||
varchar_type
|
||||
varchar_type,
|
||||
date_type,
|
||||
bool_type
|
||||
};
|
||||
|
||||
enum class NodeType {
|
||||
@@ -20,7 +26,7 @@ namespace usql {
|
||||
int_value,
|
||||
float_value,
|
||||
string_value,
|
||||
database_value,
|
||||
bool_value,
|
||||
logical_operator,
|
||||
relational_operator,
|
||||
arithmetical_operator,
|
||||
@@ -33,7 +39,11 @@ namespace usql {
|
||||
load_table,
|
||||
save_table,
|
||||
drop_table,
|
||||
column_name,
|
||||
set,
|
||||
show,
|
||||
database_value,
|
||||
offset_limit,
|
||||
column_order,
|
||||
column_value,
|
||||
function,
|
||||
column_def,
|
||||
@@ -43,21 +53,35 @@ namespace usql {
|
||||
struct Node {
|
||||
NodeType node_type;
|
||||
|
||||
Node(const NodeType type) : node_type(type) {}
|
||||
explicit Node(const NodeType type) : node_type(type) {}
|
||||
};
|
||||
|
||||
struct ColNameNode : Node {
|
||||
std::string name;
|
||||
|
||||
ColNameNode(const std::string col_name) : Node(NodeType::column_name), name(col_name) {}
|
||||
struct ColOrderNode : Node {
|
||||
std::string col_name;
|
||||
int col_index;
|
||||
bool ascending;
|
||||
|
||||
ColOrderNode(const std::string& name, bool asc) : Node(NodeType::column_order), col_name(name), col_index(-1), ascending(asc) {}
|
||||
ColOrderNode(int index, bool asc) : Node(NodeType::database_value), col_name(""), col_index(index), ascending(asc) {}
|
||||
};
|
||||
|
||||
|
||||
|
||||
struct OffsetLimitNode : Node {
|
||||
int offset;
|
||||
int limit;
|
||||
|
||||
OffsetLimitNode(int off, int lim) : Node(NodeType::offset_limit), offset(off), limit(lim) {}
|
||||
};
|
||||
|
||||
|
||||
struct SelectColNode : Node {
|
||||
std::unique_ptr<Node> value;
|
||||
std::string name;
|
||||
|
||||
SelectColNode(std::unique_ptr<Node> column, const std::string& alias) :
|
||||
Node(NodeType::column_name), value(std::move(column)), name(alias) {}
|
||||
SelectColNode(std::unique_ptr<Node> column, const std::string &alias) :
|
||||
Node(NodeType::database_value), value(std::move(column)), name(alias) {}
|
||||
};
|
||||
|
||||
struct ColDefNode : Node {
|
||||
@@ -67,16 +91,16 @@ namespace usql {
|
||||
int length;
|
||||
bool null;
|
||||
|
||||
ColDefNode(const std::string col_name, ColumnType col_type, int col_order, int col_len, bool nullable) :
|
||||
ColDefNode(const std::string& col_name, ColumnType col_type, int col_order, int col_len, bool nullable) :
|
||||
Node(NodeType::column_def), name(col_name), type(col_type), order(col_order), length(col_len),
|
||||
null(nullable) {}
|
||||
};
|
||||
|
||||
struct FunctionNode : Node {
|
||||
std::string function;
|
||||
std::string function; // TODO use enum
|
||||
std::vector<std::unique_ptr<Node>> params;
|
||||
|
||||
FunctionNode(const std::string func_name, std::vector<std::unique_ptr<Node>> pars) :
|
||||
FunctionNode(const std::string& func_name, std::vector<std::unique_ptr<Node>> pars) :
|
||||
Node(NodeType::function), function(func_name), params(std::move(pars)) {}
|
||||
};
|
||||
|
||||
@@ -85,14 +109,16 @@ namespace usql {
|
||||
};
|
||||
|
||||
struct ValueNode : Node {
|
||||
ValueNode(NodeType type) : Node(type) {}
|
||||
explicit ValueNode(NodeType type) : Node(type) {}
|
||||
|
||||
virtual bool isNull() { return false; }
|
||||
virtual long getIntValue() = 0;
|
||||
virtual long getIntegerValue() = 0;
|
||||
virtual double getDoubleValue() = 0;
|
||||
virtual std::string getStringValue() = 0;
|
||||
virtual long getDateValue() = 0;
|
||||
virtual bool getBooleanValue() = 0;
|
||||
|
||||
virtual ~ValueNode() {};
|
||||
virtual ~ValueNode() = default;
|
||||
};
|
||||
|
||||
struct NullValueNode : ValueNode {
|
||||
@@ -101,45 +127,66 @@ namespace usql {
|
||||
|
||||
bool isNull() override { return true; }
|
||||
|
||||
long getIntValue() override { throw Exception("not supported on null value"); };
|
||||
double getDoubleValue() override { throw Exception("not supported on null value"); };
|
||||
std::string getStringValue() override { throw Exception("not supported on null value"); };
|
||||
long getIntegerValue() override { throw Exception("getIntegerValue not supported on NullValueNode"); };
|
||||
double getDoubleValue() override { throw Exception("getDoubleValue not supported on NullValueNode"); };
|
||||
std::string getStringValue() override { throw Exception("getStringValue not supported on NullValueNode"); };
|
||||
long getDateValue() override { throw Exception("getDateValue not supported on NullValueNode"); };
|
||||
bool getBooleanValue() override { throw Exception("getBooleanValue not supported on NullValueNode"); };
|
||||
};
|
||||
|
||||
struct IntValueNode : ValueNode {
|
||||
long value;
|
||||
|
||||
IntValueNode(long value) : ValueNode(NodeType::int_value), value(value) {}
|
||||
explicit IntValueNode(long value) : ValueNode(NodeType::int_value), value(value) {}
|
||||
|
||||
long getIntValue() override { return value; };
|
||||
long getIntegerValue() override { return value; };
|
||||
double getDoubleValue() override { return (double) value; };
|
||||
std::string getStringValue() override { return std::to_string(value); }
|
||||
long getDateValue() override { return value; };
|
||||
bool getBooleanValue() override { return value != 0; };
|
||||
};
|
||||
|
||||
struct DoubleValueNode : ValueNode {
|
||||
double value;
|
||||
|
||||
DoubleValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
|
||||
explicit DoubleValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
|
||||
|
||||
long getIntValue() override { return (long) value; };
|
||||
long getIntegerValue() override { return (long) value; };
|
||||
double getDoubleValue() override { return value; };
|
||||
std::string getStringValue() override { return std::to_string(value); }
|
||||
long getDateValue() override { return (long) value; };
|
||||
bool getBooleanValue() override { return value != 0.0; };
|
||||
};
|
||||
|
||||
struct StringValueNode : ValueNode {
|
||||
std::string value;
|
||||
|
||||
StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(value) {}
|
||||
explicit StringValueNode(const std::string &value) : ValueNode(NodeType::string_value), value(value) {}
|
||||
|
||||
long getIntValue() override { return std::stoi(value); };
|
||||
long getIntegerValue() override { return std::stoi(value); };
|
||||
double getDoubleValue() override { return std::stod(value); };
|
||||
std::string getStringValue() override { return value; };
|
||||
long getDateValue() override { return Settings::string_to_date(value); };
|
||||
bool getBooleanValue() override { return value == "true"; };
|
||||
};
|
||||
|
||||
struct BooleanValueNode : ValueNode {
|
||||
bool value;
|
||||
|
||||
explicit BooleanValueNode(bool value) : ValueNode(NodeType::bool_value), value(value) {}
|
||||
|
||||
long getIntegerValue() override { return (long) value; };
|
||||
double getDoubleValue() override { return (double) value; };
|
||||
std::string getStringValue() override { return value ? "true" : "false"; }
|
||||
long getDateValue() override { return (long) value; };
|
||||
bool getBooleanValue() override { return value; };
|
||||
};
|
||||
|
||||
|
||||
struct DatabaseValueNode : Node {
|
||||
std::string col_name;
|
||||
|
||||
DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(name) {}
|
||||
explicit DatabaseValueNode(const std::string &name) : Node(NodeType::database_value), col_name(name) {}
|
||||
};
|
||||
|
||||
enum class LogicalOperatorType {
|
||||
@@ -200,42 +247,45 @@ namespace usql {
|
||||
std::vector<ColDefNode> cols_defs;
|
||||
|
||||
CreateTableNode(const std::string& name, std::vector<ColDefNode> defs) :
|
||||
Node(NodeType::create_table), table_name(name), cols_defs(defs) {}
|
||||
Node(NodeType::create_table), table_name(name), cols_defs(std::move(defs)) {}
|
||||
};
|
||||
|
||||
struct InsertIntoTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::vector<DatabaseValueNode> cols_names;
|
||||
std::vector<std::unique_ptr<Node>> cols_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)) {}
|
||||
InsertIntoTableNode(const std::string& name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> values) :
|
||||
Node(NodeType::insert_into), table_name(name), cols_names(std::move(names)), cols_values(std::move(values)) {}
|
||||
};
|
||||
|
||||
struct SelectFromTableNode : Node {
|
||||
std::string table_name;
|
||||
std::unique_ptr<std::vector<SelectColNode>> cols_names;
|
||||
std::unique_ptr<Node> where;
|
||||
std::vector<ColOrderNode> order_by;
|
||||
OffsetLimitNode offset_limit;
|
||||
bool distinct;
|
||||
|
||||
SelectFromTableNode(std::string name, std::unique_ptr<std::vector<SelectColNode>> names, std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::select_from), table_name(name), cols_names(std::move(names)), where(std::move(where_clause)) {}
|
||||
SelectFromTableNode(std::string name, std::unique_ptr<std::vector<SelectColNode>> names, std::unique_ptr<Node> where_clause, std::vector<ColOrderNode> orderby, OffsetLimitNode offlim, bool distinct_):
|
||||
Node(NodeType::select_from), table_name(std::move(name)), cols_names(std::move(names)), where(std::move(where_clause)), order_by(std::move(orderby)), offset_limit(offlim), distinct(distinct_) {}
|
||||
};
|
||||
|
||||
struct CreateTableAsSelectNode : Node {
|
||||
std::string table_name;
|
||||
std::unique_ptr<Node> select_table;
|
||||
|
||||
CreateTableAsSelectNode(const std::string name, std::unique_ptr<Node> table) :
|
||||
CreateTableAsSelectNode(const std::string& name, std::unique_ptr<Node> table) :
|
||||
Node(NodeType::create_table_as_select), table_name(name), select_table(std::move(table)) {}
|
||||
};
|
||||
|
||||
struct UpdateTableNode : Node {
|
||||
std::string table_name;
|
||||
std::vector<ColNameNode> cols_names;
|
||||
std::vector<DatabaseValueNode> cols_names;
|
||||
std::vector<std::unique_ptr<Node>> values;
|
||||
std::unique_ptr<Node> where;
|
||||
|
||||
UpdateTableNode(std::string name, std::vector<ColNameNode> names, std::vector<std::unique_ptr<Node>> vals,
|
||||
UpdateTableNode(const std::string &name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> vals,
|
||||
std::unique_ptr<Node> where_clause) :
|
||||
Node(NodeType::update_table), table_name(name), cols_names(names), values(std::move(vals)),
|
||||
where(std::move(where_clause)) {}
|
||||
@@ -245,7 +295,7 @@ namespace usql {
|
||||
std::string table_name;
|
||||
std::string filename;
|
||||
|
||||
LoadIntoTableNode(const std::string name, std::string file) :
|
||||
LoadIntoTableNode(const std::string& name, const std::string &file) :
|
||||
Node(NodeType::load_table), table_name(name), filename(file) {}
|
||||
};
|
||||
|
||||
@@ -253,14 +303,14 @@ namespace usql {
|
||||
std::string table_name;
|
||||
std::string filename;
|
||||
|
||||
SaveTableNode(const std::string& name, std::string file) :
|
||||
SaveTableNode(const std::string& name, const std::string &file) :
|
||||
Node(NodeType::save_table), table_name(name), filename(file) {}
|
||||
};
|
||||
|
||||
struct DropTableNode : Node {
|
||||
std::string table_name;
|
||||
|
||||
DropTableNode(const std::string& name) : Node(NodeType::drop_table), table_name(name) {}
|
||||
explicit DropTableNode(const std::string& name) : Node(NodeType::drop_table), table_name(name) {}
|
||||
};
|
||||
|
||||
struct DeleteFromTableNode : Node {
|
||||
@@ -271,6 +321,21 @@ namespace usql {
|
||||
Node(NodeType::delete_from), table_name(name), where(std::move(where_clause)) {}
|
||||
};
|
||||
|
||||
struct SetNode : Node {
|
||||
std::string name;
|
||||
std::string value;
|
||||
|
||||
SetNode(const std::string& name_, const std::string& value_) :
|
||||
Node(NodeType::set), name(name_), value(value_) {}
|
||||
};
|
||||
|
||||
struct ShowNode : Node {
|
||||
std::string name;
|
||||
|
||||
explicit ShowNode(const std::string& name_) : Node(NodeType::show), name(name_) {}
|
||||
};
|
||||
|
||||
|
||||
|
||||
class Parser {
|
||||
private:
|
||||
@@ -282,17 +347,24 @@ namespace usql {
|
||||
|
||||
private:
|
||||
std::unique_ptr<Node> parse_create_table();
|
||||
std::unique_ptr<Node> parse_drop_table();
|
||||
std::unique_ptr<Node> parse_load_table();
|
||||
std::unique_ptr<Node> parse_save_table();
|
||||
std::unique_ptr<Node> parse_drop_table();
|
||||
std::unique_ptr<Node> parse_set();
|
||||
std::unique_ptr<Node> parse_show();
|
||||
|
||||
std::unique_ptr<Node> parse_insert_into_table();
|
||||
std::unique_ptr<Node> parse_select_from_table();
|
||||
std::unique_ptr<Node> parse_delete_from_table();
|
||||
std::unique_ptr<Node> parse_update_table();
|
||||
|
||||
std::vector<ColOrderNode> parse_order_by_clause();
|
||||
OffsetLimitNode parse_offset_limit_clause();
|
||||
|
||||
std::unique_ptr<Node> parse_where_clause();
|
||||
std::unique_ptr<Node> parse_operand_node();
|
||||
std::unique_ptr<Node> parse_expression();
|
||||
std::unique_ptr<Node> parse_expression(std::unique_ptr<Node> left);
|
||||
|
||||
std::unique_ptr<Node> parse_value();
|
||||
RelationalOperatorType parse_relational_operator();
|
||||
LogicalOperatorType parse_logical_operator();
|
||||
@@ -300,8 +372,6 @@ namespace usql {
|
||||
|
||||
private:
|
||||
Lexer m_lexer;
|
||||
|
||||
std::unique_ptr<Node> parse_relational_expression();
|
||||
};
|
||||
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user