Skip to content

Commit 19fe58d

Browse files
committed
Full value converter refactor
1 parent 602f00e commit 19fe58d

File tree

7 files changed

+27
-33
lines changed

7 files changed

+27
-33
lines changed

src/driver/connection.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,6 @@ impl Connection {
150150
)
151151
});
152152

153-
let db_pool_2 = db_pool.clone();
154153
if db_client.is_some() {
155154
return Ok(self_);
156155
}
@@ -163,11 +162,8 @@ impl Connection {
163162
.await??;
164163
pyo3::Python::with_gil(|gil| {
165164
let mut self_ = self_.borrow_mut(gil);
166-
self_.db_client = Some(Arc::new(PsqlpyConnection::PoolConn(
167-
db_connection,
168-
db_pool_2.unwrap(),
169-
prepare,
170-
)));
165+
self_.db_client =
166+
Some(Arc::new(PsqlpyConnection::PoolConn(db_connection, prepare)));
171167
});
172168
return Ok(self_);
173169
}

src/driver/connection_pool.rs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -407,19 +407,14 @@ impl ConnectionPool {
407407
let slf = self_.borrow(gil);
408408
(slf.pool.clone(), slf.pg_config.clone(), slf.prepare)
409409
});
410-
let db_pool_2 = db_pool.clone();
411410
let db_connection = tokio_runtime()
412411
.spawn(async move {
413412
Ok::<deadpool_postgres::Object, RustPSQLDriverError>(db_pool.get().await?)
414413
})
415414
.await??;
416415

