Skip to content

Commit 84d8041

Browse files
committed
Use name aliases for update and delete in datamapper
1 parent 8085d0b commit 84d8041

File tree

3 files changed

+68
-24
lines changed

3 files changed

+68
-24
lines changed

src/Lightweight/DataMapper/DataMapper.hpp

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -832,7 +832,7 @@ void DataMapper::Update(Record& record)
832832
{
833833
if (record.[:el:].IsModified())
834834
{
835-
_stmt.BindInputParameter(i++, record.[:el:].Value(), std::meta::identifier_of(el));
835+
_stmt.BindInputParameter(i++, record.[:el:].Value(), FieldNameOf<el>);
836836
}
837837
}
838838

@@ -841,23 +841,21 @@ void DataMapper::Update(Record& record)
841841
using FieldType = typename[:std::meta::type_of(el):];
842842
if constexpr (FieldType::IsPrimaryKey)
843843
{
844-
_stmt.BindInputParameter(i++, record.[:el:].Value(), std::meta::identifier_of(el));
844+
_stmt.BindInputParameter(i++, record.[:el:].Value(), FieldNameOf<el>);
845845
}
846846
}
847847
#else
848848
// Bind the SET clause
849-
Reflection::CallOnMembers(
850-
record, [this, &i]<typename Name, typename FieldType>(Name const& name, FieldType const& field) mutable {
851-
if (field.IsModified())
852-
_stmt.BindInputParameter(i++, field.Value(), name);
853-
});
849+
Reflection::CallOnMembersWithoutName(record, [this, &i]<size_t I, typename FieldType>(FieldType const& field) {
850+
if (field.IsModified())
851+
_stmt.BindInputParameter(i++, field.Value(), FieldNameAt<I, Record>);
852+
});
854853

855854
// Bind the WHERE clause
856-
Reflection::CallOnMembers(
857-
record, [this, &i]<typename Name, typename FieldType>(Name const& name, FieldType const& field) mutable {
858-
if constexpr (FieldType::IsPrimaryKey)
859-
_stmt.BindInputParameter(i++, field.Value(), name);
860-
});
855+
Reflection::CallOnMembersWithoutName(record, [this, &i]<size_t I, typename FieldType>(FieldType const& field) {
856+
if constexpr (IsPrimaryKey<Reflection::MemberTypeOf<I, Record>>)
857+
_stmt.BindInputParameter(i++, field.Value(), FieldNameAt<I, Record>);
858+
});
861859
#endif
862860

863861
_stmt.Execute();
@@ -878,14 +876,13 @@ std::size_t DataMapper::Delete(Record const& record)
878876
{
879877
using FieldType = typename[:std::meta::type_of(el):];
880878
if constexpr (FieldType::IsPrimaryKey)
881-
std::ignore = query.Where(std::meta::identifier_of(el), SqlWildcard);
879+
std::ignore = query.Where(FieldNameOf<el>, SqlWildcard);
882880
}
883881
#else
884-
Reflection::CallOnMembers(record,
885-
[&query]<typename Name, typename FieldType>(Name const& name, FieldType const& /*field*/) {
886-
if constexpr (FieldType::IsPrimaryKey)
887-
std::ignore = query.Where(name, SqlWildcard);
888-
});
882+
Reflection::CallOnMembersWithoutName(record, [&query]<size_t I, typename FieldType>(FieldType const& /*field*/) {
883+
if constexpr (IsPrimaryKey<Reflection::MemberTypeOf<I, Record>>)
884+
std::ignore = query.Where(FieldNameAt<I, Record>, SqlWildcard);
885+
});
889886
#endif
890887

