Skip to content

Refactor value converter #131

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion src/driver/inner_connection.rs
Original file line number Diff line number Diff line change
@@ -8,7 +8,11 @@
use crate::{
exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult},
query_result::{PSQLDriverPyQueryResult, PSQLDriverSinglePyQueryResult},
value_converter::{convert_parameters_and_qs, postgres_to_py, PythonDTO, QueryParameter},
value_converter::{
consts::QueryParameter,
funcs::{from_python::convert_parameters_and_qs, to_python::postgres_to_py},
models::dto::PythonDTO,
},
};

#[allow(clippy::module_name_repetitions)]
@@ -82,12 +86,12 @@
}
}

pub async fn cursor_execute(
&self,
querystring: String,
parameters: Option<pyo3::Py<PyAny>>,
prepared: Option<bool>,
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {

Check warning on line 94 in src/driver/inner_connection.rs

GitHub Actions / clippy

docs for function returning `Result` missing `# Errors` section

warning: docs for function returning `Result` missing `# Errors` section --> src/driver/inner_connection.rs:89:5 | 89 | / pub async fn cursor_execute( 90 | | &self, 91 | | querystring: String, 92 | | parameters: Option<pyo3::Py<PyAny>>, 93 | | prepared: Option<bool>, 94 | | ) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { | |________________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc = note: `-W clippy::missing-errors-doc` implied by `-W clippy::pedantic` = help: to override `-W clippy::pedantic` add `#[allow(clippy::missing_errors_doc)]`
let prepared = prepared.unwrap_or(true);

let (qs, params) = convert_parameters_and_qs(querystring, parameters)?;
@@ -124,12 +128,12 @@
Ok(PSQLDriverPyQueryResult::new(result))
}

pub async fn execute(
&self,
querystring: String,
parameters: Option<pyo3::Py<PyAny>>,
prepared: Option<bool>,
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {

Check warning on line 136 in src/driver/inner_connection.rs

GitHub Actions / clippy

docs for function returning `Result` missing `# Errors` section

warning: docs for function returning `Result` missing `# Errors` section --> src/driver/inner_connection.rs:131:5 | 131 | / pub async fn execute( 132 | | &self, 133 | | querystring: String, 134 | | parameters: Option<pyo3::Py<PyAny>>, 135 | | prepared: Option<bool>, 136 | | ) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> { | |________________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc
let prepared = prepared.unwrap_or(true);

let (qs, params) = convert_parameters_and_qs(querystring, parameters)?;
@@ -166,12 +170,12 @@
Ok(PSQLDriverPyQueryResult::new(result))
}

pub async fn execute_many(
&self,
mut querystring: String,
parameters: Option<Vec<Py<PyAny>>>,
prepared: Option<bool>,
) -> RustPSQLDriverPyResult<()> {

Check warning on line 178 in src/driver/inner_connection.rs

GitHub Actions / clippy

docs for function returning `Result` missing `# Errors` section

warning: docs for function returning `Result` missing `# Errors` section --> src/driver/inner_connection.rs:173:5 | 173 | / pub async fn execute_many( 174 | | &self, 175 | | mut querystring: String, 176 | | parameters: Option<Vec<Py<PyAny>>>, 177 | | prepared: Option<bool>, 178 | | ) -> RustPSQLDriverPyResult<()> { | |___________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc
let prepared = prepared.unwrap_or(true);

let mut params: Vec<Vec<PythonDTO>> = vec![];
@@ -212,15 +216,15 @@
}
}

return Ok(());

Check warning on line 219 in src/driver/inner_connection.rs

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/driver/inner_connection.rs:219:9 | 219 | return Ok(()); | ^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 219 - return Ok(()); 219 + Ok(()) |
}

pub async fn fetch_row_raw(
&self,
querystring: String,
parameters: Option<pyo3::Py<PyAny>>,
prepared: Option<bool>,
) -> RustPSQLDriverPyResult<Row> {

Check warning on line 227 in src/driver/inner_connection.rs

GitHub Actions / clippy

docs for function returning `Result` missing `# Errors` section

warning: docs for function returning `Result` missing `# Errors` section --> src/driver/inner_connection.rs:222:5 | 222 | / pub async fn fetch_row_raw( 223 | | &self, 224 | | querystring: String, 225 | | parameters: Option<pyo3::Py<PyAny>>, 226 | | prepared: Option<bool>, 227 | | ) -> RustPSQLDriverPyResult<Row> { | |____________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc
let prepared = prepared.unwrap_or(true);

let (qs, params) = convert_parameters_and_qs(querystring, parameters)?;
@@ -254,36 +258,36 @@
})?
};

return Ok(result);

Check warning on line 261 in src/driver/inner_connection.rs

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/driver/inner_connection.rs:261:9 | 261 | return Ok(result); | ^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 261 - return Ok(result); 261 + Ok(result) |
}

pub async fn fetch_row(
&self,
querystring: String,
parameters: Option<pyo3::Py<PyAny>>,
prepared: Option<bool>,
) -> RustPSQLDriverPyResult<PSQLDriverSinglePyQueryResult> {

Check warning on line 269 in src/driver/inner_connection.rs

GitHub Actions / clippy

docs for function returning `Result` missing `# Errors` section

warning: docs for function returning `Result` missing `# Errors` section --> src/driver/inner_connection.rs:264:5 | 264 | / pub async fn fetch_row( 265 | | &self, 266 | | querystring: String, 267 | | parameters: Option<pyo3::Py<PyAny>>, 268 | | prepared: Option<bool>, 269 | | ) -> RustPSQLDriverPyResult<PSQLDriverSinglePyQueryResult> { | |______________________________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc
let result = self
.fetch_row_raw(querystring, parameters, prepared)
.await?;

return Ok(PSQLDriverSinglePyQueryResult::new(result));

Check warning on line 274 in src/driver/inner_connection.rs

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/driver/inner_connection.rs:274:9 | 274 | return Ok(PSQLDriverSinglePyQueryResult::new(result)); | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 274 - return Ok(PSQLDriverSinglePyQueryResult::new(result)); 274 + Ok(PSQLDriverSinglePyQueryResult::new(result)) |
}

pub async fn fetch_val(
&self,
querystring: String,
parameters: Option<pyo3::Py<PyAny>>,
prepared: Option<bool>,
) -> RustPSQLDriverPyResult<Py<PyAny>> {

Check warning on line 282 in src/driver/inner_connection.rs

GitHub Actions / clippy

docs for function returning `Result` missing `# Errors` section

warning: docs for function returning `Result` missing `# Errors` section --> src/driver/inner_connection.rs:277:5 | 277 | / pub async fn fetch_val( 278 | | &self, 279 | | querystring: String, 280 | | parameters: Option<pyo3::Py<PyAny>>, 281 | | prepared: Option<bool>, 282 | | ) -> RustPSQLDriverPyResult<Py<PyAny>> { | |__________________________________________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#missing_errors_doc
let result = self
.fetch_row_raw(querystring, parameters, prepared)
.await?;

return Python::with_gil(|gil| match result.columns().first() {
Some(first_column) => postgres_to_py(gil, &result, first_column, 0, &None),
None => Ok(gil.None()),
});

Check warning on line 290 in src/driver/inner_connection.rs

GitHub Actions / clippy

unneeded `return` statement

warning: unneeded `return` statement --> src/driver/inner_connection.rs:287:9 | 287 | / return Python::with_gil(|gil| match result.columns().first() { 288 | | Some(first_column) => postgres_to_py(gil, &result, first_column, 0, &None), 289 | | None => Ok(gil.None()), 290 | | }); | |__________^ | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_return help: remove `return` | 287 ~ Python::with_gil(|gil| match result.columns().first() { 288 + Some(first_column) => postgres_to_py(gil, &result, first_column, 0, &None), 289 + None => Ok(gil.None()), 290 ~ }) |
}

/// Prepare cached statement.
8 changes: 5 additions & 3 deletions src/extra_types.rs
Original file line number Diff line number Diff line change
@@ -10,11 +10,13 @@ use pyo3::{
use serde_json::Value;

use crate::{
additional_types::{Circle as RustCircle, Line as RustLine},
exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult},
value_converter::{
build_flat_geo_coords, build_geo_coords, build_serde_value,
py_sequence_into_postgres_array, PythonDTO,
additional_types::{Circle as RustCircle, Line as RustLine},
funcs::from_python::{
build_flat_geo_coords, build_geo_coords, py_sequence_into_postgres_array,
},
models::{dto::PythonDTO, serde_value::build_serde_value},
},
};

1 change: 0 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod additional_types;
pub mod common;
pub mod driver;
pub mod exceptions;
5 changes: 4 additions & 1 deletion src/query_result.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use pyo3::{prelude::*, pyclass, pymethods, types::PyDict, Py, PyAny, Python, ToPyObject};

Check warning on line 1 in src/query_result.rs

GitHub Actions / clippy

use of deprecated trait `pyo3::ToPyObject`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information.

warning: use of deprecated trait `pyo3::ToPyObject`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information. --> src/query_result.rs:1:78 | 1 | use pyo3::{prelude::*, pyclass, pymethods, types::PyDict, Py, PyAny, Python, ToPyObject}; | ^^^^^^^^^^ | = note: `#[warn(deprecated)]` on by default
use tokio_postgres::Row;

use crate::{exceptions::rust_errors::RustPSQLDriverPyResult, value_converter::postgres_to_py};
use crate::{
exceptions::rust_errors::RustPSQLDriverPyResult,
value_converter::funcs::to_python::postgres_to_py,
};

/// Convert postgres `Row` into Python Dict.
///
@@ -19,7 +22,7 @@
let python_dict = PyDict::new(py);
for (column_idx, column) in postgres_row.columns().iter().enumerate() {
let python_type = postgres_to_py(py, postgres_row, column, column_idx, custom_decoders)?;
python_dict.set_item(column.name().to_object(py), python_type)?;

Check warning on line 25 in src/query_result.rs

GitHub Actions / clippy

use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information.

warning: use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information. --> src/query_result.rs:25:44 | 25 | python_dict.set_item(column.name().to_object(py), python_type)?; | ^^^^^^^^^
}
Ok(python_dict)
}
@@ -67,7 +70,7 @@
for row in &self.inner {
result.push(row_to_dict(py, row, &custom_decoders)?);
}
Ok(result.to_object(py))

Check warning on line 73 in src/query_result.rs

GitHub Actions / clippy

use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information.

warning: use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information. --> src/query_result.rs:73:19 | 73 | Ok(result.to_object(py)) | ^^^^^^^^^
}

/// Convert result from database to any class passed from Python.
@@ -89,7 +92,7 @@
res.push(convert_class_inst);
}

