
#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;

	for (auto row : table->m_rows) {
		columns.clear();
		for (int i = 0; i < table->columns_count(); i++) {
			auto c = row.ithColumn(i);
			auto type = table->m_col_defs[i].type;
			if (type == ColumnType::integer_type) {
				columns.push_back(MlValue(c->getIntValue()));
			} else if (type == ColumnType::float_type) {
				columns.push_back(MlValue(c->getDoubleValue()));
			} else {
				columns.push_back(MlValue::string(c->getStringValue()));
			}
		}
		rows.push_back(columns);
	}
	return rows;
}
