Skip to content

Commit

Permalink
Merge pull request #21 from qaspen-python/feature/make_statement_prep…
Browse files Browse the repository at this point in the history
…aration_changable
  • Loading branch information
chandr-andr authored Mar 14, 2024
2 parents 1af0139 + 536e98c commit 8718e2f
Show file tree
Hide file tree
Showing 5 changed files with 207 additions and 95 deletions.
28 changes: 28 additions & 0 deletions python/psqlpy/_internal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ class Transaction:
self: Self,
querystring: str,
parameters: list[Any] | None = None,
prepared: bool = True,
) -> QueryResult:
"""Execute the query.
Expand All @@ -330,6 +331,8 @@ class Transaction:
### Parameters:
- `querystring`: querystring to execute.
- `parameters`: list of parameters to pass in the query.
- `prepared`: should the querystring be prepared before the request.
By default any querystring will be prepared.
### Example:
```python
Expand Down Expand Up @@ -358,6 +361,7 @@ class Transaction:
self: Self,
querystring: str,
parameters: list[list[Any]] | None = None,
prepared: bool = True,
) -> None: ...
"""Execute query multiple times with different parameters.
Expand All @@ -367,6 +371,8 @@ class Transaction:
### Parameters:
- `querystring`: querystring to execute.
- `parameters`: list of list of parameters to pass in the query.
- `prepared`: should the querystring be prepared before the request.
By default any querystring will be prepared.
### Example:
```python
Expand Down Expand Up @@ -395,6 +401,7 @@ class Transaction:
self: Self,
querystring: str,
parameters: list[Any] | None = None,
prepared: bool = True,
) -> SingleQueryResult:
"""Fetch exaclty single row from query.
Expand All @@ -406,6 +413,8 @@ class Transaction:
### Parameters:
- `querystring`: querystring to execute.
- `parameters`: list of parameters to pass in the query.
- `prepared`: should the querystring be prepared before the request.
By default any querystring will be prepared.
### Example:
```python
Expand Down Expand Up @@ -434,6 +443,7 @@ class Transaction:
self: Self,
querystring: str,
parameters: list[Any] | None = None,
prepared: bool = True,
) -> Any | None:
"""Execute the query and return first value of the first row.
Expand All @@ -443,6 +453,8 @@ class Transaction:
### Parameters:
- `querystring`: querystring to execute.
- `parameters`: list of parameters to pass in the query.
- `prepared`: should the querystring be prepared before the request.
By default any querystring will be prepared.
### Example:
```python
Expand All @@ -467,6 +479,7 @@ class Transaction:
async def pipeline(
self,
queries: list[tuple[str, list[Any] | None]],
prepared: bool = True,
) -> list[QueryResult]:
"""Execute queries in pipeline.
Expand All @@ -492,6 +505,12 @@ class Transaction:
| receive rows 3 | |
```
Read more: https://docs.rs/tokio-postgres/latest/tokio_postgres/#pipelining
### Parameters:
- `queries`: queries with parameters to execute.
- `prepared`: should the querystring/querystrings be prepared before the request.
By default any querystrings will be prepared.
### Example:
```python
import asyncio
Expand Down Expand Up @@ -640,6 +659,7 @@ class Transaction:
parameters: list[Any] | None = None,
fetch_number: int | None = None,
scroll: bool | None = None,
prepared: bool = True,
) -> Cursor:
"""Create new cursor object.
Expand All @@ -650,6 +670,8 @@ class Transaction:
- `parameters`: list of parameters to pass in the query.
- `fetch_number`: how many rows need to fetch.
- `scroll`: SCROLL or NO SCROLL cursor.
- `prepared`: should the querystring be prepared before the request.
By default any querystring will be prepared.
### Returns:
new initialized cursor.
Expand Down Expand Up @@ -693,6 +715,7 @@ class Connection:
self: Self,
querystring: str,
parameters: list[Any] | None = None,
prepared: bool = True,
) -> QueryResult:
"""Execute the query.
Expand All @@ -702,6 +725,8 @@ class Connection:
### Parameters:
- `querystring`: querystring to execute.
- `parameters`: list of parameters to pass in the query.
- `prepared`: should the querystring be prepared before the request.
By default any querystring will be prepared.
### Returns:
query result as `QueryResult`
Expand Down Expand Up @@ -794,6 +819,7 @@ class PSQLPool:
self: Self,
querystring: str,
parameters: list[Any] | None = None,
prepared: bool = True,
) -> QueryResult:
"""Execute the query.
Expand All @@ -803,6 +829,8 @@ class PSQLPool:
### Parameters:
- `querystring`: querystring to execute.
- `parameters`: list of parameters to pass in the query.
- `prepared`: should the querystring be prepared before the request.
By default any querystring will be prepared.
### Example:
```python
Expand Down
22 changes: 17 additions & 5 deletions src/driver/connection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,26 @@ impl RustConnection {
&self,
querystring: String,
params: Vec<PythonDTO>,
prepared: bool,
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
let db_client = &self.db_client;
let mut vec_parameters: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(params.len());
for param in &params {
vec_parameters.push(param);
}
let statement: tokio_postgres::Statement = db_client.prepare_cached(&querystring).await?;

let result = db_client
.query(&statement, &vec_parameters.into_boxed_slice())
.await?;
let result = if prepared {
db_client
.query(
&db_client.prepare_cached(&querystring).await?,
&vec_parameters.into_boxed_slice(),
)
.await?
} else {
db_client
.query(&querystring, &vec_parameters.into_boxed_slice())
.await?
};

Ok(PSQLDriverPyQueryResult::new(result))
}
Expand Down Expand Up @@ -104,6 +113,7 @@ impl Connection {
py: Python<'a>,
querystring: String,
parameters: Option<&'a PyAny>,
prepared: Option<bool>,
) -> RustPSQLDriverPyResult<&PyAny> {
let connection_arc = self.inner_connection.clone();

Expand All @@ -112,7 +122,9 @@ impl Connection {
params = convert_parameters(parameters)?;
}
rustengine_future(py, async move {
connection_arc.inner_execute(querystring, params).await
connection_arc
.inner_execute(querystring, params, prepared.unwrap_or(true))
.await
})
}

Expand Down
25 changes: 17 additions & 8 deletions src/driver/connection_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ impl RustPSQLPool {
&self,
querystring: String,
parameters: Vec<PythonDTO>,
prepared: bool,
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
let db_pool_manager = self
.db_pool
Expand All @@ -103,12 +104,18 @@ impl RustPSQLPool {
vec_parameters.push(param);
}

let result = db_pool_manager
.query(
&db_pool_manager.prepare_cached(&querystring).await?,
&vec_parameters.into_boxed_slice(),
)
.await?;
let result = if prepared {
db_pool_manager
.query(
&db_pool_manager.prepare_cached(&querystring).await?,
&vec_parameters.into_boxed_slice(),
)
.await?
} else {
db_pool_manager
.query(&querystring, &vec_parameters.into_boxed_slice())
.await?
};
Ok(PSQLDriverPyQueryResult::new(result))
}

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

rustengine_future(py, async move {
let db_pool_guard = db_pool_arc.read().await;

db_pool_guard.inner_execute(querystring, params).await
db_pool_guard
.inner_execute(querystring, params, prepared.unwrap_or(true))
.await
})
}

Expand Down
Loading

0 comments on commit 8718e2f

Please sign in to comment.