Skip to content

Commit

Permalink
debug
Browse files Browse the repository at this point in the history
  • Loading branch information
Ce11an committed Aug 26, 2024
1 parent 0f3f26c commit 4d4f360
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
35 changes: 22 additions & 13 deletions src/operations/query/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,43 +3,52 @@ use pyo3::prelude::*;
use pyo3::types::PyAny;
use serde_json::value::Value;

use crate::connection::interface::WrappedConnection;
use super::core::{query, select};
use crate::connection::interface::WrappedConnection;
use crate::py_future_wrapper;


/// Creates a new record in the database in an non-async manner.
///
///
/// # Arguments
/// * `connection_id` - The database connection being used for the operation
/// * `table_name` - The name of the table to create the record in
/// * `data` - The data to be inserted into the table
///
///
/// # Returns
/// * `Ok(())` - The operation was successful
#[pyfunction]
pub fn rust_query_future<'a>(py: Python<'a>, connection: WrappedConnection, sql: String, bindings: Option<&'a PyAny>) -> Result<&'a PyAny, PyErr> {

pub fn rust_query_future<'a>(
py: Python<'a>,
connection: WrappedConnection,
sql: String,
bindings: Option<&'a PyAny>,
) -> Result<&'a PyAny, PyErr> {
let processed_bindings = match bindings {
Some(bindings) => {
let bindings: Value = serde_json::from_str(&bindings.to_string()).map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;
let bindings_str = bindings.to_string();
println!("Processed bindings: {}", bindings_str);
let bindings: Value = serde_json::from_str(&bindings_str)
.map_err(|e| PyErr::new::<pyo3::exceptions::PyRuntimeError, _>(e.to_string()))?;
Some(bindings)
},
None => None
}
None => None,
};
py_future_wrapper!(py, query(connection, sql, processed_bindings))
}


/// Performs a select on the database in an non-async manner.
///
///
/// # Arguments
/// * `connection` - The connection to perform the select with
/// * `resource` - The resource to select (can be a table or a range)
///
///
/// # Returns
/// * `Ok(String)` - The result of the select
#[pyfunction]
pub fn rust_select_future(py: Python, connection: WrappedConnection, resource: String) -> Result<&PyAny, PyErr> {
pub fn rust_select_future(
py: Python,
connection: WrappedConnection,
resource: String,
) -> Result<&PyAny, PyErr> {
py_future_wrapper!(py, select(connection, resource))
}
3 changes: 3 additions & 0 deletions surrealdb/async_execution_mixins/query.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ async def query(
:return: None
"""
if bind is not None:
if not all(isinstance(key, str) for key in bind):
raise ValueError("All keys in 'bind' must be strings.")
try:
return json.loads(await rust_query_future(self._connection, query, bind))[0]
except Exception as e:
Expand Down

0 comments on commit 4d4f360

Please sign in to comment.