22
22
FillDataQuery = """PRAGMA TablePathPrefix("{}");
23
23
24
24
DECLARE $seriesData AS List<Struct<
25
- series_id: Uint64 ,
25
+ series_id: Int64 ,
26
26
title: Utf8,
27
27
series_info: Utf8,
28
28
release_date: Date>>;
29
29
30
30
DECLARE $seasonsData AS List<Struct<
31
- series_id: Uint64 ,
32
- season_id: Uint64 ,
31
+ series_id: Int64 ,
32
+ season_id: Int64 ,
33
33
title: Utf8,
34
34
first_aired: Date,
35
35
last_aired: Date>>;
36
36
37
37
DECLARE $episodesData AS List<Struct<
38
- series_id: Uint64 ,
39
- season_id: Uint64 ,
40
- episode_id: Uint64 ,
38
+ series_id: Int64 ,
39
+ season_id: Int64 ,
40
+ episode_id: Int64 ,
41
41
title: Utf8,
42
42
air_date: Date>>;
43
43
46
46
series_id,
47
47
title,
48
48
series_info,
49
- CAST(release_date AS Uint64) AS release_date
49
+ release_date
50
50
FROM AS_TABLE($seriesData);
51
51
52
52
REPLACE INTO seasons
53
53
SELECT
54
54
series_id,
55
55
season_id,
56
56
title,
57
- CAST(first_aired AS Uint64) AS first_aired,
58
- CAST(last_aired AS Uint64) AS last_aired
57
+ first_aired,
58
+ last_aired
59
59
FROM AS_TABLE($seasonsData);
60
60
61
61
REPLACE INTO episodes
64
64
season_id,
65
65
episode_id,
66
66
title,
67
- CAST(air_date AS Uint64) AS air_date
67
+ air_date
68
68
FROM AS_TABLE($episodesData);
69
69
"""
70
70
@@ -76,9 +76,9 @@ def fill_tables_with_data(pool, path):
76
76
77
77
def callee (session ):
78
78
79
- prepared_query = FillDataQuery .format (path )
79
+ query = FillDataQuery .format (path )
80
80
with session .transaction (ydb .QuerySerializableReadWrite ()).execute (
81
- prepared_query ,
81
+ query ,
82
82
{
83
83
"$seriesData" : (basic_example_data .get_series_data (), basic_example_data .get_series_data_type ()),
84
84
"$seasonsData" : (basic_example_data .get_seasons_data (), basic_example_data .get_seasons_data_type ()),
@@ -93,40 +93,31 @@ def callee(session):
93
93
94
94
def select_simple (pool , path ):
95
95
print ("\n Check series table..." )
96
+ result_sets = pool .execute_with_retries (
97
+ """
98
+ PRAGMA TablePathPrefix("{}");
99
+ SELECT
100
+ series_id,
101
+ title,
102
+ release_date
103
+ FROM series
104
+ WHERE series_id = 1;
105
+ """ .format (
106
+ path
107
+ ),
108
+ )
109
+ first_set = result_sets [0 ]
110
+ for row in first_set .rows :
111
+ print (
112
+ "series, id: " ,
113
+ row .series_id ,
114
+ ", title: " ,
115
+ row .title ,
116
+ ", release date: " ,
117
+ row .release_date ,
118
+ )
96
119
97
- def callee (session ):
98
- # new transaction in serializable read write mode
99
- # if query successfully completed you will get result sets.
100
- # otherwise exception will be raised
101
- with session .transaction (ydb .QuerySerializableReadWrite ()).execute (
102
- """
103
- PRAGMA TablePathPrefix("{}");
104
- $format = DateTime::Format("%Y-%m-%d");
105
- SELECT
106
- series_id,
107
- title,
108
- $format(DateTime::FromSeconds(CAST(DateTime::ToSeconds(DateTime::IntervalFromDays(CAST(release_date AS Int16))) AS Uint32))) AS release_date
109
- FROM series
110
- WHERE series_id = 1;
111
- """ .format (
112
- path
113
- ),
114
- commit_tx = True ,
115
- ) as result_sets :
116
- first_set = next (result_sets )
117
- for row in first_set .rows :
118
- print (
119
- "series, id: " ,
120
- row .series_id ,
121
- ", title: " ,
122
- row .title ,
123
- ", release date: " ,
124
- row .release_date ,
125
- )
126
-
127
- return first_set
128
-
129
- return pool .retry_operation_sync (callee )
120
+ return first_set
130
121
131
122
132
123
def upsert_simple (pool , path ):
@@ -151,10 +142,9 @@ def select_with_parameters(pool, path, series_id, season_id, episode_id):
151
142
def callee (session ):
152
143
query = """
153
144
PRAGMA TablePathPrefix("{}");
154
- $format = DateTime::Format("%Y-%m-%d");
155
145
SELECT
156
146
title,
157
- $format(DateTime::FromSeconds(CAST(DateTime::ToSeconds(DateTime::IntervalFromDays(CAST(air_date AS Int16))) AS Uint32))) AS air_date
147
+ air_date
158
148
FROM episodes
159
149
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
160
150
""" .format (
@@ -164,10 +154,10 @@ def callee(session):
164
154
with session .transaction (ydb .QuerySerializableReadWrite ()).execute (
165
155
query ,
166
156
{
167
- "$seriesId" : ( series_id , ydb . PrimitiveType . Uint64 ),
168
- "$seasonId" : (season_id , ydb .PrimitiveType .Uint64 ), # could be defined via tuple
157
+ "$seriesId" : series_id , # could be defined implicit
158
+ "$seasonId" : (season_id , ydb .PrimitiveType .Int64 ), # could be defined via tuple
169
159
"$episodeId" : ydb .TypedValue (
170
- episode_id , ydb .PrimitiveType .Uint64
160
+ episode_id , ydb .PrimitiveType .Int64
171
161
), # could be defined via special class
172
162
},
173
163
commit_tx = True ,
@@ -186,12 +176,12 @@ def callee(session):
186
176
# In most cases it's better to use transaction control settings in session.transaction
187
177
# calls instead to avoid additional hops to YDB cluster and allow more efficient
188
178
# execution of queries.
189
- def explicit_tcl (pool , path , series_id , season_id , episode_id ):
179
+ def explicit_transaction_control (pool , path , series_id , season_id , episode_id ):
190
180
def callee (session ):
191
181
query = """
192
182
PRAGMA TablePathPrefix("{}");
193
183
UPDATE episodes
194
- SET air_date = CAST( CurrentUtcDate() AS Uint64 )
184
+ SET air_date = CurrentUtcDate()
195
185
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
196
186
""" .format (
197
187
path
@@ -205,9 +195,9 @@ def callee(session):
205
195
with tx .execute (
206
196
query ,
207
197
{
208
- "$seriesId" : (series_id , ydb .PrimitiveType .Uint64 ),
209
- "$seasonId" : (season_id , ydb .PrimitiveType .Uint64 ),
210
- "$episodeId" : (episode_id , ydb .PrimitiveType .Uint64 ),
198
+ "$seriesId" : (series_id , ydb .PrimitiveType .Int64 ),
199
+ "$seasonId" : (season_id , ydb .PrimitiveType .Int64 ),
200
+ "$episodeId" : (episode_id , ydb .PrimitiveType .Int64 ),
211
201
},
212
202
) as _ :
213
203
pass
@@ -231,10 +221,10 @@ def create_tables(pool, path):
231
221
"""
232
222
PRAGMA TablePathPrefix("{}");
233
223
CREATE table `series` (
234
- `series_id` Uint64 ,
224
+ `series_id` Int64 ,
235
225
`title` Utf8,
236
226
`series_info` Utf8,
237
- `release_date` Uint64 ,
227
+ `release_date` Date ,
238
228
PRIMARY KEY (`series_id`)
239
229
)
240
230
""" .format (
@@ -247,11 +237,11 @@ def create_tables(pool, path):
247
237
"""
248
238
PRAGMA TablePathPrefix("{}");
249
239
CREATE table `seasons` (
250
- `series_id` Uint64 ,
251
- `season_id` Uint64 ,
240
+ `series_id` Int64 ,
241
+ `season_id` Int64 ,
252
242
`title` Utf8,
253
- `first_aired` Uint64 ,
254
- `last_aired` Uint64 ,
243
+ `first_aired` Date ,
244
+ `last_aired` Date ,
255
245
PRIMARY KEY (`series_id`, `season_id`)
256
246
)
257
247
""" .format (
@@ -264,11 +254,11 @@ def create_tables(pool, path):
264
254
"""
265
255
PRAGMA TablePathPrefix("{}");
266
256
CREATE table `episodes` (
267
- `series_id` Uint64 ,
268
- `season_id` Uint64 ,
269
- `episode_id` Uint64 ,
257
+ `series_id` Int64 ,
258
+ `season_id` Int64 ,
259
+ `episode_id` Int64 ,
270
260
`title` Utf8,
271
- `air_date` Uint64 ,
261
+ `air_date` Date ,
272
262
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
273
263
)
274
264
""" .format (
@@ -328,5 +318,5 @@ def run(endpoint, database, path):
328
318
select_with_parameters (pool , full_path , 2 , 3 , 7 )
329
319
select_with_parameters (pool , full_path , 2 , 3 , 8 )
330
320
331
- explicit_tcl (pool , full_path , 2 , 6 , 1 )
321
+ explicit_transaction_control (pool , full_path , 2 , 6 , 1 )
332
322
select_with_parameters (pool , full_path , 2 , 6 , 1 )
0 commit comments