usql/table.cpp

217 lines
6.1 KiB
C++

#include "table.h"
#include "csvreader.h"
#include "ml_string.h"
namespace usql {
Table::Table(const std::string& name, const std::vector<ColDefNode>& columns) {
m_name = name;
m_col_defs = columns;
m_rows.reserve(256);
}
Table::Table(const Table &other) {
m_name = other.m_name;
m_col_defs = other.m_col_defs;
m_rows.reserve(other.m_rows.size());
for(const Row& orig_row : other.m_rows) {
commit_copy_of_row(orig_row);
}
}
ColDefNode Table::get_column_def(const std::string &col_name) {
auto name_cmp = [col_name](const ColDefNode& cd) { return cd.name == col_name; };
auto col_def = std::find_if(begin(m_col_defs), end(m_col_defs), name_cmp);
if (col_def != std::end(m_col_defs)) {
return *col_def;
} else {
throw Exception("column not exists (" + col_name + ")");
}
}
ColDefNode Table::get_column_def(int col_index) {
if (col_index >= 0 && col_index < columns_count()) {
return m_col_defs[col_index];
} else {
throw Exception("column with this index does not exists (" + std::to_string(col_index) + ")");
}
}
Row& Table::create_empty_row() {
m_rows.emplace_back(columns_count());
return m_rows.back();
}
std::string Table::csv_string() {
// header
std::string out_string;
for(int i = 0; i < m_col_defs.size(); i++) {
if (i > 0) out_string += ",";
out_string += m_col_defs[i].name;
}
// rows
for (auto & m_row : m_rows) {
std::string csv_line{"\n"};
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
}
}
out_string += csv_line;
}
return out_string;
}
int Table::load_csv_string(const std::string &content) {
int row_cnt = 0;
CsvReader csvparser{};
auto csv = csvparser.parseCSV(content);
std::vector<ColDefNode> &colDefs = m_col_defs;
for (auto it = csv.begin() + 1; it != csv.end(); ++it) {
std::vector<std::string> &csv_line = *it;
// prepare empty new_row
Row& new_row = create_empty_row();
// copy values
for (size_t i = 0; i < columns_count(); i++) {
ColDefNode col_def = get_column_def(colDefs[i].name);
if (csv_line[i].empty()) {
new_row.setColumnNull(col_def.order);
} else if (col_def.type == ColumnType::integer_type) {
new_row.setIntColumnValue(col_def.order, string_to_long(csv_line[i]));
} else if (col_def.type == ColumnType::float_type) {
new_row.setFloatColumnValue(col_def.order, string_to_double(csv_line[i]));
} else if (col_def.type == ColumnType::varchar_type) {
new_row.setStringColumnValue(col_def.order, csv_line[i]);
} else if (col_def.type == ColumnType::date_type) {
new_row.setDateColumnValue(col_def.order, csv_line[i]);
} else if (col_def.type == ColumnType::bool_type) {
new_row.setBoolColumnValue(col_def.order, csv_line[i]);
} else
throw Exception("unsupported column type");
}
// append new_row
commit_row(new_row);
row_cnt++;
}
return row_cnt;
}
double Table::string_to_double(const std::string &s) {
try {
return std::stod(s);
} catch (std::invalid_argument &e) {
throw Exception("error parsing as double: " + s);
}
}
long Table::string_to_long(const std::string &s) {
try {
return std::stol(s);
} catch (std::invalid_argument &e) {
throw Exception("error parsing as integer: " + s);
}
}
void Table::print() {
std::string out{"| "};
std::string out2{"+-"};
std::vector<int> col_char_sizes{};
for(const auto& col_def : m_col_defs) {
int col_size = col_def.type == ColumnType::varchar_type ? col_def.length :
col_def.type == ColumnType::float_type ? 20 : 10;
col_char_sizes.push_back(col_size);
out.append(string_padd(col_def.name, col_size, ' ', true) + " | ");
out2.append(string_padd("-", col_size, '-', true) + "-+ ");
}
// std::cout << "** " << m_name << " **" << std::endl;
std::cout << out << std::endl;
std::cout << out2 << std::endl;
for(auto& row : m_rows) {
row.print(col_char_sizes);
}
std::cout << std::endl;
}
void Table::commit_row(const Row &row) {
try {
validate_row(row);
} catch (Exception &e) {
m_rows.erase(m_rows.end() - 1);
throw e;
}
}
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);
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());
} else if (m_col_defs[i].type == ColumnType::float_type) {
new_row.setFloatColumnValue(i, row.ith_column(i)->getDoubleValue());
} else if (m_col_defs[i].type == ColumnType::varchar_type) {
new_row.setStringColumnValue(i, row.ith_column(i)->getStringValue());
} else if (m_col_defs[i].type == ColumnType::date_type) {
new_row.setDateColumnValue(i, row.ith_column(i)->getDateValue());
} else if (m_col_defs[i].type == ColumnType::bool_type) {
new_row.setBoolColumnValue(i, row.ith_column(i)->getBoolValue());
} else
throw Exception("unsupported column type");
}
}
validate_row(new_row);
}
void Table::validate_column(const ColDefNode *col_def, ValueNode *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() + ")");
}
}
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() + ")");
}
}
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);
validate_column(&col_def, col_val);
}
}
} // namespace