From dad2f62e5846d361b9543b1d72d77a4572c71ac1 Mon Sep 17 00:00:00 2001 From: "chandr-andr (Kiselev Aleksandr)" Date: Tue, 20 Feb 2024 21:37:17 +0100 Subject: [PATCH] Added more cursor method. --- python/psqlpy/_internal/__init__.pyi | 22 ++++++++++++++++ src/driver/cursor.rs | 38 ++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+) diff --git a/python/psqlpy/_internal/__init__.pyi b/python/psqlpy/_internal/__init__.pyi index cb9b7b40..40481a8c 100644 --- a/python/psqlpy/_internal/__init__.pyi +++ b/python/psqlpy/_internal/__init__.pyi @@ -49,6 +49,28 @@ class Cursor: result as `QueryResult`. """ + async def fetch_next( + self: Self, + ) -> QueryResult: + """Fetch next row. + + Execute FETCH NEXT + + ### Returns: + result as `QueryResult`. + """ + + async def fetch_prior( + self: Self, + ) -> QueryResult: + """Fetch previous row. + + Execute FETCH PRIOR + + ### Returns: + result as `QueryResult`. + """ + async def close(self: Self) -> None: """Close the cursor. diff --git a/src/driver/cursor.rs b/src/driver/cursor.rs index 1903fb47..7eac6b4a 100644 --- a/src/driver/cursor.rs +++ b/src/driver/cursor.rs @@ -62,6 +62,44 @@ impl Cursor { }) } + /// Fetch row from cursor. + /// + /// Execute FETCH NEXT. + /// + /// # Errors + /// May return Err Result if cannot execute query. + pub fn fetch_next<'a>(&'a self, py: Python<'a>) -> RustPSQLDriverPyResult<&PyAny> { + let db_client_arc = self.db_client.clone(); + let cursor_name = self.cursor_name.clone(); + + rustengine_future(py, async move { + let db_client_guard = db_client_arc.read().await; + let result = db_client_guard + .query(format!("FETCH NEXT FROM {cursor_name}").as_str(), &[]) + .await?; + Ok(PSQLDriverPyQueryResult::new(result)) + }) + } + + /// Fetch previous from cursor. + /// + /// Execute FETCH PRIOR. + /// + /// # Errors + /// May return Err Result if cannot execute query. + pub fn fetch_prior<'a>(&'a self, py: Python<'a>) -> RustPSQLDriverPyResult<&PyAny> { + let db_client_arc = self.db_client.clone(); + let cursor_name = self.cursor_name.clone(); + + rustengine_future(py, async move { + let db_client_guard = db_client_arc.read().await; + let result = db_client_guard + .query(format!("FETCH PRIOR FROM {cursor_name}").as_str(), &[]) + .await?; + Ok(PSQLDriverPyQueryResult::new(result)) + }) + } + #[must_use] pub fn __aiter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> { slf