Skip to content

Commit 30fbf40

Browse files
committed
update docs
1 parent c41c7ae commit 30fbf40

File tree

2 files changed

+155
-151
lines changed

2 files changed

+155
-151
lines changed

docs/examples.rst

Lines changed: 3 additions & 151 deletions
Original file line numberDiff line numberDiff line change
@@ -1,155 +1,7 @@
11
Examples
22
===============
33

4-
Basic example
5-
^^^^^^^^^^^^^
6-
7-
All examples in this section are parts of `basic example <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/basic_example_v2>`_.
8-
9-
For deeper upderstanding it is better to read the whole example.
10-
11-
Create table
12-
------------
13-
14-
.. code-block:: python
15-
16-
def create_tables(pool: ydb.QuerySessionPool):
17-
print("\nCreating table series...")
18-
pool.execute_with_retries(
19-
"""
20-
CREATE table `series` (
21-
`series_id` Int64,
22-
`title` Utf8,
23-
`series_info` Utf8,
24-
`release_date` Date,
25-
PRIMARY KEY (`series_id`)
26-
)
27-
"""
28-
)
29-
30-
print("\nCreating table seasons...")
31-
pool.execute_with_retries(
32-
"""
33-
CREATE table `seasons` (
34-
`series_id` Int64,
35-
`season_id` Int64,
36-
`title` Utf8,
37-
`first_aired` Date,
38-
`last_aired` Date,
39-
PRIMARY KEY (`series_id`, `season_id`)
40-
)
41-
"""
42-
)
43-
44-
print("\nCreating table episodes...")
45-
pool.execute_with_retries(
46-
"""
47-
CREATE table `episodes` (
48-
`series_id` Int64,
49-
`season_id` Int64,
50-
`episode_id` Int64,
51-
`title` Utf8,
52-
`air_date` Date,
53-
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
54-
)
55-
"""
56-
)
57-
58-
59-
Upsert Simple
60-
-------------
61-
62-
.. code-block:: python
63-
64-
def upsert_simple(pool: ydb.QuerySessionPool):
65-
print("\nPerforming UPSERT into episodes...")
66-
67-
pool.execute_with_retries(
68-
"""
69-
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
70-
"""
71-
)
72-
73-
74-
Simple Select
75-
----------
76-
77-
.. code-block:: python
78-
79-
def select_simple(pool: ydb.QuerySessionPool):
80-
print("\nCheck series table...")
81-
result_sets = pool.execute_with_retries(
82-
"""
83-
SELECT
84-
series_id,
85-
title,
86-
release_date
87-
FROM series
88-
WHERE series_id = 1;
89-
""",
90-
)
91-
first_set = result_sets[0]
92-
for row in first_set.rows:
93-
print(
94-
"series, id: ",
95-
row.series_id,
96-
", title: ",
97-
row.title,
98-
", release date: ",
99-
row.release_date,
100-
)
101-
102-
return first_set
103-
104-
Select With Parameters
105-
----------------------
106-
107-
.. code-block:: python
108-
109-
def select_with_parameters(pool: ydb.QuerySessionPool, series_id, season_id, episode_id):
110-
result_sets = pool.execute_with_retries(
111-
"""
112-
DECLARE $seriesId AS Int64;
113-
DECLARE $seasonId AS Int64;
114-
DECLARE $episodeId AS Int64;
115-
116-
SELECT
117-
title,
118-
air_date
119-
FROM episodes
120-
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
121-
""",
122-
{
123-
"$seriesId": series_id, # could be defined implicit
124-
"$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple
125-
"$episodeId": ydb.TypedValue(episode_id, ydb.PrimitiveType.Int64), # could be defined via special class
126-
},
127-
)
128-
129-
print("\n> select_with_parameters:")
130-
first_set = result_sets[0]
131-
for row in first_set.rows:
132-
print("episode title:", row.title, ", air date:", row.air_date)
133-
134-
return first_set
135-
136-
Huge Select
137-
-----------
138-
139-
.. code-block:: python
140-
141-
def huge_select(pool: ydb.QuerySessionPool):
142-
def callee(session: ydb.QuerySessionSync):
143-
query = """SELECT * from episodes;"""
144-
145-
with session.transaction().execute(
146-
query,
147-
commit_tx=True,
148-
) as result_sets:
149-
print("\n> Huge SELECT call")
150-
for result_set in result_sets:
151-
for row in result_set.rows:
152-
print("episode title:", row.title, ", air date:", row.air_date)
153-
154-
return pool.retry_operation_sync(callee)
4+
.. toctree::
5+
:maxdepth: 3
1556

7+
examples/basic_example

