Skip to content

Commit

Permalink
handle overwriting id columns names
Browse files Browse the repository at this point in the history
  • Loading branch information
wsekta committed Jan 9, 2024
1 parent 9c48aa5 commit 6a64262
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 17 deletions.
13 changes: 12 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,20 @@ The goal of the ORM C++ is to provide a decent Object-Relational Mapping library

struct ObjectModel
{
// INTEGER - if field is optional it will not be marked as NOT NULL
std::optional<int> field1;
std::string field2; // not null

// TEXT NOT NULL
std::string field2;

// defining id_columns is optional
inline static const std::vector<std::string> id_columns = {"field1", "field2"};

// other way to define id column - will be over writen by using id_columns
// int id;

// defining table_name is optional
inline static const std::string table_name = "object_model";
};

int main()
Expand Down
13 changes: 12 additions & 1 deletion examples/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,20 @@

struct ObjectModel
{
// INTEGER - if field is optional it will not be marked as NOT NULL
std::optional<int> field1;
std::string field2; // not null

// TEXT NOT NULL
std::string field2;

// defining id_columns is optional
inline static const std::vector<std::string> id_columns = {"field1", "field2"};

// other way to define id column - will be over writen by using id_columns
// int id;

// defining table_name is optional
inline static const std::string table_name = "object_model";
};

int main()
Expand Down
27 changes: 12 additions & 15 deletions include/orm-cxx/model/IdInfo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,17 @@ namespace orm::model
template <typename T>
auto getIdColumnsNames() -> std::unordered_set<std::string>
{
return {};
}

template <typename T>
auto getIdColumnsNames() -> std::unordered_set<std::string>
requires requires(T t) { t.id; }
{
return {"id"};
}

template <typename T>
auto getIdColumnsNames() -> std::unordered_set<std::string>
requires requires { T::id_columns; }
{
return {T::id_columns.begin(), T::id_columns.end()};
if constexpr (requires { T::id_columns; })
{
return {T::id_columns.begin(), T::id_columns.end()};
}
else if constexpr (requires(T t) { t.id; })
{
return {"id"};
}
else
{
return {};
}
}
}
17 changes: 17 additions & 0 deletions tests/DatabaseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,15 @@ struct ModelWithId
std::string field2;
};

struct ModelWithOverwrittenId
{
int id;
int field1;
std::string field2;

inline static const std::vector<std::string> id_columns = {"field1", "field2"};
};

template <typename T>
auto generateSomeDataModels(int count) -> std::vector<T>
{
Expand Down Expand Up @@ -160,3 +169,11 @@ TEST_F(DatabaseTest, shouldCreateTableWithIdColumn)

database.deleteTable<ModelWithId>();
}

TEST_F(DatabaseTest, shouldCreateTableWithOverwrittenIdColumn)
{
database.connect(connectionString);
database.createTable<ModelWithOverwrittenId>();

database.deleteTable<ModelWithOverwrittenId>();
}

0 comments on commit 6a64262

Please sign in to comment.