Skip to content

Commit 06d164e

Browse files
author
Daniil Babin
committed
Fixed using connections in type usage docs
1 parent 7ae6d27 commit 06d164e

File tree

4 files changed

+98
-110
lines changed

4 files changed

+98
-110
lines changed

docs/usage/types/advanced_type_usage.md

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,19 @@ Let's assume we have table `for_test` in the database and `PSQLPy` doesn't suppo
1515
```python
1616
from typing import Final
1717

18-
from psqlpy import Connection, ConnectionPool
18+
from psqlpy import ConnectionPool
1919
from psqlpy.extra_types import CustomType
2020

2121

2222
async def main() -> None:
2323
# It uses default connection parameters
2424
db_pool: Final = ConnectionPool()
25-
connection: Connection = await db_pool.connection()
26-
27-
await connection.execute(
28-
"INSERT INTO for_test (nickname) VALUES ($1)",
29-
[CustomType(b"SomeDataInBytes")],
30-
)
31-
connection.close()
25+
26+
async with db_pool.acquire() as connection:
27+
await connection.execute(
28+
"INSERT INTO for_test (nickname) VALUES ($1)",
29+
[CustomType(b"SomeDataInBytes")],
30+
)
3231
```
3332

3433
Here we pass `CustomType` into the parameters. It accepts only bytes.
@@ -49,7 +48,7 @@ Let's assume we have table `for_test` in the database and `PSQLPy` doesn't suppo
4948
```python
5049
from typing import Final, Any
5150

52-
from psqlpy import Connection, ConnectionPool, QueryResult
51+
from psqlpy import ConnectionPool, QueryResult
5352
from psqlpy.extra_types import CustomType
5453

5554

@@ -60,19 +59,18 @@ def nickname_decoder(bytes_from_psql: bytes | None) -> str:
6059
async def main() -> None:
6160
# It uses default connection parameters
6261
db_pool: Final = ConnectionPool()
63-
connection: Connection = await db_pool.connection()
6462

65-
result: QueryResult = await connection.execute(
66-
"SELECT * FROM for_test",
67-
[CustomType(b"SomeDataInBytes")],
68-
)
63+
async with db_pool.acquire() as connection:
64+
result: QueryResult = await connection.execute(
65+
"SELECT * FROM for_test",
66+
[CustomType(b"SomeDataInBytes")],
67+
)
6968

7069
parsed_result: list[dict[str, Any]] = result.result(
7170
custom_decoders={
7271
"nickname": nickname_decoder,
7372
},
7473
)
75-
connection.close()
7674
```
7775

7876
::: important

docs/usage/types/array_types.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,17 @@ For type safety and better performance we have predefined array types.
3636
### Example:
3737

3838
```python
39-
from psqlpy import Connection, ConnectionPool
39+
from psqlpy import ConnectionPool
4040
from psqlpy.extra_types import TextArray
4141

4242

4343
async def main() -> None:
4444
pool = ConnectionPool()
45-
connection: Connection = await pool.connection()
46-
result = await connection.execute(
47-
querystring="SELECT * FROM users WHERE name = ANY($1)",
48-
parameters=[
49-
TextArray(["Alex", "Dev", "Who"]),
50-
]
51-
)
45+
async with db_pool.acquire() as connection:
46+
result = await connection.execute(
47+
querystring="SELECT * FROM users WHERE name = ANY($1)",
48+
parameters=[
49+
TextArray(["Alex", "Dev", "Who"]),
50+
]
51+
)
5252
```

docs/usage/types/extra_types.md

Lines changed: 53 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -48,20 +48,18 @@ And we want to INSERT new data to this table:
4848
```python
4949
from typing import Final
5050

51-
from psqlpy import Connection, ConnectionPool, QueryResult
51+
from psqlpy import ConnectionPool, QueryResult
5252
from psqlpy.extra_types import SmallInt, Integer, BigInt, Float32, Float64
5353

5454

5555
async def main() -> None:
5656
# It uses default connection parameters
5757
db_pool: Final = ConnectionPool()
58-
connection: Connection = await db_pool.connection()
59-
60-
await connection.execute(
61-
"INSERT INTO numbers (index, elf_life, elon_musk_money) VALUES ($1, $2, $3, $4, $5)",
62-
[SmallInt(101), Integer(10500), BigInt(300000000000), Float32(123.11), Float64(222.12)],
63-
)
64-
connection.close()
58+
async with db_pool.acquire() as connection:
59+
await connection.execute(
60+
"INSERT INTO numbers (index, elf_life, elon_musk_money) VALUES ($1, $2, $3, $4, $5)",
61+
[SmallInt(101), Integer(10500), BigInt(300000000000), Float32(123.11), Float64(222.12)],
62+
)
6563
```
6664

