From 7685d0bb3761688dd8fb400f52d6b8021498e7a8 Mon Sep 17 00:00:00 2001 From: VaclavT Date: Tue, 17 Aug 2021 14:34:28 +0200 Subject: [PATCH] using std::variant instead of pointer --- row.cpp | 40 ++++++++++++++++++++-------------------- row.h | 25 +++++++++++++++++++++++-- 2 files changed, 43 insertions(+), 22 deletions(-) diff --git a/row.cpp b/row.cpp index bb3666a..f453255 100644 --- a/row.cpp +++ b/row.cpp @@ -38,7 +38,7 @@ int ColBooleanValue::compare(ColValue &other) { Row::Row(int cols_count) { m_columns.reserve(cols_count); for (int i = 0; i < cols_count; i++) { - m_columns.emplace_back(std::make_unique()); + m_columns.emplace_back(ColNullValue()); } } @@ -46,29 +46,29 @@ Row::Row(const Row &other) { m_columns.reserve(other.m_columns.size()); // PERF here we first set cols null and then immediately replace it for (int i = 0; i < other.m_columns.size(); i++) { - m_columns.emplace_back(std::make_unique()); + m_columns.emplace_back(ColNullValue()); } for (int i = 0; i < other.m_columns.size(); i++) { - if (other.m_columns[i]->isNull()) + if (other[i].isNull()) continue; // for null NOP - ColumnType col_type = other.m_columns[i]->getColType(); + ColumnType col_type = other[i].getColType(); switch (col_type) { case ColumnType::integer_type : - setIntColumnValue(i, (static_cast(other.m_columns[i].get())->getIntValue())); + setIntColumnValue(i, other[i].getIntValue()); break; case ColumnType::float_type : - setFloatColumnValue(i, (static_cast(other.m_columns[i].get())->getDoubleValue())); + setFloatColumnValue(i, other[i].getDoubleValue()); break; case ColumnType::varchar_type : - setStringColumnValue(i, (static_cast(other.m_columns[i].get())->getStringValue())); + setStringColumnValue(i, other[i].getStringValue()); break; case ColumnType::date_type : - setDateColumnValue(i, (static_cast(other.m_columns[i].get())->getDateValue())); + setDateColumnValue(i, other[i].getDateValue()); break; case ColumnType::bool_type : - setBoolColumnValue(i, (static_cast(other.m_columns[i].get())->getBoolValue())); + setBoolColumnValue(i, other[i].getBoolValue()); break; default: throw Exception("unsupported column type"); @@ -83,36 +83,36 @@ Row &Row::operator=(Row other) { } void Row::setColumnNull(int col_index) { - m_columns[col_index] = std::make_unique(); + m_columns[col_index] = ColNullValue(); } void Row::setIntColumnValue(int col_index, long value) { - m_columns[col_index] = std::make_unique(value); + m_columns[col_index] = ColIntegerValue(value); } void Row::setFloatColumnValue(int col_index, double value) { - m_columns[col_index] = std::make_unique(value); + m_columns[col_index] = ColDoubleValue(value); } void Row::setStringColumnValue(int col_index, const std::string &value) { - m_columns[col_index] = std::make_unique(value); + m_columns[col_index] = ColStringValue(value); } void Row::setDateColumnValue(int col_index, long value) { - m_columns[col_index] = std::make_unique(value); + m_columns[col_index] = ColDateValue(value); } void Row::setDateColumnValue(int col_index, const std::string &value) { - m_columns[col_index] = std::make_unique(Settings::string_to_date(value)); + m_columns[col_index] = ColDateValue(Settings::string_to_date(value)); } void Row::setBoolColumnValue(int col_index, bool value) { - m_columns[col_index] = std::make_unique(value); + m_columns[col_index] = ColBooleanValue(value); } void Row::setBoolColumnValue(int col_index, const std::string &value) { bool v = (value == "Y" || value == "1"); - m_columns[col_index] = std::make_unique(v); + m_columns[col_index] = ColBooleanValue(v); } void Row::setColumnValue(ColDefNode *col_def, ColValue &col_value) { @@ -153,7 +153,7 @@ void Row::setColumnValue(ColDefNode *col_def, ValueNode *col_value) { int Row::compare(const Row &other) const { for (int ci = 0; ci < m_columns.size(); ci++) { - int cmp = m_columns[ci]->compare(other[ci]); + int cmp = this->operator[](ci).compare(other[ci]); if (cmp != 0) return cmp; } return 0; @@ -167,9 +167,9 @@ void Row::print(const std::vector &col_defs) { int col_size = print_get_column_size(col_def); if (col_def.type==ColumnType::integer_type || col_def.type==ColumnType::float_type || col_def.type==ColumnType::bool_type) - out.append(string_padd(m_columns[ci]->getStringValue(), col_size, ' ', false) + " | "); + out.append(string_padd(this->operator[](ci).getStringValue(), col_size, ' ', false) + " | "); else - out.append(string_padd(m_columns[ci]->getStringValue(), col_size, ' ', true) + " | "); + out.append(string_padd(this->operator[](ci).getStringValue(), col_size, ' ', true) + " | "); } std::cout << out << std::endl; diff --git a/row.h b/row.h index 68a0fbb..0109303 100644 --- a/row.h +++ b/row.h @@ -4,6 +4,7 @@ #include "parser.h" #include "settings.h" +#include #include namespace usql { @@ -119,6 +120,8 @@ namespace usql { bool m_bool; }; + + class Row { public: @@ -139,14 +142,32 @@ namespace usql { void setColumnValue(ColDefNode *col_def, ColValue &col_value); void setColumnValue(ColDefNode *col_def, ValueNode *col_value); - ColValue &operator[](int i) const { return *m_columns[i]; } + ColValue &operator[](int i) const { + auto type_index = m_columns[i].index(); + switch (type_index) { + case 0: + return (ColValue &) *std::get_if(&m_columns[i]); + case 1: + return (ColValue &) *std::get_if(&m_columns[i]); + case 2: + return (ColValue &) *std::get_if(&m_columns[i]); + case 3: + return (ColValue &) *std::get_if(&m_columns[i]); + case 4: + return (ColValue &) *std::get_if(&m_columns[i]); + case 5: + return (ColValue &) *std::get_if(&m_columns[i]); + } + throw Exception("should not happen"); + } int compare(const Row &other) const; void print(const std::vector &col_defs); static int print_get_column_size(const ColDefNode &col_def); private: - std::vector> m_columns; + // xx std::vector> m_columns; + std::vector> m_columns; }; } // namespace \ No newline at end of file