usql updated

This commit is contained in:
vaclavt
2022-02-17 20:42:30 +01:00
parent 765f2bc673
commit 7ad26ba427
7 changed files with 201 additions and 85 deletions

View File

@@ -21,6 +21,9 @@ enum class ColumnType {
bool_type
};
std::string column_type_name(const ColumnType type);
enum class NodeType {
true_node,
null_value,
@@ -51,6 +54,7 @@ enum class NodeType {
error
};
struct Node {
NodeType node_type;
@@ -58,7 +62,7 @@ struct Node {
virtual ~Node() = default;
virtual void dump() const {
std::cout << "type: Node" << std::endl;
std::cout << "type: Node" << (int)node_type << std::endl;
}
};
@@ -115,19 +119,76 @@ struct ColDefNode : Node {
null(nullable) {}
void dump() const override {
std::cout << "type: ColDefNode, name: " << name << ", type: " << (int)type << " TODO add more" << std::endl;
std::cout << "type: ColDefNode, name: " << name << ", type: " << column_type_name(type) << ", order: " << order << ", length: " << length << ", null: " << null << std::endl;
}
};
struct FunctionNode : Node {
std::string function; // TODO use enum
enum class Type {
to_string,
to_date,
date_add,
pp,
lower,
upper,
min,
max,
count
};
static Type get_function(const std::string &str) {
if (str=="to_string") return Type::to_string;
if (str=="to_date") return Type::to_date;
if (str=="date_add") return Type::date_add;
if (str=="pp") return Type::pp;
if (str=="lower") return Type::lower;
if (str=="upper") return Type::upper;
if (str=="min") return Type::min;
if (str=="max") return Type::max;
if (str=="count") return Type::count;
throw Exception("invalid function: " + str);
};
static std::string function_name(const Type type) {
if (type == Type::to_string) return "to_string";
if (type == Type::to_date) return "to_date";
if (type == Type::date_add) return "date_add";
if (type == Type::pp) return "pp";
if (type == Type::lower) return "lower";
if (type == Type::upper) return "upper";
if (type == Type::min) return "min";
if (type == Type::max) return "max";
if (type == Type::count) return "count";
throw Exception("invalid function: " + (int)type);
};
Type function;
std::vector<std::unique_ptr<Node>> params;
FunctionNode(std::string func_name, std::vector<std::unique_ptr<Node>> pars) :
Node(NodeType::function), function(std::move(func_name)), params(std::move(pars)) {}
Node(NodeType::function), function(get_function(func_name)), params(std::move(pars)) {}
bool is_agg_function() {
return (function == Type::count || function == Type::min || function == Type::max);
}
friend std::ostream &operator<<(std::ostream &output, const Type &t ) {
output << function_name(t);
return output;
}
void dump() const override {
std::cout << "type: FunctionNode, function: " << function << " TODO add more" << std::endl;
std::cout << "type: FunctionNode, function: " << function_name(function) << "(";
for(int i = 0; i < params.size(); i++){
if (i > 0) std::cout << ",";
params[i]->dump();
}
std::cout << ")" << std::endl;
}
};
@@ -325,12 +386,17 @@ struct CreateTableNode : Node {
Node(NodeType::create_table), table_name(std::move(name)), cols_defs(std::move(defs)) {}
void dump() const override {
std::cout << "type: CreateTableNode, table_name: " << table_name << "TODO complete me" << std::endl;
std::cout << "type: CreateTableNode, table_name: " << table_name << "(";
for(int i = 0; i < cols_defs.size(); i++) {
if (i > 0) std::cout << ",";
cols_defs[i].dump();
}
std::cout << ")" << std::endl;
}
};
struct InsertIntoTableNode : Node {
std::string table_name;
std::string table_name;
std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> cols_values;
@@ -338,29 +404,53 @@ struct InsertIntoTableNode : Node {
Node(NodeType::insert_into), table_name(std::move(name)), cols_names(std::move(names)), cols_values(std::move(values)) {}
void dump() const override {
std::cout << "type: InsertIntoTableNode, table_name: " << table_name << "TODO complete me" << std::endl;
std::cout << "type: InsertIntoTableNode, table_name: " << table_name << "(";
for(int i = 0; i < cols_names.size(); i++) {
if (i > 0) std::cout << ",";
cols_names[i].dump();
}
std::cout << ") values (";
for(int i = 0; i < cols_values.size(); i++) {
if (i > 0) std::cout << ",";
cols_values[i]->dump();
}
std::cout << ")" << std::endl;
}
};
struct SelectFromTableNode : Node {
std::string table_name;
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;
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, 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(std::move(offlim)), distinct(distinct_) {}
void dump() const override {
std::cout << "type: SelectFromTableNode, table_name: " << table_name << "TODO complete me" << std::endl;
std::cout << "type: SelectFromTableNode, table_name: " << table_name;
std::cout << "colums: ";
for(int i = 0; i < cols_names->size(); i++) {
if (i > 0) std::cout << ",";
cols_names->operator[](i).dump();
}
std::cout << "where: ";
where->dump();
std::cout << "offset,limit: ";
for(int i = 0; i < order_by.size(); i++) {
if (i > 0) std::cout << ",";
order_by[i].dump();
}
std::cout << "offset,limit: ";
offset_limit.dump();
std::cout << std::endl;
}
};
struct CreateTableAsSelectNode : Node {
std::string table_name;
std::string table_name;
std::unique_ptr<Node> select_table;
CreateTableAsSelectNode(std::string name, std::unique_ptr<Node> table) :
@@ -373,10 +463,10 @@ struct CreateTableAsSelectNode : Node {
};
struct UpdateTableNode : Node {
std::string table_name;
std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> values;
std::unique_ptr<Node> where;
std::string table_name;
std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> values;
std::unique_ptr<Node> where;
UpdateTableNode(std::string name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> vals,
std::unique_ptr<Node> where_clause) :
@@ -384,8 +474,16 @@ struct UpdateTableNode : Node {
where(std::move(where_clause)) {}
void dump() const override {
std::cout << "type: UpdateTableNode, table_name: " << table_name << "TODO complete me" << std::endl;
std::cout << "type: UpdateTableNode, table_name: " << table_name << " set ";
for(int i = 0; i < cols_names.size(); i++) {
if (i > 0) std::cout << ",";
cols_names[i].dump();
std::cout << " = ";
values[i]->dump();
}
std::cout << " where: ";
where->dump();
std::cout << std::endl;
}
};
@@ -431,8 +529,10 @@ struct DeleteFromTableNode : Node {
Node(NodeType::delete_from), table_name(std::move(name)), where(std::move(where_clause)) {}
void dump() const override {
std::cout << "type: DeleteFromTableNode, table_name: " << table_name << std::endl;
std::cout << "type: DeleteFromTableNode, table_name: " << table_name;
std::cout << "where: ";
where->dump();
std::cout << std::endl;
}
};