Skip to content

Commit 54357cd

Browse files
committed
Continue implementing execute_many in transaction
1 parent ee4f77e commit 54357cd

File tree

2 files changed

+68
-6
lines changed

2 files changed

+68
-6
lines changed

python/psqlpy/_internal/__init__.pyi

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,57 @@ class Transaction:
255255
256256
# Or you can transaction as a async context manager
257257
258+
async def main() -> None:
259+
db_pool = PSQLPool()
260+
await psqlpy.startup()
261+
262+
transaction = await db_pool.transaction()
263+
async with transaction:
264+
query_result: QueryResult = await transaction.execute(
265+
"SELECT username FROM users WHERE id = $1",
266+
[100],
267+
)
268+
dict_result: List[Dict[Any, Any]] = query_result.result()
269+
# This way transaction begins and commits by itself.
270+
```
271+
"""
272+
async def execute_many(
273+
self: Self,
274+
querystring: str,
275+
parameters: list[list[Any]] | None = None,
276+
) -> None: ...
277+
"""Execute query multiple times with different parameters.
278+
279+
Querystring can contain `$<number>` parameters
280+
for converting them in the driver side.
281+
282+
### Parameters:
283+
- `querystring`: querystring to execute.
284+
- `parameters`: list of list of parameters to pass in the query.
285+
286+
### Example:
287+
```python
288+
import asyncio
289+
290+
from psqlpy import PSQLPool, QueryResult
291+
292+
293+
async def main() -> None:
294+
db_pool = PSQLPool()
295+
await db_pool.startup()
296+
297+
transaction = await db_pool.transaction()
298+
await transaction.begin()
299+
query_result: QueryResult = await transaction.execute_many(
300+
"INSERT INTO users (name, age) VALUES ($1, $2)",
301+
[["boba", 10], ["boba", 20]],
302+
)
303+
dict_result: List[Dict[Any, Any]] = query_result.result()
304+
# You must call commit manually
305+
await transaction.commit()
306+
307+
# Or you can transaction as a async context manager
308+
258309
async def main() -> None:
259310
db_pool = PSQLPool()
260311
await psqlpy.startup()

src/driver/transaction.rs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -146,18 +146,29 @@ impl RustTransaction {
146146
"Transaction is already committed or rolled back".into(),
147147
));
148148
}
149+
if parameters.len() == 0 {
150+
return Err(RustPSQLDriverError::DataBaseTransactionError(
151+
"No parameters passed to execute_many".into(),
152+
));
153+
}
154+
let mut vec_parameters: Vec<Box<dyn ToSql + Sync>> =
155+
Vec::with_capacity(parameters[0].len());
149156
for single_parameters in parameters {
150-
let mut vec_parameters: Vec<&(dyn ToSql + Sync)> =
151-
Vec::with_capacity(single_parameters.len());
152-
for param in &single_parameters {
153-
vec_parameters.push(param);
157+
for param in single_parameters {
158+
vec_parameters.push(Box::new(param));
154159
}
155160

156161
let statement = db_client_guard.prepare_cached(&querystring).await?;
157-
158162
db_client_guard
159-
.query(&statement, &vec_parameters.into_boxed_slice())
163+
.query(
164+
&statement,
165+
&*vec_parameters
166+
.iter()
167+
.map(|p| &**p as &(dyn ToSql + Sync))
168+
.collect::<Vec<_>>(),
169+
)
160170
.await?;
171+
vec_parameters.clear()
161172
}
162173

163174
Ok(())

0 commit comments

Comments
 (0)