File tree Expand file tree Collapse file tree 7 files changed +147
-0
lines changed Expand file tree Collapse file tree 7 files changed +147
-0
lines changed Original file line number Diff line number Diff line change @@ -61,6 +61,28 @@ async def main() -> None:
61
61
dict_results: list[dict[str , Any]] = results.result()
62
62
```
63
63
64
+ ### Execute Batch
65
+
66
+ #### Parameters:
67
+
68
+ - ` querystring ` : querystrings separated by semicolons.
69
+
70
+ Executes a sequence of SQL statements using the simple query protocol.
71
+
72
+ Statements should be separated by semicolons.
73
+ If an error occurs, execution of the sequence will stop at that point.
74
+ This is intended for use when, for example,
75
+ initializing a database schema.
76
+
77
+ ``` python
78
+ async def main () -> None :
79
+ ...
80
+ connection = await db_pool.connection()
81
+ await connection.execute_batch(
82
+ " CREATE TABLE psqlpy (name VARCHAR); CREATE TABLE psqlpy2 (name VARCHAR);" ,
83
+ )
84
+ ```
85
+
64
86
### Fetch
65
87
66
88
#### Parameters:
Original file line number Diff line number Diff line change @@ -144,6 +144,29 @@ async def main() -> None:
144
144
dict_results: list[dict[str , Any]] = results.result()
145
145
```
146
146
147
+ ### Execute Batch
148
+
149
+ #### Parameters:
150
+
151
+ - ` querystring ` : querystrings separated by semicolons.
152
+
153
+ Executes a sequence of SQL statements using the simple query protocol.
154
+
155
+ Statements should be separated by semicolons.
156
+ If an error occurs, execution of the sequence will stop at that point.
157
+ This is intended for use when, for example,
158
+ initializing a database schema.
159
+
160
+ ``` python
161
+ async def main () -> None :
162
+ ...
163
+ connection = await db_pool.connection()
164
+ async with connection.transaction() as transaction:
165
+ await transaction.execute_batch(
166
+ " CREATE TABLE psqlpy (name VARCHAR); CREATE TABLE psqlpy2 (name VARCHAR);" ,
167
+ )
168
+ ```
169
+
147
170
### Fetch
148
171
149
172
#### Parameters:
Original file line number Diff line number Diff line change @@ -455,6 +455,21 @@ class Transaction:
455
455
await transaction.commit()
456
456
```
457
457
"""
458
+ async def execute_batch (
459
+ self : Self ,
460
+ querystring : str ,
461
+ ) -> None :
462
+ """
463
+ Executes a sequence of SQL statements using the simple query protocol.
464
+
465
+ Statements should be separated by semicolons.
466
+ If an error occurs, execution of the sequence will stop at that point.
467
+ This is intended for use when, for example,
468
+ initializing a database schema.
469
+
470
+ ### Parameters:
471
+ - `querystring`: querystrings separated by semicolons.
472
+ """
458
473
async def execute_many (
459
474
self : Self ,
460
475
querystring : str ,
@@ -885,6 +900,21 @@ class Connection:
885
900
dict_result: List[Dict[Any, Any]] = query_result.result()
886
901
```
887
902
"""
903
+ async def execute_batch (
904
+ self : Self ,
905
+ querystring : str ,
906
+ ) -> None :
907
+ """
908
+ Executes a sequence of SQL statements using the simple query protocol.
909
+
910
+ Statements should be separated by semicolons.
911
+ If an error occurs, execution of the sequence will stop at that point.
912
+ This is intended for use when, for example,
913
+ initializing a database schema.
914
+
915
+ ### Parameters:
916
+ - `querystring`: querystrings separated by semicolons.
917
+ """
888
918
async def execute_many (
889
919
self : Self ,
890
920
querystring : str ,
Original file line number Diff line number Diff line change @@ -236,3 +236,14 @@ async def test_binary_copy_to_table(
236
236
f"SELECT COUNT(*) AS rows_count FROM { table_name } " ,
237
237
)
238
238
assert real_table_rows .result ()[0 ]["rows_count" ] == expected_inserted_row
239
+
240
+
241
+ async def test_execute_batch_method (psql_pool : ConnectionPool ) -> None :
242
+ """Test `execute_batch` method."""
243
+ await psql_pool .execute (querystring = "DROP TABLE IF EXISTS execute_batch" )
244
+ await psql_pool .execute (querystring = "DROP TABLE IF EXISTS execute_batch2" )
245
+ query = "CREATE TABLE execute_batch (name VARCHAR);CREATE TABLE execute_batch2 (name VARCHAR);"
246
+ async with psql_pool .acquire () as conn :
247
+ await conn .execute_batch (querystring = query )
248
+ await conn .execute (querystring = "SELECT * FROM execute_batch" )
249
+ await conn .execute (querystring = "SELECT * FROM execute_batch2" )
Original file line number Diff line number Diff line change @@ -390,3 +390,14 @@ async def test_binary_copy_to_table(
390
390
f"SELECT COUNT(*) AS rows_count FROM { table_name } " ,
391
391
)
392
392
assert real_table_rows .result ()[0 ]["rows_count" ] == expected_inserted_row
393
+
394
+
395
+ async def test_execute_batch_method (psql_pool : ConnectionPool ) -> None :
396
+ """Test `execute_batch` method."""
397
+ await psql_pool .execute (querystring = "DROP TABLE IF EXISTS execute_batch" )
398
+ await psql_pool .execute (querystring = "DROP TABLE IF EXISTS execute_batch2" )
399
+ query = "CREATE TABLE execute_batch (name VARCHAR);CREATE TABLE execute_batch2 (name VARCHAR);"
400
+ async with psql_pool .acquire () as conn , conn .transaction () as transaction :
401
+ await transaction .execute_batch (querystring = query )
402
+ await transaction .execute (querystring = "SELECT * FROM execute_batch" )
403
+ await transaction .execute (querystring = "SELECT * FROM execute_batch2" )
Original file line number Diff line number Diff line change @@ -260,6 +260,31 @@ impl Connection {
260
260
Err ( RustPSQLDriverError :: ConnectionClosedError )
261
261
}
262
262
263
+ /// Executes a sequence of SQL statements using the simple query protocol.
264
+ ///
265
+ /// Statements should be separated by semicolons.
266
+ /// If an error occurs, execution of the sequence will stop at that point.
267
+ /// This is intended for use when, for example,
268
+ /// initializing a database schema.
269
+ ///
270
+ /// # Errors
271
+ ///
272
+ /// May return Err Result if:
273
+ /// 1) Connection is closed.
274
+ /// 2) Cannot execute querystring.
275
+ pub async fn execute_batch (
276
+ self_ : pyo3:: Py < Self > ,
277
+ querystring : String ,
278
+ ) -> RustPSQLDriverPyResult < ( ) > {
279
+ let db_client = pyo3:: Python :: with_gil ( |gil| self_. borrow ( gil) . db_client . clone ( ) ) ;
280
+
281
+ if let Some ( db_client) = db_client {
282
+ return Ok ( db_client. batch_execute ( & querystring) . await ?) ;
283
+ }
284
+
285
+ Err ( RustPSQLDriverError :: ConnectionClosedError )
286
+ }
287
+
263
288
/// Execute querystring with parameters.
264
289
///
265
290
/// It converts incoming parameters to rust readable
Original file line number Diff line number Diff line change @@ -301,6 +301,31 @@ impl Transaction {
301
301
Err ( RustPSQLDriverError :: TransactionClosedError )
302
302
}
303
303
304
+ /// Executes a sequence of SQL statements using the simple query protocol.
305
+ ///
306
+ /// Statements should be separated by semicolons.
307
+ /// If an error occurs, execution of the sequence will stop at that point.
308
+ /// This is intended for use when, for example,
309
+ /// initializing a database schema.
310
+ ///
311
+ /// # Errors
312
+ ///
313
+ /// May return Err Result if:
314
+ /// 1) Transaction is closed.
315
+ /// 2) Cannot execute querystring.
316
+ pub async fn execute_batch ( self_ : Py < Self > , querystring : String ) -> RustPSQLDriverPyResult < ( ) > {
317
+ let ( is_transaction_ready, db_client) = pyo3:: Python :: with_gil ( |gil| {
318
+ let self_ = self_. borrow ( gil) ;
319
+ ( self_. check_is_transaction_ready ( ) , self_. db_client . clone ( ) )
320
+ } ) ;
321
+ is_transaction_ready?;
322
+ if let Some ( db_client) = db_client {
323
+ return Ok ( db_client. batch_execute ( & querystring) . await ?) ;
324
+ }
325
+
326
+ Err ( RustPSQLDriverError :: TransactionClosedError )
327
+ }
328
+
304
329
/// Fetch result from the database.
305
330
///
306
331
/// It converts incoming parameters to rust readable
You can’t perform that action at this time.
0 commit comments