Skip to content

Commit 8718e2f

Browse files
authored
Merge pull request #21 from qaspen-python/feature/make_statement_preparation_changable
2 parents 1af0139 + 536e98c commit 8718e2f

File tree

5 files changed

+207
-95
lines changed

5 files changed

+207
-95
lines changed

python/psqlpy/_internal/__init__.pyi

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,7 @@ class Transaction:
321321
self: Self,
322322
querystring: str,
323323
parameters: list[Any] | None = None,
324+
prepared: bool = True,
324325
) -> QueryResult:
325326
"""Execute the query.
326327
@@ -330,6 +331,8 @@ class Transaction:
330331
### Parameters:
331332
- `querystring`: querystring to execute.
332333
- `parameters`: list of parameters to pass in the query.
334+
- `prepared`: should the querystring be prepared before the request.
335+
By default any querystring will be prepared.
333336
334337
### Example:
335338
```python
@@ -358,6 +361,7 @@ class Transaction:
358361
self: Self,
359362
querystring: str,
360363
parameters: list[list[Any]] | None = None,
364+
prepared: bool = True,
361365
) -> None: ...
362366
"""Execute query multiple times with different parameters.
363367
@@ -367,6 +371,8 @@ class Transaction:
367371
### Parameters:
368372
- `querystring`: querystring to execute.
369373
- `parameters`: list of list of parameters to pass in the query.
374+
- `prepared`: should the querystring be prepared before the request.
375+
By default any querystring will be prepared.
370376
371377
### Example:
372378
```python
@@ -395,6 +401,7 @@ class Transaction:
395401
self: Self,
396402
querystring: str,
397403
parameters: list[Any] | None = None,
404+
prepared: bool = True,
398405
) -> SingleQueryResult:
399406
"""Fetch exaclty single row from query.
400407
@@ -406,6 +413,8 @@ class Transaction:
406413
### Parameters:
407414
- `querystring`: querystring to execute.
408415
- `parameters`: list of parameters to pass in the query.
416+
- `prepared`: should the querystring be prepared before the request.
417+
By default any querystring will be prepared.
409418
410419
### Example:
411420
```python
@@ -434,6 +443,7 @@ class Transaction:
434443
self: Self,
435444
querystring: str,
436445
parameters: list[Any] | None = None,
446+
prepared: bool = True,
437447
) -> Any | None:
438448
"""Execute the query and return first value of the first row.
439449
@@ -443,6 +453,8 @@ class Transaction:
443453
### Parameters:
444454
- `querystring`: querystring to execute.
445455
- `parameters`: list of parameters to pass in the query.
456+
- `prepared`: should the querystring be prepared before the request.
457+
By default any querystring will be prepared.
446458
447459
### Example:
448460
```python
@@ -467,6 +479,7 @@ class Transaction:
467479
async def pipeline(
468480
self,
469481
queries: list[tuple[str, list[Any] | None]],
482+
prepared: bool = True,
470483
) -> list[QueryResult]:
471484
"""Execute queries in pipeline.
472485
@@ -492,6 +505,12 @@ class Transaction:
492505
| receive rows 3 | |
493506
```
494507
Read more: https://docs.rs/tokio-postgres/latest/tokio_postgres/#pipelining
508+
509+
### Parameters:
510+
- `queries`: queries with parameters to execute.
511+
- `prepared`: should the querystring/querystrings be prepared before the request.
512+
By default any querystrings will be prepared.
513+
495514
### Example:
496515
```python
497516
import asyncio
@@ -640,6 +659,7 @@ class Transaction:
640659
parameters: list[Any] | None = None,
641660
fetch_number: int | None = None,
642661
scroll: bool | None = None,
662+
prepared: bool = True,
643663
) -> Cursor:
644664
"""Create new cursor object.
645665
@@ -650,6 +670,8 @@ class Transaction:
650670
- `parameters`: list of parameters to pass in the query.
651671
- `fetch_number`: how many rows need to fetch.
652672
- `scroll`: SCROLL or NO SCROLL cursor.
673+
- `prepared`: should the querystring be prepared before the request.
674+
By default any querystring will be prepared.
653675
654676
### Returns:
655677
new initialized cursor.
@@ -693,6 +715,7 @@ class Connection:
693715
self: Self,
694716
querystring: str,
695717
parameters: list[Any] | None = None,
718+
prepared: bool = True,
696719
) -> QueryResult:
697720
"""Execute the query.
698721
@@ -702,6 +725,8 @@ class Connection:
702725
### Parameters:
703726
- `querystring`: querystring to execute.
704727
- `parameters`: list of parameters to pass in the query.
728+
- `prepared`: should the querystring be prepared before the request.
729+
By default any querystring will be prepared.
705730
706731
### Returns:
707732
query result as `QueryResult`
@@ -794,6 +819,7 @@ class PSQLPool:
794819
self: Self,
795820
querystring: str,
796821
parameters: list[Any] | None = None,
822+
prepared: bool = True,
797823
) -> QueryResult:
798824
"""Execute the query.
799825
@@ -803,6 +829,8 @@ class PSQLPool:
803829
### Parameters:
804830
- `querystring`: querystring to execute.
805831
- `parameters`: list of parameters to pass in the query.
832+
- `prepared`: should the querystring be prepared before the request.
833+
By default any querystring will be prepared.
806834
807835
### Example:
808836
```python

src/driver/connection.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,17 +37,26 @@ impl RustConnection {
3737
&self,
3838
querystring: String,
3939
params: Vec<PythonDTO>,
40+
prepared: bool,
4041
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
4142
let db_client = &self.db_client;
4243
let mut vec_parameters: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(params.len());
4344
for param in &params {
4445
vec_parameters.push(param);
4546
}
46-
let statement: tokio_postgres::Statement = db_client.prepare_cached(&querystring).await?;
4747

48-
let result = db_client
49-
.query(&statement, &vec_parameters.into_boxed_slice())
50-
.await?;
48+
let result = if prepared {
49+
db_client
50+
.query(
51+
&db_client.prepare_cached(&querystring).await?,
52+
&vec_parameters.into_boxed_slice(),
53+
)
54+
.await?
55+
} else {
56+
db_client
57+
.query(&querystring, &vec_parameters.into_boxed_slice())
58+
.await?
59+
};
5160

5261
Ok(PSQLDriverPyQueryResult::new(result))
5362
}
@@ -104,6 +113,7 @@ impl Connection {
104113
py: Python<'a>,
105114
querystring: String,
106115
parameters: Option<&'a PyAny>,
116+
prepared: Option<bool>,
107117
) -> RustPSQLDriverPyResult<&PyAny> {
108118
let connection_arc = self.inner_connection.clone();
109119

@@ -112,7 +122,9 @@ impl Connection {
112122
params = convert_parameters(parameters)?;
113123
}
114124
rustengine_future(py, async move {
115-
connection_arc.inner_execute(querystring, params).await
125+
connection_arc
126+
.inner_execute(querystring, params, prepared.unwrap_or(true))
127+
.await
116128
})
117129
}
118130

src/driver/connection_pool.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ impl RustPSQLPool {
8888
&self,
8989
querystring: String,
9090
parameters: Vec<PythonDTO>,
91+
prepared: bool,
9192
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
9293
let db_pool_manager = self
9394
.db_pool
@@ -103,12 +104,18 @@ impl RustPSQLPool {
103104
vec_parameters.push(param);
104105
}
105106

106-
let result = db_pool_manager
107-
.query(
108-
&db_pool_manager.prepare_cached(&querystring).await?,
109-
&vec_parameters.into_boxed_slice(),
110-
)
111-
.await?;
107+
let result = if prepared {
108+
db_pool_manager
109+
.query(
110+
&db_pool_manager.prepare_cached(&querystring).await?,
111+
&vec_parameters.into_boxed_slice(),
112+
)
113+
.await?
114+
} else {
115+
db_pool_manager
116+
.query(&querystring, &vec_parameters.into_boxed_slice())
117+
.await?
118+
};
112119
Ok(PSQLDriverPyQueryResult::new(result))
113120
}
114121

@@ -273,6 +280,7 @@ impl PSQLPool {
273280
py: Python<'a>,
274281
querystring: String,
275282
parameters: Option<&'a PyAny>,
283+
prepared: Option<bool>,
276284
) -> RustPSQLDriverPyResult<&'a PyAny> {
277285
let db_pool_arc = self.rust_psql_pool.clone();
278286
let mut params: Vec<PythonDTO> = vec![];
@@ -282,8 +290,9 @@ impl PSQLPool {
282290

283291
rustengine_future(py, async move {
284292
let db_pool_guard = db_pool_arc.read().await;
285-
286-
db_pool_guard.inner_execute(querystring, params).await
293+
db_pool_guard
294+
.inner_execute(querystring, params, prepared.unwrap_or(true))
295+
.await
287296
})
288297
}
289298

0 commit comments

Comments
 (0)