Row::ith_column() method removed
This commit is contained in:
parent
869e5881df
commit
b37b0b55ff
48
row.cpp
48
row.cpp
|
|
@ -4,35 +4,35 @@
|
|||
|
||||
namespace usql {
|
||||
|
||||
int ColNullValue::compare(ColValue *other) {
|
||||
return other->isNull() ? 0 : -1; // null goes to end
|
||||
int ColNullValue::compare(ColValue &other) {
|
||||
return other.isNull() ? 0 : -1; // null goes to end
|
||||
}
|
||||
|
||||
int ColIntegerValue::compare(ColValue *other) {
|
||||
long r = m_integer - other->getIntValue();
|
||||
return other->isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1;
|
||||
int ColIntegerValue::compare(ColValue &other) {
|
||||
long r = m_integer - other.getIntValue();
|
||||
return other.isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
int ColDoubleValue::compare(ColValue *other) {
|
||||
if (other->isNull()) return 1; // null goes to end
|
||||
int ColDoubleValue::compare(ColValue &other) {
|
||||
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;
|
||||
}
|
||||
|
||||
int ColStringValue::compare(ColValue *other) {
|
||||
return other->isNull() ? 1 : m_string.compare(other->getStringValue()); // null goes to end
|
||||
int ColStringValue::compare(ColValue &other) {
|
||||
return other.isNull() ? 1 : m_string.compare(other.getStringValue()); // null goes to end
|
||||
}
|
||||
|
||||
int ColDateValue::compare(ColValue *other) {
|
||||
long r = m_date - other->getIntValue();
|
||||
return other->isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1;
|
||||
int ColDateValue::compare(ColValue &other) {
|
||||
long r = m_date - other.getIntValue();
|
||||
return other.isNull() ? 1 : r > 0 ? 1 : r == 0 ? 0 : -1;
|
||||
}
|
||||
|
||||
int ColBooleanValue::compare(ColValue *other) {
|
||||
if (other->isNull()) return 1; // null goes to end
|
||||
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
|
||||
return m_bool == other.getBoolValue() ? 0 : m_bool && !other.getBoolValue() ? -1 : 1; // true first
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
void Row::setColumnValue(ColDefNode *col_def, ColValue *col_value) {
|
||||
if (!col_value->isNull()) {
|
||||
void Row::setColumnValue(ColDefNode *col_def, ColValue &col_value) {
|
||||
if (!col_value.isNull()) {
|
||||
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)
|
||||
setFloatColumnValue(col_def->order, col_value->getDoubleValue());
|
||||
setFloatColumnValue(col_def->order, col_value.getDoubleValue());
|
||||
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)
|
||||
setDateColumnValue(col_def->order, col_value->getDateValue());
|
||||
setDateColumnValue(col_def->order, col_value.getDateValue());
|
||||
else if (col_def->type == ColumnType::bool_type)
|
||||
setBoolColumnValue(col_def->order, col_value->getBoolValue());
|
||||
setBoolColumnValue(col_def->order, col_value.getBoolValue());
|
||||
} else {
|
||||
setColumnNull(col_def->order);
|
||||
}
|
||||
|
|
@ -158,7 +158,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.ith_column(ci));
|
||||
int cmp = m_columns[ci]->compare(other[ci]);
|
||||
if (cmp != 0) return cmp;
|
||||
}
|
||||
return 0;
|
||||
|
|
|
|||
19
row.h
19
row.h
|
|
@ -17,7 +17,7 @@ namespace usql {
|
|||
virtual long getDateValue() = 0;
|
||||
virtual bool getBoolValue() = 0;
|
||||
|
||||
virtual int compare(ColValue * other) = 0;
|
||||
virtual int compare(ColValue &other) = 0;
|
||||
|
||||
virtual ~ColValue() = default;
|
||||
|
||||
|
|
@ -33,7 +33,7 @@ namespace usql {
|
|||
long getDateValue() 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; };
|
||||
bool getBoolValue() override { throw Exception("Not supported on ColIntegerValue"); };
|
||||
|
||||
int compare(ColValue * other) override;
|
||||
int compare(ColValue &other) override;
|
||||
|
||||
long m_integer;
|
||||
};
|
||||
|
|
@ -65,7 +65,7 @@ namespace usql {
|
|||
long getDateValue() override { return (long) m_double; };
|
||||
bool getBoolValue() override { throw Exception("Not supported on ColDoubleValue"); };
|
||||
|
||||
int compare(ColValue * other) override;
|
||||
int compare(ColValue &other) override;
|
||||
|
||||
double m_double;
|
||||
};
|
||||
|
|
@ -82,7 +82,7 @@ namespace usql {
|
|||
long getDateValue() override { return std::stoi(m_string); };
|
||||
bool getBoolValue() override { throw Exception("Not supported on ColStringValue"); };
|
||||
|
||||
int compare(ColValue * other) override;
|
||||
int compare(ColValue &other) override;
|
||||
|
||||
std::string m_string;
|
||||
};
|
||||
|
|
@ -98,7 +98,7 @@ namespace usql {
|
|||
long getDateValue() override { return m_date; };
|
||||
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
|
||||
};
|
||||
|
|
@ -114,7 +114,7 @@ namespace usql {
|
|||
long getDateValue() override { throw Exception("Not supported on ColBooleanValue"); };
|
||||
bool getBoolValue() override { return m_bool; };
|
||||
|
||||
int compare(ColValue * other) override;
|
||||
int compare(ColValue &other) override;
|
||||
|
||||
bool m_bool;
|
||||
};
|
||||
|
|
@ -136,12 +136,11 @@ namespace usql {
|
|||
void setDateColumnValue(int col_index, const std::string &value);
|
||||
void setBoolColumnValue(int col_index, bool 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);
|
||||
|
||||
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;
|
||||
|
||||
void print(const std::vector<ColDefNode> &col_defs);
|
||||
|
|
|
|||
30
table.cpp
30
table.cpp
|
|
@ -60,9 +60,9 @@ std::string Table::csv_string() {
|
|||
for(int i = 0; i < m_col_defs.size(); i++) {
|
||||
if (i > 0) csv_line += ",";
|
||||
|
||||
auto col = m_row.ith_column(i);
|
||||
if (!col->isNull()) {
|
||||
csv_line += col->getStringValue(); // TODO handle enclosing commas etc
|
||||
auto & col = m_row[i];
|
||||
if (!col.isNull()) {
|
||||
csv_line += col.getStringValue(); // TODO handle enclosing commas etc
|
||||
}
|
||||
}
|
||||
out_string += csv_line;
|
||||
|
|
@ -190,21 +190,21 @@ void Table::commit_copy_of_row(const Row &row) {
|
|||
Row& new_row = create_empty_row();
|
||||
|
||||
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);
|
||||
} else {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
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) {
|
||||
new_row.setBoolColumnValue(i, row.ith_column(i)->getBoolValue());
|
||||
new_row.setBoolColumnValue(i, row[i].getBoolValue());
|
||||
} else
|
||||
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) {
|
||||
if (!col_def->null && col_val->isNull()) {
|
||||
void Table::validate_column(const ColDefNode *col_def, ColValue &col_val) {
|
||||
if (!col_def->null && col_val.isNull()) {
|
||||
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) {
|
||||
throw Exception("Column value of " + col_def->name + " is too long (" + col_val->getStringValue() + ")");
|
||||
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() + ")");
|
||||
}
|
||||
}
|
||||
|
||||
void Table::validate_row(const Row &row) {
|
||||
for(int i = 0; i < m_col_defs.size(); 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);
|
||||
}
|
||||
|
|
|
|||
2
table.h
2
table.h
|
|
@ -23,7 +23,7 @@ namespace usql {
|
|||
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, ColValue *col_val);
|
||||
static void validate_column(const ColDefNode *col_def, ColValue &col_val);
|
||||
void validate_row(const Row &row);
|
||||
|
||||
std::string csv_string();
|
||||
|
|
|
|||
22
usql.cpp
22
usql.cpp
|
|
@ -205,7 +205,7 @@ std::unique_ptr<Table> USql::execute_select(SelectFromTableNode &node) {
|
|||
|
||||
new_row.setColumnValue(&result_tbl_col_defs[idx], col_value);
|
||||
} 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);
|
||||
}
|
||||
}
|
||||
|
|
@ -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) {
|
||||
// TODO validate index
|
||||
ColDefNode col_def = result->get_column_def(order_by_col_def.col_index - 1);
|
||||
ColValue *a_val = a.ith_column(col_def.order);
|
||||
ColValue *b_val = b.ith_column(col_def.order);
|
||||
ColValue &a_val = a[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;
|
||||
|
|
@ -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) {
|
||||
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
|
||||
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>();
|
||||
|
||||
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)
|
||||
return std::make_unique<DoubleValueNode>(db_value->getDoubleValue());
|
||||
return std::make_unique<DoubleValueNode>(db_value.getDoubleValue());
|
||||
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)
|
||||
return std::make_unique<BooleanValueNode>(db_value->getBoolValue());
|
||||
return std::make_unique<BooleanValueNode>(db_value.getBoolValue());
|
||||
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");
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue