extract not null constraint into enum

This commit is contained in:
2024-01-19 07:58:37 +01:00
parent ed6366fef6
commit 9355f7ea27
13 changed files with 90 additions and 87 deletions
+15 -12
View File
@@ -90,12 +90,9 @@ sql::data_type_t to_type(enum_field_types type, unsigned int flags)
}
}
utils::constraints to_options(unsigned int flags)
utils::constraints to_constraints(unsigned int flags)
{
utils::constraints options{utils::constraints::NONE};
if (flags & NOT_NULL_FLAG) {
options |= utils::constraints::NOT_NULL;
}
if (flags & PRI_KEY_FLAG) {
options |= utils::constraints::PRIMARY_KEY;
}
@@ -106,6 +103,11 @@ utils::constraints to_options(unsigned int flags)
return options;
}
sql::null_option to_null_option(unsigned int flags)
{
return flags & NOT_NULL_FLAG ? sql::null_option::NOT_NULL : sql::null_option::NULLABLE;
}
sql::data_type_t string2type(const std::string &type_string)
{
// if (strcmp(type_string.c_str(), "int")
@@ -149,9 +151,10 @@ std::unique_ptr<sql::query_result_impl> mysql_connection::fetch(const std::strin
sql::record prototype;
for (unsigned i = 0; i < field_count; ++i) {
auto type = to_type(fields[i].type, fields[i].flags);
auto options = to_options(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});
prototype.append({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)));
@@ -200,17 +203,17 @@ sql::record mysql_connection::describe(const std::string &table)
char *end = nullptr;
// Todo: Handle error
auto index = strtoul(reader.column(0), &end, 10);
std::string name = reader.column(1);
std::string name = reader.column(0);
// Todo: extract size
auto typeinfo = determine_type_info(reader.column(2));
auto typeinfo = determine_type_info(reader.column(1));
end = nullptr;
utils::constraints options{};
if (strtoul(reader.column(4), &end, 10) == 0) {
options = utils::constraints::NOT_NULL;
sql::null_option null_opt{sql::null_option::NULLABLE};
if (strtoul(reader.column(2), &end, 10) == 0) {
null_opt = sql::null_option::NOT_NULL;
}
// f.default_value(res->column(4));
prototype.append({name, typeinfo.type, {typeinfo.size, options}});
prototype.append({name, typeinfo.type, {typeinfo.size}, null_opt});
}
return prototype;