2222FillDataQuery = """PRAGMA TablePathPrefix("{}");
2323
2424DECLARE $seriesData AS List<Struct<
25- series_id: Uint64 ,
25+ series_id: Int64 ,
2626 title: Utf8,
2727 series_info: Utf8,
2828 release_date: Date>>;
2929
3030DECLARE $seasonsData AS List<Struct<
31- series_id: Uint64 ,
32- season_id: Uint64 ,
31+ series_id: Int64 ,
32+ season_id: Int64 ,
3333 title: Utf8,
3434 first_aired: Date,
3535 last_aired: Date>>;
3636
3737DECLARE $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 ,
4141 title: Utf8,
4242 air_date: Date>>;
4343
4646 series_id,
4747 title,
4848 series_info,
49- CAST(release_date AS Uint64) AS release_date
49+ release_date
5050FROM AS_TABLE($seriesData);
5151
5252REPLACE INTO seasons
5353SELECT
5454 series_id,
5555 season_id,
5656 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
5959FROM AS_TABLE($seasonsData);
6060
6161REPLACE INTO episodes
6464 season_id,
6565 episode_id,
6666 title,
67- CAST(air_date AS Uint64) AS air_date
67+ air_date
6868FROM AS_TABLE($episodesData);
6969"""
7070
@@ -76,9 +76,9 @@ def fill_tables_with_data(pool, path):
7676
7777 def callee (session ):
7878
79- prepared_query = FillDataQuery .format (path )
79+ query = FillDataQuery .format (path )
8080 with session .transaction (ydb .QuerySerializableReadWrite ()).execute (
81- prepared_query ,
81+ query ,
8282 {
8383 "$seriesData" : (basic_example_data .get_series_data (), basic_example_data .get_series_data_type ()),
8484 "$seasonsData" : (basic_example_data .get_seasons_data (), basic_example_data .get_seasons_data_type ()),
@@ -93,40 +93,31 @@ def callee(session):
9393
9494def select_simple (pool , path ):
9595 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+ )
96119
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
130121
131122
132123def upsert_simple (pool , path ):
@@ -151,10 +142,9 @@ def select_with_parameters(pool, path, series_id, season_id, episode_id):
151142 def callee (session ):
152143 query = """
153144 PRAGMA TablePathPrefix("{}");
154- $format = DateTime::Format("%Y-%m-%d");
155145 SELECT
156146 title,
157- $format(DateTime::FromSeconds(CAST(DateTime::ToSeconds(DateTime::IntervalFromDays(CAST(air_date AS Int16))) AS Uint32))) AS air_date
147+ air_date
158148 FROM episodes
159149 WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
160150 """ .format (
@@ -164,10 +154,10 @@ def callee(session):
164154 with session .transaction (ydb .QuerySerializableReadWrite ()).execute (
165155 query ,
166156 {
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
169159 "$episodeId" : ydb .TypedValue (
170- episode_id , ydb .PrimitiveType .Uint64
160+ episode_id , ydb .PrimitiveType .Int64
171161 ), # could be defined via special class
172162 },
173163 commit_tx = True ,
@@ -186,12 +176,12 @@ def callee(session):
186176# In most cases it's better to use transaction control settings in session.transaction
187177# calls instead to avoid additional hops to YDB cluster and allow more efficient
188178# 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 ):
190180 def callee (session ):
191181 query = """
192182 PRAGMA TablePathPrefix("{}");
193183 UPDATE episodes
194- SET air_date = CAST( CurrentUtcDate() AS Uint64 )
184+ SET air_date = CurrentUtcDate()
195185 WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
196186 """ .format (
197187 path
@@ -205,9 +195,9 @@ def callee(session):
205195 with tx .execute (
206196 query ,
207197 {
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 ),
211201 },
212202 ) as _ :
213203 pass
@@ -231,10 +221,10 @@ def create_tables(pool, path):
231221 """
232222 PRAGMA TablePathPrefix("{}");
233223 CREATE table `series` (
234- `series_id` Uint64 ,
224+ `series_id` Int64 ,
235225 `title` Utf8,
236226 `series_info` Utf8,
237- `release_date` Uint64 ,
227+ `release_date` Date ,
238228 PRIMARY KEY (`series_id`)
239229 )
240230 """ .format (
@@ -247,11 +237,11 @@ def create_tables(pool, path):
247237 """
248238 PRAGMA TablePathPrefix("{}");
249239 CREATE table `seasons` (
250- `series_id` Uint64 ,
251- `season_id` Uint64 ,
240+ `series_id` Int64 ,
241+ `season_id` Int64 ,
252242 `title` Utf8,
253- `first_aired` Uint64 ,
254- `last_aired` Uint64 ,
243+ `first_aired` Date ,
244+ `last_aired` Date ,
255245 PRIMARY KEY (`series_id`, `season_id`)
256246 )
257247 """ .format (
@@ -264,11 +254,11 @@ def create_tables(pool, path):
264254 """
265255 PRAGMA TablePathPrefix("{}");
266256 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 ,
270260 `title` Utf8,
271- `air_date` Uint64 ,
261+ `air_date` Date ,
272262 PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
273263 )
274264 """ .format (
@@ -328,5 +318,5 @@ def run(endpoint, database, path):
328318 select_with_parameters (pool , full_path , 2 , 3 , 7 )
329319 select_with_parameters (pool , full_path , 2 , 3 , 8 )
330320
331- explicit_tcl (pool , full_path , 2 , 6 , 1 )
321+ explicit_transaction_control (pool , full_path , 2 , 6 , 1 )
332322 select_with_parameters (pool , full_path , 2 , 6 , 1 )
0 commit comments