Skip to content

Commit 1c2a7b2

Browse files
authored
Merge pull request #477 from weres-sa/add_python_prepared_request_example
Add prepared query with global cache usage example
2 parents 80cbd88 + 8ae452d commit 1c2a7b2

File tree

1 file changed

+52
-1
lines changed

1 file changed

+52
-1
lines changed

examples/basic_example_v1/basic_example.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,54 @@ def callee(session):
133133
return pool.retry_operation_sync(callee)
134134

135135

136+
def select_parametrized(pool, path, series_id, season_id, episode_id):
137+
def callee(session):
138+
query = """
139+
PRAGMA TablePathPrefix("{}");
140+
141+
DECLARE $seriesId AS Uint64;
142+
DECLARE $seasonId AS Uint64;
143+
DECLARE $episodeId AS Uint64;
144+
145+
$format = DateTime::Format("%Y-%m-%d");
146+
SELECT
147+
title,
148+
$format(DateTime::FromSeconds(CAST(DateTime::ToSeconds(DateTime::IntervalFromDays(CAST(air_date AS Int16))) AS Uint32))) AS air_date
149+
FROM episodes
150+
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
151+
""".format(
152+
path
153+
)
154+
155+
data_query = ydb.types.DataQuery(
156+
query,
157+
parameters_types={
158+
"$seriesId": ydb.types.PrimitiveType.Uint64,
159+
"$seasonId": ydb.types.PrimitiveType.Uint64,
160+
"$episodeId": ydb.types.PrimitiveType.Uint64,
161+
},
162+
)
163+
164+
result_sets = session.transaction(ydb.SerializableReadWrite()).execute(
165+
data_query,
166+
{
167+
"$seriesId": series_id,
168+
"$seasonId": season_id,
169+
"$episodeId": episode_id,
170+
},
171+
commit_tx=True,
172+
settings=ydb.table.ExecDataQuerySettings().with_keep_in_cache(True),
173+
)
174+
print("\n> select_parametrized_transaction:")
175+
for row in result_sets[0].rows:
176+
print("episode title:", row.title, ", air date:", row.air_date)
177+
178+
return result_sets[0]
179+
180+
return pool.retry_operation_sync(callee)
181+
182+
183+
# Prepared query with session-based cache
136184
def select_prepared(pool, path, series_id, season_id, episode_id):
137185
def callee(session):
138186
query = """
@@ -338,5 +386,8 @@ def run(endpoint, database, path):
338386
select_prepared(pool, full_path, 2, 3, 7)
339387
select_prepared(pool, full_path, 2, 3, 8)
340388

389+
select_parametrized(pool, full_path, 2, 3, 9)
390+
select_parametrized(pool, full_path, 2, 3, 10)
391+
341392
explicit_tcl(pool, full_path, 2, 6, 1)
342-
select_prepared(pool, full_path, 2, 6, 1)
393+
select_parametrized(pool, full_path, 2, 6, 1)

0 commit comments

Comments
 (0)