using std::variant instead of pointer
This commit is contained in:
parent
f25b6cd4c8
commit
7685d0bb37
40
row.cpp
40
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<ColNullValue>());
|
||||
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<ColNullValue>());
|
||||
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<ColIntegerValue *>(other.m_columns[i].get())->getIntValue()));
|
||||
setIntColumnValue(i, other[i].getIntValue());
|
||||
break;
|
||||
case ColumnType::float_type :
|
||||
setFloatColumnValue(i, (static_cast<ColDoubleValue *>(other.m_columns[i].get())->getDoubleValue()));
|
||||
setFloatColumnValue(i, other[i].getDoubleValue());
|
||||
break;
|
||||
case ColumnType::varchar_type :
|
||||
setStringColumnValue(i, (static_cast<ColStringValue *>(other.m_columns[i].get())->getStringValue()));
|
||||
setStringColumnValue(i, other[i].getStringValue());
|
||||
break;
|
||||
case ColumnType::date_type :
|
||||
setDateColumnValue(i, (static_cast<ColDateValue *>(other.m_columns[i].get())->getDateValue()));
|
||||
setDateColumnValue(i, other[i].getDateValue());
|
||||
break;
|
||||
case ColumnType::bool_type :
|
||||
setBoolColumnValue(i, (static_cast<ColBooleanValue *>(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<ColNullValue>();
|
||||
m_columns[col_index] = ColNullValue();
|
||||
}
|
||||
|
||||
void Row::setIntColumnValue(int col_index, long value) {
|
||||
m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
|
||||
m_columns[col_index] = ColIntegerValue(value);
|
||||
}
|
||||
|
||||
void Row::setFloatColumnValue(int col_index, double value) {
|
||||
m_columns[col_index] = std::make_unique<ColDoubleValue>(value);
|
||||
m_columns[col_index] = ColDoubleValue(value);
|
||||
}
|
||||
|
||||
void Row::setStringColumnValue(int col_index, const std::string &value) {
|
||||
m_columns[col_index] = std::make_unique<ColStringValue>(value);
|
||||
m_columns[col_index] = ColStringValue(value);
|
||||
}
|
||||
|
||||
void Row::setDateColumnValue(int col_index, long value) {
|
||||
m_columns[col_index] = std::make_unique<ColDateValue>(value);
|
||||
m_columns[col_index] = ColDateValue(value);
|
||||
}
|
||||
|
||||
void Row::setDateColumnValue(int col_index, const std::string &value) {
|
||||
m_columns[col_index] = std::make_unique<ColDateValue>(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<ColBooleanValue>(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<ColBooleanValue>(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<ColDefNode> &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;
|
||||
|
|
|
|||
25
row.h
25
row.h
|
|
@ -4,6 +4,7 @@
|
|||
#include "parser.h"
|
||||
#include "settings.h"
|
||||
|
||||
#include <variant>
|
||||
#include <vector>
|
||||
|
||||
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<ColNullValue>(&m_columns[i]);
|
||||
case 1:
|
||||
return (ColValue &) *std::get_if<ColIntegerValue>(&m_columns[i]);
|
||||
case 2:
|
||||
return (ColValue &) *std::get_if<ColDoubleValue>(&m_columns[i]);
|
||||
case 3:
|
||||
return (ColValue &) *std::get_if<ColStringValue>(&m_columns[i]);
|
||||
case 4:
|
||||
return (ColValue &) *std::get_if<ColDateValue>(&m_columns[i]);
|
||||
case 5:
|
||||
return (ColValue &) *std::get_if<ColBooleanValue>(&m_columns[i]);
|
||||
}
|
||||
throw Exception("should not happen");
|
||||
}
|
||||
|
||||
int compare(const Row &other) const;
|
||||
|
||||
void print(const std::vector<ColDefNode> &col_defs);
|
||||
static int print_get_column_size(const ColDefNode &col_def);
|
||||
private:
|
||||
std::vector<std::unique_ptr<ColValue>> m_columns;
|
||||
// xx std::vector<std::unique_ptr<ColValue>> m_columns;
|
||||
std::vector<std::variant<ColNullValue, ColIntegerValue, ColDoubleValue, ColStringValue, ColDateValue, ColBooleanValue>> m_columns;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
Loading…
Reference in New Issue