Skip to content

Commit 3ddf96f

Browse files
committed
simplify examples
1 parent 34d91cd commit 3ddf96f

File tree

3 files changed

+42
-54
lines changed

3 files changed

+42
-54
lines changed

examples/basic_example_v2/basic_example.py

Lines changed: 34 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -69,29 +69,22 @@
6969
"""
7070

7171

72-
def fill_tables_with_data(pool, path):
72+
def fill_tables_with_data(pool: ydb.QuerySessionPool, path: str):
7373
print("\nFilling tables with data...")
7474

75-
global FillDataQuery
75+
query = FillDataQuery.format(path)
7676

77-
def callee(session):
78-
79-
query = FillDataQuery.format(path)
80-
with session.transaction(ydb.QuerySerializableReadWrite()).execute(
81-
query,
82-
{
83-
"$seriesData": (basic_example_data.get_series_data(), basic_example_data.get_series_data_type()),
84-
"$seasonsData": (basic_example_data.get_seasons_data(), basic_example_data.get_seasons_data_type()),
85-
"$episodesData": (basic_example_data.get_episodes_data(), basic_example_data.get_episodes_data_type()),
86-
},
87-
commit_tx=True,
88-
) as _:
89-
pass
90-
91-
return pool.retry_operation_sync(callee)
77+
pool.execute_with_retries(
78+
query,
79+
{
80+
"$seriesData": (basic_example_data.get_series_data(), basic_example_data.get_series_data_type()),
81+
"$seasonsData": (basic_example_data.get_seasons_data(), basic_example_data.get_seasons_data_type()),
82+
"$episodesData": (basic_example_data.get_episodes_data(), basic_example_data.get_episodes_data_type()),
83+
},
84+
)
9285

9386

94-
def select_simple(pool, path):
87+
def select_simple(pool: ydb.QuerySessionSync, path):
9588
print("\nCheck series table...")
9689
result_sets = pool.execute_with_retries(
9790
"""
@@ -120,27 +113,22 @@ def select_simple(pool, path):
120113
return first_set
121114

122115

123-
def upsert_simple(pool, path):
116+
def upsert_simple(pool: ydb.QuerySessionPool, path):
124117
print("\nPerforming UPSERT into episodes...")
125118

126-
def callee(session):
127-
with session.transaction().execute(
128-
"""
129-
PRAGMA TablePathPrefix("{}");
130-
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
131-
""".format(
132-
path
133-
),
134-
commit_tx=True,
135-
) as _:
136-
pass
137-
138-
return pool.retry_operation_sync(callee)
119+
pool.execute_with_retries(
120+
"""
121+
PRAGMA TablePathPrefix("{}");
122+
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
123+
""".format(
124+
path
125+
)
126+
)
139127

140128

141129
def select_with_parameters(pool, path, series_id, season_id, episode_id):
142-
def callee(session):
143-
query = """
130+
result_sets = pool.execute_with_retries(
131+
"""
144132
PRAGMA TablePathPrefix("{}");
145133
SELECT
146134
title,
@@ -149,25 +137,20 @@ def callee(session):
149137
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
150138
""".format(
151139
path
152-
)
153-
154-
with session.transaction(ydb.QuerySerializableReadWrite()).execute(
155-
query,
156-
{
157-
"$seriesId": series_id, # could be defined implicit
158-
"$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple
159-
"$episodeId": ydb.TypedValue(episode_id, ydb.PrimitiveType.Int64), # could be defined via special class
160-
},
161-
commit_tx=True,
162-
) as result_sets:
163-
print("\n> select_prepared_transaction:")
164-
first_set = next(result_sets)
165-
for row in first_set.rows:
166-
print("episode title:", row.title, ", air date:", row.air_date)
140+
),
141+
{
142+
"$seriesId": series_id, # could be defined implicit
143+
"$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple
144+
"$episodeId": ydb.TypedValue(episode_id, ydb.PrimitiveType.Int64), # could be defined via special class
145+
}
146+
)
167147

168-
return first_set
148+
print("\n> select_with_parameters:")
149+
first_set = result_sets[0]
150+
for row in first_set.rows:
151+
print("episode title:", row.title, ", air date:", row.air_date)
169152

170-
return pool.retry_operation_sync(callee)
153+
return first_set
171154

172155

173156
# Show usage of explicit Begin/Commit transaction control calls.

ydb/query/base.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ def transaction(self, tx_mode: Optional[BaseQueryTxMode] = None) -> "IQueryTxCon
166166
def execute(
167167
self,
168168
query: str,
169+
parameters: Optional[dict] = None,
169170
syntax: Optional[QuerySyntax] = None,
170171
exec_mode: Optional[QueryExecMode] = None,
171-
parameters: Optional[dict] = None,
172172
concurrent_result_sets: Optional[bool] = False,
173173
) -> Iterator:
174174
"""WARNING: This API is experimental and could be changed.

ydb/query/pool.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,12 @@ def wrapped_callee():
5555
return retry_operation_sync(wrapped_callee, retry_settings)
5656

5757
def execute_with_retries(
58-
self, query: str, retry_settings: Optional[RetrySettings] = None, *args, **kwargs
58+
self,
59+
query: str,
60+
parameters: Optional[dict] = None,
61+
retry_settings: Optional[RetrySettings] = None,
62+
*args,
63+
**kwargs,
5964
) -> List[convert.ResultSet]:
6065
"""WARNING: This API is experimental and could be changed.
6166
Special interface to execute a one-shot queries in a safe, retriable way.
@@ -72,7 +77,7 @@ def execute_with_retries(
7277

7378
def wrapped_callee():
7479
with self.checkout() as session:
75-
it = session.execute(query, *args, **kwargs)
80+
it = session.execute(query, parameters, *args, **kwargs)
7681
return [result_set for result_set in it]
7782

7883
return retry_operation_sync(wrapped_callee, retry_settings)

0 commit comments

Comments
 (0)