FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
sqlite_orm/tests/tests4.cpp at master · fnc12/sqlite_orm · GitHub
fnc12
/
sqlite_orm
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
343
Star
2.7k
Code
Issues
13
Pull requests
3
Discussions
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
sqlite_orm
/
tests
/
tests4.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
292 lines (255 loc) · 11.9 KB
Breadcrumbs
sqlite_orm
/
tests
/
tests4.cpp
Copy path
File metadata and controls
292 lines (255 loc) · 11.9 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#
include
<
sqlite_orm/sqlite_orm.h
>
#
include
<
catch2/catch_all.hpp
>
#
include
<
algorithm
>
//
std::count_if
#
ifdef
SQLITE_ORM_OPTIONAL_SUPPORTED
#
include
<
optional
>
//
std::optional
#
endif
//
SQLITE_ORM_OPTIONAL_SUPPORTED
using
namespace
sqlite_orm
;
TEST_CASE
(
"
Unique ptr in update
"
) {
struct
User
{
int
id =
0
;
std::unique_ptr<std::string> name;
};
auto
nameColumn =
make_column
(
"
name
"
, &User::name);
auto
nameTable =
make_table
(
"
users
"
,
make_column
(
"
id
"
, &User::id,
primary_key
()), nameColumn);
auto
storage =
make_storage
({}, nameTable);
storage.
sync_schema
();
storage.
insert
(User{});
storage.
insert
(User{});
storage.
insert
(User{});
{
storage.
update_all
(
set
(
assign
(&User::name, std::make_unique<std::string>(
"
Nick
"
))));
REQUIRE
(storage.
count
<User>(
where
(
is_null
(&User::name))) ==
0
);
}
{
std::unique_ptr<std::string> ptr;
storage.
update_all
(
set
(
assign
(&User::name,
std::move
(ptr))));
REQUIRE
(storage.
count
<User>(
where
(
is_not_null
(&User::name))) ==
0
);
}
}
#
ifdef
SQLITE_ORM_OPTIONAL_SUPPORTED
TEST_CASE
(
"
optional in update
"
) {
struct
User
{
int
id =
0
;
std::optional<
int
> carYear;
//
will be empty if user takes the bus.
};
auto
storage =
make_storage
(
{},
make_table
(
"
users
"
,
make_column
(
"
id
"
, &User::id,
primary_key
()),
make_column
(
"
car_year
"
, &User::carYear)));
storage.
sync_schema
();
storage.
insert
(User{});
storage.
insert
(User{});
storage.
insert
(User{});
storage.
insert
(User{
0
,
2006
});
REQUIRE
(storage.
count
<User>(
where
(
is_not_null
(&User::carYear))) ==
1
);
{
storage.
update_all
(
set
(
assign
(&User::carYear, std::optional<
int
>{})));
REQUIRE
(storage.
count
<User>(
where
(
is_not_null
(&User::carYear))) ==
0
);
}
{
storage.
update_all
(
set
(
assign
(&User::carYear,
1994
)));
REQUIRE
(storage.
count
<User>(
where
(
is_null
(&User::carYear))) ==
0
);
}
{
storage.
update_all
(
set
(
assign
(&User::carYear,
nullptr
)));
REQUIRE
(storage.
count
<User>(
where
(
is_not_null
(&User::carYear))) ==
0
);
}
}
#
endif
//
SQLITE_ORM_OPTIONAL_SUPPORTED
TEST_CASE
(
"
join
"
) {
struct
User
{
int
id =
0
;
std::string name;
#
ifndef
SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
User
() = default;
User
(
int
id, std::string name) : id{id}, name{
std::move
(name)} {}
#
endif
};
struct
Visit
{
int
id =
0
;
int
userId =
0
;
time_t
date =
0
;
#
ifndef
SQLITE_ORM_AGGREGATE_NSDMI_SUPPORTED
Visit
() = default;
Visit
(
int
id,
int
userId,
time_t
date) : id{id}, userId{userId}, date{date} {}
#
endif
};
auto
storage =
make_storage
({},
make_table
(
"
users
"
,
make_column
(
"
id
"
, &User::id,
primary_key
()),
make_column
(
"
name
"
, &User::name)),
make_table
(
"
visits
"
,
make_column
(
"
id
"
, &Visit::id,
primary_key
()),
make_column
(
"
user_id
"
, &Visit::userId),
make_column
(
"
date
"
, &Visit::date)));
storage.
sync_schema
();
int
id =
1
;
User will{id++,
"
Will
"
};
User smith{id++,
"
Smith
"
};
User nicole{id++,
"
Nicole
"
};
storage.
replace
(will);
storage.
replace
(smith);
storage.
replace
(nicole);
id =
1
;
storage.
replace
(Visit{id++, will.
id
,
10
});
storage.
replace
(Visit{id++, will.
id
,
20
});
storage.
replace
(Visit{id++, will.
id
,
30
});
storage.
replace
(Visit{id++, smith.
id
,
25
});
storage.
replace
(Visit{id++, smith.
id
,
35
});
{
auto
rows = storage.
get_all
<User>(left_join<Visit>(
on
(
is_equal
(&Visit::userId,
2
))));
REQUIRE
(rows.
size
() ==
6
);
}
{
auto
rows = storage.
get_all
<User>(join<Visit>(
on
(
is_equal
(&Visit::userId,
2
))));
REQUIRE
(rows.
size
() ==
6
);
}
{
auto
rows = storage.
get_all
<User>(left_outer_join<Visit>(
on
(
is_equal
(&Visit::userId,
2
))));
REQUIRE
(rows.
size
() ==
6
);
}
{
auto
rows = storage.
get_all
<User>(inner_join<Visit>(
on
(
is_equal
(&Visit::userId,
2
))));
REQUIRE
(rows.
size
() ==
6
);
}
{
auto
rows = storage.
get_all
<User>(cross_join<Visit>());
REQUIRE
(rows.
size
() ==
15
);
}
{
auto
rows = storage.
get_all
<User>(natural_join<Visit>());
REQUIRE
(rows.
size
() ==
3
);
}
}
#
if
SQLITE_VERSION_NUMBER >= 3006019
TEST_CASE
(
"
two joins
"
) {
struct
Statement
{
int
id_statement;
long
date;
};
struct
Concepto
{
int
id_concepto;
std::string name;
//
TFT-SINPE A: 15103-02**-****-8467
int
fkey_account;
//
{ 15103-02**-****-8467, ...}
};
struct
Account
{
int
id_account;
std::string number;
//
15103-02**-****-8467
int
fkey_bank;
//
{ BAC San Jose, "Barrio Dent", { Costa Rica} }
int
fkey_account_owner;
//
{ Juan Dent Herrera, ... }
std::string description;
//
AMEX Cashback Premium
bool
is_tarjeta;
//
true
};
struct
Pais
{
int
id_pais;
std::string name;
};
struct
Banco
{
int
id_bank;
std::string nombre;
std::string ubicacion;
int
fkey_pais;
};
struct
AccountOwner
{
int
id_owner;
std::string name;
};
struct
Transaccion
{
int
id_transaccion;
double
amount_colones;
double
amount_dolares;
int
fkey_account_own;
//
Account
int
fkey_account_other =
0
;
//
Account optional
long
line_date;
std::string descripcion;
int
fkey_category;
int
fkey_concepto;
int
fkey_statement;
int
row;
//
fkey_statement + row is unique
};
struct
Categoria
{
int
id_categoria;
std::string name;
bool
is_expense_or_income;
};
auto
storage =
make_storage
(
{},
make_table
(
"
Statement
"
,
make_column
(
"
id_statement
"
, &Statement::id_statement,
primary_key
().
autoincrement
()),
make_column
(
"
date
"
, &Statement::date)),
make_table
(
"
Categoria
"
,
make_column
(
"
id_category
"
, &Categoria::id_categoria,
primary_key
().
autoincrement
()),
make_column
(
"
name
"
, &Categoria::name,
collate_nocase
()),
make_column
(
"
is_expense_or_income
"
, &Categoria::is_expense_or_income)),
make_table
(
"
Concepto
"
,
make_column
(
"
id_concepto
"
, &Concepto::id_concepto,
primary_key
().
autoincrement
()),
make_column
(
"
name
"
, &Concepto::name,
collate_nocase
()),
make_column
(
"
fkey_account
"
, &Concepto::fkey_account),
foreign_key
(&Concepto::fkey_account).
references
(&Account::id_account)),
make_table
(
"
Account
"
,
make_column
(
"
id_account
"
, &Account::id_account,
primary_key
().
autoincrement
()),
make_column
(
"
number
"
, &Account::number,
collate_nocase
()),
make_column
(
"
fkey_bank
"
, &Account::fkey_bank),
make_column
(
"
fkey_account_owner
"
, &Account::fkey_account_owner),
make_column
(
"
description
"
, &Account::description,
collate_nocase
()),
make_column
(
"
is_tarjeta
"
, &Account::is_tarjeta),
foreign_key
(&Account::fkey_account_owner).
references
(&AccountOwner::id_owner),
foreign_key
(&Account::fkey_bank).
references
(&Banco::id_bank)),
make_table
(
"
Banco
"
,
make_column
(
"
id_bank
"
, &Banco::id_bank,
primary_key
().
autoincrement
()),
make_column
(
"
nombre
"
, &Banco::nombre,
collate_nocase
()),
make_column
(
"
ubicacion
"
, &Banco::ubicacion,
collate_nocase
()),
make_column
(
"
fkey_Pais
"
, &Banco::fkey_pais),
foreign_key
(&Banco::fkey_pais).
references
(&Pais::id_pais)),
make_table
(
"
AccountOwner
"
,
make_column
(
"
id_owner
"
, &AccountOwner::id_owner,
primary_key
().
autoincrement
()),
make_column
(
"
name
"
, &AccountOwner::name,
collate_nocase
())),
make_table
(
"
Transaccion
"
,
make_column
(
"
id_transaccion
"
, &Transaccion::id_transaccion,
primary_key
().
autoincrement
()),
make_column
(
"
colones
"
, &Transaccion::amount_colones),
make_column
(
"
dolares
"
, &Transaccion::amount_dolares),
make_column
(
"
fkey_account_own
"
, &Transaccion::fkey_account_own),
make_column
(
"
fkey_account_other
"
, &Transaccion::fkey_account_other),
make_column
(
"
line_date
"
, &Transaccion::line_date),
make_column
(
"
descripcion
"
, &Transaccion::descripcion),
make_column
(
"
fkey_category
"
, &Transaccion::fkey_category),
make_column
(
"
concepto
"
, &Transaccion::fkey_concepto),
make_column
(
"
fkey_statement
"
, &Transaccion::fkey_statement),
make_column
(
"
row
"
, &Transaccion::row),
foreign_key
(&Transaccion::fkey_account_own).
references
(&Account::id_account),
foreign_key
(&Transaccion::fkey_account_other).
references
(&Account::id_account),
foreign_key
(&Transaccion::fkey_category).
references
(&Categoria::id_categoria),
foreign_key
(&Transaccion::fkey_concepto).
references
(&Concepto::id_concepto),
foreign_key
(&Transaccion::fkey_statement).
references
(&Statement::id_statement)),
make_table
(
"
Pais
"
,
make_column
(
"
id_pais
"
, &Pais::id_pais,
primary_key
().
autoincrement
()),
make_column
(
"
name
"
, &Pais::name,
collate_nocase
())));
storage.
sync_schema
();
using
als_t
=
alias_t
<Transaccion>;
using
als_a = alias_a<Account>;
using
als_b = alias_b<Account>;
std::ignore = storage.
select
(
columns
(alias_column<
als_t
>(&Transaccion::fkey_account_other),
alias_column<
als_t
>(&Transaccion::fkey_account_own),
alias_column<als_a>(&Account::id_account),
alias_column<als_b>(&Account::id_account)),
left_outer_join<als_a>(
on
(alias_column<
als_t
>(&Transaccion::fkey_account_other) ==
alias_column<als_a>(&Account::id_account))),
inner_join<als_b>(
on
(alias_column<
als_t
>(&Transaccion::fkey_account_own) ==
alias_column<als_b>(&Account::id_account))));
std::ignore = storage.
select
(
columns
(alias_column<
als_t
>(&Transaccion::fkey_account_other),
alias_column<
als_t
>(&Transaccion::fkey_account_own),
alias_column<als_a>(&Account::id_account),
alias_column<als_b>(&Account::id_account)),
left_join<als_a>(
on
(alias_column<
als_t
>(&Transaccion::fkey_account_other) ==
alias_column<als_a>(&Account::id_account))),
inner_join<als_b>(
on
(alias_column<
als_t
>(&Transaccion::fkey_account_own) ==
alias_column<als_b>(&Account::id_account))));
std::ignore = storage.
select
(
columns
(alias_column<
als_t
>(&Transaccion::fkey_account_other),
alias_column<
als_t
>(&Transaccion::fkey_account_own),
alias_column<als_a>(&Account::id_account),
alias_column<als_b>(&Account::id_account)),
join<als_a>(
on
(alias_column<
als_t
>(&Transaccion::fkey_account_other) ==
alias_column<als_a>(&Account::id_account))),
inner_join<als_b>(
on
(alias_column<
als_t
>(&Transaccion::fkey_account_own) ==
alias_column<als_b>(&Account::id_account))));
}
#
endif
Back
|
FazBrowse Home
|
New Git URL