Skip to content

Commit 7c03935

Browse files
committed
Continue implementing new realization. Implementing Cursor. pre-commit disabled.
Signed-off-by: chandr-andr (Kiselev Aleksandr) <chandr@chandr.net>
1 parent edd4e81 commit 7c03935

File tree

1 file changed

+200
-12
lines changed

1 file changed

+200
-12
lines changed

src/driver/cursor.rs

Lines changed: 200 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -513,20 +513,18 @@
513513
// }
514514
// }
515515

516-
use std::future::IntoFuture;
517516
use std::sync::Arc;
518517

519518
use deadpool_postgres::Object;
520-
use pyo3::coroutine::Coroutine;
521-
use pyo3::{exceptions::PyStopAsyncIteration, pyclass, pymethods, Py, PyAny, PyErr, Python};
522-
use pyo3::{pyfunction, PyObject};
519+
use pyo3::{
520+
exceptions::PyStopAsyncIteration, pyclass, pymethods, Py, PyAny, PyErr, PyObject, Python,
521+
};
523522

524523
use crate::runtime::rustdriver_future;
525524
use crate::{
526525
common::ObjectQueryTrait,
527526
exceptions::rust_errors::{RustPSQLDriverError, RustPSQLDriverPyResult},
528527
query_result::PSQLDriverPyQueryResult,
529-
runtime::tokio_runtime,
530528
};
531529

532530
trait CursorObjectTrait {
@@ -591,11 +589,6 @@ impl CursorObjectTrait for Object {
591589
}
592590
}
593591

