some methods marked const
This commit is contained in:
2
.gitignore
vendored
2
.gitignore
vendored
@@ -5,3 +5,5 @@ tmp/.idea
|
|||||||
Makefile
|
Makefile
|
||||||
localhost.session.sql
|
localhost.session.sql
|
||||||
CMakeFiles
|
CMakeFiles
|
||||||
|
cmake-build-debug-coverage
|
||||||
|
.idea
|
||||||
|
|||||||
30
index.h
30
index.h
@@ -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));
|
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);
|
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);
|
return remove(to_index_value(key), rowid);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -53,13 +53,9 @@ public:
|
|||||||
return m_index_name;
|
return m_index_name;
|
||||||
}
|
}
|
||||||
|
|
||||||
[[nodiscard]] ColumnType get_data_type() const {
|
|
||||||
return m_data_type;
|
|
||||||
}
|
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
IndexValue to_index_value(const ValueNode *key) {
|
||||||
IndexValue to_index_value(ValueNode *key) {
|
|
||||||
if (m_data_type == ColumnType::integer_type)
|
if (m_data_type == ColumnType::integer_type)
|
||||||
return key->getIntegerValue();
|
return key->getIntegerValue();
|
||||||
else if (m_data_type == ColumnType::varchar_type)
|
else if (m_data_type == ColumnType::varchar_type)
|
||||||
@@ -68,16 +64,16 @@ private:
|
|||||||
throw Exception("using index on unsupported type");
|
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)
|
if (m_data_type == ColumnType::integer_type)
|
||||||
return key->getIntValue();
|
return key->getIntegerValue();
|
||||||
else if (m_data_type == ColumnType::varchar_type)
|
else if (m_data_type == ColumnType::varchar_type)
|
||||||
return key->getStringValue();
|
return key->getStringValue();
|
||||||
else
|
else
|
||||||
throw Exception("using index on unsupported type");
|
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);
|
auto search = m_index.find(key);
|
||||||
if (search != m_index.end()) {
|
if (search != m_index.end()) {
|
||||||
if (m_uniq)
|
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);
|
auto search = m_index.find(key);
|
||||||
if (search != m_index.end()) {
|
if (search != m_index.end()) {
|
||||||
search->second.erase(find(search->second.begin(), search->second.end(), rowid));
|
search->second.erase(find(search->second.begin(), search->second.end(), rowid));
|
||||||
@@ -111,16 +107,6 @@ 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:
|
private:
|
||||||
bool m_uniq;
|
bool m_uniq;
|
||||||
std::string m_index_name;
|
std::string m_index_name;
|
||||||
|
|||||||
65
parser.h
65
parser.h
@@ -142,12 +142,12 @@ namespace usql {
|
|||||||
struct ValueNode : Node {
|
struct ValueNode : Node {
|
||||||
explicit ValueNode(NodeType type) : Node(type) {}
|
explicit ValueNode(NodeType type) : Node(type) {}
|
||||||
|
|
||||||
virtual bool isNull() { return false; }
|
virtual bool isNull() const { return false; }
|
||||||
virtual long getIntegerValue() = 0;
|
virtual long getIntegerValue() const = 0;
|
||||||
virtual double getDoubleValue() = 0;
|
virtual double getDoubleValue() const = 0;
|
||||||
virtual std::string getStringValue() = 0;
|
virtual std::string getStringValue() const = 0;
|
||||||
virtual long getDateValue() = 0;
|
virtual long getDateValue() const = 0;
|
||||||
virtual bool getBooleanValue() = 0;
|
virtual bool getBooleanValue() const = 0;
|
||||||
|
|
||||||
~ValueNode() override = default;
|
~ValueNode() override = default;
|
||||||
};
|
};
|
||||||
@@ -156,13 +156,13 @@ namespace usql {
|
|||||||
|
|
||||||
NullValueNode() : ValueNode(NodeType::null_value) {}
|
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"); };
|
long getIntegerValue() const override { throw Exception("getIntegerValue not supported on NullValueNode"); };
|
||||||
double getDoubleValue() override { throw Exception("getDoubleValue not supported on NullValueNode"); };
|
double getDoubleValue() const override { throw Exception("getDoubleValue not supported on NullValueNode"); };
|
||||||
std::string getStringValue() override { throw Exception("getStringValue not supported on NullValueNode"); };
|
std::string getStringValue() const override { throw Exception("getStringValue not supported on NullValueNode"); };
|
||||||
long getDateValue() override { throw Exception("getDateValue not supported on NullValueNode"); };
|
long getDateValue() const override { throw Exception("getDateValue not supported on NullValueNode"); };
|
||||||
bool getBooleanValue() override { throw Exception("getBooleanValue not supported on NullValueNode"); };
|
bool getBooleanValue() const override { throw Exception("getBooleanValue not supported on NullValueNode"); };
|
||||||
|
|
||||||
void dump() const override {
|
void dump() const override {
|
||||||
std::cout << "type: NullValueNode," << std::endl;
|
std::cout << "type: NullValueNode," << std::endl;
|
||||||
@@ -174,11 +174,11 @@ namespace usql {
|
|||||||
|
|
||||||
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; };
|
long getIntegerValue() const override { return value; };
|
||||||
double getDoubleValue() override { return (double) value; };
|
double getDoubleValue() const override { return (double) value; };
|
||||||
std::string getStringValue() override { return Settings::int_to_string(value); }
|
std::string getStringValue() const override { return Settings::int_to_string(value); }
|
||||||
long getDateValue() override { return value; };
|
long getDateValue() const override { return value; };
|
||||||
bool getBooleanValue() override { return value != 0; };
|
bool getBooleanValue() const override { return value != 0; };
|
||||||
|
|
||||||
void dump() const override {
|
void dump() const override {
|
||||||
std::cout << "type: IntValueNode, value: " << value << std::endl;
|
std::cout << "type: IntValueNode, value: " << value << std::endl;
|
||||||
@@ -190,11 +190,11 @@ namespace usql {
|
|||||||
|
|
||||||
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; };
|
long getIntegerValue() const override { return (long) value; };
|
||||||
double getDoubleValue() override { return value; };
|
double getDoubleValue() const override { return value; };
|
||||||
std::string getStringValue() override { return Settings::double_to_string(value); }
|
std::string getStringValue() const override { return Settings::double_to_string(value); }
|
||||||
long getDateValue() override { return (long) value; };
|
long getDateValue() const override { return (long) value; };
|
||||||
bool getBooleanValue() override { return value != 0.0; };
|
bool getBooleanValue() const override { return value != 0.0; };
|
||||||
|
|
||||||
void dump() const override {
|
void dump() const override {
|
||||||
std::cout << "type: DoubleValueNode, value: " << value << std::endl;
|
std::cout << "type: DoubleValueNode, value: " << value << std::endl;
|
||||||
@@ -206,11 +206,11 @@ namespace usql {
|
|||||||
|
|
||||||
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); };
|
long getIntegerValue() const override { return Settings::string_to_int(value); };
|
||||||
double getDoubleValue() override { return Settings::string_to_double(value); };
|
double getDoubleValue() const override { return Settings::string_to_double(value); };
|
||||||
std::string getStringValue() override { return value; };
|
std::string getStringValue() const override { return value; };
|
||||||
long getDateValue() override { return Settings::string_to_date(value); };
|
long getDateValue() const override { return Settings::string_to_date(value); };
|
||||||
bool getBooleanValue() override { return Settings::string_to_bool(value); };
|
bool getBooleanValue() const override { return Settings::string_to_bool(value); };
|
||||||
|
|
||||||
void dump() const override {
|
void dump() const override {
|
||||||
std::cout << "type: StringValueNode, value: " << value << std::endl;
|
std::cout << "type: StringValueNode, value: " << value << std::endl;
|
||||||
@@ -222,11 +222,11 @@ namespace usql {
|
|||||||
|
|
||||||
explicit BooleanValueNode(bool value) : ValueNode(NodeType::bool_value), value(value) {}
|
explicit BooleanValueNode(bool value) : ValueNode(NodeType::bool_value), value(value) {}
|
||||||
|
|
||||||
long getIntegerValue() override { return (long) value; };
|
long getIntegerValue() const override { return (long) value; };
|
||||||
double getDoubleValue() override { return (double) value; };
|
double getDoubleValue() const override { return (double) value; };
|
||||||
std::string getStringValue() override { return Settings::bool_to_string(value); }
|
std::string getStringValue() const override { return Settings::bool_to_string(value); }
|
||||||
long getDateValue() override { return (long) value; };
|
long getDateValue() const override { return (long) value; };
|
||||||
bool getBooleanValue() override { return value; };
|
bool getBooleanValue() const override { return value; };
|
||||||
|
|
||||||
void dump() const override {
|
void dump() const override {
|
||||||
std::cout << "type: BooleanValueNode, value: " << value << std::endl;
|
std::cout << "type: BooleanValueNode, value: " << value << std::endl;
|
||||||
@@ -473,7 +473,6 @@ namespace usql {
|
|||||||
|
|
||||||
class Parser {
|
class Parser {
|
||||||
private:
|
private:
|
||||||
|
|
||||||
public:
|
public:
|
||||||
Parser();
|
Parser();
|
||||||
|
|
||||||
|
|||||||
20
row.cpp
20
row.cpp
@@ -4,16 +4,16 @@
|
|||||||
|
|
||||||
namespace usql {
|
namespace usql {
|
||||||
|
|
||||||
int ColNullValue::compare(ColValue &other) {
|
int ColNullValue::compare(ColValue &other) const {
|
||||||
return other.isNull() ? 0 : -1; // null goes to end
|
return other.isNull() ? 0 : -1; // null goes to end
|
||||||
}
|
}
|
||||||
|
|
||||||
int ColIntegerValue::compare(ColValue &other) {
|
int ColIntegerValue::compare(ColValue &other) const {
|
||||||
long r = m_integer - other.getIntValue();
|
long r = m_integer - other.getIntegerValue();
|
||||||
return other.isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1;
|
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
|
if (other.isNull()) return 1; // null goes to end
|
||||||
|
|
||||||
double c = m_double - other.getDoubleValue();
|
double c = m_double - other.getDoubleValue();
|
||||||
@@ -25,16 +25,16 @@ ColStringValue & ColStringValue::operator=(ColStringValue other) {
|
|||||||
return *this;
|
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
|
return other.isNull() ? 1 : m_string->compare(other.getStringValue()); // null goes to end
|
||||||
}
|
}
|
||||||
|
|
||||||
int ColDateValue::compare(ColValue &other) {
|
int ColDateValue::compare(ColValue &other) const {
|
||||||
long r = m_date - other.getIntValue();
|
long r = m_date - other.getIntegerValue();
|
||||||
return other.isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1;
|
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
|
if (other.isNull()) return 1; // null goes to end
|
||||||
|
|
||||||
return m_bool == other.getBoolValue() ? 0 : m_bool && !other.getBoolValue() ? -1 : 1; // true first
|
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();
|
ColumnType col_type = other[i].getColType();
|
||||||
switch (col_type) {
|
switch (col_type) {
|
||||||
case ColumnType::integer_type :
|
case ColumnType::integer_type :
|
||||||
setIntColumnValue(i, other[i].getIntValue());
|
setIntColumnValue(i, other[i].getIntegerValue());
|
||||||
break;
|
break;
|
||||||
case ColumnType::float_type :
|
case ColumnType::float_type :
|
||||||
setFloatColumnValue(i, other[i].getDoubleValue());
|
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) {
|
void Row::setColumnValue(ColDefNode *col_def, ColValue &col_value) {
|
||||||
if (!col_value.isNull()) {
|
if (!col_value.isNull()) {
|
||||||
if (col_def->type == ColumnType::integer_type)
|
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)
|
else if (col_def->type == ColumnType::float_type)
|
||||||
setFloatColumnValue(col_def->order, col_value.getDoubleValue());
|
setFloatColumnValue(col_def->order, col_value.getDoubleValue());
|
||||||
else if (col_def->type == ColumnType::varchar_type)
|
else if (col_def->type == ColumnType::varchar_type)
|
||||||
|
|||||||
141
row.h
141
row.h
@@ -9,134 +9,133 @@
|
|||||||
|
|
||||||
namespace usql {
|
namespace usql {
|
||||||
|
|
||||||
struct ColValue {
|
struct ColValue {
|
||||||
virtual bool isNull() { return false; };
|
virtual bool isNull() const { return false; };
|
||||||
virtual ColumnType getColType() = 0;
|
virtual ColumnType getColType() const = 0;
|
||||||
virtual long getIntValue() = 0;
|
virtual long getIntegerValue() const = 0;
|
||||||
virtual double getDoubleValue() = 0;
|
virtual double getDoubleValue() const = 0;
|
||||||
virtual std::string getStringValue() = 0;
|
virtual std::string getStringValue() const = 0;
|
||||||
virtual long getDateValue() = 0;
|
virtual long getDateValue() const = 0;
|
||||||
virtual bool getBoolValue() = 0;
|
virtual bool getBoolValue() const = 0;
|
||||||
|
|
||||||
virtual int compare(ColValue &other) = 0;
|
virtual int compare(ColValue &other) const = 0;
|
||||||
|
|
||||||
virtual ~ColValue() = default;
|
virtual ~ColValue() = default;
|
||||||
|
};
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
struct ColNullValue : ColValue {
|
struct ColNullValue : ColValue {
|
||||||
bool isNull() override { return true; };
|
bool isNull() const override { return true; };
|
||||||
ColumnType getColType() override { throw Exception("getColType not supported on ColNullValue"); }
|
ColumnType getColType() const override { throw Exception("getColType not supported on ColNullValue"); }
|
||||||
long getIntValue() override { throw Exception("getIntValue not supported on ColNullValue"); };
|
long getIntegerValue() const override { throw Exception("getIntegerValue not supported on ColNullValue"); };
|
||||||
double getDoubleValue() override { throw Exception("getDoubleValue not supported on ColNullValue"); };
|
double getDoubleValue() const override { throw Exception("getDoubleValue not supported on ColNullValue"); };
|
||||||
std::string getStringValue() override { return "null"; };
|
std::string getStringValue() const override { return "null"; };
|
||||||
long getDateValue() override { throw Exception("getDateValue not supported on ColNullValue"); };
|
long getDateValue() const override { throw Exception("getDateValue not supported on ColNullValue"); };
|
||||||
bool getBoolValue() 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;
|
~ColNullValue() override = default;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct ColIntegerValue : ColValue {
|
struct ColIntegerValue : ColValue {
|
||||||
explicit ColIntegerValue(long value) : m_integer(value) {};
|
explicit ColIntegerValue(long value) : m_integer(value) {};
|
||||||
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
|
ColIntegerValue(const ColIntegerValue &other) : m_integer(other.m_integer) {};
|
||||||
|
|
||||||
ColumnType getColType() override { return ColumnType::integer_type; };
|
ColumnType getColType() const override { return ColumnType::integer_type; };
|
||||||
long getIntValue() override { return m_integer; };
|
long getIntegerValue() const override { return m_integer; };
|
||||||
double getDoubleValue() override { return (double) m_integer; };
|
double getDoubleValue() const override { return (double) m_integer; };
|
||||||
std::string getStringValue() override { return std::to_string(m_integer); };
|
std::string getStringValue() const override { return std::to_string(m_integer); };
|
||||||
long getDateValue() override { return m_integer; };
|
long getDateValue() const override { return m_integer; };
|
||||||
bool getBoolValue() override { throw Exception("Not supported on ColIntegerValue"); };
|
bool getBoolValue() const override { throw Exception("Not supported on ColIntegerValue"); };
|
||||||
|
|
||||||
int compare(ColValue &other) override;
|
int compare(ColValue &other) const override;
|
||||||
|
|
||||||
long m_integer;
|
|
||||||
|
|
||||||
~ColIntegerValue() override = default;
|
~ColIntegerValue() override = default;
|
||||||
};
|
|
||||||
|
long m_integer;
|
||||||
|
};
|
||||||
|
|
||||||
|
|
||||||
struct ColDoubleValue : ColValue {
|
struct ColDoubleValue : ColValue {
|
||||||
explicit ColDoubleValue(double value) : m_double(value) {};
|
explicit ColDoubleValue(double value) : m_double(value) {};
|
||||||
ColDoubleValue(const ColDoubleValue &other) : m_double(other.m_double) {}
|
ColDoubleValue(const ColDoubleValue &other) : m_double(other.m_double) {}
|
||||||
|
|
||||||
ColumnType getColType() override { return ColumnType::float_type; };
|
ColumnType getColType() const override { return ColumnType::float_type; };
|
||||||
long getIntValue() override { return (long) m_double; };
|
long getIntegerValue() const override { return (long) m_double; };
|
||||||
double getDoubleValue() override { return m_double; };
|
double getDoubleValue() const override { return m_double; };
|
||||||
std::string getStringValue() override { return Settings::double_to_string(m_double); };
|
std::string getStringValue() const override { return Settings::double_to_string(m_double); };
|
||||||
long getDateValue() override { return (long) m_double; };
|
long getDateValue() const override { return (long) m_double; };
|
||||||
bool getBoolValue() override { throw Exception("Not supported on ColDoubleValue"); };
|
bool getBoolValue() const override { throw Exception("Not supported on ColDoubleValue"); };
|
||||||
|
|
||||||
int compare(ColValue &other) override;
|
int compare(ColValue &other) const override;
|
||||||
|
|
||||||
~ColDoubleValue() override = default;
|
~ColDoubleValue() override = default;
|
||||||
|
|
||||||
double m_double;
|
double m_double;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
struct ColStringValue : ColValue {
|
struct ColStringValue : ColValue {
|
||||||
explicit ColStringValue(const std::string &value) : m_string(std::make_unique<std::string>(value)) {};
|
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(const ColStringValue &other) : m_string(std::make_unique<std::string>(*other.m_string)) {};
|
||||||
|
|
||||||
ColStringValue & operator=(ColStringValue other);
|
ColStringValue & operator=(ColStringValue other);
|
||||||
|
|
||||||
ColumnType getColType() override { return ColumnType::varchar_type; };
|
ColumnType getColType() const override { return ColumnType::varchar_type; };
|
||||||
long getIntValue() override { return std::stoi(*m_string); };
|
long getIntegerValue() const override { return std::stoi(*m_string); };
|
||||||
double getDoubleValue() override { return std::stod(*m_string); };
|
double getDoubleValue() const override { return std::stod(*m_string); };
|
||||||
std::string getStringValue() override { return *m_string; };
|
std::string getStringValue() const override { return *m_string; };
|
||||||
long getDateValue() override { return std::stoi(*m_string); };
|
long getDateValue() const override { return std::stoi(*m_string); };
|
||||||
bool getBoolValue() override { throw Exception("Not supported on ColStringValue"); };
|
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;
|
std::unique_ptr<std::string> m_string;
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ColDateValue : ColValue {
|
struct ColDateValue : ColValue {
|
||||||
explicit ColDateValue(long value) : m_date(value) {};
|
explicit ColDateValue(long value) : m_date(value) {};
|
||||||
ColDateValue(const ColDateValue &other) : m_date(other.m_date) {};
|
ColDateValue(const ColDateValue &other) : m_date(other.m_date) {};
|
||||||
|
|
||||||
ColumnType getColType() override { return ColumnType::date_type; };
|
ColumnType getColType() const override { return ColumnType::date_type; };
|
||||||
long getIntValue() override { return m_date; };
|
long getIntegerValue() const override { return m_date; };
|
||||||
double getDoubleValue() override { return (double) m_date; };
|
double getDoubleValue() const override { return (double) m_date; };
|
||||||
std::string getStringValue() override { return Settings::date_to_string(m_date); };
|
std::string getStringValue() const override { return Settings::date_to_string(m_date); };
|
||||||
long getDateValue() override { return m_date; };
|
long getDateValue() const override { return m_date; };
|
||||||
bool getBoolValue() override { throw Exception("Not supported on ColDateValue"); };
|
bool getBoolValue() const override { throw Exception("Not supported on ColDateValue"); };
|
||||||
|
|
||||||
int compare(ColValue &other) override;
|
int compare(ColValue &other) const override;
|
||||||
|
|
||||||
~ColDateValue() override = default;
|
~ColDateValue() override = default;
|
||||||
|
|
||||||
long m_date; // seconds since epoch for now
|
long m_date; // seconds since epoch for now
|
||||||
};
|
};
|
||||||
|
|
||||||
struct ColBooleanValue : ColValue {
|
struct ColBooleanValue : ColValue {
|
||||||
explicit ColBooleanValue(bool value) : m_bool(value) {};
|
explicit ColBooleanValue(bool value) : m_bool(value) {};
|
||||||
ColBooleanValue(const ColBooleanValue &other) : m_bool(other.m_bool) {};
|
ColBooleanValue(const ColBooleanValue &other) : m_bool(other.m_bool) {};
|
||||||
|
|
||||||
ColumnType getColType() override { return ColumnType::bool_type; };
|
ColumnType getColType() const override { return ColumnType::bool_type; };
|
||||||
long getIntValue() override { return (long) m_bool; };
|
long getIntegerValue() const override { return (long) m_bool; };
|
||||||
double getDoubleValue() override { return (double) m_bool; };
|
double getDoubleValue() const override { return (double) m_bool; };
|
||||||
std::string getStringValue() override { return m_bool ? "Y" : "N"; };
|
std::string getStringValue() const override { return m_bool ? "Y" : "N"; };
|
||||||
long getDateValue() override { throw Exception("Not supported on ColBooleanValue"); };
|
long getDateValue() const override { throw Exception("Not supported on ColBooleanValue"); };
|
||||||
bool getBoolValue() override { return m_bool; };
|
bool getBoolValue() const override { return m_bool; };
|
||||||
|
|
||||||
int compare(ColValue &other) override;
|
int compare(ColValue &other) const override;
|
||||||
|
|
||||||
~ColBooleanValue() override = default;
|
~ColBooleanValue() override = default;
|
||||||
|
|
||||||
bool m_bool;
|
bool m_bool;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
class Row {
|
class Row {
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit Row(int cols_count, bool visible) : m_columns(cols_count), m_visible(visible) {};
|
explicit Row(int cols_count, bool visible) : m_columns(cols_count), m_visible(visible) {};
|
||||||
Row(const Row &other);
|
Row(const Row &other);
|
||||||
|
|
||||||
@@ -186,6 +185,6 @@ namespace usql {
|
|||||||
private:
|
private:
|
||||||
bool m_visible;
|
bool m_visible;
|
||||||
std::vector<std::variant<ColNullValue, ColIntegerValue, ColDoubleValue, ColStringValue, ColDateValue, ColBooleanValue>> m_columns;
|
std::vector<std::variant<ColNullValue, ColIntegerValue, ColDoubleValue, ColStringValue, ColDateValue, ColBooleanValue>> m_columns;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
@@ -217,7 +217,7 @@ void Table::commit_copy_of_row(Row &row) {
|
|||||||
new_row.setColumnNull(i);
|
new_row.setColumnNull(i);
|
||||||
} else {
|
} else {
|
||||||
if (m_col_defs[i].type == ColumnType::integer_type) {
|
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) {
|
} else if (m_col_defs[i].type == ColumnType::float_type) {
|
||||||
new_row.setFloatColumnValue(i, row[i].getDoubleValue());
|
new_row.setFloatColumnValue(i, row[i].getDoubleValue());
|
||||||
} else if (m_col_defs[i].type == ColumnType::varchar_type) {
|
} else if (m_col_defs[i].type == ColumnType::varchar_type) {
|
||||||
|
|||||||
5
table.h
5
table.h
@@ -7,17 +7,12 @@
|
|||||||
#include <utility>
|
#include <utility>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include <iterator> // For std::forward_iterator_tag
|
|
||||||
#include <cstddef> // For std::ptrdiff_t
|
|
||||||
|
|
||||||
|
|
||||||
namespace usql {
|
namespace usql {
|
||||||
|
|
||||||
//using IndexValue=std::variant<ColNullValue, ColIntegerValue, ColStringValue>;
|
|
||||||
using IndexValue=std::variant<long, std::string>;
|
using IndexValue=std::variant<long, std::string>;
|
||||||
|
|
||||||
struct Table {
|
struct Table {
|
||||||
|
|
||||||
Table(const Table &other);
|
Table(const Table &other);
|
||||||
Table(const std::string& name, const std::vector<ColDefNode>& columns);
|
Table(const std::string& name, const std::vector<ColDefNode>& columns);
|
||||||
|
|
||||||
|
|||||||
14
usql.cpp
14
usql.cpp
@@ -130,7 +130,7 @@ std::unique_ptr<ValueNode> USql::eval_database_value_node(Table *table, Row &row
|
|||||||
return std::make_unique<NullValueNode>();
|
return std::make_unique<NullValueNode>();
|
||||||
|
|
||||||
if (col_def.type == ColumnType::integer_type)
|
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)
|
if (col_def.type == ColumnType::float_type)
|
||||||
return std::make_unique<DoubleValueNode>(db_value.getDoubleValue());
|
return std::make_unique<DoubleValueNode>(db_value.getDoubleValue());
|
||||||
if (col_def.type == ColumnType::varchar_type)
|
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)
|
if (col_def.type == ColumnType::bool_type)
|
||||||
return std::make_unique<BooleanValueNode>(db_value.getBoolValue());
|
return std::make_unique<BooleanValueNode>(db_value.getBoolValue());
|
||||||
if (col_def.type == ColumnType::date_type)
|
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");
|
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) {
|
std::unique_ptr<ValueNode> USql::count_function(ColValue *agg_func_value, const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars) {
|
||||||
long c = 1;
|
long c = 1;
|
||||||
if (!agg_func_value->isNull()) {
|
if (!agg_func_value->isNull()) {
|
||||||
c = agg_func_value->getIntValue() + 1;
|
c = agg_func_value->getIntegerValue() + 1;
|
||||||
}
|
}
|
||||||
return std::make_unique<IntValueNode>(c);
|
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()) {
|
if (agg_func_value->isNull()) {
|
||||||
return std::make_unique<IntValueNode>(val);
|
return std::make_unique<IntValueNode>(val);
|
||||||
} else {
|
} 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 {
|
} 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) {
|
} else if (col_def_node->type == ColumnType::float_type) {
|
||||||
if (!evaluatedPars[0]->isNull()) {
|
if (!evaluatedPars[0]->isNull()) {
|
||||||
@@ -390,10 +390,10 @@ USql::min_function(const std::vector<std::unique_ptr<ValueNode>> &evaluatedPars,
|
|||||||
if (agg_func_value->isNull()) {
|
if (agg_func_value->isNull()) {
|
||||||
return std::make_unique<IntValueNode>(val);
|
return std::make_unique<IntValueNode>(val);
|
||||||
} else {
|
} 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 {
|
} 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) {
|
} else if (col_def_node->type == ColumnType::float_type) {
|
||||||
if (!evaluatedPars[0]->isNull()) {
|
if (!evaluatedPars[0]->isNull()) {
|
||||||
|
|||||||
Reference in New Issue
Block a user