891888
_stmt.Prepare(query);
@@ -897,16 +894,15 @@ std::size_t DataMapper::Delete(Record const& record)
897894
using FieldType = typename[:std::meta::type_of(el):];
898895
if constexpr (FieldType::IsPrimaryKey)
899896
{
900-
_stmt.BindInputParameter(i++, record.[:el:].Value(), std::meta::identifier_of(el));
897+
_stmt.BindInputParameter(i++, record.[:el:].Value(), FieldNameOf<el>);
901898
}
902899
}
903900
#else
904901
// Bind the WHERE clause
905-
Reflection::CallOnMembers(
906-
record,
907-
[this, i = SQLSMALLINT { 1 }]<typename Name, typename FieldType>(Name const& name, FieldType const& field) mutable {
908-
if constexpr (FieldType::IsPrimaryKey)
909-
_stmt.BindInputParameter(i++, field.Value(), name);
902+
Reflection::CallOnMembersWithoutName(
903+
record, [this, i = SQLSMALLINT { 1 }]<size_t I, typename FieldType>(FieldType const& field) mutable {
904+
if constexpr (IsPrimaryKey<Reflection::MemberTypeOf<I, Record>>)
905+
_stmt.BindInputParameter(i++, field.Value(), FieldNameAt<I, Record>);
910906
});
911907
#endif
912908

src/tests/DataMapper/NamingTests.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,13 @@ struct NamingTest2
7070
static constexpr std::string_view TableName = "NamingTest2_aliased"sv;
7171
};
7272

73+
struct Person
74+
{
75+
Light::Field<int, Light::PrimaryKey::AutoAssign, Light::SqlRealName { "index" }> id;
76+
Light::Field<Light::SqlAnsiString<50>, Light::SqlRealName { "not_name" }> name;
77+
static constexpr std::string_view TableName = "Human"sv;
78+
};
79+
7380
} // namespace Models
7481

7582
TEST_CASE_METHOD(SqlTestFixture, "SQL entity naming (namespace)", "[DataMapper]")
@@ -82,3 +89,44 @@ TEST_CASE_METHOD(SqlTestFixture, "SQL entity naming (namespace)", "[DataMapper]"
8289
CHECK(FieldNameAt<1, Models::NamingTest2> == "Second_PK"sv);
8390
CHECK(RecordTableName<Models::NamingTest2> == "NamingTest2_aliased"sv);
8491
}
92+
93+
TEST_CASE_METHOD(SqlTestFixture, "Check aliasing of the columns and table for crud operation", "[DataMapper]")
94+
{
95+
auto dm = DataMapper::Create();
96+
97+
dm->CreateTable<Models::Person>();
98+
99+
auto record1 = Models::Person {};
100+
record1.name = "42";
101+
102+
CHECK(dm->Query<Models::Person>().Count() == 0);
103+
CHECK(record1.id.Value() == 0);
104+
dm->Create(record1);
105+
CHECK(dm->Query<Models::Person>().Count() == 1);
106+
CHECK(record1.id.Value() != 0);
107+
{
108+
auto queriedRecordResult = dm->QuerySingle<Models::Person>(record1.id);
109+
CHECK(queriedRecordResult.has_value());
110+
CHECK(queriedRecordResult.value().name.Value().ToStringView()
111+
== "42"sv); // NOLINT(bugprone-unchecked-optional-access)
112+
}
113+
114+
record1.name = "43";
115+
dm->Update(record1);
116+
117+
SqlStatement(dm->Connection())
118+
.ExecuteDirect(R"( INSERT INTO "Human" ("index", "not_name") VALUES (5, 'Direct Insert') )");
119+
CHECK(dm->Query<Models::Person>().Count() == 2);
120+
{
121+
auto queriedRecordResult = dm->QuerySingle<Models::Person>(record1.id);
122+
CHECK(queriedRecordResult.has_value());
123+
CHECK(queriedRecordResult.value().name.Value().ToStringView()
124+
== "43"sv); // NOLINT(bugprone-unchecked-optional-access)
125+
}
126+
dm->Delete(record1);
127+
CHECK(dm->Query<Models::Person>().Count() == 1);
128+
{
129+
auto queriedRecord = dm->QuerySingle<Models::Person>(record1.id);
130+
CHECK(!queriedRecord.has_value());
131+
}
132+
}

test.db

8 KB
Binary file not shown.

0 commit comments

Comments
 (0)