Skip to content

Commit c5e172c

Browse files
authored
Merge pull request #406 from LASTRADA-Software/improvement/datamapper_create
Add DataMapperOptions template argument to the Create function
2 parents fc8f00d + 9a14806 commit c5e172c

File tree

6 files changed

+35
-7
lines changed

6 files changed

+35
-7
lines changed

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ jobs:
4545
steps:
4646
- uses: actions/checkout@v2
4747
with:
48-
persist-credentials: true
48+
#persist-credentials: true
4949
repository: ${{ github.event.pull_request.head.repo.full_name }}
5050
ref: ${{ github.event.pull_request.head.ref }}
5151
# token: ${{ secrets.CLANG_FORMAT_PAT }}

src/Lightweight/DataMapper/DataMapper.hpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ class DataMapper
155155
/// The record is inserted into the database and the primary key is set on this record.
156156
///
157157
/// @return The primary key of the newly created record.
158-
template <typename Record>
158+
template <DataMapperOptions QueryOptions = {}, typename Record>
159159
RecordPrimaryKeyType<Record> Create(Record& record);
160160

161161
/// @brief Creates a new record in the database.
@@ -1306,7 +1306,7 @@ RecordPrimaryKeyType<Record> DataMapper::CreateExplicit(Record const& record)
13061306
}
13071307
}
13081308

1309-
template <typename Record>
1309+
template <DataMapperOptions QueryOptions, typename Record>
13101310
RecordPrimaryKeyType<Record> DataMapper::Create(Record& record)
13111311
{
13121312
static_assert(!std::is_const_v<Record>);
@@ -1371,7 +1371,9 @@ RecordPrimaryKeyType<Record> DataMapper::Create(Record& record)
13711371
SetId(record, _stmt.LastInsertId(RecordTableName<Record>));
13721372

13731373
ClearModifiedState(record);
1374-
ConfigureRelationAutoLoading(record);
1374+
1375+
if constexpr (QueryOptions.loadRelations)
1376+
ConfigureRelationAutoLoading(record);
13751377

13761378
if constexpr (HasPrimaryKey<Record>)
13771379
return GetPrimaryKeyField(record);

src/Lightweight/DataMapper/HasMany.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ inline LIGHTWEIGHT_FORCE_INLINE std::size_t HasMany<OtherRecord>::Count() const
188188
if (_records)
189189
return _records->size();
190190

191-
if (!_count)
191+
if (!_count && _loader.count)
192192
const_cast<HasMany<OtherRecord>*>(this)->_count = _loader.count();
193193

194194
return _count.value_or(0);

src/Lightweight/DataMapper/QueryBuilders.hpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ class DataMapper;
2020
/// when using query builder directly from the DataMapper
2121
struct DataMapperOptions
2222
{
23-
/// This is the default behavior since compilation times significantly increase otherwise.
24-
bool loadRelations { false };
23+
bool loadRelations { true };
2524
};
2625

2726
/// Main API for mapping records to C++ from the database using high level C++ syntax.

src/tests/DataMapper/CreateTests.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// SPDX-License-Identifier: Apache-2.0
33

44
#include "../Utils.hpp"
5+
#include "Entities.hpp"
56

67
#include <Lightweight/Lightweight.hpp>
78

@@ -137,4 +138,24 @@ TEST_CASE_METHOD(SqlTestFixture, "Table with multiple primary keys", "[DataMappe
137138
CHECK(queriedRecord == record);
138139
}
139140

141+
TEST_CASE_METHOD(SqlTestFixture, "Loading of the dependent records after create", "[DataMapper]")
142+
{
143+
auto dm = DataMapper();
144+
145+
dm.CreateTables<User, NullableForeignKeyUser>();
146+
147+
auto user = User { .id = SqlGuid::Create(), .name = "John Doe" };
148+
dm.Create(user);
149+
150+
auto nullableFKUser = NullableForeignKeyUser { .user = user };
151+
dm.Create(nullableFKUser);
152+
REQUIRE(nullableFKUser.user.Value().has_value());
153+
REQUIRE(nullableFKUser.user->id.Value() == user.id.Value());
154+
155+
auto nullableFKUserNotSet = NullableForeignKeyUser {};
156+
dm.Create(nullableFKUserNotSet);
157+
REQUIRE(!nullableFKUserNotSet.user.Value().has_value());
158+
}
159+
160+
140161
// NOLINTEND(bugprone-unchecked-optional-access)

src/tests/DataMapper/Entities.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,12 @@ struct Email
5353
constexpr std::weak_ordering operator<=>(Email const& other) const = default;
5454
};
5555

56+
struct NullableForeignKeyUser
57+
{
58+
Light::Field<Light::SqlGuid, Light::PrimaryKey::AutoAssign> id {};
59+
Light::BelongsTo<Member(User::id), Light::SqlRealName { "user_id" }, Light::SqlNullable::Null> user {};
60+
};
61+
5662
struct Physician;
5763
struct Appointment;
5864
struct Patient;

0 commit comments

Comments
 (0)