Use if constexpr to make things a lot more readable

This commit is contained in:
louiz’
2018-03-16 01:13:50 +01:00
parent ba879a882e
commit d62ca9f879
7 changed files with 163 additions and 212 deletions
+25 -27
View File
@@ -29,41 +29,39 @@ struct Row
return col.value;
}
template <bool Coucou=true>
void save(std::unique_ptr<DatabaseEngine>& db, typename std::enable_if<!is_one_of<Id, T...> && Coucou>::type* = nullptr)
void save(std::unique_ptr<DatabaseEngine>& db)
{
this->insert(*db);
}
template <bool Coucou=true>
void save(std::unique_ptr<DatabaseEngine>& db, typename std::enable_if<is_one_of<Id, T...> && Coucou>::type* = nullptr)
{
const Id& id = std::get<Id>(this->columns);
if (id.value == Id::unset_value)
if constexpr(is_one_of<Id, T...>)
{
this->insert(*db);
if (db->last_inserted_rowid >= 0)
std::get<Id>(this->columns).value = static_cast<Id::real_type>(db->last_inserted_rowid);
const Id& id = std::get<Id>(this->columns);
if (id.value == Id::unset_value)
{
this->insert(*db);
if (db->last_inserted_rowid >= 0)
std::get<Id>(this->columns).value = static_cast<Id::real_type>(db->last_inserted_rowid);
}
else
this->update(*db);
}
else
this->update(*db);
this->insert(*db);
}
private:
template <bool Coucou=true>
void insert(DatabaseEngine& db, typename std::enable_if<is_one_of<Id, T...> && Coucou>::type* = nullptr)
void insert(DatabaseEngine& db)
{
InsertQuery query(this->table_name, this->columns);
// Ugly workaround for non portable stuff
query.body += db.get_returning_id_sql_string(Id::name);
query.execute(db, this->columns);
}
template <bool Coucou=true>
void insert(DatabaseEngine& db, typename std::enable_if<!is_one_of<Id, T...> && Coucou>::type* = nullptr)
{
InsertQuery query(this->table_name, this->columns);
query.execute(db, this->columns);
if constexpr(is_one_of<Id, T...>)
{
InsertQuery query(this->table_name, this->columns);
// Ugly workaround for non portable stuff
query.body += db.get_returning_id_sql_string(Id::name);
query.execute(db, this->columns);
}
else
{
InsertQuery query(this->table_name, this->columns);
query.execute(db, this->columns);
}
}
void update(DatabaseEngine& db)