8
8
using namespace NYdb ;
9
9
using namespace NYdb ::NTable;
10
10
11
- class TYdbErrorException : public yexception {
12
- public:
13
- TYdbErrorException (const TStatus& status)
14
- : Status(status) {}
15
-
16
- TStatus Status;
17
- };
18
-
19
- static void ThrowOnError (const TStatus& status) {
11
+ void ThrowOnError (const TStatus& status) {
20
12
if (!status.IsSuccess ()) {
21
13
throw TYdbErrorException (status) << status;
22
14
}
23
15
}
24
16
25
- static void PrintStatus (const TStatus& status) {
17
+ void PrintStatus (const TStatus& status) {
26
18
std::cerr << " Status: " << ToString (status.GetStatus ()) << std::endl;
27
19
status.GetIssues ().PrintTo (std::cerr);
28
20
}
@@ -41,7 +33,7 @@ static std::string JoinPath(const std::string& basePath, const std::string& path
41
33
// /////////////////////////////////////////////////////////////////////////////
42
34
43
35
// ! Creates sample tables with CrateTable API.
44
- static void CreateTables (TTableClient client, const std::string& path) {
36
+ void CreateTables (TTableClient client, const std::string& path) {
45
37
ThrowOnError (client.RetryOperationSync ([path](TSession session) {
46
38
auto seriesDesc = TTableBuilder ()
47
39
.AddNullableColumn (" series_id" , EPrimitiveType::Uint64)
@@ -83,7 +75,7 @@ static void CreateTables(TTableClient client, const std::string& path) {
83
75
}
84
76
85
77
// ! Describe existing table.
86
- static void DescribeTable (TTableClient client, const std::string& path, const std::string& name) {
78
+ void DescribeTable (TTableClient client, const std::string& path, const std::string& name) {
87
79
std::optional<TTableDescription> desc;
88
80
std::string result;
89
81
ThrowOnError (client.RetryOperationSync ([path, name, &desc](TSession session) {
@@ -104,7 +96,7 @@ static void DescribeTable(TTableClient client, const std::string& path, const st
104
96
// /////////////////////////////////////////////////////////////////////////////
105
97
106
98
// ! Fills sample tables with data in single parameterized data query.
107
- static TStatus FillTableDataTransaction (TSession session, const std::string& path) {
99
+ TStatus FillTableDataTransaction (TSession session, const std::string& path) {
108
100
auto query = std::format (R"(
109
101
PRAGMA TablePathPrefix("{}");
110
102
@@ -443,10 +435,9 @@ std::string SelectSimple(TTableClient client, const std::string& path) {
443
435
std::cout << std::format (" > SelectSimple:\n Series, Id: {}, Title: {}, Release date: {}\n "
444
436
, ToString (parser.ColumnParser (" series_id" ).GetOptionalUint64 ())
445
437
, ToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
446
- , ToString (parser.ColumnParser (" release_date" ).GetOptionalString ()));
447
- return FormatResultSetJson (resultSet.value (), EBinaryStringEncoding::Unicode);
438
+ , ToString (parser.ColumnParser (" release_date" ).GetOptionalString ()));
448
439
}
449
- return " " ;
440
+ return FormatResultSetJson (resultSet. value (), EBinaryStringEncoding::Unicode);
450
441
}
451
442
452
443
void UpsertSimple (TTableClient client, const std::string& path) {
@@ -464,50 +455,47 @@ std::string SelectWithParams(TTableClient client, const std::string& path) {
464
455
465
456
TResultSetParser parser (*resultSet);
466
457
if (parser.TryNextRow ()) {
467
- return std::format (" > SelectWithParams:\n Season, Title: {}, Series title: {}\n "
458
+ std::cout << std::format (" > SelectWithParams:\n Season, Title: {}, Series title: {}\n "
468
459
, ToString (parser.ColumnParser (" season_title" ).GetOptionalUtf8 ())
469
- , ToString (parser.ColumnParser (" series_title" ).GetOptionalUtf8 ()));
460
+ , ToString (parser.ColumnParser (" series_title" ).GetOptionalUtf8 ()));
470
461
}
471
- return " " ;
462
+ return FormatResultSetJson (resultSet. value (), EBinaryStringEncoding::Unicode);
472
463
}
473
464
474
465
std::string PreparedSelect (TTableClient client, const std::string& path, ui32 seriesId, ui32 seasonId, ui32 episodeId) {
475
466
std::optional<TResultSet> resultSet;
476
- std::string result;
477
467
ThrowOnError (client.RetryOperationSync ([path, seriesId, seasonId, episodeId, &resultSet](TSession session) {
478
468
return PreparedSelectTransaction (session, path, seriesId, seasonId, episodeId, resultSet);
479
469
}));
480
470
481
471
TResultSetParser parser (*resultSet);
482
472
if (parser.TryNextRow ()) {
483
473
auto airDate = TInstant::Days (*parser.ColumnParser (" air_date" ).GetOptionalUint64 ());
484
-
485
- return std::format (" > PreparedSelect:\n Episode {}, Title: {}, Air date: {}\n "
474
+ std::cout << std::format (" > PreparedSelect:\n Episode {}, Title: {}, Air date: {}\n "
486
475
, ToString (parser.ColumnParser (" episode_id" ).GetOptionalUint64 ())
487
476
, ToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
488
- , airDate.FormatLocalTime (" %a %b %d, %Y" ));
477
+ , airDate.FormatLocalTime (" %a %b %d, %Y" ));
489
478
}
490
- return " " ;
479
+ return FormatResultSetJson (resultSet. value (), EBinaryStringEncoding::Unicode);
491
480
}
492
481
493
482
std::string MultiStep (TTableClient client, const std::string& path) {
494
483
std::optional<TResultSet> resultSet;
495
- std::string result;
496
484
ThrowOnError (client.RetryOperationSync ([path, &resultSet](TSession session) {
497
485
return MultiStepTransaction (session, path, 2 , 5 , resultSet);
498
486
}));
499
487
500
488
TResultSetParser parser (*resultSet);
501
- result = " > MultiStep:\n " ;
489
+ std::cout << " > MultiStep:\n " ;
502
490
while (parser.TryNextRow ()) {
503
491
auto airDate = TInstant::Days (*parser.ColumnParser (" air_date" ).GetOptionalUint64 ());
504
- result += std::format (" Episode {}, Season: {}, Title: {}, Air date: {}\n "
492
+ std::cout << std::format (" Episode {}, Season: {}, Title: {}, Air date: {}\n "
505
493
, ToString (parser.ColumnParser (" episode_id" ).GetOptionalUint64 ())
506
494
, ToString (parser.ColumnParser (" season_id" ).GetOptionalUint64 ())
507
495
, ToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
508
496
, airDate.FormatLocalTime (" %a %b %d, %Y" ));
509
497
}
510
- return result ;
498
+ return FormatResultSetJson (resultSet. value (), EBinaryStringEncoding::Unicode) ;
511
499
}
512
500
513
501
void ExplicitTcl (TTableClient client, const std::string& path) {
@@ -517,7 +505,6 @@ void ExplicitTcl(TTableClient client, const std::string& path) {
517
505
}
518
506
519
507
std::string ScanQuerySelect (TTableClient client, const std::string& path) {
520
- std::string result;
521
508
auto query = std::format (R"(
522
509
--!syntax_v1
523
510
PRAGMA TablePathPrefix("{}");
@@ -546,7 +533,7 @@ std::string ScanQuerySelect(TTableClient client, const std::string& path) {
546
533
}
547
534
548
535
bool eos = false ;
549
- result = " > ScanQuerySelect:\n " ;
536
+ std::cout << " > ScanQuerySelect:\n " ;
550
537
551
538
while (!eos) {
552
539
auto streamPart = resultScanQuery.ReadNext ().ExtractValueSync ();
@@ -565,51 +552,14 @@ std::string ScanQuerySelect(TTableClient client, const std::string& path) {
565
552
566
553
TResultSetParser parser (rs);
567
554
while (parser.TryNextRow ()) {
568
- result += std::format (" Season, SeriesId: {}, SeasonId: {}, Title: {}, Air date: {}\n "
555
+ std::cout << std::format (" Season, SeriesId: {}, SeasonId: {}, Title: {}, Air date: {}\n "
569
556
, ToString (parser.ColumnParser (" series_id" ).GetOptionalUint64 ())
570
557
, ToString (parser.ColumnParser (" season_id" ).GetOptionalUint64 ())
571
558
, ToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
572
559
, ToString (parser.ColumnParser (" first_aired" ).GetOptionalString ()));
573
560
}
561
+ return FormatResultSetJson (rs, EBinaryStringEncoding::Unicode);
574
562
}
575
563
}
576
- return result;
577
- }
578
-
579
- // /////////////////////////////////////////////////////////////////////////////
580
-
581
- bool Run (const TDriver& driver, const std::string& path) {
582
- TTableClient client (driver);
583
- try {
584
- CreateTables (client, path);
585
-
586
- DescribeTable (client, path, " series" );
587
-
588
- ThrowOnError (client.RetryOperationSync ([path](TSession session) {
589
- return FillTableDataTransaction (session, path);
590
- }));
591
-
592
- std::string expectedResultSelectSimple = " {\" series_id\" :1,\" title\" :\" IT Crowd\" ,\" release_date\" :\" 2006-02-03\" }" ;
593
- std::string resultSelectSimple = SelectSimple (client, path);
594
- UpsertSimple (client, path);
595
-
596
- SelectWithParams (client, path);
597
-
598
- PreparedSelect (client, path, 2 , 3 , 7 );
599
- PreparedSelect (client, path, 2 , 3 , 8 );
600
-
601
- MultiStep (client, path);
602
-
603
- ExplicitTcl (client, path);
604
-
605
- PreparedSelect (client, path, 2 , 6 , 1 );
606
-
607
- ScanQuerySelect (client, path);
608
- }
609
- catch (const TYdbErrorException& e) {
610
- std::cerr << " Execution failed due to fatal error:" << std::endl;
611
- PrintStatus (e.Status );
612
- return false ;
613
- }
614
- return true ;
564
+ return " " ;
615
565
}
0 commit comments