forked from microsoft/winget-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSQLiteWrapper.cpp
More file actions
856 lines (638 loc) · 27.4 KB
/
Copy pathSQLiteWrapper.cpp
File metadata and controls
856 lines (638 loc) · 27.4 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
#include "pch.h"
#include "TestCommon.h"
#include <AppInstallerErrors.h>
#include <winget/SQLiteWrapper.h>
#include <winget/SQLiteStatementBuilder.h>
using namespace AppInstaller::SQLite;
using namespace std::string_literals;
static const char* s_firstColumn = "first";
static const char* s_secondColumn = "second";
static const char* s_tableName = "simpletest";
static const char* s_savepoint = "simplesave";
static const char* s_CreateSimpleTestTableSQL = R"(
CREATE TABLE [main].[simpletest](
[first] INT,
[second] TEXT);
)";
static const char* s_insertToSimpleTestTableSQL = R"(
insert into simpletest (first, second) values (?, ?)
)";
static const char* s_selectFromSimpleTestTableSQL = R"(
select first, second from simpletest
)";
void CreateSimpleTestTable(Connection& connection)
{
Builder::StatementBuilder builder;
builder.CreateTable(s_tableName).Columns({
Builder::ColumnBuilder(s_firstColumn, Builder::Type::Int),
Builder::ColumnBuilder(s_secondColumn, Builder::Type::Text),
});
Statement createTable = builder.Prepare(connection);
REQUIRE_FALSE(createTable.Step());
REQUIRE(createTable.GetState() == Statement::State::Completed);
}
void InsertIntoSimpleTestTable(Connection& connection, int firstVal, const std::string& secondVal)
{
Builder::StatementBuilder builder;
builder.InsertInto(s_tableName).Columns({ s_firstColumn, s_secondColumn }).Values(firstVal, secondVal);
Statement insert = builder.Prepare(connection);
REQUIRE_FALSE(insert.Step());
REQUIRE(insert.GetState() == Statement::State::Completed);
}
void UpdateSimpleTestTable(Connection& connection, int firstVal, const std::string& secondVal)
{
Builder::StatementBuilder update;
update.Update(s_tableName).Set().Column(s_firstColumn).Equals(firstVal).Column(s_secondColumn).Equals(secondVal);
update.Execute(connection);
}
void InsertIntoSimpleTestTableWithNull(Connection& connection, int firstVal)
{
Builder::StatementBuilder builder;
builder.InsertInto(s_tableName).Columns({ s_firstColumn, s_secondColumn }).Values(firstVal, nullptr);
Statement insert = builder.Prepare(connection);
REQUIRE_FALSE(insert.Step());
REQUIRE(insert.GetState() == Statement::State::Completed);
}
void SelectFromSimpleTestTableOnlyOneRow(Connection& connection, int firstVal, const std::string& secondVal)
{
Builder::StatementBuilder builder;
builder.Select({ s_firstColumn, s_secondColumn }).From(s_tableName);
Statement select = builder.Prepare(connection);
REQUIRE(select.Step());
REQUIRE(select.GetState() == Statement::State::HasRow);
int firstRead = select.GetColumn<int>(0);
std::string secondRead = select.GetColumn<std::string>(1);
REQUIRE(firstVal == firstRead);
REQUIRE(secondVal == secondRead);
auto tuple = select.GetRow<int, std::string>();
REQUIRE(firstVal == std::get<0>(tuple));
REQUIRE(secondVal == std::get<1>(tuple));
REQUIRE_FALSE(select.Step());
REQUIRE(select.GetState() == Statement::State::Completed);
select.Reset();
REQUIRE(select.GetState() == Statement::State::Prepared);
REQUIRE(select.Step());
REQUIRE(select.GetState() == Statement::State::HasRow);
}
TEST_CASE("SQLiteWrapperMemoryCreate", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
int firstVal = 1;
std::string secondVal = "test";
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
}
TEST_CASE("SQLiteWrapperFileCreateAndReopen", "[sqlitewrapper]")
{
TestCommon::TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
int firstVal = 1;
std::string secondVal = "test";
// Create the DB and some data
{
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
}
// Reopen the DB and read data
{
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
}
}
TEST_CASE("SQLiteWrapperSavepointRollback", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
int firstVal = 1;
std::string secondVal = "test";
CreateSimpleTestTable(connection);
Savepoint savepoint = Savepoint::Create(connection, "test_savepoint");
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
savepoint.Rollback();
Statement select = Statement::Create(connection, s_selectFromSimpleTestTableSQL);
REQUIRE(!select.Step());
REQUIRE(select.GetState() == Statement::State::Completed);
}
TEST_CASE("SQLiteWrapperSavepointRollbackOnDestruct", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
int firstVal = 1;
std::string secondVal = "test";
CreateSimpleTestTable(connection);
{
Savepoint savepoint = Savepoint::Create(connection, "test_savepoint");
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
}
Statement select = Statement::Create(connection, s_selectFromSimpleTestTableSQL);
REQUIRE(!select.Step());
REQUIRE(select.GetState() == Statement::State::Completed);
}
TEST_CASE("SQLiteWrapperSavepointCommit", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
int firstVal = 1;
std::string secondVal = "test";
CreateSimpleTestTable(connection);
{
Savepoint savepoint = Savepoint::Create(connection, "test_savepoint");
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
savepoint.Commit();
}
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
}
TEST_CASE("SQLiteWrapperSavepointReuse", "[sqlitewrapper]")
{
TestCommon::TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
int firstVal = 1;
std::string secondVal = "test";
// Create the DB and some data
{
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
}
// Reopen the DB and update with a single savepoint
{
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
Savepoint savepoint = Savepoint::Create(connection, s_savepoint);
firstVal = 2;
secondVal = "test2";
UpdateSimpleTestTable(connection, firstVal, secondVal);
savepoint.Commit();
}
{
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
}
// Reopen the DB and update with a multiple savepoint
{
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
{
Savepoint savepoint = Savepoint::Create(connection, s_savepoint);
firstVal = 3;
secondVal = "test3";
UpdateSimpleTestTable(connection, firstVal, secondVal);
}
{
Savepoint savepoint = Savepoint::Create(connection, s_savepoint);
firstVal = 4;
secondVal = "test4";
UpdateSimpleTestTable(connection, firstVal, secondVal);
savepoint.Commit();
}
}
{
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
}
}
TEST_CASE("SQLiteWrapper_EscapeStringForLike", "[sqlitewrapper]")
{
std::string escape(EscapeCharForLike);
std::string input = "test";
std::string output = EscapeStringForLike(input);
REQUIRE(input == output);
input = EscapeCharForLike;
output = EscapeStringForLike(input);
REQUIRE((input + input) == output);
input = "%";
output = EscapeStringForLike(input);
REQUIRE((escape + input) == output);
input = "_";
output = EscapeStringForLike(input);
REQUIRE((escape + input) == output);
input = "%_A_%";
std::string expected = escape + "%" + escape + "_A" + escape + "_" + escape + "%";
output = EscapeStringForLike(input);
REQUIRE(expected == output);
}
TEST_CASE("SQLiteWrapper_BindWithEmbeddedNull", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
int firstVal = 1;
std::string secondVal = "test";
secondVal[1] = '\0';
REQUIRE_THROWS_HR(InsertIntoSimpleTestTable(connection, firstVal, secondVal), APPINSTALLER_CLI_ERROR_BIND_WITH_EMBEDDED_NULL);
}
TEST_CASE("SQLiteWrapper_PrepareFailure", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
Builder::StatementBuilder builder;
builder.Select({ s_firstColumn, s_secondColumn }).From(std::string{ s_tableName } + "2").Where(s_firstColumn).Equals(2);
REQUIRE_THROWS_HR(builder.Prepare(connection), MAKE_HRESULT(SEVERITY_ERROR, FACILITY_SQLITE, SQLITE_ERROR));
}
TEST_CASE("SQLiteWrapper_BusyTimeout_None", "[sqlitewrapper]")
{
TestCommon::TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
wil::unique_event busy, done;
busy.create();
done.create();
std::thread busyThread([&]()
{
Connection threadConnection = Connection::Create(tempFile, Connection::OpenDisposition::Create);
Statement threadStatement = Statement::Create(threadConnection, "BEGIN EXCLUSIVE TRANSACTION");
threadStatement.Execute();
busy.SetEvent();
done.wait(500);
});
busyThread.detach();
busy.wait(500);
Connection testConnection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
testConnection.SetBusyTimeout(0ms);
Statement testStatement = Statement::Create(testConnection, "BEGIN EXCLUSIVE TRANSACTION");
REQUIRE_THROWS_HR(testStatement.Execute(), MAKE_HRESULT(SEVERITY_ERROR, FACILITY_SQLITE, SQLITE_BUSY));
done.SetEvent();
}
TEST_CASE("SQLiteWrapper_BusyTimeout_Some", "[sqlitewrapper]")
{
TestCommon::TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
wil::unique_event busy, ready, done;
busy.create();
ready.create();
done.create();
std::thread busyThread([&]()
{
Connection threadConnection = Connection::Create(tempFile, Connection::OpenDisposition::Create);
Statement threadBeginStatement = Statement::Create(threadConnection, "BEGIN EXCLUSIVE TRANSACTION");
Statement threadCommitStatement = Statement::Create(threadConnection, "COMMIT");
threadBeginStatement.Execute();
busy.SetEvent();
ready.wait(500);
done.wait(100);
threadCommitStatement.Execute();
});
busyThread.detach();
busy.wait(500);
Connection testConnection = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
testConnection.SetBusyTimeout(500ms);
Statement testStatement = Statement::Create(testConnection, "BEGIN EXCLUSIVE TRANSACTION");
ready.SetEvent();
testStatement.Execute();
done.SetEvent();
}
TEST_CASE("SQLiteWrapper_CloseConnectionOnError", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
Builder::StatementBuilder builder;
builder.CreateTable(s_tableName).Columns({
Builder::ColumnBuilder(s_firstColumn, Builder::Type::Int),
Builder::ColumnBuilder(s_secondColumn, Builder::Type::Text),
});
Statement createTable = builder.Prepare(connection);
REQUIRE_FALSE(createTable.Step());
REQUIRE(createTable.GetState() == Statement::State::Completed);
createTable.Reset();
REQUIRE_THROWS(createTable.Step(true));
// Do anything that needs the connection
REQUIRE_THROWS_HR(connection.GetLastInsertRowID(), APPINSTALLER_CLI_ERROR_SQLITE_CONNECTION_TERMINATED);
}
TEST_CASE("SQLBuilder_SimpleSelectBind", "[sqlbuilder]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
InsertIntoSimpleTestTable(connection, 1, "1");
InsertIntoSimpleTestTable(connection, 2, "2");
InsertIntoSimpleTestTable(connection, 3, "3");
Builder::StatementBuilder builder;
builder.Select({ s_firstColumn, s_secondColumn }).From(s_tableName).Where(s_firstColumn).Equals(2);
auto statement = builder.Prepare(connection);
REQUIRE(statement.Step());
REQUIRE(statement.GetColumn<int>(0) == 2);
REQUIRE(statement.GetColumn<std::string>(0) == "2");
REQUIRE(!statement.Step());
Builder::StatementBuilder buildCount;
buildCount.Select(Builder::RowCount).From(s_tableName);
auto rows = buildCount.Prepare(connection);
REQUIRE(rows.Step());
REQUIRE(rows.GetColumn<int>(0) == 3);
REQUIRE(!rows.Step());
}
TEST_CASE("SQLBuilder_SimpleSelectUnbound", "[sqlbuilder]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
InsertIntoSimpleTestTable(connection, 1, "1");
InsertIntoSimpleTestTable(connection, 2, "2");
InsertIntoSimpleTestTable(connection, 3, "3");
Builder::StatementBuilder builder;
builder.Select({ s_firstColumn, s_secondColumn }).From(s_tableName).Where(s_firstColumn).Equals(Builder::Unbound);
auto statement = builder.Prepare(connection);
statement.Bind(1, 2);
REQUIRE(statement.Step());
REQUIRE(statement.GetColumn<int>(0) == 2);
REQUIRE(statement.GetColumn<std::string>(0) == "2");
REQUIRE(!statement.Step());
}
TEST_CASE("SQLBuilder_SimpleSelectNull", "[sqlbuilder]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
InsertIntoSimpleTestTable(connection, 1, "1");
InsertIntoSimpleTestTable(connection, 2, "2");
InsertIntoSimpleTestTableWithNull(connection, 3);
Builder::StatementBuilder builder;
builder.Select({ s_firstColumn, s_secondColumn }).From(s_tableName).Where(s_secondColumn).IsNull();
auto statement = builder.Prepare(connection);
REQUIRE(statement.Step());
REQUIRE(statement.GetColumn<int>(0) == 3);
REQUIRE(statement.GetColumnIsNull(1));
REQUIRE(!statement.Step());
}
TEST_CASE("SQLBuilder_SimpleSelectOptional", "[sqlbuilder]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
InsertIntoSimpleTestTable(connection, 1, "1");
InsertIntoSimpleTestTable(connection, 2, "2");
InsertIntoSimpleTestTableWithNull(connection, 3);
std::optional<std::string> secondValue;
{
Builder::StatementBuilder builder;
builder.Select({ s_firstColumn, s_secondColumn }).From(s_tableName).Where(s_secondColumn).Equals(secondValue);
auto statement = builder.Prepare(connection);
REQUIRE(statement.Step());
REQUIRE(statement.GetColumn<int>(0) == 3);
REQUIRE(statement.GetColumnIsNull(1));
REQUIRE(!statement.Step());
}
{
secondValue = "2";
Builder::StatementBuilder builder;
builder.Select({ s_firstColumn, s_secondColumn }).From(s_tableName).Where(s_secondColumn).Equals(secondValue);
auto statement = builder.Prepare(connection);
REQUIRE(statement.Step());
REQUIRE(statement.GetColumn<int>(0) == 2);
REQUIRE(statement.GetColumn<std::string>(1) == "2");
REQUIRE(!statement.Step());
}
}
TEST_CASE("SQLBuilder_Update", "[sqlbuilder]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
CreateSimpleTestTable(connection);
int firstVal = 1;
std::string secondVal = "test";
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
firstVal = 2;
secondVal = "testing";
UpdateSimpleTestTable(connection, firstVal, secondVal);
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
}
TEST_CASE("SQLBuilder_CaseInsensitive", "[sqlbuilder")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
Builder::StatementBuilder createTable;
createTable.CreateTable(s_tableName).Columns({
Builder::ColumnBuilder(s_firstColumn, Builder::Type::Text).CollateNoCase()
});
createTable.Execute(connection);
std::string upperCaseVal = "TEST";
std::string lowerCaseVal = "test";
{
INFO("Insert initial value");
Builder::StatementBuilder builder;
builder.InsertInto(s_tableName)
.Columns({ s_firstColumn })
.Values(upperCaseVal);
builder.Execute(connection);
}
{
INFO("Retrieve using case-insensitive value");
Builder::StatementBuilder builder;
builder.Select({ s_firstColumn }).From(s_tableName).Where(s_firstColumn).Equals(lowerCaseVal);
auto statement = builder.Prepare(connection);
REQUIRE(statement.Step());
}
}
TEST_CASE("SQLBuilder_CreateTable", "[sqlbuilder]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
int testRun = GENERATE(0, 1, 2, 3, 4, 5, 6, 7);
bool notNull = ((testRun & 1) != 0);
bool unique = ((testRun & 2) != 0);
bool pk = ((testRun & 4) != 0);
CAPTURE(notNull, unique, pk);
Builder::StatementBuilder createTable;
createTable.CreateTable(s_tableName).Columns({
Builder::ColumnBuilder(s_firstColumn, Builder::Type::Int).NotNull(notNull).Unique(unique).PrimaryKey(pk)
});
createTable.Execute(connection);
Builder::StatementBuilder insertBuilder;
insertBuilder.InsertInto(s_tableName).Columns(s_firstColumn).Values(Builder::Unbound);
Statement insertStatement = insertBuilder.Prepare(connection);
{
INFO("Insert NULL");
insertStatement.Bind(1, nullptr);
if (notNull)
{
REQUIRE_THROWS_HR(insertStatement.Execute(), MAKE_HRESULT(SEVERITY_ERROR, FACILITY_SQLITE, SQLITE_CONSTRAINT_NOTNULL));
}
else
{
insertStatement.Execute();
}
}
{
INFO("Insert unique values");
insertStatement.Reset();
insertStatement.Bind(1, 1);
insertStatement.Execute();
insertStatement.Reset();
insertStatement.Bind(1, 2);
insertStatement.Execute();
}
{
INFO("Insert duplicate values");
insertStatement.Reset();
insertStatement.Bind(1, 1);
if (unique || pk)
{
HRESULT expectedHR = S_OK;
if (pk)
{
expectedHR = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_SQLITE, SQLITE_CONSTRAINT_PRIMARYKEY);
}
else
{
expectedHR = MAKE_HRESULT(SEVERITY_ERROR, FACILITY_SQLITE, SQLITE_CONSTRAINT_UNIQUE);
}
REQUIRE_THROWS_HR(insertStatement.Execute(), expectedHR);
}
else
{
insertStatement.Execute();
}
}
}
TEST_CASE("SQLBuilder_InsertValueBinding", "[sqlbuilder]")
{
char const* const columns[] = { "a", "b", "c", "d", "e", "f" };
TestCommon::TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::Create);
{
INFO("Create table");
Builder::StatementBuilder createTable;
createTable.CreateTable(s_tableName).BeginColumns();
for (const auto c : columns)
{
createTable.Column(Builder::ColumnBuilder(c, Builder::Type::Int));
}
createTable.EndColumns();
createTable.Execute(connection);
}
{
INFO("Insert values");
Builder::StatementBuilder insertBuilder;
insertBuilder.InsertInto(s_tableName).BeginColumns();
for (const auto c : columns)
{
insertBuilder.Column(c);
}
insertBuilder.EndColumns().Values(0, 1, 2, 3, 4, 5);
insertBuilder.Execute(connection);
}
{
INFO("Insert values");
Builder::StatementBuilder insertBuilder;
insertBuilder.InsertInto(s_tableName).BeginColumns();
for (const auto c : columns)
{
insertBuilder.Column(c);
}
insertBuilder.EndColumns().BeginValues();
insertBuilder.Value(5);
insertBuilder.Value(nullptr);
insertBuilder.Value(3);
insertBuilder.Value(std::optional<int>{});
insertBuilder.Value(std::optional<int>{ 1 });
insertBuilder.Value(Builder::Unbound);
insertBuilder.EndValues();
insertBuilder.Execute(connection);
}
{
INFO("Select values");
Builder::StatementBuilder selectBuilder;
selectBuilder.Select();
for (const auto c : columns)
{
selectBuilder.Column(c);
}
selectBuilder.From(s_tableName);
Statement select = selectBuilder.Prepare(connection);
REQUIRE(select.Step());
for (int i = 0; i < ARRAYSIZE(columns); ++i)
{
REQUIRE(i == select.GetColumn<int>(i));
}
REQUIRE(select.Step());
for (int i = 0; i < ARRAYSIZE(columns); ++i)
{
if (i & 1)
{
REQUIRE(select.GetColumnIsNull(i));
}
else
{
REQUIRE((5 - i) == select.GetColumn<int>(i));
}
}
REQUIRE(!select.Step());
}
}
TEST_CASE("SQLiteWrapperTransactionRollback", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
int firstVal = 1;
std::string secondVal = "test";
CreateSimpleTestTable(connection);
Transaction transaction = Transaction::Create(connection, "test_transaction", false);
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
transaction.Rollback();
Statement select = Statement::Create(connection, s_selectFromSimpleTestTableSQL);
REQUIRE(!select.Step());
REQUIRE(select.GetState() == Statement::State::Completed);
}
TEST_CASE("SQLiteWrapperTransactionRollbackOnDestruct", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
int firstVal = 1;
std::string secondVal = "test";
CreateSimpleTestTable(connection);
{
Transaction transaction = Transaction::Create(connection, "test_transaction", false);
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
}
Statement select = Statement::Create(connection, s_selectFromSimpleTestTableSQL);
REQUIRE(!select.Step());
REQUIRE(select.GetState() == Statement::State::Completed);
}
TEST_CASE("SQLiteWrapperTransactionCommit", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
int firstVal = 1;
std::string secondVal = "test";
CreateSimpleTestTable(connection);
{
Transaction transaction = Transaction::Create(connection, "test_transaction", false);
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
transaction.Commit();
}
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
}
TEST_CASE("SQLiteWrapperTransactionImmediate", "[sqlitewrapper]")
{
Connection connection = Connection::Create(SQLITE_MEMORY_DB_CONNECTION_TARGET, Connection::OpenDisposition::Create);
int firstVal = 1;
std::string secondVal = "test";
CreateSimpleTestTable(connection);
{
Transaction transaction = Transaction::Create(connection, "test_transaction", true);
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
transaction.Commit();
}
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
}
TEST_CASE("SQLiteWrapperTransactionWriteConflict", "[sqlitewrapper]")
{
TestCommon::TempFile tempFile{ "repolibtest_tempdb"s, ".db"s };
INFO("Using temporary file named: " << tempFile.GetPath());
Connection connection = Connection::Create(tempFile, Connection::OpenDisposition::Create);
connection.SetJournalMode("WAL");
int firstVal = 1;
std::string secondVal = "test";
CreateSimpleTestTable(connection);
Connection connection2 = Connection::Create(tempFile, Connection::OpenDisposition::ReadWrite);
std::chrono::milliseconds busyWait = 250ms;
connection2.SetBusyTimeout(busyWait);
{
Transaction transaction = Transaction::Create(connection, "test_transaction", true);
InsertIntoSimpleTestTable(connection, firstVal, secondVal);
// Start second transaction
std::chrono::system_clock::time_point start = std::chrono::system_clock::now();
std::chrono::system_clock::time_point end = start;
try
{
Transaction transaction2 = Transaction::Create(connection2, "test_transaction2", true);
}
catch (...)
{
end = std::chrono::system_clock::now();
}
std::chrono::milliseconds duration = std::chrono::duration_cast<std::chrono::milliseconds>(end - start);
REQUIRE(duration >= busyWait);
transaction.Commit();
Transaction transaction2 = Transaction::Create(connection2, "test_transaction2", true);
InsertIntoSimpleTestTable(connection2, firstVal, secondVal);
}
SelectFromSimpleTestTableOnlyOneRow(connection, firstVal, secondVal);
}