date and boolean data types added
This commit is contained in:
121
row.cpp
121
row.cpp
@@ -8,13 +8,12 @@ namespace usql {
|
||||
}
|
||||
|
||||
int ColIntegerValue::compare(ColValue * other) {
|
||||
return other->isNull() ? 1 : m_integer - other->getIntValue(); // null goes to end
|
||||
return other->isNull() ? 1 : m_integer - other->getIntValue(); // TODO implicit conversion from long to int
|
||||
}
|
||||
|
||||
int ColDoubleValue::compare(ColValue * other) {
|
||||
if (other->isNull()) { // null goes to end
|
||||
return 1;
|
||||
}
|
||||
if (other->isNull()) return 1; // null goes to end
|
||||
|
||||
double c = m_double - other->getDoubleValue();
|
||||
return c < 0 ? -1 : c == 0.0 ? 0 : 1;
|
||||
}
|
||||
@@ -23,6 +22,16 @@ namespace usql {
|
||||
return other->isNull() ? 1 : m_string.compare(other->getStringValue()); // null goes to end
|
||||
}
|
||||
|
||||
int ColDateValue::compare(ColValue * other) {
|
||||
return other->isNull() ? 1 : m_date - other->getIntValue(); // TODO implicit conversion from long to int
|
||||
}
|
||||
|
||||
int ColBooleanValue::compare(ColValue * other) {
|
||||
if (other->isNull()) return 1; // null goes to end
|
||||
|
||||
return m_bool==other->getBoolValue() ? 0 : m_bool && !other->getBoolValue() ? -1 : 1; // true first
|
||||
}
|
||||
|
||||
Row::Row(int cols_count) {
|
||||
m_columns.reserve(cols_count);
|
||||
for (int i = 0; i < cols_count; i++) {
|
||||
@@ -32,20 +41,27 @@ namespace usql {
|
||||
|
||||
Row::Row(const Row &other) {
|
||||
m_columns.reserve(other.m_columns.size());
|
||||
// TODO fixme this is nonsense
|
||||
// TODO fixme, here first set cols null and then immediately replace it
|
||||
for (int i = 0; i < other.m_columns.size(); i++) {
|
||||
m_columns.push_back(std::make_unique<ColNullValue>());
|
||||
}
|
||||
|
||||
// TODO get rid of dynamic_cast
|
||||
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());
|
||||
if (auto *other_v = dynamic_cast<ColIntegerValue *>(other.m_columns[i].get())) {
|
||||
setIntColumnValue(i, other_v->getIntValue());
|
||||
} else if (auto *other_v = dynamic_cast<ColDoubleValue *>(other.m_columns[i].get())) {
|
||||
setFloatColumnValue(i, other_v->getDoubleValue());
|
||||
} else if (auto *other_v = dynamic_cast<ColStringValue *>(other.m_columns[i].get())) {
|
||||
setStringColumnValue(i, other_v->getStringValue());
|
||||
} else if (auto *other_v = dynamic_cast<ColDateValue *>(other.m_columns[i].get())) {
|
||||
setDateColumnValue(i, other_v->getDateValue());
|
||||
} else if (auto *other_v = dynamic_cast<ColBooleanValue *>(other.m_columns[i].get())) {
|
||||
setBoolColumnValue(i, other_v->getBoolValue());
|
||||
} else if (auto *other_v = dynamic_cast<ColNullValue *>(other.m_columns[i].get())) {
|
||||
// NOP
|
||||
} else {
|
||||
throw Exception("unsupported data type");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -59,43 +75,70 @@ namespace usql {
|
||||
m_columns[col_index] = std::make_unique<ColNullValue>();
|
||||
}
|
||||
|
||||
void Row::setColumnValue(int col_index, long value) {
|
||||
void Row::setIntColumnValue(int col_index, long value) {
|
||||
m_columns[col_index] = std::make_unique<ColIntegerValue>(value);
|
||||
}
|
||||
|
||||
void Row::setColumnValue(int col_index, double value) {
|
||||
void Row::setFloatColumnValue(int col_index, double value) {
|
||||
m_columns[col_index] = std::make_unique<ColDoubleValue>(value);
|
||||
}
|
||||
|
||||
void Row::setColumnValue(int col_index, const std::string &value) {
|
||||
void Row::setStringColumnValue(int col_index, const std::string &value) {
|
||||
m_columns[col_index] = std::make_unique<ColStringValue>(value);
|
||||
};
|
||||
}
|
||||
|
||||
void Row::setDateColumnValue(int col_index, long value) {
|
||||
m_columns[col_index] = std::make_unique<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));
|
||||
}
|
||||
|
||||
void Row::setBoolColumnValue(int col_index, bool value) {
|
||||
m_columns[col_index] = std::make_unique<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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
if (!col_value->isNull()) {
|
||||
if (col_def->type == ColumnType::integer_type)
|
||||
setIntColumnValue(col_def->order, col_value->getIntValue());
|
||||
else if (col_def->type == ColumnType::float_type)
|
||||
setFloatColumnValue(col_def->order, col_value->getDoubleValue());
|
||||
else if (col_def->type == ColumnType::varchar_type)
|
||||
setStringColumnValue(col_def->order, col_value->getStringValue());
|
||||
else if (col_def->type == ColumnType::date_type)
|
||||
setDateColumnValue(col_def->order, col_value->getDateValue());
|
||||
else if (col_def->type == ColumnType::bool_type)
|
||||
setBoolColumnValue(col_def->order, col_value->getBoolValue());
|
||||
} 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::setColumnValue(ColDefNode *col_def, ValueNode *col_value) {
|
||||
if (!col_value->isNull()) {
|
||||
if (col_def->type == ColumnType::integer_type)
|
||||
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)
|
||||
setStringColumnValue(col_def->order, col_value->getStringValue());
|
||||
else if (col_def->type == ColumnType::date_type)
|
||||
setIntColumnValue(col_def->order, col_value->getDateValue());
|
||||
else if (col_def->type == ColumnType::bool_type)
|
||||
setBoolColumnValue(col_def->order, col_value->getBooleanValue());
|
||||
else
|
||||
throw Exception("unsupported data type");
|
||||
} else {
|
||||
setColumnNull(col_def->order);
|
||||
}
|
||||
}
|
||||
|
||||
int Row::compare(const Row & other) const {
|
||||
for (int ci = 0; ci < m_columns.size(); ci++) {
|
||||
|
||||
Reference in New Issue
Block a user