usql/row.cpp

113 lines
3.6 KiB
C++

#include "row.h"
namespace usql {
int ColNullValue::compare(ColValue * other) {
return other->isNull() ? 0 : -1; // null goes to end
}
int ColIntegerValue::compare(ColValue * other) {
return other->isNull() ? 1 : m_integer - other->getIntValue(); // null goes to end
}
int ColDoubleValue::compare(ColValue * other) {
if (other->isNull()) { // null goes to end
return 1;
}
double c = m_double - other->getDoubleValue();
return c < 0 ? -1 : c == 0.0 ? 0 : 1;
}
int ColStringValue::compare(ColValue * other) {
return other->isNull() ? 1 : m_string.compare(other->getStringValue()); // null goes to end
}
Row::Row(int cols_count) {
m_columns.reserve(cols_count);
for (int i = 0; i < cols_count; i++) {
m_columns.push_back(std::make_unique<ColNullValue>());
}
}
Row::Row(const Row &other) {
m_columns.reserve(other.m_columns.size());
// TODO fixme this is nonsense
for (int i = 0; i < other.m_columns.size(); i++) {
m_columns.push_back(std::make_unique<ColNullValue>());
}
for (int i = 0; i < other.m_columns.size(); i++) {
if (ColIntegerValue *other_v = dynamic_cast<ColIntegerValue *>(other.m_columns[i].get())) {
setColumnValue(i, other_v->getIntValue());
}
if (ColDoubleValue *other_v = dynamic_cast<ColDoubleValue *>(other.m_columns[i].get())) {
setColumnValue(i, other_v->getDoubleValue());
}
if (ColStringValue *other_v = dynamic_cast<ColStringValue *>(other.m_columns[i].get())) {
setColumnValue(i, other_v->getStringValue());
}
}
}
Row &Row::operator=(Row other) {
std::swap(m_columns, other.m_columns);
return *this;
}
void Row::setColumnNull(int col_index) {
m_columns[col_index] = std::make_unique<ColNullValue>();
}
void Row::setColumnValue(int col_index, long value) {
m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
}
void Row::setColumnValue(int col_index, double value) {
m_columns[col_index] = std::make_unique<ColDoubleValue>(value);
}
void Row::setColumnValue(int col_index, const std::string &value) {
m_columns[col_index] = std::make_unique<ColStringValue>(value);
};
void Row::setColumnValue(ColDefNode *col_def, ColValue *col_value) {
if (!col_value->isNull()) {
if (col_def->type == ColumnType::integer_type)
setColumnValue(col_def->order, col_value->getIntValue());
else if (col_def->type == ColumnType::float_type)
setColumnValue(col_def->order, col_value->getDoubleValue());
else if (col_def->type == ColumnType::varchar_type)
setColumnValue(col_def->order, col_value->getStringValue());
} else {
setColumnNull(col_def->order);
}
}
void Row::setColumnValue(ColDefNode *col_def, ValueNode *col_value) {
if (!col_value->isNull()) {
if (col_def->type == ColumnType::integer_type)
setColumnValue(col_def->order, col_value->getIntValue());
else if (col_def->type == ColumnType::float_type)
setColumnValue(col_def->order, col_value->getDoubleValue());
else if (col_def->type == ColumnType::varchar_type)
setColumnValue(col_def->order, col_value->getStringValue());
} else {
setColumnNull(col_def->order);
}
}
void Row::print(const std::vector<int> & col_char_sizes) {
std::string out{"| "};
for (int ci = 0; ci < m_columns.size(); ci++) {
auto value = m_columns[ci]->getStringValue();
// TODO use string functions
out.append(value + std::string(col_char_sizes[ci] - value.size(), ' ') + " | ");
}
std::cout << out << std::endl;
}
}