@@ -48,20 +48,18 @@ And we want to INSERT new data to this table:
4848``` python
4949from typing import Final
5050
51- from psqlpy import Connection, ConnectionPool, QueryResult
51+ from psqlpy import ConnectionPool, QueryResult
5252from psqlpy.extra_types import SmallInt, Integer, BigInt, Float32, Float64
5353
5454
5555async 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
8381from typing import Final
8482
85- from psqlpy import Connection, ConnectionPool, QueryResult
83+ from psqlpy import ConnectionPool, QueryResult
8684from psqlpy.extra_types import PyText
8785
8886
8987async 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
129126from typing import Final
130127
131- from psqlpy import Connection, ConnectionPool, QueryResult
128+ from psqlpy import ConnectionPool, QueryResult
132129from psqlpy.extra_types import PyJSON
133130
134131
135132async 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
176171from typing import Final
177172
178- from psqlpy import Connection, ConnectionPool, QueryResult
173+ from psqlpy import ConnectionPool, QueryResult
179174from psqlpy.extra_types import PyMacAddr6, PyMacAddr8
180175
181176
182177async 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
213206from typing import Final
214207
215- from psqlpy import Connection, ConnectionPool, QueryResult
208+ from psqlpy import ConnectionPool, QueryResult
216209from psqlpy.extra_types import Point, Box, Path, Line, LineSegment, Circle
217210
218211
219212async 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```
0 commit comments