Skip to content

Commit c514caf

Browse files
author
Daniil Babin
committed
Fixed code example of type usage docs
1 parent 66450d5 commit c514caf

File tree

4 files changed

+49
-38
lines changed

4 files changed

+49
-38
lines changed

docs/usage/types/advanced_type_usage.md

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ Due to an unavailability to support all possible types in PostgreSQL, we have a
66
This section has `Advanced` in the name because you'll need to work with raw bytes which can be difficult for some developers.
77

88
## Pass unsupported type into PostgreSQL
9-
If you are using some type that we don't support and want to insert it into PostgreSQL from PSQLPy, you must use `PyCustomType` class.
9+
If you are using some type that we don't support and want to insert it into PostgreSQL from PSQLPy, you must use `CustomType` class.
1010

1111
Let's assume we have table `for_test` in the database and `PSQLPy` doesn't support (only for demonstration) `VARCHAR` type:
1212
| database type | database column name |
@@ -15,25 +15,26 @@ 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 ConnectionPool
19-
from psqlpy.extra_types import PyCustomType
18+
from psqlpy import Connection, ConnectionPool
19+
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()
2526

26-
await db_pool.execute(
27+
await connection.execute(
2728
"INSERT INTO for_test (nickname) VALUES ($1)",
28-
[PyCustomType(b"SomeDataInBytes")],
29+
[CustomType(b"SomeDataInBytes")],
2930
)
30-
db_pool.close()
31+
connection.close()
3132
```
3233

33-
Here we pass `PyCustomType` into the parameters. It accepts only bytes.
34+
Here we pass `CustomType` into the parameters. It accepts only bytes.
3435

3536
::: important
36-
You must make bytes passed into `PyCustomType` readable for `PostgreSQL`.
37+
You must make bytes passed into `CustomType` readable for `PostgreSQL`.
3738
If bytes will be wrong, you will get an exception.
3839
:::
3940

@@ -48,8 +49,8 @@ Let's assume we have table `for_test` in the database and `PSQLPy` doesn't suppo
4849
```python
4950
from typing import Final, Any
5051

51-
from psqlpy import ConnectionPool, QueryResult
52-
from psqlpy.extra_types import PyCustomType
52+
from psqlpy import Connection, ConnectionPool, QueryResult
53+
from psqlpy.extra_types import CustomType
5354

5455

5556
def nickname_decoder(bytes_from_psql: bytes | None) -> str:
@@ -59,18 +60,19 @@ def nickname_decoder(bytes_from_psql: bytes | None) -> str:
5960
async def main() -> None:
6061
# It uses default connection parameters
6162
db_pool: Final = ConnectionPool()
63+
connection: Connection = await db_pool.connection()
6264

63-
result: QueryResult = await db_pool.execute(
65+
result: QueryResult = await connection.execute(
6466
"SELECT * FROM for_test",
65-
[PyCustomType(b"SomeDataInBytes")],
67+
[CustomType(b"SomeDataInBytes")],
6668
)
6769

6870
parsed_result: list[dict[str, Any]] = result.result(
6971
custom_decoders={
7072
"nickname": nickname_decoder,
7173
},
7274
)
73-
db_pool.close()
75+
connection.close()
7476
```
7577

7678
::: important

docs/usage/types/array_types.md

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

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

4242

4343
async def main() -> None:
4444
pool = ConnectionPool()
45-
result = await pool.execute(
45+
connection: Connection = await pool.connection()
46+
result = await connection.execute(
4647
querystring="SELECT * FROM users WHERE name = ANY($1)",
4748
parameters=[
4849
TextArray(["Alex", "Dev", "Who"]),

docs/usage/types/extra_types.md

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

51-
from psqlpy import ConnectionPool, QueryResult
51+
from psqlpy import Connection, 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()
5859

59-
await db_pool.execute(
60+
await connection.execute(
6061
"INSERT INTO numbers (index, elf_life, elon_musk_money) VALUES ($1, $2, $3, $4, $5)",
6162
[SmallInt(101), Integer(10500), BigInt(300000000000), Float32(123.11), Float64(222.12)],
6263
)
63-
db_pool.close()
64+
connection.close()
6465
```
6566

6667
::: important
@@ -81,24 +82,25 @@ Let's assume we have table `banners` in the database:
8182
```python
8283
from typing import Final
8384

84-
from psqlpy import ConnectionPool, QueryResult
85+
from psqlpy import Connection, ConnectionPool, QueryResult
8586
from psqlpy.extra_types import PyText
8687

8788

8889
async def main() -> None:
8990
# It uses default connection parameters
9091
db_pool: Final = ConnectionPool()
92+
connection: Connection = await db_pool.connection()
9193

92-
await db_pool.execute(
94+
await connection.execute(
9395
"INSERT INTO banners (title, description) VALUES ($1, $2)",
9496
["SomeTitle", PyText("Very long description")],
9597
)
9698
# Alternatively, you can do this:
97-
await db_pool.execute(
99+
await connection.execute(
98100
"INSERT INTO banners (title, description) VALUES ($1, $2)",
99101
[PyVarChar("SomeTitle"), PyText("Very long description")],
100102
)
101-
db_pool.close()
103+
connection.close()
102104
```
103105

104106
## PyJSON & PyJSONB
@@ -126,13 +128,15 @@ Let's assume we have table `users` in the database, and field `additional_user_i
126128
```python
127129
from typing import Final
128130

129-
from psqlpy import ConnectionPool, QueryResult
131+
from psqlpy import Connection, ConnectionPool, QueryResult
130132
from psqlpy.extra_types import PyJSON
131133

132134

133135
async def main() -> None:
134136
# It uses default connection parameters
135137
db_pool: Final = ConnectionPool()
138+
connection: Connection = await db_pool.connection()
139+
136140
list_for_jsonb_field = [
137141
{"some": "dict"},
138142
[
@@ -147,16 +151,16 @@ async def main() -> None:
147151
]
148152
}
149153

150-
await db_pool.execute(
154+
await connection.execute(
151155
"INSERT INTO users (additional_user_info) VALUES ($1)",
152156
[PyJSONB(list_for_jsonb_field)],
153157
)
154-
await db_pool.execute(
158+
await connection.execute(
155159
"INSERT INTO users (additional_user_info) VALUES ($1)",
156160
[dict_for_jsonb_field,],
157161
)
158162

159-
db_pool.close()
163+
connection.close()
160164
```
161165

162166
## PyMacAddr6 & PyMacAddr8
@@ -171,23 +175,24 @@ Let's assume we have table `devices` in the database:
171175
```python
172176
from typing import Final
173177

174-
from psqlpy import ConnectionPool, QueryResult
178+
from psqlpy import Connection, ConnectionPool, QueryResult
175179
from psqlpy.extra_types import PyMacAddr6, PyMacAddr8
176180

177181

178182
async def main() -> None:
179183
# It uses default connection parameters
180184
db_pool: Final = ConnectionPool()
185+
connection: Connection = await db_pool.connection()
181186

182-
await db_pool.execute(
187+
await connection.execute(
183188
"INSERT INTO devices (device_macaddr6, device_macaddr8) VALUES ($1, $2)",
184189
[
185190
PyMacAddr6("08:00:2b:01:02:03"),
186191
PyMacAddr8("08:00:2b:01:02:03:04:05"),
187192
],
188193
)
189194

190-
db_pool.close()
195+
connection.close()
191196
```
192197

193198
## Geo Types
@@ -207,15 +212,16 @@ Let's assume we have table `geo_info` with all PostgreSQL geo types in the datab
207212
```python
208213
from typing import Final
209214

210-
from psqlpy import ConnectionPool, QueryResult
215+
from psqlpy import Connection, ConnectionPool, QueryResult
211216
from psqlpy.extra_types import Point, Box, Path, Line, LineSegment, Circle
212217

213218

214219
async def main() -> None:
215220
# It uses default connection parameters
216221
db_pool: Final = ConnectionPool()
222+
connection: Connection = await db_pool.connection()
217223

218-
await db_pool.execute(
224+
await connection.execute(
219225
"INSERT INTO geo_info VALUES ($1, $2, $3, $4, $5, $6)",
220226
[
221227
Point([1.5, 2]),
@@ -227,5 +233,5 @@ async def main() -> None:
227233
],
228234
)
229235

230-
db_pool.close()
236+
connection.close()
231237
```

docs/usage/types/supported_types.md

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

82-
from psqlpy import ConnectionPool, QueryResult
82+
from psqlpy import Connection, 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()
8990

90-
result = await db_pool.execute(
91+
result = await connection.execute(
9192
"SELECT user_info FROM custom_table",
9293
)
9394
print(result.result()[0])
@@ -121,7 +122,7 @@ Let's see how we can INSERT and SELECT such data.
121122
from enum import Enum
122123
from typing import Final
123124

124-
from psqlpy import ConnectionPool, QueryResult
125+
from psqlpy import Connection, ConnectionPool, QueryResult
125126

126127

127128
class Weather(str, Enum):
@@ -132,20 +133,21 @@ class Weather(str, Enum):
132133
async def main() -> None:
133134
# It uses default connection parameters
134135
db_pool: Final = ConnectionPool()
136+
connection: Connection = await db_pool.connection()
135137

136138
# Insert new data
137-
await db_pool.execute(
139+
await connection.execute(
138140
querystring="INSERT INTO weather_plus VALUES($1)",
139141
parameters=[Weather.SUN],
140142
)
141143

142144
# Or you can pass string directly
143-
await db_pool.execute(
145+
await connection.execute(
144146
querystring="INSERT INTO weather_plus VALUES($1)",
145147
parameters=["sun"],
146148
)
147149

148-
result = await db_pool.execute(
150+
result = await connection.execute(
149151
querystring="SELECT * FROM weather_plus",
150152
)
151153
print(result.result()[0])

0 commit comments

Comments
 (0)