417416
Ok(Connection::new(
418-
Some(Arc::new(PsqlpyConnection::PoolConn(
419-
db_connection,
420-
db_pool_2.clone(),
421-
prepare,
422-
))),
417+
Some(Arc::new(PsqlpyConnection::PoolConn(db_connection, prepare))),
423418
None,
424419
pg_config,
425420
prepare,

src/driver/inner_connection.rs

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414

1515
#[allow(clippy::module_name_repetitions)]
1616
pub enum PsqlpyConnection {
17-
PoolConn(Object, Pool, bool),
17+
PoolConn(Object, bool),
1818
SingleConn(Client),
1919
}
2020

@@ -25,13 +25,14 @@ impl PsqlpyConnection {
2525
/// May return Err if cannot prepare statement.
2626
pub async fn prepare(&self, query: &str, prepared: bool) -> PSQLPyResult<Statement> {
2727
match self {
28-
PsqlpyConnection::PoolConn(pconn, _, _) => {
28+
PsqlpyConnection::PoolConn(pconn, _) => {
2929
if prepared {
3030
return Ok(pconn.prepare_cached(query).await?);
3131
} else {
32-
println!("999999");
32+
pconn.batch_execute("BEGIN").await?;
3333
let prepared = pconn.prepare(query).await?;
3434
self.drop_prepared(&prepared).await?;
35+
pconn.batch_execute("COMMIT").await?;
3536
return Ok(prepared);
3637
}
3738
}
@@ -46,8 +47,9 @@ impl PsqlpyConnection {
4647
pub async fn drop_prepared(&self, stmt: &Statement) -> PSQLPyResult<()> {
4748
let deallocate_query = format!("DEALLOCATE PREPARE {}", stmt.name());
4849
match self {
49-
PsqlpyConnection::PoolConn(pconn, _, _) => {
50-
return Ok(pconn.batch_execute(&deallocate_query).await?)
50+
PsqlpyConnection::PoolConn(pconn, _) => {
51+
let res = Ok(pconn.batch_execute(&deallocate_query).await?);
52+
res
5153
}
5254
PsqlpyConnection::SingleConn(sconn) => {
5355
return Ok(sconn.batch_execute(&deallocate_query).await?)
@@ -68,7 +70,7 @@ impl PsqlpyConnection {
6870
T: ?Sized + ToStatement,
6971
{
7072
match self {
71-
PsqlpyConnection::PoolConn(pconn, _, _) => {
73+
PsqlpyConnection::PoolConn(pconn, _) => {
7274
return Ok(pconn.query(statement, params).await?)
7375
}
7476
PsqlpyConnection::SingleConn(sconn) => {
@@ -87,7 +89,7 @@ impl PsqlpyConnection {
8789
params: &[(&(dyn ToSql + Sync), Type)],
8890
) -> PSQLPyResult<Vec<Row>> {
8991
match self {
90-
PsqlpyConnection::PoolConn(pconn, _, _) => {
92+
PsqlpyConnection::PoolConn(pconn, _) => {
9193
return Ok(pconn.query_typed(statement, params).await?)
9294
}
9395
PsqlpyConnection::SingleConn(sconn) => {
@@ -102,9 +104,7 @@ impl PsqlpyConnection {
102104
/// May return Err if cannot execute statement.
103105
pub async fn batch_execute(&self, query: &str) -> PSQLPyResult<()> {
104106
match self {
105-
PsqlpyConnection::PoolConn(pconn, _, _) => {
106-
return Ok(pconn.batch_execute(query).await?)
107-
}
107+
PsqlpyConnection::PoolConn(pconn, _) => return Ok(pconn.batch_execute(query).await?),
108108
PsqlpyConnection::SingleConn(sconn) => return Ok(sconn.batch_execute(query).await?),
109109
}
110110
}
@@ -119,7 +119,7 @@ impl PsqlpyConnection {
119119
U: Buf + 'static + Send,
120120
{
121121
match self {
122-
PsqlpyConnection::PoolConn(pconn, _, _) => return Ok(pconn.copy_in(statement).await?),
122+
PsqlpyConnection::PoolConn(pconn, _) => return Ok(pconn.copy_in(statement).await?),
123123
PsqlpyConnection::SingleConn(sconn) => return Ok(sconn.copy_in(statement).await?),
124124
}
125125
}
@@ -137,7 +137,7 @@ impl PsqlpyConnection {
137137
T: ?Sized + ToStatement,
138138
{
139139
match self {
140-
PsqlpyConnection::PoolConn(pconn, _, _) => {
140+
PsqlpyConnection::PoolConn(pconn, _) => {
141141
return Ok(pconn.query_one(statement, params).await?)
142142
}
143143
PsqlpyConnection::SingleConn(sconn) => {
@@ -202,8 +202,14 @@ impl PsqlpyConnection {
202202
"Cannot prepare statement, error - {err}"
203203
))
204204
})?,
205+
// false => {
206+
// self
207+
// .query_typed(statement.raw_query(), &statement.params_typed())
208+
// .await
209+
// .map_err(|err| RustPSQLDriverError::ConnectionExecuteError(format!("{err}")))?
210+
// },
205211
false => self
206-
.query_typed(statement.raw_query(), &statement.params_typed())
212+
.query_typed("SELECT * FROM users", &[])
207213
.await
208214
.map_err(|err| RustPSQLDriverError::ConnectionExecuteError(format!("{err}")))?,
209215
};

src/statement/query.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::value_converter::consts::KWARGS_PARAMS_REGEXP;
66

77
use super::utils::hash_str;
88

9-
#[derive(Clone)]
9+
#[derive(Clone, Debug)]
1010
pub struct QueryString {
1111
pub(crate) initial_qs: String,
1212
// This field are used when kwargs passed
@@ -68,7 +68,7 @@ impl QueryString {
6868
}
6969
}
7070

71-
#[derive(Clone)]
71+
#[derive(Clone, Debug)]
7272
pub(crate) struct ConvertedQueryString {
7373
converted_qs: String,
7474
params_names: Vec<String>,

src/statement/statement.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::exceptions::rust_errors::{PSQLPyResult, RustPSQLDriverError};
55

66
use super::{parameters::PreparedParameters, query::QueryString};
77

8-
#[derive(Clone)]
8+
#[derive(Clone, Debug)]
99
pub struct PsqlpyStatement {
1010
query: QueryString,
1111
prepared_parameters: PreparedParameters,

src/statement/statement_builder.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,10 +93,8 @@ impl<'a> StatementBuilder<'a> {
9393
))
9494
}
9595
false => {
96-
{
97-
self.write_to_cache(cache_guard, &querystring, &prepared_stmt)
98-
.await;
99-
}
96+
self.write_to_cache(cache_guard, &querystring, &prepared_stmt)
97+
.await;
10098
return Ok(PsqlpyStatement::new(querystring, prepared_parameters, None));
10199
}
102100
}

src/value_converter/to_python.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ use crate::{
2323
Circle, Line, RustLineSegment, RustLineString, RustMacAddr6, RustMacAddr8, RustPoint,
2424
RustRect,
2525
},
26-
consts::KWARGS_QUERYSTRINGS,
2726
models::{
2827
decimal::InnerDecimal, interval::InnerInterval, serde_value::InternalSerdeValue,
2928
uuid::InternalUuid,

0 commit comments

Comments
 (0)