mirror of
https://github.com/getml/sqlgen.git
synced 2026-01-04 16:40:07 -06:00
Add the primary keys
This commit is contained in:
@@ -63,6 +63,9 @@ class Connection : public sqlgen::Connection {
|
||||
std::string create_table_to_sql(
|
||||
const dynamic::CreateTable& _stmt) const noexcept;
|
||||
|
||||
std::vector<std::string> get_primary_keys(
|
||||
const dynamic::CreateTable& _stmt) const noexcept;
|
||||
|
||||
static ConnPtr make_conn(const std::string& _conn_str);
|
||||
|
||||
std::string select_from_to_sql(
|
||||
|
||||
@@ -45,11 +45,39 @@ std::string Connection::create_table_to_sql(
|
||||
stream << "(";
|
||||
stream << internal::strings::join(
|
||||
", ", internal::collect::vector(_stmt.columns | transform(col_to_sql)));
|
||||
|
||||
const auto primary_keys = get_primary_keys(_stmt);
|
||||
|
||||
if (primary_keys.size() != 0) {
|
||||
stream << ", PRIMARY KEY (" << internal::strings::join(", ", primary_keys)
|
||||
<< ")";
|
||||
}
|
||||
|
||||
stream << ");";
|
||||
|
||||
return stream.str();
|
||||
}
|
||||
|
||||
std::vector<std::string> Connection::get_primary_keys(
|
||||
const dynamic::CreateTable& _stmt) const noexcept {
|
||||
using namespace std::ranges::views;
|
||||
|
||||
const auto is_primary_key = [](const auto& _col) -> bool {
|
||||
return _col.type.visit(
|
||||
[](const auto& _t) -> bool { return _t.properties.primary; });
|
||||
};
|
||||
|
||||
const auto get_name = [](const auto& _col) { return _col.name; };
|
||||
|
||||
const auto wrap_in_quotes = [](const std::string& _name) -> std::string {
|
||||
return "\"" + _name + "\"";
|
||||
};
|
||||
|
||||
return internal::collect::vector(_stmt.columns | filter(is_primary_key) |
|
||||
transform(get_name) |
|
||||
transform(wrap_in_quotes));
|
||||
}
|
||||
|
||||
rfl::Result<Ref<sqlgen::Connection>> Connection::make(
|
||||
const Credentials& _credentials) noexcept {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user