docs/examples/basic_example.rst

Lines changed: 152 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,152 @@
1+
Basic example
2+
=============
3+
4+
All examples in this section are parts of `basic example <https://github.com/ydb-platform/ydb-python-sdk/tree/main/examples/basic_example_v2>`_.
5+
6+
For deeper upderstanding it is better to read the whole example.
7+
8+
Create table
9+
------------
10+
11+
.. code-block:: python
12+
13+
def create_tables(pool: ydb.QuerySessionPool):
14+
print("\nCreating table series...")
15+
pool.execute_with_retries(
16+
"""
17+
CREATE table `series` (
18+
`series_id` Int64,
19+
`title` Utf8,
20+
`series_info` Utf8,
21+
`release_date` Date,
22+
PRIMARY KEY (`series_id`)
23+
)
24+
"""
25+
)
26+
27+
print("\nCreating table seasons...")
28+
pool.execute_with_retries(
29+
"""
30+
CREATE table `seasons` (
31+
`series_id` Int64,
32+
`season_id` Int64,
33+
`title` Utf8,
34+
`first_aired` Date,
35+
`last_aired` Date,
36+
PRIMARY KEY (`series_id`, `season_id`)
37+
)
38+
"""
39+
)
40+
41+
print("\nCreating table episodes...")
42+
pool.execute_with_retries(
43+
"""
44+
CREATE table `episodes` (
45+
`series_id` Int64,
46+
`season_id` Int64,
47+
`episode_id` Int64,
48+
`title` Utf8,
49+
`air_date` Date,
50+
PRIMARY KEY (`series_id`, `season_id`, `episode_id`)
51+
)
52+
"""
53+
)
54+
55+
56+
Upsert Simple
57+
-------------
58+
59+
.. code-block:: python
60+
61+
def upsert_simple(pool: ydb.QuerySessionPool):
62+
print("\nPerforming UPSERT into episodes...")
63+
64+
pool.execute_with_retries(
65+
"""
66+
UPSERT INTO episodes (series_id, season_id, episode_id, title) VALUES (2, 6, 1, "TBD");
67+
"""
68+
)
69+
70+
71+
Simple Select
72+
----------
73+
74+
.. code-block:: python
75+
76+
def select_simple(pool: ydb.QuerySessionPool):
77+
print("\nCheck series table...")
78+
result_sets = pool.execute_with_retries(
79+
"""
80+
SELECT
81+
series_id,
82+
title,
83+
release_date
84+
FROM series
85+
WHERE series_id = 1;
86+
""",
87+
)
88+
first_set = result_sets[0]
89+
for row in first_set.rows:
90+
print(
91+
"series, id: ",
92+
row.series_id,
93+
", title: ",
94+
row.title,
95+
", release date: ",
96+
row.release_date,
97+
)
98+
99+
return first_set
100+
101+
Select With Parameters
102+
----------------------
103+
104+
.. code-block:: python
105+
106+
def select_with_parameters(pool: ydb.QuerySessionPool, series_id, season_id, episode_id):
107+
result_sets = pool.execute_with_retries(
108+
"""
109+
DECLARE $seriesId AS Int64;
110+
DECLARE $seasonId AS Int64;
111+
DECLARE $episodeId AS Int64;
112+
113+
SELECT
114+
title,
115+
air_date
116+
FROM episodes
117+
WHERE series_id = $seriesId AND season_id = $seasonId AND episode_id = $episodeId;
118+
""",
119+
{
120+
"$seriesId": series_id, # could be defined implicit
121+
"$seasonId": (season_id, ydb.PrimitiveType.Int64), # could be defined via tuple
122+
"$episodeId": ydb.TypedValue(episode_id, ydb.PrimitiveType.Int64), # could be defined via special class
123+
},
124+
)
125+
126+
print("\n> select_with_parameters:")
127+
first_set = result_sets[0]
128+
for row in first_set.rows:
129+
print("episode title:", row.title, ", air date:", row.air_date)
130+
131+
return first_set
132+
133+
Huge Select
134+
-----------
135+
136+
.. code-block:: python
137+
138+
def huge_select(pool: ydb.QuerySessionPool):
139+
def callee(session: ydb.QuerySessionSync):
140+
query = """SELECT * from episodes;"""
141+
142+
with session.transaction().execute(
143+
query,
144+
commit_tx=True,
145+
) as result_sets:
146+
print("\n> Huge SELECT call")
147+
for result_set in result_sets:
148+
for row in result_set.rows:
149+
print("episode title:", row.title, ", air date:", row.air_date)
150+
151+
return pool.retry_operation_sync(callee)
152+

0 commit comments

Comments
 (0)