Skip to content

Commit edf13c6

Browse files
committed
Small conversion optimization
1 parent 6b4dc84 commit edf13c6

File tree

4 files changed

+25
-30
lines changed

4 files changed

+25
-30
lines changed

src/driver/connection.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
use deadpool_postgres::Object;
22
use pyo3::{pyclass, pymethods, types::PyList, PyAny, Python};
33
use std::{collections::HashSet, sync::Arc, vec};
4-
use tokio_postgres::types::ToSql;
54

65
use crate::{
76
common::rustdriver_future,
87
exceptions::rust_errors::RustPSQLDriverPyResult,
98
query_result::{PSQLDriverPyQueryResult, PSQLDriverSinglePyQueryResult},
10-
value_converter::{convert_parameters, postgres_to_py, PythonDTO},
9+
value_converter::{convert_parameters, postgres_to_py, PythonDTO, QueryParameter},
1110
};
1211
use tokio_postgres::Row;
1312

@@ -37,14 +36,14 @@ impl RustConnection {
3736
pub async fn inner_execute(
3837
&self,
3938
querystring: String,
40-
params: Vec<PythonDTO>,
39+
parameters: Vec<PythonDTO>,
4140
prepared: bool,
4241
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
4342
let db_client = self.db_client.read().await;
44-
let mut vec_parameters: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(params.len());
45-
for param in &params {
46-
vec_parameters.push(param);
47-
}
43+
let vec_parameters: Vec<&QueryParameter> = parameters
44+
.iter()
45+
.map(|param| param as &QueryParameter)
46+
.collect();
4847

4948
let result = if prepared {
5049
db_client
@@ -90,7 +89,7 @@ impl RustConnection {
9089
&transaction.prepare_cached(&querystring).await?,
9190
&single_parameters
9291
.iter()
93-
.map(|p| p as &(dyn ToSql + Sync))
92+
.map(|p| p as &QueryParameter)
9493
.collect::<Vec<_>>(),
9594
)
9695
.await?;
@@ -100,7 +99,7 @@ impl RustConnection {
10099
&querystring,
101100
&single_parameters
102101
.iter()
103-
.map(|p| p as &(dyn ToSql + Sync))
102+
.map(|p| p as &QueryParameter)
104103
.collect::<Vec<_>>(),
105104
)
106105
.await?;
@@ -132,10 +131,10 @@ impl RustConnection {
132131
parameters: Vec<PythonDTO>,
133132
prepared: bool,
134133
) -> RustPSQLDriverPyResult<PSQLDriverSinglePyQueryResult> {
135-
let mut vec_parameters: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(parameters.len());
136-
for param in &parameters {
137-
vec_parameters.push(param);
138-
}
134+
let vec_parameters: Vec<&QueryParameter> = parameters
135+
.iter()
136+
.map(|param| param as &QueryParameter)
137+
.collect();
139138
let db_client_guard = self.db_client.read().await;
140139

141140
let result = if prepared {
@@ -176,10 +175,10 @@ impl RustConnection {
176175
prepared: bool,
177176
) -> RustPSQLDriverPyResult<Vec<Row>> {
178177
let db_client_guard = self.db_client.read().await;
179-
let mut vec_parameters: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(parameters.len());
180-
for param in &parameters {
181-
vec_parameters.push(param);
182-
}
178+
let vec_parameters: Vec<&QueryParameter> = parameters
179+
.iter()
180+
.map(|param| param as &QueryParameter)
181+
.collect();
183182

184183
let result = if prepared {
185184
db_client_guard

src/driver/connection_pool.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use deadpool_postgres::{Manager, ManagerConfig, Pool, RecyclingMethod};
22
use pyo3::{pyclass, pymethods, PyAny, Python};
33
use std::{str::FromStr, sync::Arc, vec};
4-
use tokio_postgres::{types::ToSql, NoTls};
4+
use tokio_postgres::NoTls;
55

66
use crate::{
77
common::rustdriver_future,
88
exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult},
99
query_result::PSQLDriverPyQueryResult,
10-
value_converter::{convert_parameters, PythonDTO},
10+
value_converter::{convert_parameters, PythonDTO, QueryParameter},
1111
};
1212

1313
use super::{
@@ -99,10 +99,10 @@ impl RustPSQLPool {
9999
.get()
100100
.await?;
101101

102-
let mut vec_parameters: Vec<&(dyn ToSql + Sync)> = Vec::with_capacity(parameters.len());
103-
for param in &parameters {
104-
vec_parameters.push(param);
105-
}
102+
let vec_parameters: Vec<&QueryParameter> = parameters
103+
.iter()
104+
.map(|param| param as &QueryParameter)
105+
.collect();
106106

107107
let result = if prepared {
108108
db_pool_manager

src/driver/cursor.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use pyo3::{
33
PyRefMut, Python,
44
};
55
use std::sync::Arc;
6-
use tokio_postgres::{types::ToSql, Row};
6+
use tokio_postgres::Row;
77

88
use crate::{
99
common::rustdriver_future,
@@ -60,12 +60,6 @@ impl InnerCursor {
6060
pub async fn inner_start(&mut self) -> RustPSQLDriverPyResult<()> {
6161
let db_transaction_arc = self.db_transaction.clone();
6262

63-
let mut vec_parameters: Vec<&(dyn ToSql + Sync)> =
64-
Vec::with_capacity(self.parameters.len());
65-
for param in &self.parameters {
66-
vec_parameters.push(param);
67-
}
68-
6963
let mut cursor_init_query = format!("DECLARE {}", self.cursor_name);
7064
if let Some(scroll) = self.scroll {
7165
if scroll {

src/value_converter.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ use crate::{
2222
extra_types::{BigInt, Integer, PyJSON, PyUUID, SmallInt},
2323
};
2424

25+
pub type QueryParameter = (dyn ToSql + Sync);
26+
2527
/// Additional type for types come from Python.
2628
///
2729
/// It's necessary because we need to pass this

0 commit comments

Comments
 (0)