Skip to content

Commit

Permalink
Added more cursor method.
Browse files Browse the repository at this point in the history
  • Loading branch information
chandr-andr committed Feb 20, 2024
1 parent 8aa7406 commit dad2f62
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
22 changes: 22 additions & 0 deletions python/psqlpy/_internal/__init__.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
38 changes: 38 additions & 0 deletions src/driver/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit dad2f62

Please sign in to comment.