6765
::: important
@@ -82,25 +80,24 @@ Let's assume we have table `banners` in the database:
8280
```python
8381
from typing import Final
8482

85-
from psqlpy import Connection, ConnectionPool, QueryResult
83+
from psqlpy import ConnectionPool, QueryResult
8684
from psqlpy.extra_types import PyText
8785

8886

8987
async def main() -> None:
9088
# It uses default connection parameters
9189
db_pool: Final = ConnectionPool()
92-
connection: Connection = await db_pool.connection()
93-
94-
await connection.execute(
95-
"INSERT INTO banners (title, description) VALUES ($1, $2)",
96-
["SomeTitle", PyText("Very long description")],
97-
)
98-
# Alternatively, you can do this:
99-
await connection.execute(
100-
"INSERT INTO banners (title, description) VALUES ($1, $2)",
101-
[PyVarChar("SomeTitle"), PyText("Very long description")],
102-
)
103-
connection.close()
90+
91+
async with db_pool.acquire() as connection:
92+
await connection.execute(
93+
"INSERT INTO banners (title, description) VALUES ($1, $2)",
94+
["SomeTitle", PyText("Very long description")],
95+
)
96+
# Alternatively, you can do this:
97+
await connection.execute(
98+
"INSERT INTO banners (title, description) VALUES ($1, $2)",
99+
[PyVarChar("SomeTitle"), PyText("Very long description")],
100+
)
104101
```
105102

106103
## PyJSON & PyJSONB
@@ -128,15 +125,14 @@ Let's assume we have table `users` in the database, and field `additional_user_i
128125
```python
129126
from typing import Final
130127

131-
from psqlpy import Connection, ConnectionPool, QueryResult
128+
from psqlpy import ConnectionPool, QueryResult
132129
from psqlpy.extra_types import PyJSON
133130

134131

135132
async def main() -> None:
136133
# It uses default connection parameters
137134
db_pool: Final = ConnectionPool()
138-
connection: Connection = await db_pool.connection()
139-
135+
140136
list_for_jsonb_field = [
141137
{"some": "dict"},
142138
[
@@ -151,16 +147,15 @@ async def main() -> None:
151147
]
152148
}
153149

154-
await connection.execute(
155-
"INSERT INTO users (additional_user_info) VALUES ($1)",
156-
[PyJSONB(list_for_jsonb_field)],
157-
)
158-
await connection.execute(
159-
"INSERT INTO users (additional_user_info) VALUES ($1)",
160-
[dict_for_jsonb_field,],
161-
)
162-
163-
connection.close()
150+
async with db_pool.acquire() as connection:
151+
await connection.execute(
152+
"INSERT INTO users (additional_user_info) VALUES ($1)",
153+
[PyJSONB(list_for_jsonb_field)],
154+
)
155+
await connection.execute(
156+
"INSERT INTO users (additional_user_info) VALUES ($1)",
157+
[dict_for_jsonb_field,],
158+
)
164159
```
165160

166161
## PyMacAddr6 & PyMacAddr8
@@ -175,24 +170,22 @@ Let's assume we have table `devices` in the database:
175170
```python
176171
from typing import Final
177172

178-
from psqlpy import Connection, ConnectionPool, QueryResult
173+
from psqlpy import ConnectionPool, QueryResult
179174
from psqlpy.extra_types import PyMacAddr6, PyMacAddr8
180175

181176

182177
async def main() -> None:
183178
# It uses default connection parameters
184179
db_pool: Final = ConnectionPool()
185-
connection: Connection = await db_pool.connection()
186-
187-
await connection.execute(
188-
"INSERT INTO devices (device_macaddr6, device_macaddr8) VALUES ($1, $2)",
189-
[
190-
PyMacAddr6("08:00:2b:01:02:03"),
191-
PyMacAddr8("08:00:2b:01:02:03:04:05"),
192-
],
193-
)
194-
195-
connection.close()
180+
181+
async with db_pool.acquire() as connection:
182+
await connection.execute(
183+
"INSERT INTO devices (device_macaddr6, device_macaddr8) VALUES ($1, $2)",
184+
[
185+
PyMacAddr6("08:00:2b:01:02:03"),
186+
PyMacAddr8("08:00:2b:01:02:03:04:05"),
187+
],
188+
)
196189
```
197190

