usql update
This commit is contained in:
157
usql/table.cpp
157
usql/table.cpp
@@ -44,81 +44,12 @@ ColDefNode Table::get_column_def(int col_index) {
|
||||
}
|
||||
|
||||
Row& Table::create_empty_row() {
|
||||
std::unique_lock guard(m_insert_guard);
|
||||
|
||||
m_rows.emplace_back(columns_count(), false);
|
||||
return m_rows.back();
|
||||
}
|
||||
|
||||
std::string Table::csv_string() {
|
||||
const size_t k_row_size_est = m_col_defs.size() * 16;
|
||||
|
||||
std::string out_string;
|
||||
out_string.reserve(m_rows.size() * k_row_size_est);
|
||||
|
||||
// header
|
||||
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 & row : m_rows) {
|
||||
if (row.is_visible()) {
|
||||
std::string csv_line{"\n"};
|
||||
csv_line.reserve(k_row_size_est);
|
||||
|
||||
for (int i = 0; i < m_col_defs.size(); i++) {
|
||||
if (i > 0) csv_line += ',';
|
||||
|
||||
auto &col = row[i];
|
||||
if (!col.isNull()) {
|
||||
csv_line += col.getCsvStringValue();
|
||||
}
|
||||
}
|
||||
out_string += csv_line;
|
||||
}
|
||||
}
|
||||
|
||||
return out_string;
|
||||
}
|
||||
|
||||
int Table::load_csv_string(const std::string &content) {
|
||||
std::vector<ColDefNode> &colDefs = m_col_defs;
|
||||
|
||||
CsvReader csvparser{};
|
||||
int row_cnt = csvparser.parseCSV2(content, colDefs, *this);
|
||||
|
||||
return row_cnt;
|
||||
}
|
||||
|
||||
int Table::load_csv_file(const std::string &filename) {
|
||||
std::vector<ColDefNode> &colDefs = m_col_defs;
|
||||
|
||||
// allocate enough space
|
||||
int line_size = 128;
|
||||
|
||||
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
|
||||
auto file_size = in.tellg();
|
||||
|
||||
std::ifstream infile(filename);
|
||||
if (infile.good()) {
|
||||
std::string sLine;
|
||||
std::getline(infile, sLine);
|
||||
line_size = (int)sLine.size() + 1;
|
||||
}
|
||||
infile.close();
|
||||
|
||||
if (file_size > 0) {
|
||||
auto new_size = m_rows.size() + int((file_size / line_size) * 1.20);
|
||||
m_rows.reserve(new_size);
|
||||
}
|
||||
|
||||
// load rows
|
||||
CsvReader csvparser{};
|
||||
int row_cnt = csvparser.parseCSV(filename, colDefs, *this);
|
||||
|
||||
return row_cnt;
|
||||
}
|
||||
|
||||
void Table::create_row_from_vector(const std::vector<ColDefNode> &colDefs, const std::vector<std::string> &csv_line) {
|
||||
// prepare empty new_row
|
||||
Row& new_row = create_empty_row();
|
||||
@@ -147,6 +78,77 @@ void Table::create_row_from_vector(const std::vector<ColDefNode> &colDefs, const
|
||||
commit_row(new_row);
|
||||
}
|
||||
|
||||
std::string Table::csv_string() {
|
||||
const size_t k_row_size_est = m_col_defs.size() * 16;
|
||||
|
||||
std::string out_string;
|
||||
out_string.reserve(m_rows.size() * k_row_size_est);
|
||||
|
||||
// header
|
||||
for(size_t i = 0; i < m_col_defs.size(); i++) {
|
||||
if (i > 0) out_string += ',';
|
||||
out_string += m_col_defs[i].name;
|
||||
}
|
||||
|
||||
// rows
|
||||
for (auto & row : m_rows) {
|
||||
if (row.is_visible()) {
|
||||
std::string csv_line{"\n"};
|
||||
csv_line.reserve(k_row_size_est);
|
||||
|
||||
for (size_t i = 0; i < m_col_defs.size(); i++) {
|
||||
if (i > 0) csv_line += ',';
|
||||
|
||||
auto &col = row[i];
|
||||
if (!col.isNull()) {
|
||||
csv_line += col.getCsvStringValue();
|
||||
}
|
||||
}
|
||||
out_string += csv_line;
|
||||
}
|
||||
}
|
||||
|
||||
return out_string;
|
||||
}
|
||||
|
||||
size_t Table::load_csv_string(const std::string &content) {
|
||||
std::vector<ColDefNode> &colDefs = m_col_defs;
|
||||
|
||||
CsvReader csvparser{};
|
||||
auto row_cnt = csvparser.parseCSVString(content, colDefs, *this);
|
||||
|
||||
return row_cnt;
|
||||
}
|
||||
|
||||
size_t Table::load_csv_file(const std::string &filename) {
|
||||
std::vector<ColDefNode> &colDefs = m_col_defs;
|
||||
|
||||
// allocate enough space
|
||||
int line_size = 256;
|
||||
|
||||
std::ifstream in(filename, std::ifstream::ate | std::ifstream::binary);
|
||||
auto file_size = in.tellg();
|
||||
|
||||
std::ifstream infile(filename);
|
||||
if (infile.good()) {
|
||||
std::string sLine;
|
||||
std::getline(infile, sLine);
|
||||
line_size = (int)sLine.size() + 1;
|
||||
}
|
||||
infile.close();
|
||||
|
||||
if (file_size > 0) {
|
||||
auto new_size = m_rows.size() + int((file_size / line_size) * 1.20);
|
||||
m_rows.reserve(new_size);
|
||||
}
|
||||
|
||||
// load rows
|
||||
CsvReader csvparser{};
|
||||
auto row_cnt = csvparser.parseCSVFile(filename, colDefs, *this);
|
||||
|
||||
return row_cnt;
|
||||
}
|
||||
|
||||
void Table::print() {
|
||||
std::string out{"| "};
|
||||
std::string out2{"+-"};
|
||||
@@ -191,7 +193,7 @@ void Table::commit_row(Row &row) {
|
||||
void Table::commit_copy_of_row(Row &row) {
|
||||
Row& new_row = create_empty_row();
|
||||
|
||||
for(int i = 0; i < m_col_defs.size(); i++) {
|
||||
for(size_t i = 0; i < m_col_defs.size(); i++) {
|
||||
ColValue &ct = row[i];
|
||||
|
||||
if (ct.isNull()) {
|
||||
@@ -226,16 +228,15 @@ 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()) {
|
||||
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) {
|
||||
|
||||
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(Row &row) {
|
||||
for(int i = 0; i < m_col_defs.size(); i++) {
|
||||
for(size_t i = 0; i < m_col_defs.size(); i++) {
|
||||
ColDefNode col_def = m_col_defs[i];
|
||||
ColValue &col_val = row[i];
|
||||
|
||||
@@ -277,6 +278,8 @@ void Table::reindex_row(Index &index, const ColDefNode &col_def, const Row &old_
|
||||
void Table::index_row(const Row &row) {
|
||||
if (!m_indexes.empty()) {
|
||||
const size_t rowid = get_rowid(row);
|
||||
|
||||
std::unique_lock guard(m_insert_guard);
|
||||
for (auto &idx : m_indexes) {
|
||||
ColDefNode cDef = get_column_def(idx.get_column_name());
|
||||
index_row(idx, cDef, row, rowid);
|
||||
|
||||
Reference in New Issue
Block a user