mlisp/ml_usql.cpp

40 lines
1.1 KiB
C++

#include "ml_usql.h"
using namespace usql;
MlValue uSQL::execute(const std::string &command) {
std::unique_ptr<usql::Table> sql = USql::execute(command);
return ivaluize(sql.get());
}
MlValue uSQL::ivaluize(const usql::Table *table) {
std::vector<MlValue> rows;
std::vector<MlValue> columns;
columns.reserve(table->columns_count());
rows.reserve(table->rows_count());
for (auto row : table->m_rows) {
columns.clear();
for (int i = 0; i < table->columns_count(); i++) {
const auto & c = row[i];
const auto type = table->m_col_defs[i].type;
if (c.isNull()) {
columns.push_back(MlValue::nil());
} else if (type == ColumnType::integer_type || type == ColumnType::date_type) {
columns.push_back(MlValue(c.getIntegerValue()));
} else if (type == ColumnType::bool_type) {
columns.push_back(c.getBoolValue() ? MlValue(c.getIntegerValue()) : MlValue::nil());
} else if (type == ColumnType::float_type) {
columns.push_back(MlValue(c.getDoubleValue()));
} else {
columns.push_back(MlValue::string(c.getStringValue()));
}
}
rows.emplace_back(columns);
}
return rows;
}