198191
## Geo Types
@@ -212,26 +205,24 @@ Let's assume we have table `geo_info` with all PostgreSQL geo types in the datab
212205
```python
213206
from typing import Final
214207

215-
from psqlpy import Connection, ConnectionPool, QueryResult
208+
from psqlpy import ConnectionPool, QueryResult
216209
from psqlpy.extra_types import Point, Box, Path, Line, LineSegment, Circle
217210

218211

219212
async def main() -> None:
220213
# It uses default connection parameters
221214
db_pool: Final = ConnectionPool()
222-
connection: Connection = await db_pool.connection()
223-
224-
await connection.execute(
225-
"INSERT INTO geo_info VALUES ($1, $2, $3, $4, $5, $6)",
226-
[
227-
Point([1.5, 2]),
228-
Box([(1.7, 2.8), (9, 9)]),
229-
Path([(3.5, 3), (9, 9), (8, 8)]),
230-
Line([1, -2, 3]),
231-
LineSegment([(5.6, 3.1), (4, 5)]),
232-
Circle([5, 1.8, 10]),
233-
],
234-
)
235-
236-
connection.close()
215+
216+
async with db_pool.acquire() as connection:
217+
await connection.execute(
218+
"INSERT INTO geo_info VALUES ($1, $2, $3, $4, $5, $6)",
219+
[
220+
Point([1.5, 2]),
221+
Box([(1.7, 2.8), (9, 9)]),
222+
Path([(3.5, 3), (9, 9), (8, 8)]),
223+
Line([1, -2, 3]),
224+
LineSegment([(5.6, 3.1), (4, 5)]),
225+
Circle([5, 1.8, 10]),
226+
],
227+
)
237228
```

docs/usage/types/supported_types.md

Lines changed: 24 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -79,19 +79,19 @@ Now we can see what result will be returned.
7979
```python
8080
from typing import Final
8181

82-
from psqlpy import Connection, ConnectionPool, QueryResult
82+
from psqlpy import ConnectionPool, QueryResult
8383
from psqlpy.extra_types import SmallInt, Integer, BigInt
8484

8585

8686
async def main() -> None:
8787
# It uses default connection parameters
8888
db_pool: Final = ConnectionPool()
89-
connection: Connection = await db_pool.connection()
90-
91-
result = await connection.execute(
92-
"SELECT user_info FROM custom_table",
93-
)
94-
print(result.result()[0])
89+
90+
async with db_pool.acquire() as connection:
91+
result = await connection.execute(
92+
"SELECT user_info FROM custom_table",
93+
)
94+
print(result.result()[0])
9595
```
9696
It will return:
9797
```json
@@ -122,7 +122,7 @@ Let's see how we can INSERT and SELECT such data.
122122
from enum import Enum
123123
from typing import Final
124124

125-
from psqlpy import Connection, ConnectionPool, QueryResult
125+
from psqlpy import ConnectionPool, QueryResult
126126

127127

128128
class Weather(str, Enum):
@@ -133,23 +133,22 @@ class Weather(str, Enum):
133133
async def main() -> None:
134134
# It uses default connection parameters
135135
db_pool: Final = ConnectionPool()
136-
connection: Connection = await db_pool.connection()
137-
138-
# Insert new data
139-
await connection.execute(
140-
querystring="INSERT INTO weather_plus VALUES($1)",
141-
parameters=[Weather.SUN],
142-
)
143-
144-
# Or you can pass string directly
145-
await connection.execute(
146-
querystring="INSERT INTO weather_plus VALUES($1)",
147-
parameters=["sun"],
148-
)
149-
150-
result = await connection.execute(
151-
querystring="SELECT * FROM weather_plus",
152-
)
136+
async with db_pool.acquire() as connection:
137+
# Insert new data
138+
await connection.execute(
139+
querystring="INSERT INTO weather_plus VALUES($1)",
140+
parameters=[Weather.SUN],
141+
)
142+
143+
# Or you can pass string directly
144+
await connection.execute(
145+
querystring="INSERT INTO weather_plus VALUES($1)",
146+
parameters=["sun"],
147+
)
148+
149+
result = await connection.execute(
150+
querystring="SELECT * FROM weather_plus",
151+
)
153152
print(result.result()[0])
154153
```
155154
You will receive:

0 commit comments

Comments
 (0)