Skip to content

Commit 1231bdd

Browse files
committed
Fix bindings parsing
1 parent 326b19c commit 1231bdd

File tree

1 file changed

+20
-9
lines changed

1 file changed

+20
-9
lines changed

src/operations/query/python.rs

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
//! Python entry point for running a query.
22
use pyo3::prelude::*;
33
use pyo3::types::PyAny;
4+
use pyo3::types::PyDict;
45
use serde_json::value::Value;
6+
use serde_json::Value;
57

6-
use crate::connection::interface::WrappedConnection;
78
use super::core::{query, select};
9+
use crate::connection::interface::WrappedConnection;
810
use crate::py_future_wrapper;
911

1012

@@ -18,15 +20,24 @@ use crate::py_future_wrapper;
1820
/// # Returns
1921
/// * `Ok(())` - The operation was successful
2022
#[pyfunction]
21-
pub fn rust_query_future<'a>(py: Python<'a>, connection: WrappedConnection, sql: String, bindings: Option<&'a PyAny>) -> Result<&'a PyAny, PyErr> {
23+
pub fn rust_query_future<'a>(
24+
py: Python<'a>,
25+
connection: WrappedConnection,
26+
sql: String,
27+
bindings: Option<&'a PyDict>,
28+
) -> Result<&'a PyAny, PyErr> {
29+
let processed_bindings = bindings
30+
.map(|dict| {
31+
let value = dict.extract::<Value>().map_err(|e| {
32+
PyErr::new::<pyo3::exceptions::PyValueError, _>(format!(
33+
"Failed to convert bindings to Value: {}",
34+
e
35+
))
36+
})?;
37+
Ok(value)
38+
})
39+
.transpose()?;
2240

23-
let processed_bindings = match bindings {
24-
Some(bindings) => {
25-
let bindings: Value = serde_json::from_str(&bindings.to_string()).map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;
26-
Some(bindings)
27-
},
28-
None => None
29-
};
3041
py_future_wrapper!(py, query(connection, sql, processed_bindings))
3142
}
3243

0 commit comments

Comments
 (0)