594-
#[pyfunction]
595-
async fn test() -> usize {
596-
42
597-
}
598-
599592
#[pyclass]
600593
pub struct Cursor {
601594
db_transaction: Arc<Object>,
@@ -710,7 +703,7 @@ impl Cursor {
710703
let future = rustdriver_future(gil, async move {
711704
let result = db_transaction
712705
.psqlpy_query(
713-
format!("FETCH {} FROM {}", fetch_number, cursor_name),
706+
format!("FETCH {fetch_number} FROM {cursor_name}"),
714707
None,
715708
Some(false),
716709
)
@@ -792,7 +785,202 @@ impl Cursor {
792785

793786
let result = db_transaction
794787
.psqlpy_query(
795-
format!("FETCH {fetch_number} FROM {}", cursor_name),
788+
format!("FETCH {fetch_number} FROM {cursor_name}"),
789+
None,
790+
Some(false),
791+
)
792+
.await?;
793+
Ok(result)
794+
}
795+
796+
/// Fetch row from cursor.
797+
///
798+
/// Execute FETCH NEXT.
799+
///
800+
/// # Errors
801+
/// May return Err Result if cannot execute query.
802+
pub async fn fetch_next<'a>(slf: Py<Self>) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
803+
let (db_transaction, cursor_name) = Python::with_gil(|gil| {
804+
let self_ = slf.borrow(gil);
805+
return (self_.db_transaction.clone(), self_.cursor_name.clone());
806+
});
807+
808+
let result = db_transaction
809+
.psqlpy_query(format!("FETCH NEXT FROM {cursor_name}"), None, Some(false))
810+
.await?;
811+
Ok(result)
812+
}
813+
814+
/// Fetch previous from cursor.
815+
///
816+
/// Execute FETCH PRIOR.
817+
///
818+
/// # Errors
819+
/// May return Err Result if cannot execute query.
820+
pub async fn fetch_prior<'a>(slf: Py<Self>) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
821+
let (db_transaction, cursor_name) = Python::with_gil(|gil| {
822+
let self_ = slf.borrow(gil);
823+
return (self_.db_transaction.clone(), self_.cursor_name.clone());
824+
});
825+
826+
let result = db_transaction
827+
.psqlpy_query(format!("FETCH PRIOR FROM {cursor_name}"), None, Some(false))
828+
.await?;
829+
Ok(result)
830+
}
831+
832+
/// Fetch first row from cursor.
833+
///
834+
/// Execute FETCH FIRST (same as ABSOLUTE 1)
835+
///
836+
/// # Errors
837+
/// May return Err Result if cannot execute query.
838+
pub async fn fetch_first<'a>(slf: Py<Self>) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
839+
let (db_transaction, cursor_name) = Python::with_gil(|gil| {
840+
let self_ = slf.borrow(gil);
841+
return (self_.db_transaction.clone(), self_.cursor_name.clone());
842+
});
843+
844+
let result = db_transaction
845+
.psqlpy_query(format!("FETCH FIRST FROM {cursor_name}"), None, Some(false))
846+
.await?;
847+
Ok(result)
848+
}
849+
850+
/// Fetch last row from cursor.
851+
///
852+
/// Execute FETCH LAST (same as ABSOLUTE -1)
853+
///
854+
/// # Errors
855+
/// May return Err Result if cannot execute query.
856+
pub async fn fetch_last<'a>(slf: Py<Self>) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
857+
let (db_transaction, cursor_name) = Python::with_gil(|gil| {
858+
let self_ = slf.borrow(gil);
859+
return (self_.db_transaction.clone(), self_.cursor_name.clone());
860+
});
861+
862+
let result = db_transaction
863+
.psqlpy_query(format!("FETCH LAST FROM {cursor_name}"), None, Some(false))
864+
.await?;
865+
Ok(result)
866+
}
867+
868+
/// Fetch absolute row from cursor.
869+
///
870+
/// Execute FETCH ABSOLUTE<absolute_number>.
871+
///
872+
/// # Errors
873+
/// May return Err Result if cannot execute query.
874+
pub async fn fetch_absolute<'a>(
875+
slf: Py<Self>,
876+
absolute_number: i64,
877+
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
878+
let (db_transaction, cursor_name) = Python::with_gil(|gil| {
879+
let self_ = slf.borrow(gil);
880+
return (self_.db_transaction.clone(), self_.cursor_name.clone());
881+
});
882+
883+
let result = db_transaction
884+
.psqlpy_query(
885+
format!("FETCH ABSOLUTE {absolute_number} FROM {cursor_name}"),
886+
None,
887+
Some(false),
888+
)
889+
.await?;
890+
Ok(result)
891+
}
892+
893+
/// Fetch absolute row from cursor.
894+
///
895+
/// Execute FETCH ABSOLUTE<absolute_number>.
896+
///
897+
/// # Errors
898+
/// May return Err Result if cannot execute query.
899+
pub async fn fetch_relative<'a>(
900+
slf: Py<Self>,
901+
relative_number: i64,
902+
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
903+
let (db_transaction, cursor_name) = Python::with_gil(|gil| {
904+
let self_ = slf.borrow(gil);
905+
return (self_.db_transaction.clone(), self_.cursor_name.clone());
906+
});
907+
908+
let result = db_transaction
909+
.psqlpy_query(
910+
format!("FETCH RELATIVE {relative_number} FROM {cursor_name}"),
911+
None,
912+
Some(false),
913+
)
914+
.await?;
915+
Ok(result)
916+
}
917+
918+
/// Fetch forward all from cursor.
919+
///
920+
/// Execute FORWARD ALL.
921+
///
922+
/// # Errors
923+
/// May return Err Result if cannot execute query.
924+
pub async fn fetch_forward_all<'a>(
925+
slf: Py<Self>,
926+
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
927+
let (db_transaction, cursor_name) = Python::with_gil(|gil| {
928+
let self_ = slf.borrow(gil);
929+
return (self_.db_transaction.clone(), self_.cursor_name.clone());
930+
});
931+
932+
let result = db_transaction
933+
.psqlpy_query(
934+
format!("FETCH FORWARD ALL FROM {cursor_name}"),
935+
None,
936+
Some(false),
937+
)
938+
.await?;
939+
Ok(result)
940+
}
941+
942+
/// Fetch backward from cursor.
943+
///
944+
/// Execute BACKWARD <backward_count>.
945+
///
946+
/// # Errors
947+
/// May return Err Result if cannot execute query.
948+
pub async fn fetch_backward<'a>(
949+
slf: Py<Self>,
950+
backward_count: i64,
951+
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
952+
let (db_transaction, cursor_name) = Python::with_gil(|gil| {
953+
let self_ = slf.borrow(gil);
954+
return (self_.db_transaction.clone(), self_.cursor_name.clone());
955+
});
956+
957+
let result = db_transaction
958+
.psqlpy_query(
959+
format!("FETCH BACKWARD {backward_count} FROM {cursor_name}",),
960+
None,
961+
Some(false),
962+
)
963+
.await?;
964+
Ok(result)
965+
}
966+
967+
/// Fetch backward from cursor.
968+
///
969+
/// Execute BACKWARD <backward_count>.
970+
///
971+
/// # Errors
972+
/// May return Err Result if cannot execute query.
973+
pub async fn fetch_backward_all<'a>(
974+
slf: Py<Self>,
975+
) -> RustPSQLDriverPyResult<PSQLDriverPyQueryResult> {
976+
let (db_transaction, cursor_name) = Python::with_gil(|gil| {
977+
let self_ = slf.borrow(gil);
978+
return (self_.db_transaction.clone(), self_.cursor_name.clone());
979+
});
980+
981+
let result = db_transaction
982+
.psqlpy_query(
983+
format!("FETCH BACKWARD ALL FROM {cursor_name}"),
796984
None,
797985
Some(false),
798986
)

0 commit comments

Comments
 (0)