entity query and record progress
This commit is contained in:
@@ -37,7 +37,7 @@ public:
|
||||
|
||||
size_t execute(const std::string &stmt) override;
|
||||
|
||||
sql::record describe(const std::string& table) override;
|
||||
std::vector<sql::column_definition> describe(const std::string& table) override;
|
||||
|
||||
bool exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
|
||||
|
||||
@@ -181,13 +181,13 @@ std::unique_ptr<sql::query_result_impl> mysql_connection::fetch(const std::strin
|
||||
|
||||
auto field_count = mysql_num_fields(result);
|
||||
auto fields = mysql_fetch_fields(result);
|
||||
sql::record prototype;
|
||||
std::vector<sql::column_definition> prototype;
|
||||
for (unsigned i = 0; i < field_count; ++i) {
|
||||
auto type = to_type(fields[i].type, fields[i].flags);
|
||||
auto options = to_constraints(fields[i].flags);
|
||||
auto null_opt = to_null_option(fields[i].flags);
|
||||
|
||||
prototype.append({fields[i].name, type, options, null_opt});
|
||||
prototype.emplace_back(fields[i].name, type, options, null_opt);
|
||||
}
|
||||
|
||||
return std::move(std::make_unique<sql::query_result_impl>(std::make_unique<mysql_result_reader>(result, field_count), std::move(prototype)));
|
||||
@@ -216,7 +216,7 @@ size_t mysql_connection::execute(const std::string &stmt)
|
||||
return mysql_affected_rows(mysql_.get());
|
||||
}
|
||||
|
||||
sql::record mysql_connection::describe(const std::string &table)
|
||||
std::vector<sql::column_definition> mysql_connection::describe(const std::string &table)
|
||||
{
|
||||
std::string stmt("SHOW COLUMNS FROM " + table);
|
||||
|
||||
@@ -230,7 +230,7 @@ sql::record mysql_connection::describe(const std::string &table)
|
||||
}
|
||||
|
||||
mysql_result_reader reader(result, mysql_num_fields(result));
|
||||
sql::record prototype;
|
||||
std::vector<sql::column_definition> prototype;
|
||||
while (reader.fetch()) {
|
||||
|
||||
char *end = nullptr;
|
||||
@@ -242,7 +242,7 @@ sql::record mysql_connection::describe(const std::string &table)
|
||||
if (strtoul(reader.column(2), &end, 10) == 0) {
|
||||
null_opt = sql::null_option::NOT_NULL;
|
||||
}
|
||||
prototype.append({name, typeinfo.type, {typeinfo.size}, null_opt, prototype.size()});
|
||||
prototype.push_back({name, typeinfo.type, {typeinfo.size}, null_opt, prototype.size()});
|
||||
}
|
||||
|
||||
return prototype;
|
||||
|
||||
@@ -33,7 +33,7 @@ public:
|
||||
|
||||
size_t execute(const std::string &stmt) override;
|
||||
|
||||
sql::record describe(const std::string& table) override;
|
||||
std::vector<sql::column_definition> describe(const std::string& table) override;
|
||||
|
||||
bool exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
|
||||
|
||||
@@ -51,14 +51,13 @@ std::unique_ptr<sql::query_result_impl> postgres_connection::fetch(const std::st
|
||||
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
|
||||
sql::record prototype;
|
||||
std::vector<sql::column_definition> prototype;
|
||||
auto num_col = PQnfields(res);
|
||||
for (int i = 0; i < num_col; ++i) {
|
||||
const char *col_name = PQfname(res, i);
|
||||
auto type = PQftype(res, i);
|
||||
auto size = PQfmod(res, i);
|
||||
// std::cout << "column " << col_name << ", type " << type << " (size: " << size << ")\n";
|
||||
prototype.append(sql::column_definition{col_name});
|
||||
prototype.emplace_back(col_name);
|
||||
}
|
||||
return std::move(std::make_unique<sql::query_result_impl>(std::make_unique<postgres_result_reader>(res), std::move(prototype)));
|
||||
}
|
||||
@@ -131,7 +130,7 @@ sql::data_type_t string2type(const char *type)
|
||||
}
|
||||
}
|
||||
|
||||
sql::record postgres_connection::describe(const std::string &table)
|
||||
std::vector<sql::column_definition> postgres_connection::describe(const std::string &table)
|
||||
{
|
||||
std::string stmt(
|
||||
"SELECT ordinal_position, column_name, udt_name, data_type, is_nullable, column_default FROM information_schema.columns WHERE table_schema='public' AND table_name='" + table + "'");
|
||||
@@ -141,7 +140,7 @@ sql::record postgres_connection::describe(const std::string &table)
|
||||
throw_postgres_error(res, conn_, "postgres", stmt);
|
||||
|
||||
postgres_result_reader reader(res);
|
||||
sql::record prototype;
|
||||
std::vector<sql::column_definition> prototype;
|
||||
while (reader.fetch()) {
|
||||
char *end = nullptr;
|
||||
// Todo: Handle error
|
||||
@@ -156,7 +155,7 @@ sql::record postgres_connection::describe(const std::string &table)
|
||||
null_opt = sql::null_option::NOT_NULL;
|
||||
}
|
||||
// f.default_value(res->column(4));
|
||||
prototype.append({name, type, utils::null_attributes, null_opt, index});
|
||||
prototype.emplace_back(name, type, utils::null_attributes, null_opt, index);
|
||||
}
|
||||
|
||||
return std::move(prototype);
|
||||
|
||||
@@ -33,14 +33,14 @@ public:
|
||||
|
||||
size_t execute(const std::string &stmt) override;
|
||||
|
||||
sql::record describe(const std::string& table) override;
|
||||
std::vector<sql::column_definition> describe(const std::string& table) override;
|
||||
|
||||
bool exists(const std::string &schema_name, const std::string &table_name) override;
|
||||
|
||||
private:
|
||||
struct fetch_context
|
||||
{
|
||||
sql::record prototype;
|
||||
std::vector<sql::column_definition> prototype;
|
||||
sqlite_result_reader::rows rows;
|
||||
};
|
||||
|
||||
|
||||
@@ -65,7 +65,7 @@ int sqlite_connection::parse_result(void* param, int column_count, char** values
|
||||
|
||||
if (context->prototype.empty()) {
|
||||
for(int i = 0; i < column_count; ++i) {
|
||||
context->prototype.append(sql::column_definition{columns[i]});
|
||||
context->prototype.emplace_back(columns[i]);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -144,12 +144,12 @@ sql::data_type_t string2type(const char *type)
|
||||
}
|
||||
}
|
||||
|
||||
sql::record sqlite_connection::describe(const std::string& table)
|
||||
std::vector<sql::column_definition> sqlite_connection::describe(const std::string& table)
|
||||
{
|
||||
const auto result = fetch_internal("PRAGMA table_info(" + table + ")");
|
||||
|
||||
sqlite_result_reader reader(result.rows, result.prototype.size());
|
||||
sql::record prototype;
|
||||
std::vector<sql::column_definition> prototype;
|
||||
while (reader.fetch()) {
|
||||
char *end = nullptr;
|
||||
// Todo: add index to column
|
||||
@@ -163,7 +163,7 @@ sql::record sqlite_connection::describe(const std::string& table)
|
||||
null_opt = sql::null_option::NOT_NULL;
|
||||
}
|
||||
// f.default_value(res->column(4));
|
||||
prototype.append({name, type, utils::null_attributes, null_opt, index});
|
||||
prototype.emplace_back(name, type, utils::null_attributes, null_opt, index);
|
||||
}
|
||||
|
||||
return std::move(prototype);
|
||||
|
||||
@@ -37,10 +37,19 @@ private:
|
||||
using namespace matador;
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Session relation test", "[session][relation]") {
|
||||
|
||||
ses.attach<matador::test::airplane>("airplane");
|
||||
using namespace matador;
|
||||
ses.attach<test::airplane>("airplane");
|
||||
ses.create_schema();
|
||||
ses.insert<test::airplane>(1, "Boeing", "A380");
|
||||
|
||||
REQUIRE(true);
|
||||
}
|
||||
|
||||
TEST_CASE_METHOD(SessionFixture, "Find object with id", "[session][find]") {
|
||||
ses.attach<matador::test::airplane>("airplane");
|
||||
ses.create_schema();
|
||||
auto a380 = ses.insert<test::airplane>(1, "Boeing", "A380");
|
||||
|
||||
ses.find<test::airplane>(1);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user