Skip to content

Commit

Permalink
Merge pull request #477 from weres-sa/add_python_prepared_request_exa…
Browse files Browse the repository at this point in the history
…mple

Add prepared query with global cache usage example
  • Loading branch information
vgvoleg authored Sep 3, 2024
2 parents 80cbd88 + 8ae452d commit 1c2a7b2
Showing 1 changed file with 52 additions and 1 deletion.
53 changes: 52 additions & 1 deletion examples/basic_example_v1/basic_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,54 @@ def callee(session):
return pool.retry_operation_sync(callee)


def select_parametrized(pool, path, series_id, season_id, episode_id):
def callee(session):
query = """
PRAGMA TablePathPrefix("{}");
DECLARE $seriesId AS Uint64;
DECLARE $seasonId AS Uint64;
DECLARE $episodeId AS Uint64;
$format = DateTime::Format("%Y-%m-%d");
SELECT
title,
$format(DateTime::FromSeconds(CAST(DateTime::ToSeconds(DateTime::IntervalFromDays(CAST(air_date AS Int16))) AS Uint32))) AS air_date
FROM episodes
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
""".format(
path
)

data_query = ydb.types.DataQuery(
query,
parameters_types={
"$seriesId": ydb.types.PrimitiveType.Uint64,
"$seasonId": ydb.types.PrimitiveType.Uint64,
"$episodeId": ydb.types.PrimitiveType.Uint64,
},
)

result_sets = session.transaction(ydb.SerializableReadWrite()).execute(
data_query,
{
"$seriesId": series_id,
"$seasonId": season_id,
"$episodeId": episode_id,
},
commit_tx=True,
settings=ydb.table.ExecDataQuerySettings().with_keep_in_cache(True),
)
print("\n> select_parametrized_transaction:")
for row in result_sets[0].rows:
print("episode title:", row.title, ", air date:", row.air_date)

return result_sets[0]

return pool.retry_operation_sync(callee)


# Prepared query with session-based cache
def select_prepared(pool, path, series_id, season_id, episode_id):
def callee(session):
query = """
Expand Down Expand Up @@ -338,5 +386,8 @@ def run(endpoint, database, path):
select_prepared(pool, full_path, 2, 3, 7)
select_prepared(pool, full_path, 2, 3, 8)

select_parametrized(pool, full_path, 2, 3, 9)
select_parametrized(pool, full_path, 2, 3, 10)

explicit_tcl(pool, full_path, 2, 6, 1)
select_prepared(pool, full_path, 2, 6, 1)
select_parametrized(pool, full_path, 2, 6, 1)

0 comments on commit 1c2a7b2

Please sign in to comment.