Skip to content

Commit e772ec7

Browse files
committed
Remove pragma from example & add declare
1 parent f07df64 commit e772ec7

File tree

2 files changed

+196
-220
lines changed
  • ydb/docs

2 files changed

+196
-220
lines changed

ydb/docs/en/core/dev/example-app/python/index.md

Lines changed: 98 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ App code snippet for driver initialization:
2626
- Synchronous
2727

2828
```python
29-
def run(endpoint, database, path):
29+
def run(endpoint, database):
3030
driver_config = ydb.DriverConfig(
3131
endpoint, database, credentials=ydb.credentials_from_env_variables(),
3232
root_certificates=ydb.load_ydb_root_certificate(),
@@ -44,7 +44,7 @@ App code snippet for driver initialization:
4444
- Asynchronous
4545

4646
```python
47-
async def run(endpoint, database, path):
47+
async def run(endpoint, database):
4848
driver_config = ydb.DriverConfig(
4949
endpoint, database, credentials=ydb.credentials_from_env_variables(),
5050
root_certificates=ydb.load_ydb_root_certificate(),
@@ -70,109 +70,97 @@ To execute YQL queries, use the `pool.execute_with_retries()` method. For exampl
7070
- Synchronous
7171

7272
```python
73-
def create_tables(pool: ydb.QuerySessionPool, path: str):
73+
def create_tables(pool: ydb.QuerySessionPool):
7474
print("\nCreating table series...")
7575
pool.execute_with_retries(
76-
f"""
77-
PRAGMA TablePathPrefix("{path}");
78-
CREATE table `series` (
79-
`series_id` Uint64,
80-
`title` Utf8,
81-
`series_info` Utf8,
82-
`release_date` Uint64,
83-
PRIMARY KEY (`series_id`)
84-
)
85-
"""
76+
"""
77+
CREATE table `series` (
78+
`series_id` Int64,
79+
`title` Utf8,
80+
`series_info` Utf8,
81+
`release_date` Date,
82+
PRIMARY KEY (`series_id`)
83+
)
84+
"""
8685
)
8786

8887
print("\nCreating table seasons...")
8988
pool.execute_with_retries(
90-
f"""
91-
PRAGMA TablePathPrefix("{path}");
92-
CREATE table `seasons` (
93-
`series_id` Uint64,
94-
`season_id` Uint64,
95-
`title` Utf8,
96-
`first_aired` Uint64,
97-
`last_aired` Uint64,
98-
PRIMARY KEY (`series_id`, `season_id`)
99-
)
100-
"""
89+
"""
90+
CREATE table `seasons` (
91+
`series_id` Int64,
92+
`season_id` Int64,
93+
`title` Utf8,
94+
`first_aired` Date,
95+
`last_aired` Date,
96+
PRIMARY KEY (`series_id`, `season_id`)
97+
)
98+
"""
10199
)
102100

103101
print("\nCreating table episodes...")
104102
pool.execute_with_retries(
105-
f"""
106-
PRAGMA TablePathPrefix("{path}");
107-
CREATE table `episodes` (
108-
`series_id` Uint64,
109-
`season_id` Uint64,
110-
`episode_id` Uint64,
111-
`title` Utf8,
112-
`air_date` Uint64,
113-
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
114-
)
115-
"""
103+
"""
104+
CREATE table `episodes` (
105+
`series_id` Int64,
106+
`season_id` Int64,
107+
`episode_id` Int64,
108+
`title` Utf8,
109+
`air_date` Date,
110+
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
111+
)
112+
"""
116113
)
117114
```
118115

119116
- Asynchronous
120117

121118
```python
122-
async def create_tables(pool: ydb.aio.QuerySessionPoolAsync, path: str):
119+
async def create_tables(pool: ydb.aio.QuerySessionPoolAsync):
123120
print("\nCreating table series...")
124121
await pool.execute_with_retries(
125-
f"""
126-
PRAGMA TablePathPrefix("{path}");
127-
CREATE table `series` (
128-
`series_id` Uint64,
129-
`title` Utf8,
130-
`series_info` Utf8,
131-
`release_date` Uint64,
132-
PRIMARY KEY (`series_id`)
133-
)
134-
"""
122+
"""
123+
CREATE table `series` (
124+
`series_id` Int64,
125+
`title` Utf8,
126+
`series_info` Utf8,
127+
`release_date` Date,
128+
PRIMARY KEY (`series_id`)
129+
)
130+
"""
135131
)
136132

137133
print("\nCreating table seasons...")
138134
await pool.execute_with_retries(
139-
f"""
140-
PRAGMA TablePathPrefix("{path}");
141-
CREATE table `seasons` (
142-
`series_id` Uint64,
143-
`season_id` Uint64,
144-
`title` Utf8,
145-
`first_aired` Uint64,
146-
`last_aired` Uint64,
147-
PRIMARY KEY (`series_id`, `season_id`)
148-
)
149-
"""
135+
"""
136+
CREATE table `seasons` (
137+
`series_id` Int64,
138+
`season_id` Int64,
139+
`title` Utf8,
140+
`first_aired` Date,
141+
`last_aired` Date,
142+
PRIMARY KEY (`series_id`, `season_id`)
143+
)
144+
"""
150145
)
151146

152147
print("\nCreating table episodes...")
153148
await pool.execute_with_retries(
154-
f"""
155-
PRAGMA TablePathPrefix("{path}");
156-
CREATE table `episodes` (
157-
`series_id` Uint64,
158-
`season_id` Uint64,
159-
`episode_id` Uint64,
160-
`title` Utf8,
161-
`air_date` Uint64,
162-
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
163-
)
164-
"""
149+
"""
150+
CREATE table `episodes` (
151+
`series_id` Int64,
152+
`season_id` Int64,
153+
`episode_id` Int64,
154+
`title` Utf8,
155+
`air_date` Date,
156+
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
157+
)
158+
"""
165159
)
166160
```
167161

168162
{% endlist %}
169163

170-
The path parameter accepts the absolute path starting from the root:
171-
172-
```python
173-
full_path = os.path.join(database, path)
174-
```
175-
176164
The function `pool.execute_with_retries(query)`, unlike `tx.execute()`, loads the result of the query into memory before returning it to the client. This eliminates the need to use special constructs to control the iterator, but it is necessary to use this method with caution for large `SELECT` queries. More information about streams will be discussed below.
177165

178166
{% include [steps/03_write_queries.md](../_includes/steps/03_write_queries.md) %}
@@ -184,11 +172,10 @@ Code snippet for data insert/update:
184172
- Synchronous
185173

186174
```python
187-
def upsert_simple(pool, path):
175+
def upsert_simple(pool: ydb.QuerySessionPool):
188176
print("\nPerforming UPSERT into episodes...")
189177
pool.execute_with_retries(
190-
f"""
191-
PRAGMA TablePathPrefix("{path}");
178+
"""
192179
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
193180
"""
194181
)
@@ -197,20 +184,17 @@ Code snippet for data insert/update:
197184
- Asynchronous
198185

199186
```python
200-
async def upsert_simple(pool: ydb.aio.QuerySessionPoolAsync, path: str):
187+
async def upsert_simple(pool: ydb.aio.QuerySessionPoolAsync):
201188
print("\nPerforming UPSERT into episodes...")
202189
await pool.execute_with_retries(
203-
f"""
204-
PRAGMA TablePathPrefix("{path}");
190+
"""
205191
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
206192
"""
207193
)
208194
```
209195

210196
{% endlist %}
211197

212-
{% include [pragmatablepathprefix.md](../_includes/auxilary/pragmatablepathprefix.md) %}
213-
214198
{% include [steps/04_query_processing.md](../_includes/steps/04_query_processing.md) %}
215199

216200
To execute YQL queries, it is often enough to use the already familiar `pool.execute_with_retries()` method.
@@ -220,11 +204,10 @@ To execute YQL queries, it is often enough to use the already familiar `pool.exe
220204
- Synchronous
221205

222206
```python
223-
def select_simple(pool: ydb.QuerySessionPool, path: str):
207+
def select_simple(pool: ydb.QuerySessionPool):
224208
print("\nCheck series table...")
225209
result_sets = pool.execute_with_retries(
226-
f"""
227-
PRAGMA TablePathPrefix("{path}");
210+
"""
228211
SELECT
229212
series_id,
230213
title,
@@ -249,11 +232,10 @@ To execute YQL queries, it is often enough to use the already familiar `pool.exe
249232
- Asynchronous
250233

251234
```python
252-
async def select_simple(pool: ydb.aio.QuerySessionPoolAsync, path: str):
235+
async def select_simple(pool: ydb.aio.QuerySessionPoolAsync):
253236
print("\nCheck series table...")
254237
result_sets = await pool.execute_with_retries(
255-
f"""
256-
PRAGMA TablePathPrefix("{path}");
238+
"""
257239
SELECT
258240
series_id,
259241
title,
@@ -309,10 +291,13 @@ A code snippet demonstrating the possibility of using parameterized queries:
309291
- Synchronous
310292

311293
```python
312-
def select_with_parameters(pool: ydb.QuerySessionPool, path: str, series_id, season_id, episode_id):
294+
def select_with_parameters(pool: ydb.QuerySessionPool, series_id, season_id, episode_id):
313295
result_sets = pool.execute_with_retries(
314-
f"""
315-
PRAGMA TablePathPrefix("{path}");
296+
"""
297+
DECLARE $seriesId AS Int64;
298+
DECLARE $seasonId AS Int64;
299+
DECLARE $episodeId AS Int64;
300+
316301
SELECT
317302
title,
318303
air_date
@@ -337,10 +322,13 @@ A code snippet demonstrating the possibility of using parameterized queries:
337322
- Asynchronous
338323

339324
```python
340-
async def select_with_parameters(pool: ydb.aio.QuerySessionPoolAsync, path: str, series_id, season_id, episode_id):
325+
async def select_with_parameters(pool: ydb.aio.QuerySessionPoolAsync, series_id, season_id, episode_id):
341326
result_sets = await pool.execute_with_retries(
342-
f"""
343-
PRAGMA TablePathPrefix("{path}");
327+
"""
328+
DECLARE $seriesId AS Int64;
329+
DECLARE $seasonId AS Int64;
330+
DECLARE $episodeId AS Int64;
331+
344332
SELECT
345333
title,
346334
air_date
@@ -416,10 +404,13 @@ A code snippet demonstrating the explicit use of `transaction().begin()` and `tx
416404
- Synchronous
417405

418406
```python
419-
def explicit_transaction_control(pool: ydb.QuerySessionPool, path: str, series_id, season_id, episode_id):
407+
def explicit_transaction_control(pool: ydb.QuerySessionPool, series_id, season_id, episode_id):
420408
def callee(session: ydb.QuerySessionSync):
421-
query = f"""
422-
PRAGMA TablePathPrefix("{path}");
409+
query = """
410+
DECLARE $seriesId AS Int64;
411+
DECLARE $seasonId AS Int64;
412+
DECLARE $episodeId AS Int64;
413+
423414
UPDATE episodes
424415
SET air_date = CurrentUtcDate()
425416
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
@@ -452,11 +443,14 @@ A code snippet demonstrating the explicit use of `transaction().begin()` and `tx
452443

453444
```python
454445
async def explicit_transaction_control(
455-
pool: ydb.aio.QuerySessionPoolAsync, path: str, series_id, season_id, episode_id
446+
pool: ydb.aio.QuerySessionPoolAsync, series_id, season_id, episode_id
456447
):
457448
async def callee(session: ydb.aio.QuerySessionAsync):
458-
query = f"""
459-
PRAGMA TablePathPrefix("{path}");
449+
query = """
450+
DECLARE $seriesId AS Int64;
451+
DECLARE $seasonId AS Int64;
452+
DECLARE $episodeId AS Int64;
453+
460454
UPDATE episodes
461455
SET air_date = CurrentUtcDate()
462456
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
@@ -501,12 +495,9 @@ Example of a `SELECT` with unlimited data and implicit transaction control:
501495
- Synchronous
502496

503497
```python
504-
def huge_select(pool: ydb.QuerySessionPool, path: str):
498+
def huge_select(pool: ydb.QuerySessionPool):
505499
def callee(session: ydb.QuerySessionSync):
506-
query = f"""
507-
PRAGMA TablePathPrefix("{path}");
508-
SELECT * from episodes;
509-
"""
500+
query = """SELECT * from episodes;"""
510501

511502
with session.transaction().execute(
512503
query,
@@ -523,12 +514,9 @@ Example of a `SELECT` with unlimited data and implicit transaction control:
523514
- Asynchronous
524515

525516
```python
526-
async def huge_select(pool: ydb.aio.QuerySessionPoolAsync, path: str):
517+
async def huge_select(pool: ydb.aio.QuerySessionPoolAsync):
527518
async def callee(session: ydb.aio.QuerySessionAsync):
528-
query = f"""
529-
PRAGMA TablePathPrefix("{path}");
530-
SELECT * from episodes;
531-
"""
519+
query = """SELECT * from episodes;"""
532520

533521
async with await session.transaction().execute(
534522
query,

0 commit comments

Comments
 (0)