8
8
using namespace NYdb ;
9
9
using namespace NYdb ::NTable;
10
10
11
+ namespace {
12
+
11
13
class TYdbErrorException : public yexception {
12
14
public:
13
15
TYdbErrorException (const TStatus& status)
@@ -16,18 +18,34 @@ class TYdbErrorException : public yexception {
16
18
TStatus Status;
17
19
};
18
20
19
- static void ThrowOnError (const TStatus& status) {
21
+ void ThrowOnError (const TStatus& status) {
20
22
if (!status.IsSuccess ()) {
21
23
throw TYdbErrorException (status) << status;
22
24
}
23
25
}
24
26
25
- static void PrintStatus (const TStatus& status) {
27
+ void PrintStatus (const TStatus& status) {
26
28
std::cerr << " Status: " << ToString (status.GetStatus ()) << std::endl;
27
29
std::cerr << status.GetIssues ().ToString ();
28
30
}
29
31
30
- static std::string JoinPath (const std::string& basePath, const std::string& path) {
32
+ template <class T >
33
+ std::string OptionalToString (const std::optional<T>& opt) {
34
+ if (opt.has_value ()) {
35
+ return std::to_string (opt.value ());
36
+ }
37
+ return " (NULL)" ;
38
+ }
39
+
40
+ template <>
41
+ std::string OptionalToString<std::string>(const std::optional<std::string>& opt) {
42
+ if (opt.has_value ()) {
43
+ return opt.value ();
44
+ }
45
+ return " (NULL)" ;
46
+ }
47
+
48
+ std::string JoinPath (const std::string& basePath, const std::string& path) {
31
49
if (basePath.empty ()) {
32
50
return path;
33
51
}
@@ -41,7 +59,7 @@ static std::string JoinPath(const std::string& basePath, const std::string& path
41
59
// /////////////////////////////////////////////////////////////////////////////
42
60
43
61
// ! Creates sample tables with CrateTable API.
44
- static void CreateTables (TTableClient client, const std::string& path) {
62
+ void CreateTables (TTableClient client, const std::string& path) {
45
63
ThrowOnError (client.RetryOperationSync ([path](TSession session) {
46
64
auto seriesDesc = TTableBuilder ()
47
65
.AddNullableColumn (" series_id" , EPrimitiveType::Uint64)
@@ -83,7 +101,7 @@ static void CreateTables(TTableClient client, const std::string& path) {
83
101
}
84
102
85
103
// ! Describe existing table.
86
- static void DescribeTable (TTableClient client, const std::string& path, const std::string& name) {
104
+ void DescribeTable (TTableClient client, const std::string& path, const std::string& name) {
87
105
std::optional<TTableDescription> desc;
88
106
89
107
ThrowOnError (client.RetryOperationSync ([path, name, &desc](TSession session) {
@@ -105,7 +123,7 @@ static void DescribeTable(TTableClient client, const std::string& path, const st
105
123
// /////////////////////////////////////////////////////////////////////////////
106
124
107
125
// ! Fills sample tables with data in single parameterized data query.
108
- static TStatus FillTableDataTransaction (TSession session, const std::string& path) {
126
+ TStatus FillTableDataTransaction (TSession session, const std::string& path) {
109
127
auto query = std::format (R"(
110
128
PRAGMA TablePathPrefix("{}");
111
129
@@ -165,7 +183,7 @@ static TStatus FillTableDataTransaction(TSession session, const std::string& pat
165
183
}
166
184
167
185
// ! Shows basic usage of YDB data queries and transactions.
168
- static TStatus SelectSimpleTransaction (TSession session, const std::string& path,
186
+ TStatus SelectSimpleTransaction (TSession session, const std::string& path,
169
187
std::optional<TResultSet>& resultSet)
170
188
{
171
189
auto query = std::format (R"(
@@ -194,7 +212,7 @@ static TStatus SelectSimpleTransaction(TSession session, const std::string& path
194
212
}
195
213
196
214
// ! Shows basic usage of mutating operations.
197
- static TStatus UpsertSimpleTransaction (TSession session, const std::string& path) {
215
+ TStatus UpsertSimpleTransaction (TSession session, const std::string& path) {
198
216
auto query = std::format (R"(
199
217
--!syntax_v1
200
218
PRAGMA TablePathPrefix("{}");
@@ -208,7 +226,7 @@ static TStatus UpsertSimpleTransaction(TSession session, const std::string& path
208
226
}
209
227
210
228
// ! Shows usage of parameters in data queries.
211
- static TStatus SelectWithParamsTransaction (TSession session, const std::string& path,
229
+ TStatus SelectWithParamsTransaction (TSession session, const std::string& path,
212
230
uint64_t seriesId, uint64_t seasonId, std::optional<TResultSet>& resultSet)
213
231
{
214
232
auto query = std::format (R"(
@@ -248,7 +266,7 @@ static TStatus SelectWithParamsTransaction(TSession session, const std::string&
248
266
}
249
267
250
268
// ! Shows usage of prepared queries.
251
- static TStatus PreparedSelectTransaction (TSession session, const std::string& path,
269
+ TStatus PreparedSelectTransaction (TSession session, const std::string& path,
252
270
uint64_t seriesId, uint64_t seasonId, uint64_t episodeId, std::optional<TResultSet>& resultSet)
253
271
{
254
272
// Once prepared, query data is stored in the session and identified by QueryId.
@@ -301,7 +319,7 @@ static TStatus PreparedSelectTransaction(TSession session, const std::string& pa
301
319
}
302
320
303
321
// ! Shows usage of transactions consisting of multiple data queries with client logic between them.
304
- static TStatus MultiStepTransaction (TSession session, const std::string& path, uint64_t seriesId, uint64_t seasonId,
322
+ TStatus MultiStepTransaction (TSession session, const std::string& path, uint64_t seriesId, uint64_t seasonId,
305
323
std::optional<TResultSet>& resultSet)
306
324
{
307
325
auto query1 = std::format (R"(
@@ -394,7 +412,7 @@ static TStatus MultiStepTransaction(TSession session, const std::string& path, u
394
412
// Show usage of explicit Begin/Commit transaction control calls.
395
413
// In most cases it's better to use transaction control settings in ExecuteDataQuery calls instead
396
414
// to avoid additional hops to YDB cluster and allow more efficient execution of queries.
397
- static TStatus ExplicitTclTransaction (TSession session, const std::string& path, const TInstant& airDate) {
415
+ TStatus ExplicitTclTransaction (TSession session, const std::string& path, const TInstant& airDate) {
398
416
auto beginResult = session.BeginTransaction (TTxSettings::SerializableRW ()).GetValueSync ();
399
417
if (!beginResult.IsSuccess ()) {
400
418
return beginResult;
@@ -432,6 +450,8 @@ static TStatus ExplicitTclTransaction(TSession session, const std::string& path,
432
450
return tx.Commit ().GetValueSync ();
433
451
}
434
452
453
+ }
454
+
435
455
// /////////////////////////////////////////////////////////////////////////////
436
456
437
457
void SelectSimple (TTableClient client, const std::string& path) {
@@ -443,9 +463,9 @@ void SelectSimple(TTableClient client, const std::string& path) {
443
463
TResultSetParser parser (*resultSet);
444
464
if (parser.TryNextRow ()) {
445
465
std::cout << " > SelectSimple:" << std::endl << " Series"
446
- << " , Id: " << ToString (parser.ColumnParser (" series_id" ).GetOptionalUint64 ())
447
- << " , Title: " << ToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
448
- << " , Release date: " << ToString (parser.ColumnParser (" release_date" ).GetOptionalString ())
466
+ << " , Id: " << OptionalToString (parser.ColumnParser (" series_id" ).GetOptionalUint64 ())
467
+ << " , Title: " << OptionalToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
468
+ << " , Release date: " << OptionalToString (parser.ColumnParser (" release_date" ).GetOptionalString ())
449
469
<< std::endl;
450
470
}
451
471
}
@@ -465,8 +485,8 @@ void SelectWithParams(TTableClient client, const std::string& path) {
465
485
TResultSetParser parser (*resultSet);
466
486
if (parser.TryNextRow ()) {
467
487
std::cout << " > SelectWithParams:" << std::endl << " Season"
468
- << " , Title: " << ToString (parser.ColumnParser (" season_title" ).GetOptionalUtf8 ())
469
- << " , Series title: " << ToString (parser.ColumnParser (" series_title" ).GetOptionalUtf8 ())
488
+ << " , Title: " << OptionalToString (parser.ColumnParser (" season_title" ).GetOptionalUtf8 ())
489
+ << " , Series title: " << OptionalToString (parser.ColumnParser (" series_title" ).GetOptionalUtf8 ())
470
490
<< std::endl;
471
491
}
472
492
}
@@ -481,8 +501,8 @@ void PreparedSelect(TTableClient client, const std::string& path, uint32_t serie
481
501
if (parser.TryNextRow ()) {
482
502
auto airDate = TInstant::Days (*parser.ColumnParser (" air_date" ).GetOptionalUint64 ());
483
503
484
- std::cout << " > PreparedSelect:" << std::endl << " Episode " << ToString (parser.ColumnParser (" episode_id" ).GetOptionalUint64 ())
485
- << " , Title: " << ToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
504
+ std::cout << " > PreparedSelect:" << std::endl << " Episode " << OptionalToString (parser.ColumnParser (" episode_id" ).GetOptionalUint64 ())
505
+ << " , Title: " << OptionalToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
486
506
<< " , Air date: " << airDate.FormatLocalTime (" %a %b %d, %Y" )
487
507
<< std::endl;
488
508
}
@@ -499,9 +519,9 @@ void MultiStep(TTableClient client, const std::string& path) {
499
519
while (parser.TryNextRow ()) {
500
520
auto airDate = TInstant::Days (*parser.ColumnParser (" air_date" ).GetOptionalUint64 ());
501
521
502
- std::cout << " Episode " << ToString (parser.ColumnParser (" episode_id" ).GetOptionalUint64 ())
503
- << " , Season: " << ToString (parser.ColumnParser (" season_id" ).GetOptionalUint64 ())
504
- << " , Title: " << ToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
522
+ std::cout << " Episode " << OptionalToString (parser.ColumnParser (" episode_id" ).GetOptionalUint64 ())
523
+ << " , Season: " << OptionalToString (parser.ColumnParser (" season_id" ).GetOptionalUint64 ())
524
+ << " , Title: " << OptionalToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
505
525
<< " , Air date: " << airDate.FormatLocalTime (" %a %b %d, %Y" )
506
526
<< std::endl;
507
527
}
@@ -561,10 +581,10 @@ void ScanQuerySelect(TTableClient client, const std::string& path) {
561
581
TResultSetParser parser (rs);
562
582
while (parser.TryNextRow ()) {
563
583
std::cout << " Season"
564
- << " , SeriesId: " << ToString (parser.ColumnParser (" series_id" ).GetOptionalUint64 ())
565
- << " , SeasonId: " << ToString (parser.ColumnParser (" season_id" ).GetOptionalUint64 ())
566
- << " , Title: " << ToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
567
- << " , Air date: " << ToString (parser.ColumnParser (" first_aired" ).GetOptionalString ())
584
+ << " , SeriesId: " << OptionalToString (parser.ColumnParser (" series_id" ).GetOptionalUint64 ())
585
+ << " , SeasonId: " << OptionalToString (parser.ColumnParser (" season_id" ).GetOptionalUint64 ())
586
+ << " , Title: " << OptionalToString (parser.ColumnParser (" title" ).GetOptionalUtf8 ())
587
+ << " , Air date: " << OptionalToString (parser.ColumnParser (" first_aired" ).GetOptionalString ())
568
588
<< std::endl;
569
589
}
570
590
}
0 commit comments