Skip to content

Commit d636fb6

Browse files
committed
bool member type
1 parent 9a0200c commit d636fb6

4 files changed

Lines changed: 27 additions & 4 deletions

File tree

include/reflection.h

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
#include <stddef.h>
3636
#endif
3737

38-
// todo: bool member type
3938
// todo: delete with custom predicate
4039

4140
// todo: BETWEEN / IN / NOT IN predicate
@@ -52,7 +51,8 @@ enum class REFLECTION_EXPORT SqliteStorageClass
5251
kInt,
5352
kReal,
5453
kText,
55-
kDateTime
54+
kDateTime,
55+
kBool
5656
};
5757

5858
/// A struct holding all information needed for introspection of user-defined structs
@@ -86,6 +86,7 @@ struct REFLECTION_EXPORT Reflection
8686
static const char* ToSqliteColumnName(const SqliteStorageClass storage_class) {
8787
switch (storage_class) {
8888
case SqliteStorageClass::kInt:
89+
case SqliteStorageClass::kBool:
8990
return "INTEGER";
9091
case SqliteStorageClass::kReal:
9192
return "REAL";
@@ -178,26 +179,30 @@ REFLECTION_EXPORT char* GetMemberAddress(void* p, const Reflection& record, size
178179
#define MEMBER_REAL(R) MEMBER_DECLARE(double, R)
179180
#define MEMBER_TEXT(R) MEMBER_DECLARE(std::wstring, R)
180181
#define MEMBER_DATETIME(R) MEMBER_DECLARE(sqlite_reflection::TimePoint, R)
182+
#define MEMBER_BOOL(R) MEMBER_DECLARE(bool, R)
181183
#define FUNC(SIGNATURE)
182184
FIELDS
183185
#undef MEMBER_DECLARE
184186
#undef MEMBER_INT
185187
#undef MEMBER_REAL
186188
#undef MEMBER_TEXT
187189
#undef MEMBER_DATETIME
190+
#undef MEMBER_BOOL
188191
#undef FUNC
189192

190193
// custom function declaration
191194
#define MEMBER_INT(R)
192195
#define MEMBER_REAL(R)
193196
#define MEMBER_TEXT(R)
194197
#define MEMBER_DATETIME(R)
198+
#define MEMBER_BOOL(R)
195199
#define FUNC(SIGNATURE) SIGNATURE;
196200
FIELDS
197201
#undef MEMBER_INT
198202
#undef MEMBER_REAL
199203
#undef MEMBER_TEXT
200204
#undef MEMBER_DATETIME
205+
#undef MEMBER_BOOL
201206
#undef FUNC
202207
};
203208

@@ -217,12 +222,14 @@ REFLECTION_EXPORT char* GetMemberAddress(void* p, const Reflection& record, size
217222
#define MEMBER_REAL(R) DEFINE_MEMBER(R, SqliteStorageClass::kReal)
218223
#define MEMBER_TEXT(R) DEFINE_MEMBER(R, SqliteStorageClass::kText)
219224
#define MEMBER_DATETIME(R) DEFINE_MEMBER(R, SqliteStorageClass::kDateTime)
225+
#define MEMBER_BOOL(R) DEFINE_MEMBER(R, SqliteStorageClass::kBool)
220226
#define FUNC(SIGNATURE)
221227
FIELDS
222228
#undef MEMBER_INT
223229
#undef MEMBER_REAL
224230
#undef MEMBER_TEXT
225231
#undef MEMBER_DATETIME
232+
#undef MEMBER_BOOL
226233
#undef FUNC
227234
}
228235
return name;

src/queries.cc

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,13 @@ std::vector<std::string> ExecutionQuery::GetValues(void* p) const {
8989
content = StringUtilities::FromInt(value);
9090
break;
9191
}
92+
93+
case SqliteStorageClass::kBool:
94+
{
95+
const auto& value = (*(bool*)((void*)GetMemberAddress(p, record_, j)));
96+
content = StringUtilities::FromInt(value ? 1 : 0);
97+
break;
98+
}
9299

93100
case SqliteStorageClass::kReal:
94101
{
@@ -278,6 +285,13 @@ void FetchRecordsQuery::Hydrate(void* p, const FetchQueryResults& query_results,
278285
v = StringUtilities::ToInt(content);
279286
break;
280287
}
288+
289+
case SqliteStorageClass::kBool:
290+
{
291+
auto& v = (*(bool*)((void*)GetMemberAddress(p, record, j)));
292+
v = StringUtilities::ToInt(content) == 1;
293+
break;
294+
}
281295

282296
case SqliteStorageClass::kReal:
283297
{

tests/database_test.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ TEST_F(DatabaseTest, MultipleInsertions) {
7070

7171
std::vector<Person> persons;
7272

73-
persons.push_back({3, L"παναγιώτης", L"ανδριανόπουλος", 28});
74-
persons.push_back({5, L"peter", L"meier", 32});
73+
persons.push_back({3, L"παναγιώτης", L"ανδριανόπουλος", 28, false});
74+
persons.push_back({5, L"peter", L"meier", 32, true});
7575

7676
db.Save(persons);
7777

@@ -83,6 +83,7 @@ TEST_F(DatabaseTest, MultipleInsertions) {
8383
EXPECT_EQ(persons[i].first_name, saved_persons[i].first_name);
8484
EXPECT_EQ(persons[i].last_name, saved_persons[i].last_name);
8585
EXPECT_EQ(persons[i].age, saved_persons[i].age);
86+
EXPECT_EQ(persons[i].isVaccinated, saved_persons[i].isVaccinated);
8687
}
8788
}
8889

tests/person.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
MEMBER_TEXT(first_name) \
3030
MEMBER_TEXT(last_name) \
3131
MEMBER_INT(age) \
32+
MEMBER_BOOL(isVaccinated) \
3233
FUNC(std::wstring GetFullName() const)
3334
#include "reflection.h"
3435

0 commit comments

Comments
 (0)