Row::ith_column() method removed

This commit is contained in:
VaclavT 2021-08-15 11:52:00 +02:00
parent 869e5881df
commit b37b0b55ff
5 changed files with 60 additions and 61 deletions

48
row.cpp
View File

@ -4,35 +4,35 @@
namespace usql { namespace usql {
int ColNullValue::compare(ColValue *other) { int ColNullValue::compare(ColValue &other) {
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) {
long r = m_integer - other->getIntValue(); long r = m_integer - other.getIntValue();
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) {
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();
return c < 0 ? -1 : c == 0.0 ? 0 : 1; return c < 0 ? -1 : c == 0.0 ? 0 : 1;
} }
int ColStringValue::compare(ColValue *other) { int ColStringValue::compare(ColValue &other) {
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) {
long r = m_date - other->getIntValue(); long r = m_date - other.getIntValue();
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) {
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
} }
Row::Row(int cols_count) { Row::Row(int cols_count) {
@ -120,18 +120,18 @@ void Row::setBoolColumnValue(int col_index, const std::string &value) {
m_columns[col_index] = std::make_unique<ColBooleanValue>(v); m_columns[col_index] = std::make_unique<ColBooleanValue>(v);
} }
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.getIntValue());
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)
setStringColumnValue(col_def->order, col_value->getStringValue()); setStringColumnValue(col_def->order, col_value.getStringValue());
else if (col_def->type == ColumnType::date_type) else if (col_def->type == ColumnType::date_type)
setDateColumnValue(col_def->order, col_value->getDateValue()); setDateColumnValue(col_def->order, col_value.getDateValue());
else if (col_def->type == ColumnType::bool_type) else if (col_def->type == ColumnType::bool_type)
setBoolColumnValue(col_def->order, col_value->getBoolValue()); setBoolColumnValue(col_def->order, col_value.getBoolValue());
} else { } else {
setColumnNull(col_def->order); setColumnNull(col_def->order);
} }
@ -158,7 +158,7 @@ void Row::setColumnValue(ColDefNode *col_def, ValueNode *col_value) {
int Row::compare(const Row &other) const { int Row::compare(const Row &other) const {
for (int ci = 0; ci < m_columns.size(); ci++) { for (int ci = 0; ci < m_columns.size(); ci++) {
int cmp = m_columns[ci]->compare(other.ith_column(ci)); int cmp = m_columns[ci]->compare(other[ci]);
if (cmp != 0) return cmp; if (cmp != 0) return cmp;
} }
return 0; return 0;

19
row.h
View File

@ -17,7 +17,7 @@ namespace usql {
virtual long getDateValue() = 0; virtual long getDateValue() = 0;
virtual bool getBoolValue() = 0; virtual bool getBoolValue() = 0;
virtual int compare(ColValue * other) = 0; virtual int compare(ColValue &other) = 0;
virtual ~ColValue() = default; virtual ~ColValue() = default;
@ -33,7 +33,7 @@ namespace usql {
long getDateValue() override { throw Exception("getDateValue not supported on ColNullValue"); }; long getDateValue() override { throw Exception("getDateValue not supported on ColNullValue"); };
bool getBoolValue() override { throw Exception("getDateValue not supported on ColNullValue"); }; bool getBoolValue() override { throw Exception("getDateValue not supported on ColNullValue"); };
int compare(ColValue * other) override; int compare(ColValue &other) override;
}; };
@ -48,7 +48,7 @@ namespace usql {
long getDateValue() override { return m_integer; }; long getDateValue() override { return m_integer; };
bool getBoolValue() override { throw Exception("Not supported on ColIntegerValue"); }; bool getBoolValue() override { throw Exception("Not supported on ColIntegerValue"); };
int compare(ColValue * other) override; int compare(ColValue &other) override;
long m_integer; long m_integer;
}; };
@ -65,7 +65,7 @@ namespace usql {
long getDateValue() override { return (long) m_double; }; long getDateValue() override { return (long) m_double; };
bool getBoolValue() override { throw Exception("Not supported on ColDoubleValue"); }; bool getBoolValue() override { throw Exception("Not supported on ColDoubleValue"); };
int compare(ColValue * other) override; int compare(ColValue &other) override;
double m_double; double m_double;
}; };
@ -82,7 +82,7 @@ namespace usql {
long getDateValue() override { return std::stoi(m_string); }; long getDateValue() override { return std::stoi(m_string); };
bool getBoolValue() override { throw Exception("Not supported on ColStringValue"); }; bool getBoolValue() override { throw Exception("Not supported on ColStringValue"); };
int compare(ColValue * other) override; int compare(ColValue &other) override;
std::string m_string; std::string m_string;
}; };
@ -98,7 +98,7 @@ namespace usql {
long getDateValue() override { return m_date; }; long getDateValue() override { return m_date; };
bool getBoolValue() override { throw Exception("Not supported on ColDateValue"); }; bool getBoolValue() override { throw Exception("Not supported on ColDateValue"); };
int compare(ColValue * other) override; int compare(ColValue &other) override;
long m_date; // seconds since epoch for now long m_date; // seconds since epoch for now
}; };
@ -114,7 +114,7 @@ namespace usql {
long getDateValue() override { throw Exception("Not supported on ColBooleanValue"); }; long getDateValue() override { throw Exception("Not supported on ColBooleanValue"); };
bool getBoolValue() override { return m_bool; }; bool getBoolValue() override { return m_bool; };
int compare(ColValue * other) override; int compare(ColValue &other) override;
bool m_bool; bool m_bool;
}; };
@ -136,12 +136,11 @@ namespace usql {
void setDateColumnValue(int col_index, const std::string &value); void setDateColumnValue(int col_index, const std::string &value);
void setBoolColumnValue(int col_index, bool value); void setBoolColumnValue(int col_index, bool value);
void setBoolColumnValue(int col_index, const std::string &value); void setBoolColumnValue(int col_index, const std::string &value);
void setColumnValue(ColDefNode *col_def, ColValue *col_value); void setColumnValue(ColDefNode *col_def, ColValue &col_value);
void setColumnValue(ColDefNode *col_def, ValueNode *col_value); void setColumnValue(ColDefNode *col_def, ValueNode *col_value);
ColValue &operator[](int i) { return *m_columns[i]; } ColValue &operator[](int i) const { return *m_columns[i]; }
ColValue * ith_column(int i) const { return m_columns[i].get(); }
int compare(const Row &other) const; int compare(const Row &other) const;
void print(const std::vector<ColDefNode> &col_defs); void print(const std::vector<ColDefNode> &col_defs);

View File

@ -60,9 +60,9 @@ std::string Table::csv_string() {
for(int i = 0; i < m_col_defs.size(); i++) { for(int i = 0; i < m_col_defs.size(); i++) {
if (i > 0) csv_line += ","; if (i > 0) csv_line += ",";
auto col = m_row.ith_column(i); auto & col = m_row[i];
if (!col->isNull()) { if (!col.isNull()) {
csv_line += col->getStringValue(); // TODO handle enclosing commas etc csv_line += col.getStringValue(); // TODO handle enclosing commas etc
} }
} }
out_string += csv_line; out_string += csv_line;
@ -190,21 +190,21 @@ void Table::commit_copy_of_row(const Row &row) {
Row& new_row = create_empty_row(); Row& new_row = create_empty_row();
for(int i = 0; i < m_col_defs.size(); i++) { for(int i = 0; i < m_col_defs.size(); i++) {
ColValue *ct = row.ith_column(i); ColValue &ct = row[i];
if (ct->isNull()) { if (ct.isNull()) {
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.ith_column(i)->getIntValue()); new_row.setIntColumnValue(i, row[i].getIntValue());
} 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.ith_column(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) {
new_row.setStringColumnValue(i, row.ith_column(i)->getStringValue()); new_row.setStringColumnValue(i, row[i].getStringValue());
} else if (m_col_defs[i].type == ColumnType::date_type) { } else if (m_col_defs[i].type == ColumnType::date_type) {
new_row.setDateColumnValue(i, row.ith_column(i)->getDateValue()); new_row.setDateColumnValue(i, row[i].getDateValue());
} else if (m_col_defs[i].type == ColumnType::bool_type) { } else if (m_col_defs[i].type == ColumnType::bool_type) {
new_row.setBoolColumnValue(i, row.ith_column(i)->getBoolValue()); new_row.setBoolColumnValue(i, row[i].getBoolValue());
} else } else
throw Exception("unsupported column type"); throw Exception("unsupported column type");
} }
@ -222,19 +222,19 @@ void Table::validate_column(const ColDefNode *col_def, ValueNode *col_val) {
} }
} }
void Table::validate_column(const ColDefNode *col_def, ColValue *col_val) { void Table::validate_column(const ColDefNode *col_def, ColValue &col_val) {
if (!col_def->null && col_val->isNull()) { if (!col_def->null && col_val.isNull()) {
throw Exception("Column " + col_def->name + " cannot be null"); throw Exception("Column " + col_def->name + " cannot be null");
} }
if (col_def->type == ColumnType::varchar_type && !col_val->isNull() && col_val->getStringValue().size() > col_def->length) { if (col_def->type == ColumnType::varchar_type && !col_val.isNull() && col_val.getStringValue().size() > col_def->length) {
throw Exception("Column value of " + col_def->name + " is too long (" + col_val->getStringValue() + ")"); throw Exception("Column value of " + col_def->name + " is too long (" + col_val.getStringValue() + ")");
} }
} }
void Table::validate_row(const Row &row) { void Table::validate_row(const Row &row) {
for(int i = 0; i < m_col_defs.size(); i++) { for(int i = 0; i < m_col_defs.size(); i++) {
ColDefNode col_def = m_col_defs[i]; ColDefNode col_def = m_col_defs[i];
ColValue *col_val = row.ith_column(i); ColValue &col_val = row[i];
validate_column(&col_def, col_val); validate_column(&col_def, col_val);
} }

View File

@ -23,7 +23,7 @@ namespace usql {
void commit_copy_of_row(const Row &row); void commit_copy_of_row(const Row &row);
static void validate_column(const ColDefNode *col_def, ValueNode *col_val); static void validate_column(const ColDefNode *col_def, ValueNode *col_val);
static void validate_column(const ColDefNode *col_def, ColValue *col_val); static void validate_column(const ColDefNode *col_def, ColValue &col_val);
void validate_row(const Row &row); void validate_row(const Row &row);
std::string csv_string(); std::string csv_string();

View File

@ -205,7 +205,7 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
new_row.setColumnValue(&result_tbl_col_defs[idx], col_value); new_row.setColumnValue(&result_tbl_col_defs[idx], col_value);
} else { } else {
ColValue *col_value = row->ith_column(row_col_index); ColValue &col_value = row->operator[](row_col_index);
new_row.setColumnValue(&result_tbl_col_defs[idx], col_value); new_row.setColumnValue(&result_tbl_col_defs[idx], col_value);
} }
} }
@ -240,10 +240,10 @@ void USql::execute_order_by(SelectFromTableNode &node, Table *table, Table *resu
for(const auto& order_by_col_def : node.order_by) { for(const auto& order_by_col_def : node.order_by) {
// TODO validate index // TODO validate index
ColDefNode col_def = result->get_column_def(order_by_col_def.col_index - 1); ColDefNode col_def = result->get_column_def(order_by_col_def.col_index - 1);
ColValue *a_val = a.ith_column(col_def.order); ColValue &a_val = a[col_def.order];
ColValue *b_val = b.ith_column(col_def.order); ColValue &b_val = b[col_def.order];
int compare = a_val->compare(b_val); int compare = a_val.compare(b_val);
if (compare < 0) return order_by_col_def.ascending; if (compare < 0) return order_by_col_def.ascending;
if (compare > 0) return !order_by_col_def.ascending; if (compare > 0) return !order_by_col_def.ascending;
@ -474,21 +474,21 @@ std::unique_ptr<ValueNode> USql::eval_value_node(Table *table, Row &row, Node *n
std::unique_ptr<ValueNode> USql::eval_database_value_node(Table *table, Row &row, Node *node) { std::unique_ptr<ValueNode> USql::eval_database_value_node(Table *table, Row &row, Node *node) {
auto *dvl = static_cast<DatabaseValueNode *>(node); auto *dvl = static_cast<DatabaseValueNode *>(node);
ColDefNode col_def = table->get_column_def( dvl->col_name); // TODO optimize it to just get this def once ColDefNode col_def = table->get_column_def( dvl->col_name); // TODO optimize it to just get this def once
auto db_value = row.ith_column(col_def.order); ColValue &db_value = row[col_def.order];
if (db_value->isNull()) if (db_value.isNull())
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.getIntValue());
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)
return std::make_unique<StringValueNode>(db_value->getStringValue()); return std::make_unique<StringValueNode>(db_value.getStringValue());
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.getIntValue());
throw Exception("unknown database value type"); throw Exception("unknown database value type");
} }