Skip to content

Try to support new pyo3 interfaces #141

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
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
4 changes: 3 additions & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ ci:

repos:
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v2.1.0
rev: v5.0.0
hooks:
- id: trailing-whitespace
- repo: https://github.com/pre-commit/mirrors-mypy
Expand Down Expand Up @@ -58,6 +58,8 @@ repos:
- clippy::all
- -W
- clippy::pedantic
# - -D
# - warnings

- id: check
types:
Expand Down
10 changes: 0 additions & 10 deletions python/psqlpy/_internal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -815,16 +815,6 @@ class Connection:

Return representation of prepared statement.
"""
async def commit(self: Self) -> None:
"""Commit the transaction.

Do nothing if there is no active transaction.
"""
async def rollback(self: Self) -> None:
"""Rollback the transaction.

Do nothing if there is no active transaction.
"""
async def execute(
self: Self,
querystring: str,
Expand Down
47 changes: 23 additions & 24 deletions src/connection/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ where
) -> PSQLPyResult<()> {
let start_qs = self.build_start_qs(isolation_level, read_variant, deferrable);
self.batch_execute(start_qs.as_str()).await.map_err(|err| {
RustPSQLDriverError::TransactionBeginError(
format!("Cannot start transaction due to - {err}").into(),
)
RustPSQLDriverError::TransactionBeginError(format!(
"Cannot start transaction due to - {err}"
))
})?;

Ok(())
Expand Down Expand Up @@ -65,7 +65,7 @@ impl Connection for SingleConnection {
if !prepared {
self.drop_prepared(&prepared_stmt).await?;
}
return Ok(prepared_stmt);
Ok(prepared_stmt)
}

async fn drop_prepared(&self, stmt: &Statement) -> PSQLPyResult<()> {
Expand Down Expand Up @@ -116,28 +116,27 @@ impl StartTransaction for SingleConnection {
read_variant: Option<ReadVariant>,
deferrable: Option<bool>,
) -> PSQLPyResult<()> {
let res = self
._start_transaction(isolation_level, read_variant, deferrable)
self._start_transaction(isolation_level, read_variant, deferrable)
.await?;
self.in_transaction = true;

Ok(res)
Ok(())
}
}

impl CloseTransaction for SingleConnection {
async fn commit(&mut self) -> PSQLPyResult<()> {
let res = self._commit().await?;
self._commit().await?;
self.in_transaction = false;

Ok(res)
Ok(())
}

async fn rollback(&mut self) -> PSQLPyResult<()> {
let res = self._rollback().await?;
self._rollback().await?;
self.in_transaction = false;

Ok(res)
Ok(())
}
}

Expand All @@ -149,7 +148,7 @@ impl Connection for PoolConnection {

let prepared = self.connection.prepare(query).await?;
self.drop_prepared(&prepared).await?;
return Ok(prepared);
Ok(prepared)
}

async fn drop_prepared(&self, stmt: &Statement) -> PSQLPyResult<()> {
Expand Down Expand Up @@ -208,17 +207,17 @@ impl StartTransaction for PoolConnection {

impl CloseTransaction for PoolConnection {
async fn commit(&mut self) -> PSQLPyResult<()> {
let res = self._commit().await?;
self._commit().await?;
self.in_transaction = false;

Ok(res)
Ok(())
}

async fn rollback(&mut self) -> PSQLPyResult<()> {
let res = self._rollback().await?;
self._rollback().await?;
self.in_transaction = false;

Ok(res)
Ok(())
}
}

Expand Down Expand Up @@ -407,14 +406,14 @@ impl PSQLPyConnection {

for statement in statements {
let querystring_result = if prepared {
let prepared_stmt = &self.prepare(&statement.raw_query(), true).await;
let prepared_stmt = &self.prepare(statement.raw_query(), true).await;
if let Err(error) = prepared_stmt {
return Err(RustPSQLDriverError::ConnectionExecuteError(format!(
"Cannot prepare statement in execute_many, operation rolled back {error}",
)));
}
self.query(
&self.prepare(&statement.raw_query(), true).await?,
&self.prepare(statement.raw_query(), true).await?,
&statement.params(),
)
.await
Expand All @@ -429,7 +428,7 @@ impl PSQLPyConnection {
}
}

return Ok(());
Ok(())
}

pub async fn fetch_row_raw(
Expand All @@ -447,7 +446,7 @@ impl PSQLPyConnection {
let result = if prepared {
self.query_one(
&self
.prepare(&statement.raw_query(), true)
.prepare(statement.raw_query(), true)
.await
.map_err(|err| {
RustPSQLDriverError::ConnectionExecuteError(format!(
Expand All @@ -464,7 +463,7 @@ impl PSQLPyConnection {
.map_err(|err| RustPSQLDriverError::ConnectionExecuteError(format!("{err}")))?
};

return Ok(result);
Ok(result)
}

pub async fn fetch_row(
Expand All @@ -477,7 +476,7 @@ impl PSQLPyConnection {
.fetch_row_raw(querystring, parameters, prepared)
.await?;

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

pub async fn fetch_val(
Expand All @@ -490,10 +489,10 @@ impl PSQLPyConnection {
.fetch_row_raw(querystring, parameters, prepared)
.await?;

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

pub async fn copy_in<T, U>(&self, statement: &T) -> PSQLPyResult<CopyInSink<U>>
Expand Down
1 change: 0 additions & 1 deletion src/connection/traits.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use postgres_types::{ToSql, Type};
use pyo3::PyAny;
use tokio_postgres::{Row, Statement, ToStatement};

use crate::exceptions::rust_errors::PSQLPyResult;
Expand Down
2 changes: 1 addition & 1 deletion src/driver/connection_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ impl ConnectionPool {
prepare: Option<bool>,
) -> Self {
ConnectionPool {
pool: pool,
pool,
pg_config: Arc::new(pg_config),
pool_conf: ConnectionPoolConf::new(ca_file, ssl_mode, prepare.unwrap_or(true)),
}
Expand Down
10 changes: 5 additions & 5 deletions src/driver/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ impl Cursor {
let Some(portal) = &self.inner else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
transaction.query_portal(&portal, size).await
transaction.query_portal(portal, size).await
}
}

Expand Down Expand Up @@ -157,7 +157,7 @@ impl Cursor {
fn __anext__(&self) -> PSQLPyResult<Option<PyObject>> {
let txid = self.transaction.clone();
let portal = self.inner.clone();
let size = self.array_size.clone();
let size = self.array_size;

let py_future = Python::with_gil(move |gil| {
rustdriver_future(gil, async move {
Expand All @@ -167,7 +167,7 @@ impl Cursor {
let Some(portal) = &portal else {
return Err(RustPSQLDriverError::TransactionClosedError);
};
let result = txid.query_portal(&portal, size).await?;
let result = txid.query_portal(portal, size).await?;

if result.is_empty() {
return Err(PyStopAsyncIteration::new_err(
Expand All @@ -192,7 +192,7 @@ impl Cursor {
let (txid, inner_portal) = match &self.querystring {
Some(querystring) => {
write_conn_g
.portal(Some(&querystring), &self.parameters, None)
.portal(Some(querystring), &self.parameters, None)
.await?
}
None => {
Expand All @@ -201,7 +201,7 @@ impl Cursor {
"Cannot start cursor".into(),
));
};
write_conn_g.portal(None, &None, Some(&statement)).await?
write_conn_g.portal(None, &None, Some(statement)).await?
}
};

Expand Down
6 changes: 3 additions & 3 deletions src/driver/transaction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ impl Transaction {
.start_transaction(isolation_level, read_variant, deferrable)
.await?;

return Ok(self_);
Ok(self_)
}

#[allow(clippy::needless_pass_by_value)]
Expand Down Expand Up @@ -114,7 +114,7 @@ impl Transaction {
let mut self_ = self_.borrow_mut(gil);
self_.conn = None;
});
return Err(RustPSQLDriverError::RustPyError(py_err));
Err(RustPSQLDriverError::RustPyError(py_err))
}
}

Expand Down Expand Up @@ -277,7 +277,7 @@ impl Transaction {
let mut futures = vec![];
if let Some(queries) = queries {
let gil_result = pyo3::Python::with_gil(|gil| -> PyResult<()> {
for single_query in queries.into_bound(gil).iter() {
for single_query in queries.into_bound(gil).try_iter() {
let query_tuple = single_query.downcast::<PyTuple>().map_err(|err| {
RustPSQLDriverError::PyToRustValueConversionError(format!(
"Cannot cast to tuple: {err}",
Expand Down
27 changes: 11 additions & 16 deletions src/query_result.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
use pyo3::{
prelude::*,
pyclass, pymethods,
types::{PyDict, PyTuple},
Py, PyAny, Python, ToPyObject,
};
use pyo3::{prelude::*, pyclass, pymethods, types::PyDict, IntoPyObjectExt, Py, PyAny, Python};
use tokio_postgres::Row;

use crate::{exceptions::rust_errors::PSQLPyResult, value_converter::to_python::postgres_to_py};
Expand All @@ -24,7 +19,7 @@ fn row_to_dict<'a>(
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)?;
python_dict.set_item(column.name().into_py_any(py)?, python_type)?;
}
Ok(python_dict)
}
Expand Down Expand Up @@ -72,7 +67,7 @@ impl PSQLDriverPyQueryResult {
for row in &self.inner {
result.push(row_to_dict(py, row, &custom_decoders)?);
}
Ok(result.to_object(py))
Ok(result.into_py_any(py)?)
}

/// Convert result from database to any class passed from Python.
Expand All @@ -83,14 +78,14 @@ impl PSQLDriverPyQueryResult {
/// postgres type to python or create new Python class.
#[allow(clippy::needless_pass_by_value)]
pub fn as_class<'a>(&'a self, py: Python<'a>, as_class: Py<PyAny>) -> PSQLPyResult<Py<PyAny>> {
let mut res: Vec<Py<PyAny>> = vec![];
let mut result: Vec<Py<PyAny>> = vec![];
for row in &self.inner {
let pydict: pyo3::Bound<'_, PyDict> = row_to_dict(py, row, &None)?;
let convert_class_inst = as_class.call(py, (), Some(&pydict))?;
res.push(convert_class_inst);
result.push(convert_class_inst);
}

Ok(res.to_object(py))
Ok(result.into_py_any(py)?)
}

/// Convert result from database with function passed from Python.
Expand All @@ -107,13 +102,13 @@ impl PSQLDriverPyQueryResult {
row_factory: Py<PyAny>,
custom_decoders: Option<Py<PyDict>>,
) -> PSQLPyResult<Py<PyAny>> {
let mut res: Vec<Py<PyAny>> = vec![];
let mut result: Vec<Py<PyAny>> = vec![];
for row in &self.inner {
let pydict: pyo3::Bound<'_, PyDict> = row_to_dict(py, row, &custom_decoders)?;
let row_factory_class = row_factory.call(py, (pydict,), None)?;
res.push(row_factory_class);
result.push(row_factory_class);
}
Ok(res.to_object(py))
Ok(result.into_py_any(py)?)
}
}

Expand Down Expand Up @@ -154,7 +149,7 @@ impl PSQLDriverSinglePyQueryResult {
py: Python<'_>,
custom_decoders: Option<Py<PyDict>>,
) -> PSQLPyResult<Py<PyAny>> {
Ok(row_to_dict(py, &self.inner, &custom_decoders)?.to_object(py))
Ok(row_to_dict(py, &self.inner, &custom_decoders)?.into_py_any(py)?)
}

/// Convert result from database to any class passed from Python.
Expand Down Expand Up @@ -184,7 +179,7 @@ impl PSQLDriverSinglePyQueryResult {
row_factory: Py<PyAny>,
custom_decoders: Option<Py<PyDict>>,
) -> PSQLPyResult<Py<PyAny>> {
let pydict = row_to_dict(py, &self.inner, &custom_decoders)?.to_object(py);
let pydict = row_to_dict(py, &self.inner, &custom_decoders)?.into_py_any(py)?;
Ok(row_factory.call(py, (pydict,), None)?)
}
}
11 changes: 7 additions & 4 deletions src/row_factories.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use pyo3::{
pyclass, pyfunction, pymethods,
types::{PyDict, PyDictMethods, PyModule, PyModuleMethods, PyTuple},
wrap_pyfunction, Bound, Py, PyAny, PyResult, Python, ToPyObject,
wrap_pyfunction, Bound, IntoPyObject, Py, PyAny, PyResult, Python,
};

use crate::exceptions::rust_errors::{PSQLPyResult, RustPSQLDriverError};
Expand All @@ -14,7 +14,10 @@ fn tuple_row(py: Python<'_>, dict_: Py<PyAny>) -> PSQLPyResult<Py<PyAny>> {
"as_tuple accepts only dict as a parameter".into(),
)
})?;
Ok(PyTuple::new_bound(py, dict_.items()).to_object(py))
match PyTuple::new(py, dict_.items())?.into_pyobject(py) {
Ok(x) => Ok(x.unbind().into_any()),
_ => unreachable!(),
}
}

#[pyclass]
Expand All @@ -24,7 +27,7 @@ struct class_row(Py<PyAny>);
#[pymethods]
impl class_row {
#[new]
fn constract_class(class_: Py<PyAny>) -> Self {
fn construct_class(class_: Py<PyAny>) -> Self {
Self(class_)
}

Expand All @@ -35,7 +38,7 @@ impl class_row {
"as_tuple accepts only dict as a parameter".into(),
)
})?;
Ok(self.0.call_bound(py, (), Some(dict_))?)
Ok(self.0.call(py, (), Some(dict_))?)
}
}

Expand Down
Loading
Loading