Ok(res.to_object(py))

Check warning on line 95 in src/query_result.rs

GitHub Actions / clippy

use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information.

warning: use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information. --> src/query_result.rs:95:16 | 95 | Ok(res.to_object(py)) | ^^^^^^^^^
}

/// Convert result from database with function passed from Python.
@@ -112,7 +115,7 @@
let row_factory_class = row_factory.call(py, (pydict,), None)?;
res.push(row_factory_class);
}
Ok(res.to_object(py))

Check warning on line 118 in src/query_result.rs

GitHub Actions / clippy

use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information.

warning: use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information. --> src/query_result.rs:118:16 | 118 | Ok(res.to_object(py)) | ^^^^^^^^^
}
}

@@ -153,7 +156,7 @@
py: Python<'_>,
custom_decoders: Option<Py<PyDict>>,
) -> RustPSQLDriverPyResult<Py<PyAny>> {
Ok(row_to_dict(py, &self.inner, &custom_decoders)?.to_object(py))

Check warning on line 159 in src/query_result.rs

GitHub Actions / clippy

use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information.

warning: use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information. --> src/query_result.rs:159:60 | 159 | Ok(row_to_dict(py, &self.inner, &custom_decoders)?.to_object(py)) | ^^^^^^^^^
}

/// Convert result from database to any class passed from Python.
@@ -187,7 +190,7 @@
row_factory: Py<PyAny>,
custom_decoders: Option<Py<PyDict>>,
) -> RustPSQLDriverPyResult<Py<PyAny>> {
let pydict = row_to_dict(py, &self.inner, &custom_decoders)?.to_object(py);

Check warning on line 193 in src/query_result.rs

GitHub Actions / clippy

use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information.

warning: use of deprecated method `pyo3::ToPyObject::to_object`: `ToPyObject` is going to be replaced by `IntoPyObject`. See the migration guide (https://pyo3.rs/v0.23.0/migration) for more information. --> src/query_result.rs:193:70 | 193 | let pydict = row_to_dict(py, &self.inner, &custom_decoders)?.to_object(py); | ^^^^^^^^^
Ok(row_factory.call(py, (pydict,), None)?)
}
}
Loading
Loading