some methods marked const

This commit is contained in:
2021-12-18 12:21:34 +01:00
parent 40fa78dd2a
commit c9c4f0fba3
8 changed files with 376 additions and 395 deletions

2
.gitignore vendored
View File

@@ -5,3 +5,5 @@ tmp/.idea
Makefile
localhost.session.sql
CMakeFiles
cmake-build-debug-coverage
.idea

34
index.h
View File

@@ -29,15 +29,15 @@ public:
}
std::vector<rowid_t> search(ValueNode *key) {
std::vector<rowid_t> search(const ValueNode *key) {
return search(to_index_value(key));
}
void insert(ColValue *key, rowid_t rowid) {
void insert(const ColValue *key, rowid_t rowid) {
return insert(to_index_value(key), rowid);
}
void remove(ColValue *key, rowid_t rowid) {
void remove(const ColValue *key, rowid_t rowid) {
return remove(to_index_value(key), rowid);
}
@@ -53,13 +53,9 @@ public:
return m_index_name;
}
[[nodiscard]] ColumnType get_data_type() const {
return m_data_type;
}
private:
IndexValue to_index_value(ValueNode *key) {
IndexValue to_index_value(const ValueNode *key) {
if (m_data_type == ColumnType::integer_type)
return key->getIntegerValue();
else if (m_data_type == ColumnType::varchar_type)
@@ -68,16 +64,16 @@ private:
throw Exception("using index on unsupported type");
}
IndexValue to_index_value(ColValue *key) {
IndexValue to_index_value(const ColValue *key) {
if (m_data_type == ColumnType::integer_type)
return key->getIntValue();
return key->getIntegerValue();
else if (m_data_type == ColumnType::varchar_type)
return key->getStringValue();
else
throw Exception("using index on unsupported type");
}
void insert(IndexValue key, rowid_t rowid) {
void insert(const IndexValue& key, rowid_t rowid) {
auto search = m_index.find(key);
if (search != m_index.end()) {
if (m_uniq)
@@ -92,7 +88,7 @@ private:
}
}
void remove(IndexValue key, rowid_t rowid) {
void remove(const IndexValue& key, rowid_t rowid) {
auto search = m_index.find(key);
if (search != m_index.end()) {
search->second.erase(find(search->second.begin(), search->second.end(), rowid));
@@ -111,21 +107,11 @@ private:
}
// void dump() {
// std::for_each(m_index.begin(), m_index.end(),
// [](std::pair<IndexValue, std::vector<rowid_t>> element){
// IndexValue key = element.first;
// std::vector<rowid_t> rowids = element.second;
// std::cout << "key: " << key << ", rowids count:" << rowids.size() << std::endl;
// });
// }
private:
bool m_uniq;
bool m_uniq;
std::string m_index_name;
std::string m_column_name;
ColumnType m_data_type;
ColumnType m_data_type;
std::map<IndexValue, std::vector<rowid_t> > m_index;
};

535
parser.h
View File

@@ -52,41 +52,41 @@ namespace usql {
};
struct Node {
NodeType node_type;
NodeType node_type;
explicit Node(const NodeType type) : node_type(type) {}
virtual ~Node() = default;
explicit Node(const NodeType type) : node_type(type) {}
virtual ~Node() = default;
virtual void dump() const {
std::cout << "type: Node" << std::endl;
}
virtual void dump() const {
std::cout << "type: Node" << std::endl;
}
};
struct ColOrderNode : Node {
std::string col_name;
int col_index;
bool ascending;
std::string col_name;
int col_index;
bool ascending;
ColOrderNode(std::string name, bool asc) : Node(NodeType::column_order), col_name(std::move(name)), col_index(-1), ascending(asc) {}
ColOrderNode(int index, bool asc) : Node(NodeType::database_value), col_index(index), ascending(asc) {}
ColOrderNode(std::string name, bool asc) : Node(NodeType::column_order), col_name(std::move(name)), col_index(-1), ascending(asc) {}
ColOrderNode(int index, bool asc) : Node(NodeType::database_value), col_index(index), ascending(asc) {}
void dump() const override {
std::cout << "type: ColOrderNode, col_name: " << col_name << ", col_index: " << col_index << ", asc: " << ascending << std::endl;
}
void dump() const override {
std::cout << "type: ColOrderNode, col_name: " << col_name << ", col_index: " << col_index << ", asc: " << ascending << std::endl;
}
};
struct OffsetLimitNode : Node {
int offset;
int limit;
int offset;
int limit;
OffsetLimitNode(int off, int lim) : Node(NodeType::offset_limit), offset(off), limit(lim) {}
OffsetLimitNode(int off, int lim) : Node(NodeType::offset_limit), offset(off), limit(lim) {}
void dump() const override {
std::cout << "type: OffsetLimitNode, offset: " << offset << ", limit: " << limit << std::endl;
}
void dump() const override {
std::cout << "type: OffsetLimitNode, offset: " << offset << ", limit: " << limit << std::endl;
}
};
@@ -97,26 +97,26 @@ namespace usql {
SelectColNode(std::unique_ptr<Node> column, std::string alias) :
Node(NodeType::database_value), value(std::move(column)), name(std::move(alias)) {}
void dump() const override {
std::cout << "type: SelectColNode, name:" << name << "value:" << std::endl;
value->dump();
}
void dump() const override {
std::cout << "type: SelectColNode, name:" << name << "value:" << std::endl;
value->dump();
}
};
struct ColDefNode : Node {
std::string name;
ColumnType type;
int order;
int length;
bool null;
std::string name;
ColumnType type;
int order;
int length;
bool null;
ColDefNode(std::string col_name, ColumnType col_type, int col_order, int col_len, bool nullable) :
Node(NodeType::column_def), name(std::move(col_name)), type(col_type), order(col_order), length(col_len),
null(nullable) {}
ColDefNode(std::string col_name, ColumnType col_type, int col_order, int col_len, bool nullable) :
Node(NodeType::column_def), name(std::move(col_name)), type(col_type), order(col_order), length(col_len),
null(nullable) {}
void dump() const override {
std::cout << "type: ColDefNode, name: " << name << ", type: " << (int)type << " TODO add more" << std::endl;
}
void dump() const override {
std::cout << "type: ColDefNode, name: " << name << ", type: " << (int)type << " TODO add more" << std::endl;
}
};
struct FunctionNode : Node {
@@ -126,95 +126,95 @@ namespace usql {
FunctionNode(std::string func_name, std::vector<std::unique_ptr<Node>> pars) :
Node(NodeType::function), function(std::move(func_name)), params(std::move(pars)) {}
void dump() const override {
std::cout << "type: FunctionNode, function: " << function << " TODO add more" << std::endl;
}
void dump() const override {
std::cout << "type: FunctionNode, function: " << function << " TODO add more" << std::endl;
}
};
struct TrueNode : Node {
TrueNode() : Node(NodeType::true_node) {}
TrueNode() : Node(NodeType::true_node) {}
void dump() const override {
std::cout << "type: TrueNode," << std::endl;
}
void dump() const override {
std::cout << "type: TrueNode," << std::endl;
}
};
struct ValueNode : Node {
explicit ValueNode(NodeType type) : Node(type) {}
explicit ValueNode(NodeType type) : Node(type) {}
virtual bool isNull() { return false; }
virtual long getIntegerValue() = 0;
virtual double getDoubleValue() = 0;
virtual std::string getStringValue() = 0;
virtual long getDateValue() = 0;
virtual bool getBooleanValue() = 0;
virtual bool isNull() const { return false; }
virtual long getIntegerValue() const = 0;
virtual double getDoubleValue() const = 0;
virtual std::string getStringValue() const = 0;
virtual long getDateValue() const = 0;
virtual bool getBooleanValue() const = 0;
~ValueNode() override = default;
~ValueNode() override = default;
};
struct NullValueNode : ValueNode {
NullValueNode() : ValueNode(NodeType::null_value) {}
bool isNull() override { return true; }
bool isNull() const override { return true; }
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"); };
long getIntegerValue() const override { throw Exception("getIntegerValue not supported on NullValueNode"); };
double getDoubleValue() const override { throw Exception("getDoubleValue not supported on NullValueNode"); };
std::string getStringValue() const override { throw Exception("getStringValue not supported on NullValueNode"); };
long getDateValue() const override { throw Exception("getDateValue not supported on NullValueNode"); };
bool getBooleanValue() const override { throw Exception("getBooleanValue not supported on NullValueNode"); };
void dump() const override {
std::cout << "type: NullValueNode," << std::endl;
}
void dump() const override {
std::cout << "type: NullValueNode," << std::endl;
}
};
struct IntValueNode : ValueNode {
long value;
long value;
explicit IntValueNode(long value) : ValueNode(NodeType::int_value), value(value) {}
explicit IntValueNode(long value) : ValueNode(NodeType::int_value), value(value) {}
long getIntegerValue() override { return value; };
double getDoubleValue() override { return (double) value; };
std::string getStringValue() override { return Settings::int_to_string(value); }
long getDateValue() override { return value; };
bool getBooleanValue() override { return value != 0; };
long getIntegerValue() const override { return value; };
double getDoubleValue() const override { return (double) value; };
std::string getStringValue() const override { return Settings::int_to_string(value); }
long getDateValue() const override { return value; };
bool getBooleanValue() const override { return value != 0; };
void dump() const override {
std::cout << "type: IntValueNode, value: " << value << std::endl;
}
void dump() const override {
std::cout << "type: IntValueNode, value: " << value << std::endl;
}
};
struct DoubleValueNode : ValueNode {
double value;
double value;
explicit DoubleValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
explicit DoubleValueNode(double value) : ValueNode(NodeType::float_value), value(value) {}
long getIntegerValue() override { return (long) value; };
double getDoubleValue() override { return value; };
std::string getStringValue() override { return Settings::double_to_string(value); }
long getDateValue() override { return (long) value; };
bool getBooleanValue() override { return value != 0.0; };
long getIntegerValue() const override { return (long) value; };
double getDoubleValue() const override { return value; };
std::string getStringValue() const override { return Settings::double_to_string(value); }
long getDateValue() const override { return (long) value; };
bool getBooleanValue() const override { return value != 0.0; };
void dump() const override {
std::cout << "type: DoubleValueNode, value: " << value << std::endl;
}
void dump() const override {
std::cout << "type: DoubleValueNode, value: " << value << std::endl;
}
};
struct StringValueNode : ValueNode {
std::string value;
std::string value;
explicit StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(std::move(value)) {}
explicit StringValueNode(std::string value) : ValueNode(NodeType::string_value), value(std::move(value)) {}
long getIntegerValue() override { return Settings::string_to_int(value); };
double getDoubleValue() override { return Settings::string_to_double(value); };
std::string getStringValue() override { return value; };
long getDateValue() override { return Settings::string_to_date(value); };
bool getBooleanValue() override { return Settings::string_to_bool(value); };
long getIntegerValue() const override { return Settings::string_to_int(value); };
double getDoubleValue() const override { return Settings::string_to_double(value); };
std::string getStringValue() const override { return value; };
long getDateValue() const override { return Settings::string_to_date(value); };
bool getBooleanValue() const override { return Settings::string_to_bool(value); };
void dump() const override {
std::cout << "type: StringValueNode, value: " << value << std::endl;
}
void dump() const override {
std::cout << "type: StringValueNode, value: " << value << std::endl;
}
};
struct BooleanValueNode : ValueNode {
@@ -222,141 +222,141 @@ namespace usql {
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 Settings::bool_to_string(value); }
long getDateValue() override { return (long) value; };
bool getBooleanValue() override { return value; };
long getIntegerValue() const override { return (long) value; };
double getDoubleValue() const override { return (double) value; };
std::string getStringValue() const override { return Settings::bool_to_string(value); }
long getDateValue() const override { return (long) value; };
bool getBooleanValue() const override { return value; };
void dump() const override {
std::cout << "type: BooleanValueNode, value: " << value << std::endl;
}
void dump() const override {
std::cout << "type: BooleanValueNode, value: " << value << std::endl;
}
};
struct DatabaseValueNode : Node {
std::string col_name;
std::string col_name;
explicit DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(std::move(name)) {}
explicit DatabaseValueNode(std::string name) : Node(NodeType::database_value), col_name(std::move(name)) {}
void dump() const override {
std::cout << "type: DatabaseValueNode, col_name: " << col_name << std::endl;
}
void dump() const override {
std::cout << "type: DatabaseValueNode, col_name: " << col_name << std::endl;
}
};
enum class LogicalOperatorType {
and_operator,
or_operator
// not_operator
and_operator,
or_operator
// not_operator
};
struct LogicalOperatorNode : Node {
LogicalOperatorType op;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
LogicalOperatorType op;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
LogicalOperatorNode(LogicalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::logical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
LogicalOperatorNode(LogicalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::logical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
void dump() const override {
std::cout << "type: LogicalOperatorNode, op: " << (int)op << std::endl;
left->dump();
right->dump();
}
void dump() const override {
std::cout << "type: LogicalOperatorNode, op: " << (int)op << std::endl;
left->dump();
right->dump();
}
};
enum class RelationalOperatorType {
equal,
greater,
greater_equal,
lesser,
lesser_equal,
not_equal,
is,
is_not
// like
equal,
greater,
greater_equal,
lesser,
lesser_equal,
not_equal,
is,
is_not
// like
};
struct RelationalOperatorNode : Node {
RelationalOperatorType op;
RelationalOperatorType op;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {};
RelationalOperatorNode(RelationalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::relational_operator), op(op), left(std::move(left)), right(std::move(right)) {};
void dump() const override {
std::cout << "type: RelationalOperatorNode, op: " << (int)op << std::endl;
left->dump();
right->dump();
}
void dump() const override {
std::cout << "type: RelationalOperatorNode, op: " << (int)op << std::endl;
left->dump();
right->dump();
}
};
enum class ArithmeticalOperatorType {
copy_value, // just copy lef value and do nothing with it
plus_operator,
minus_operator,
multiply_operator,
divide_operator
copy_value, // just copy lef value and do nothing with it
plus_operator,
minus_operator,
multiply_operator,
divide_operator
};
struct ArithmeticalOperatorNode : Node {
ArithmeticalOperatorType op;
ArithmeticalOperatorType op;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
std::unique_ptr<Node> left;
std::unique_ptr<Node> right;
ArithmeticalOperatorNode(ArithmeticalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
ArithmeticalOperatorNode(ArithmeticalOperatorType op, std::unique_ptr<Node> left, std::unique_ptr<Node> right) :
Node(NodeType::arithmetical_operator), op(op), left(std::move(left)), right(std::move(right)) {};
void dump() const override {
std::cout << "type: ArithmeticalOperatorNode, op: " << (int)op << std::endl;
left->dump();
right->dump();
}
void dump() const override {
std::cout << "type: ArithmeticalOperatorNode, op: " << (int)op << std::endl;
left->dump();
right->dump();
}
};
struct CreateTableNode : Node {
std::string table_name;
std::vector<ColDefNode> cols_defs;
std::string table_name;
std::vector<ColDefNode> cols_defs;
CreateTableNode(std::string name, std::vector<ColDefNode> defs) :
Node(NodeType::create_table), table_name(std::move(name)), cols_defs(std::move(defs)) {}
CreateTableNode(std::string name, std::vector<ColDefNode> defs) :
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;
}
void dump() const override {
std::cout << "type: CreateTableNode, table_name: " << table_name << "TODO complete me" << std::endl;
}
};
struct InsertIntoTableNode : Node {
std::string table_name;
std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> cols_values;
std::string table_name;
std::vector<DatabaseValueNode> cols_names;
std::vector<std::unique_ptr<Node>> cols_values;
InsertIntoTableNode(std::string name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> values) :
Node(NodeType::insert_into), table_name(std::move(name)), cols_names(std::move(names)), cols_values(std::move(values)) {}
InsertIntoTableNode(std::string name, std::vector<DatabaseValueNode> names, std::vector<std::unique_ptr<Node>> values) :
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;
}
void dump() const override {
std::cout << "type: InsertIntoTableNode, table_name: " << table_name << "TODO complete me" << std::endl;
}
};
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;
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, 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_) {}
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;
where->dump();
}
void dump() const override {
std::cout << "type: SelectFromTableNode, table_name: " << table_name << "TODO complete me" << std::endl;
where->dump();
}
};
struct CreateTableAsSelectNode : Node {
@@ -366,51 +366,51 @@ namespace usql {
CreateTableAsSelectNode(std::string name, std::unique_ptr<Node> table) :
Node(NodeType::create_table_as_select), table_name(std::move(name)), select_table(std::move(table)) {}
void dump() const override {
std::cout << "type: CreateTableAsSelectNode, table_name: " << table_name << std::endl;
select_table->dump();
}
void dump() const override {
std::cout << "type: CreateTableAsSelectNode, table_name: " << table_name << std::endl;
select_table->dump();
}
};
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) :
Node(NodeType::update_table), table_name(std::move(name)), cols_names(std::move(names)), values(std::move(vals)),
where(std::move(where_clause)) {}
UpdateTableNode(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(std::move(name)), cols_names(std::move(names)), values(std::move(vals)),
where(std::move(where_clause)) {}
void dump() const override {
std::cout << "type: UpdateTableNode, table_name: " << table_name << "TODO complete me" << std::endl;
where->dump();
}
void dump() const override {
std::cout << "type: UpdateTableNode, table_name: " << table_name << "TODO complete me" << std::endl;
where->dump();
}
};
struct LoadIntoTableNode : Node {
std::string table_name;
std::string filename;
std::string table_name;
std::string filename;
LoadIntoTableNode(std::string name, std::string file) :
Node(NodeType::load_table), table_name(std::move(name)), filename(std::move(file)) {}
LoadIntoTableNode(std::string name, std::string file) :
Node(NodeType::load_table), table_name(std::move(name)), filename(std::move(file)) {}
void dump() const override {
std::cout << "type: LoadIntoTableNode, table_name: " << table_name << ", filename" << filename << std::endl;
}
void dump() const override {
std::cout << "type: LoadIntoTableNode, table_name: " << table_name << ", filename" << filename << std::endl;
}
};
struct SaveTableNode : Node {
std::string table_name;
std::string filename;
std::string table_name;
std::string filename;
SaveTableNode(std::string name, std::string file) :
Node(NodeType::save_table), table_name(std::move(name)), filename(std::move(file)) {}
SaveTableNode(std::string name, std::string file) :
Node(NodeType::save_table), table_name(std::move(name)), filename(std::move(file)) {}
void dump() const override {
std::cout << "type: SaveTableNode, table_name: " << table_name << ", filename" << filename << std::endl;
}
void dump() const override {
std::cout << "type: SaveTableNode, table_name: " << table_name << ", filename" << filename << std::endl;
}
};
struct DropTableNode : Node {
@@ -418,95 +418,94 @@ namespace usql {
explicit DropTableNode(std::string name) : Node(NodeType::drop_table), table_name(std::move(name)) {}
void dump() const override {
std::cout << "type: SelectFromTableNode, table_name: " << table_name << std::endl;
}
void dump() const override {
std::cout << "type: SelectFromTableNode, table_name: " << table_name << std::endl;
}
};
struct DeleteFromTableNode : Node {
std::string table_name;
std::unique_ptr<Node> where;
std::string table_name;
std::unique_ptr<Node> where;
DeleteFromTableNode(std::string name, std::unique_ptr<Node> where_clause) :
Node(NodeType::delete_from), table_name(std::move(name)), where(std::move(where_clause)) {}
DeleteFromTableNode(std::string name, std::unique_ptr<Node> where_clause) :
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;
where->dump();
}
void dump() const override {
std::cout << "type: DeleteFromTableNode, table_name: " << table_name << std::endl;
where->dump();
}
};
struct SetNode : Node {
std::string name;
std::string value;
std::string name;
std::string value;
SetNode(std::string node_name, std::string node_value) :
Node(NodeType::set), name(std::move(node_name)), value(std::move(node_value)) {}
SetNode(std::string node_name, std::string node_value) :
Node(NodeType::set), name(std::move(node_name)), value(std::move(node_value)) {}
void dump() const override {
std::cout << "type: SetNode, name: " << name << ", value: " << value << std::endl;
}
void dump() const override {
std::cout << "type: SetNode, name: " << name << ", value: " << value << std::endl;
}
};
struct ShowNode : Node {
std::string name;
std::string name;
explicit ShowNode(std::string node_name) : Node(NodeType::show), name(std::move(node_name)) {}
explicit ShowNode(std::string node_name) : Node(NodeType::show), name(std::move(node_name)) {}
void dump() const override {
std::cout << "type: ShowNode, name: " << name << std::endl;
}
void dump() const override {
std::cout << "type: ShowNode, name: " << name << std::endl;
}
};
struct CreateIndexNode : Node {
std::string index_name;
std::string table_name;
std::string column_name;
std::string index_name;
std::string table_name;
std::string column_name;
CreateIndexNode(std::string idx_name, std::string tbl_name, std::string col_name) :
Node(NodeType::create_index), index_name(std::move(idx_name)), table_name(std::move(tbl_name)), column_name(std::move(col_name)) {}
CreateIndexNode(std::string idx_name, std::string tbl_name, std::string col_name) :
Node(NodeType::create_index), index_name(std::move(idx_name)), table_name(std::move(tbl_name)), column_name(std::move(col_name)) {}
void dump() const override {
std::cout << "type: CreateIndexNode, table_name: " << table_name << ", index_name: " << index_name << ", column_name: " << column_name << std::endl;
}
};
void dump() const override {
std::cout << "type: CreateIndexNode, table_name: " << table_name << ", index_name: " << index_name << ", column_name: " << column_name << std::endl;
}
};
class Parser {
private:
class Parser {
private:
public:
Parser();
public:
Parser();
std::unique_ptr<Node> parse(const std::string &code);
std::unique_ptr<Node> parse(const std::string &code);
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_set();
std::unique_ptr<Node> parse_show();
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_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::unique_ptr<Node> parse_create_index();
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::unique_ptr<Node> parse_create_index();
std::vector<ColOrderNode> parse_order_by_clause();
OffsetLimitNode parse_offset_limit_clause();
std::vector<ColOrderNode> parse_order_by_clause();
OffsetLimitNode parse_offset_limit_clause();
std::unique_ptr<Node> parse_where_clause();
std::unique_ptr<Node> parse_expression();
std::unique_ptr<Node> parse_expression(std::unique_ptr<Node> left);
std::unique_ptr<Node> parse_where_clause();
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();
ArithmeticalOperatorType parse_arithmetical_operator();
std::unique_ptr<Node> parse_value();
RelationalOperatorType parse_relational_operator();
LogicalOperatorType parse_logical_operator();
ArithmeticalOperatorType parse_arithmetical_operator();
private:
Lexer m_lexer;
};
private:
Lexer m_lexer;
};
} // namespace

20
row.cpp
View File

@@ -4,16 +4,16 @@
namespace usql {
int ColNullValue::compare(ColValue &other) {
int ColNullValue::compare(ColValue &other) const {
return other.isNull() ? 0 : -1; // null goes to end
}
int ColIntegerValue::compare(ColValue &other) {
long r = m_integer - other.getIntValue();
int ColIntegerValue::compare(ColValue &other) const {
long r = m_integer - other.getIntegerValue();
return other.isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1;
}
int ColDoubleValue::compare(ColValue &other) {
int ColDoubleValue::compare(ColValue &other) const {
if (other.isNull()) return 1; // null goes to end
double c = m_double - other.getDoubleValue();
@@ -25,16 +25,16 @@ ColStringValue & ColStringValue::operator=(ColStringValue other) {
return *this;
}
int ColStringValue::compare(ColValue &other) {
int ColStringValue::compare(ColValue &other) const {
return other.isNull() ? 1 : m_string->compare(other.getStringValue()); // null goes to end
}
int ColDateValue::compare(ColValue &other) {
long r = m_date - other.getIntValue();
int ColDateValue::compare(ColValue &other) const {
long r = m_date - other.getIntegerValue();
return other.isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1;
}
int ColBooleanValue::compare(ColValue &other) {
int ColBooleanValue::compare(ColValue &other) const {
if (other.isNull()) return 1; // null goes to end
return m_bool == other.getBoolValue() ? 0 : m_bool && !other.getBoolValue() ? -1 : 1; // true first
@@ -48,7 +48,7 @@ Row::Row(const Row &other) : m_columns(other.m_columns.size()), m_visible(other.
ColumnType col_type = other[i].getColType();
switch (col_type) {
case ColumnType::integer_type :
setIntColumnValue(i, other[i].getIntValue());
setIntColumnValue(i, other[i].getIntegerValue());
break;
case ColumnType::float_type :
setFloatColumnValue(i, other[i].getDoubleValue());
@@ -110,7 +110,7 @@ void Row::setBoolColumnValue(int col_index, const std::string &value) {
void Row::setColumnValue(ColDefNode *col_def, ColValue &col_value) {
if (!col_value.isNull()) {
if (col_def->type == ColumnType::integer_type)
setIntColumnValue(col_def->order, col_value.getIntValue());
setIntColumnValue(col_def->order, col_value.getIntegerValue());
else if (col_def->type == ColumnType::float_type)
setFloatColumnValue(col_def->order, col_value.getDoubleValue());
else if (col_def->type == ColumnType::varchar_type)

159
row.h
View File

@@ -9,135 +9,134 @@
namespace usql {
struct ColValue {
virtual bool isNull() { return false; };
virtual ColumnType getColType() = 0;
virtual long getIntValue() = 0;
virtual double getDoubleValue() = 0;
virtual std::string getStringValue() = 0;
virtual long getDateValue() = 0;
virtual bool getBoolValue() = 0;
struct ColValue {
virtual bool isNull() const { return false; };
virtual ColumnType getColType() const = 0;
virtual long getIntegerValue() const = 0;
virtual double getDoubleValue() const = 0;
virtual std::string getStringValue() const = 0;
virtual long getDateValue() const = 0;
virtual bool getBoolValue() const = 0;
virtual int compare(ColValue &other) = 0;
virtual int compare(ColValue &other) const = 0;
virtual ~ColValue() = default;
};
};
struct ColNullValue : ColValue {
bool isNull() override { return true; };
ColumnType getColType() override { throw Exception("getColType not supported on ColNullValue"); }
long getIntValue() override { throw Exception("getIntValue not supported on ColNullValue"); };
double getDoubleValue() override { throw Exception("getDoubleValue not supported on ColNullValue"); };
std::string getStringValue() override { return "null"; };
long getDateValue() override { throw Exception("getDateValue not supported on ColNullValue"); };
bool getBoolValue() override { throw Exception("getDateValue not supported on ColNullValue"); };
struct ColNullValue : ColValue {
bool isNull() const override { return true; };
ColumnType getColType() const override { throw Exception("getColType not supported on ColNullValue"); }
long getIntegerValue() const override { throw Exception("getIntegerValue not supported on ColNullValue"); };
double getDoubleValue() const override { throw Exception("getDoubleValue not supported on ColNullValue"); };
std::string getStringValue() const override { return "null"; };
long getDateValue() const override { throw Exception("getDateValue not supported on ColNullValue"); };
bool getBoolValue() const override { throw Exception("getDateValue not supported on ColNullValue"); };
int compare(ColValue &other) override;
int compare(ColValue &other) const override;
~ColNullValue() override = default;
};
};
struct ColIntegerValue : ColValue {
struct ColIntegerValue : ColValue {
explicit ColIntegerValue(long value) : m_integer(value) {};
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
ColumnType getColType() override { return ColumnType::integer_type; };
long getIntValue() override { return m_integer; };
double getDoubleValue() override { return (double) m_integer; };
std::string getStringValue() override { return std::to_string(m_integer); };
long getDateValue() override { return m_integer; };
bool getBoolValue() override { throw Exception("Not supported on ColIntegerValue"); };
ColumnType getColType() const override { return ColumnType::integer_type; };
long getIntegerValue() const override { return m_integer; };
double getDoubleValue() const override { return (double) m_integer; };
std::string getStringValue() const override { return std::to_string(m_integer); };
long getDateValue() const override { return m_integer; };
bool getBoolValue() const override { throw Exception("Not supported on ColIntegerValue"); };
int compare(ColValue &other) override;
long m_integer;
int compare(ColValue &other) const override;
~ColIntegerValue() override = default;
};
long m_integer;
};
struct ColDoubleValue : ColValue {
struct ColDoubleValue : ColValue {
explicit ColDoubleValue(double value) : m_double(value) {};
ColDoubleValue(const ColDoubleValue &other) : m_double(other.m_double) {}
ColumnType getColType() override { return ColumnType::float_type; };
long getIntValue() override { return (long) m_double; };
double getDoubleValue() override { return m_double; };
std::string getStringValue() override { return Settings::double_to_string(m_double); };
long getDateValue() override { return (long) m_double; };
bool getBoolValue() override { throw Exception("Not supported on ColDoubleValue"); };
ColumnType getColType() const override { return ColumnType::float_type; };
long getIntegerValue() const override { return (long) m_double; };
double getDoubleValue() const override { return m_double; };
std::string getStringValue() const override { return Settings::double_to_string(m_double); };
long getDateValue() const override { return (long) m_double; };
bool getBoolValue() const override { throw Exception("Not supported on ColDoubleValue"); };
int compare(ColValue &other) override;
int compare(ColValue &other) const override;
~ColDoubleValue() override = default;
double m_double;
};
};
struct ColStringValue : ColValue {
struct ColStringValue : ColValue {
explicit ColStringValue(const std::string &value) : m_string(std::make_unique<std::string>(value)) {};
ColStringValue(const ColStringValue &other) : m_string(std::make_unique<std::string>(*other.m_string)) {};
ColStringValue & operator=(ColStringValue other);
ColumnType getColType() override { return ColumnType::varchar_type; };
long getIntValue() override { return std::stoi(*m_string); };
double getDoubleValue() override { return std::stod(*m_string); };
std::string getStringValue() override { return *m_string; };
long getDateValue() override { return std::stoi(*m_string); };
bool getBoolValue() override { throw Exception("Not supported on ColStringValue"); };
ColumnType getColType() const override { return ColumnType::varchar_type; };
long getIntegerValue() const override { return std::stoi(*m_string); };
double getDoubleValue() const override { return std::stod(*m_string); };
std::string getStringValue() const override { return *m_string; };
long getDateValue() const override { return std::stoi(*m_string); };
bool getBoolValue() const override { throw Exception("Not supported on ColStringValue"); };
int compare(ColValue &other) override;
int compare(ColValue &other) const override;
std::unique_ptr<std::string> m_string;
};
};
struct ColDateValue : ColValue {
explicit ColDateValue(long value) : m_date(value) {};
ColDateValue(const ColDateValue &other) : m_date(other.m_date) {};
struct ColDateValue : ColValue {
explicit ColDateValue(long value) : m_date(value) {};
ColDateValue(const ColDateValue &other) : m_date(other.m_date) {};
ColumnType getColType() override { return ColumnType::date_type; };
long getIntValue() override { return m_date; };
double getDoubleValue() override { return (double) m_date; };
std::string getStringValue() override { return Settings::date_to_string(m_date); };
long getDateValue() override { return m_date; };
bool getBoolValue() override { throw Exception("Not supported on ColDateValue"); };
ColumnType getColType() const override { return ColumnType::date_type; };
long getIntegerValue() const override { return m_date; };
double getDoubleValue() const override { return (double) m_date; };
std::string getStringValue() const override { return Settings::date_to_string(m_date); };
long getDateValue() const override { return m_date; };
bool getBoolValue() const override { throw Exception("Not supported on ColDateValue"); };
int compare(ColValue &other) override;
int compare(ColValue &other) const override;
~ColDateValue() override = default;
long m_date; // seconds since epoch for now
};
long m_date; // seconds since epoch for now
};
struct ColBooleanValue : ColValue {
explicit ColBooleanValue(bool value) : m_bool(value) {};
ColBooleanValue(const ColBooleanValue &other) : m_bool(other.m_bool) {};
struct ColBooleanValue : ColValue {
explicit ColBooleanValue(bool value) : m_bool(value) {};
ColBooleanValue(const ColBooleanValue &other) : m_bool(other.m_bool) {};
ColumnType getColType() override { return ColumnType::bool_type; };
long getIntValue() override { return (long) m_bool; };
double getDoubleValue() override { return (double) m_bool; };
std::string getStringValue() override { return m_bool ? "Y" : "N"; };
long getDateValue() override { throw Exception("Not supported on ColBooleanValue"); };
bool getBoolValue() override { return m_bool; };
ColumnType getColType() const override { return ColumnType::bool_type; };
long getIntegerValue() const override { return (long) m_bool; };
double getDoubleValue() const override { return (double) m_bool; };
std::string getStringValue() const override { return m_bool ? "Y" : "N"; };
long getDateValue() const override { throw Exception("Not supported on ColBooleanValue"); };
bool getBoolValue() const override { return m_bool; };
int compare(ColValue &other) override;
int compare(ColValue &other) const override;
~ColBooleanValue() override = default;
bool m_bool;
};
bool m_bool;
};
class Row {
class Row {
public:
explicit Row(int cols_count, bool visible) : m_columns(cols_count), m_visible(visible) {};
public:
explicit Row(int cols_count, bool visible) : m_columns(cols_count), m_visible(visible) {};
Row(const Row &other);
Row &operator=(Row other);
@@ -154,7 +153,7 @@ namespace usql {
void setColumnValue(ColDefNode *col_def, ColValue &col_value);
void setColumnValue(ColDefNode *col_def, ValueNode *col_value);
ColValue &operator[](int i) const {
ColValue &operator[](int i) const {
auto type_index = m_columns[i].index();
switch (type_index) {
case 0:
@@ -183,9 +182,9 @@ namespace usql {
void set_visible() { m_visible = true; };
void set_deleted() { m_visible = true; };
private:
private:
bool m_visible;
std::vector<std::variant<ColNullValue, ColIntegerValue, ColDoubleValue, ColStringValue, ColDateValue, ColBooleanValue>> m_columns;
};
};
} // namespace

View File

@@ -217,7 +217,7 @@ void Table::commit_copy_of_row(Row &row) {
new_row.setColumnNull(i);
} else {
if (m_col_defs[i].type == ColumnType::integer_type) {
new_row.setIntColumnValue(i, row[i].getIntValue());
new_row.setIntColumnValue(i, row[i].getIntegerValue());
} else if (m_col_defs[i].type == ColumnType::float_type) {
new_row.setFloatColumnValue(i, row[i].getDoubleValue());
} else if (m_col_defs[i].type == ColumnType::varchar_type) {

View File

@@ -7,17 +7,12 @@
#include <utility>
#include <vector>
#include <iterator> // For std::forward_iterator_tag
#include <cstddef> // For std::ptrdiff_t
namespace usql {
//using IndexValue=std::variant<ColNullValue, ColIntegerValue, ColStringValue>;
using IndexValue=std::variant<long, std::string>;
struct Table {
Table(const Table &other);
Table(const std::string& name, const std::vector<ColDefNode>& columns);

View File

@@ -130,7 +130,7 @@ std::unique_ptr<ValueNode> USql::eval_database_value_node(Table *table, Row &row
return std::make_unique<NullValueNode>();
if (col_def.type == ColumnType::integer_type)
return std::make_unique<IntValueNode>(db_value.getIntValue());
return std::make_unique<IntValueNode>(db_value.getIntegerValue());
if (col_def.type == ColumnType::float_type)
return std::make_unique<DoubleValueNode>(db_value.getDoubleValue());
if (col_def.type == ColumnType::varchar_type)
@@ -138,7 +138,7 @@ std::unique_ptr<ValueNode> USql::eval_database_value_node(Table *table, Row &row
if (col_def.type == ColumnType::bool_type)
return std::make_unique<BooleanValueNode>(db_value.getBoolValue());
if (col_def.type == ColumnType::date_type)
return std::make_unique<IntValueNode>(db_value.getIntValue());
return std::make_unique<IntValueNode>(db_value.getIntegerValue());
throw Exception("unknown database value type");
}
@@ -197,7 +197,7 @@ USql::eval_function_value_node(Table *table, Row &row, Node *node, ColDefNode *c
std::unique_ptr<ValueNode> USql::count_function(ColValue *agg_func_value, const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
long c = 1;
if (!agg_func_value->isNull()) {
c = agg_func_value->getIntValue() + 1;
c = agg_func_value->getIntegerValue() + 1;
}
return std::make_unique<IntValueNode>(c);
}
@@ -359,10 +359,10 @@ USql::max_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars,
if (agg_func_value->isNull()) {
return std::make_unique<IntValueNode>(val);
} else {
return std::make_unique<IntValueNode>(std::max(val, agg_func_value->getIntValue()));
return std::make_unique<IntValueNode>(std::max(val, agg_func_value->getIntegerValue()));
}
} else {
return std::make_unique<IntValueNode>(agg_func_value->getIntValue());
return std::make_unique<IntValueNode>(agg_func_value->getIntegerValue());
}
} else if (col_def_node->type == ColumnType::float_type) {
if (!evaluatedPars[0]->isNull()) {
@@ -390,10 +390,10 @@ USql::min_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars,
if (agg_func_value->isNull()) {
return std::make_unique<IntValueNode>(val);
} else {
return std::make_unique<IntValueNode>(std::min(val, agg_func_value->getIntValue()));
return std::make_unique<IntValueNode>(std::min(val, agg_func_value->getIntegerValue()));
}
} else {
return std::make_unique<IntValueNode>(agg_func_value->getIntValue());
return std::make_unique<IntValueNode>(agg_func_value->getIntegerValue());
}
} else if (col_def_node->type == ColumnType::float_type) {
if (!evaluatedPars[0]->isNull()) {