Skip to content

Commit 639594e

Browse files
committed
Continue implementing new realization. pre-commit disabled.
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent 4025ac3 commit 639594e

File tree

3 files changed

+40
-84
lines changed

3 files changed

+40
-84
lines changed

src/driver/connection_pool.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -419,13 +419,10 @@ impl ConnectionPool {
419419
pub async fn execute<'a>(
420420
self_: pyo3::Py<Self>,
421421
querystring: String,
422-
prepared: bool,
422+
prepared: Option<bool>,
423423
parameters: Option<pyo3::Py<PyAny>>,
424424
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
425-
let db_pool = pyo3::Python::with_gil(|gil| {
426-
let this = self_.borrow(gil);
427-
this.db_pool.clone().unwrap()
428-
});
425+
let db_pool = pyo3::Python::with_gil(|gil| self_.borrow(gil).db_pool.clone().unwrap());
429426
let db_pool_manager = tokio()
430427
.spawn(async move { db_pool.get().await.unwrap() })
431428
.await
@@ -434,6 +431,7 @@ impl ConnectionPool {
434431
if let Some(parameters) = parameters {
435432
params = convert_parameters(parameters)?;
436433
}
434+
let prepared = prepared.unwrap_or(true);
437435
let result = if prepared {
438436
tokio()
439437
.spawn(async move {

src/extra_types.rs

Lines changed: 2 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use std::str::FromStr;
22

33
use macaddr::{MacAddr6, MacAddr8};
4-
use pyo3::{pyclass, pymethods, types::PyModule, PyAny, PyResult, Python};
4+
use pyo3::{pyclass, pymethods, types::PyModule, Py, PyAny, PyResult, Python};
55
use serde_json::Value;
66
use uuid::Uuid;
77

@@ -88,7 +88,7 @@ impl PyJSON {
8888
impl PyJSON {
8989
#[new]
9090
#[allow(clippy::missing_errors_doc)]
91-
pub fn new_json(value: &PyAny) -> RustPSQLDriverPyResult<Self> {
91+
pub fn new_json(value: Py<PyAny>) -> RustPSQLDriverPyResult<Self> {
9292
Ok(Self {
9393
inner: build_serde_value(value)?,
9494
})
@@ -126,50 +126,6 @@ macro_rules! build_macaddr_type {
126126
build_macaddr_type!(PyMacAddr6, MacAddr6);
127127
build_macaddr_type!(PyMacAddr8, MacAddr8);
128128

129-
// #[pyclass]
130-
// #[derive(Clone)]
131-
// pub struct PyMacAddr6 {
132-
// inner: MacAddr6,
133-
// }
134-
135-
// impl PyMacAddr6 {
136-
// #[must_use]
137-
// pub fn inner(self) -> MacAddr6 {
138-
// self.inner
139-
// }
140-
// }
141-
142-
// #[pymethods]
143-
// impl PyMacAddr6 {
144-
// #[new]
145-
// #[allow(clippy::missing_errors_doc)]
146-
// pub fn new_macaddr6(value: &str) -> RustPSQLDriverPyResult<Self> {
147-
// Ok(Self {
148-
// inner: MacAddr6::from_str(value)?,
149-
// })
150-
// }
151-
// }
152-
153-
// #[pyclass]
154-
// #[derive(Clone)]
155-
// pub struct PyMacAddr8 {
156-
// inner: MacAddr8,
157-
// }
158-
159-
// impl PyMacAddr8 {
160-
// #[must_use]
161-
// pub fn inner(self) -> MacAddr8 {
162-
// self.inner
163-
// }
164-
// }
165-
166-
// #[pymethods]
167-
// impl PyMacAddr8 {
168-
// #[new]
169-
// #[allow(clippy::missing_errors_doc)]
170-
// pub fn new_macaddr8(value: &str) -> RustPSQLDriverPyResult<Self> {}
171-
// }
172-
173129
#[allow(clippy::module_name_repetitions)]
174130
#[allow(clippy::missing_errors_doc)]
175131
pub fn extra_types_module(_py: Python<'_>, pymod: &PyModule) -> PyResult<()> {

src/value_converter.rs

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ use bytes::{BufMut, BytesMut};
88
use postgres_protocol::types;
99
use pyo3::{
1010
types::{
11-
PyAnyMethods, PyBool, PyBytes, PyDate, PyDateTime, PyDict, PyFloat, PyInt, PyList, PySet,
12-
PyString, PyTime, PyTuple,
11+
PyAnyMethods, PyBool, PyBytes, PyDate, PyDateTime, PyDict, PyDictMethods, PyFloat, PyInt,
12+
PyList, PySet, PyString, PyTime, PyTuple,
1313
},
1414
Bound, Py, PyAny, Python, ToPyObject,
1515
};
@@ -336,9 +336,8 @@ pub fn py_to_rust(parameter: &pyo3::Bound<'_, PyAny>) -> RustPSQLDriverPyResult<
336336

337337
let mut serde_map: Map<String, Value> = Map::new();
338338

339-
for dict_item in dict.iter().unwrap() {
340-
let dict_elem = dict_item.unwrap();
341-
let py_list = dict_elem.downcast::<PyTuple>().map_err(|error| {
339+
for dict_item in dict.items() {
340+
let py_list = dict_item.downcast::<PyTuple>().map_err(|error| {
342341
RustPSQLDriverError::PyToRustValueConversionError(format!(
343342
"Cannot cast to list: {error}"
344343
))
@@ -528,34 +527,37 @@ pub fn postgres_to_py(
528527
///
529528
/// # Errors
530529
/// May return error if cannot convert Python type into Rust one.
531-
pub fn build_serde_value(value: &PyAny) -> RustPSQLDriverPyResult<Value> {
532-
Ok(json!(123))
533-
// if value.is_instance_of::<PyList>() {
534-
// let mut result_vec: Vec<Value> = vec![];
535-
536-
// let params: Vec<&PyAny> = value.extract::<Vec<&PyAny>>()?;
537-
538-
// for inner in &params {
539-
// if inner.is_instance_of::<PyDict>() {
540-
// let python_dto = py_to_rust(inner)?;
541-
// result_vec.push(python_dto.to_serde_value()?);
542-
// } else if inner.is_instance_of::<PyList>() {
543-
// let serde_value = build_serde_value(inner)?;
544-
// result_vec.push(serde_value);
545-
// } else {
546-
// return Err(RustPSQLDriverError::PyToRustValueConversionError(
547-
// "PyJSON supports only list of lists or list of dicts.".to_string(),
548-
// ));
549-
// }
550-
// }
551-
// Ok(json!(result_vec))
552-
// } else if value.is_instance_of::<PyDict>() {
553-
// return py_to_rust(value)?.to_serde_value();
554-
// } else {
555-
// return Err(RustPSQLDriverError::PyToRustValueConversionError(
556-
// "PyJSON must be list value.".to_string(),
557-
// ));
558-
// }
530+
pub fn build_serde_value(value: Py<PyAny>) -> RustPSQLDriverPyResult<Value> {
531+
Python::with_gil(|gil| {
532+
let bind_value = value.bind(gil);
533+
if bind_value.is_instance_of::<PyList>() {
534+
let mut result_vec: Vec<Value> = vec![];
535+
536+
let params = bind_value.extract::<Vec<Py<PyAny>>>()?;
537+
538+
for inner in params {
539+
let inner_bind = inner.bind(gil);
540+
if inner_bind.is_instance_of::<PyDict>() {
541+
let python_dto = py_to_rust(inner_bind)?;
542+
result_vec.push(python_dto.to_serde_value()?);
543+
} else if inner_bind.is_instance_of::<PyList>() {
544+
let serde_value = build_serde_value(inner)?;
545+
result_vec.push(serde_value);
546+
} else {
547+
return Err(RustPSQLDriverError::PyToRustValueConversionError(
548+
"PyJSON supports only list of lists or list of dicts.".to_string(),
549+
));
550+
}
551+
}
552+
Ok(json!(result_vec))
553+
} else if bind_value.is_instance_of::<PyDict>() {
554+
return py_to_rust(bind_value)?.to_serde_value();
555+
} else {
556+
return Err(RustPSQLDriverError::PyToRustValueConversionError(
557+
"PyJSON must be list value.".to_string(),
558+
));
559+
}
560+
})
559561
}
560562

561563
/// Convert serde `Value` into Python object.

0 commit comments

Comments
 (0)