Skip to content

Commit 9ac5b58

Browse files
committed
Added FORWARD ALL cursor method.
1 parent 8fc7493 commit 9ac5b58

File tree

2 files changed

+51
-17
lines changed

2 files changed

+51
-17
lines changed

python/psqlpy/_internal/__init__.pyi

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ class Cursor:
116116
### Returns:
117117
result as `QueryResult`.
118118
"""
119+
119120
async def fetch_relative(
120121
self: Self,
121122
relative_number: int,
@@ -128,6 +129,17 @@ class Cursor:
128129
result as `QueryResult`.
129130
"""
130131

132+
async def fetch_forward_all(
133+
self: Self,
134+
) -> QueryResult:
135+
"""Fetch forward all rows.
136+
137+
Execute FETCH FORWARD ALL.
138+
139+
### Returns:
140+
result as `QueryResult`.
141+
"""
142+
131143
async def close(self: Self) -> None:
132144
"""Close the cursor.
133145

src/driver/cursor.rs

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,35 @@ impl Cursor {
3232

3333
#[pymethods]
3434
impl Cursor {
35+
#[must_use]
36+
pub fn __aiter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
37+
slf
38+
}
39+
40+
pub fn __anext__(&self, py: Python<'_>) -> RustPSQLDriverPyResult<Option<PyObject>> {
41+
let db_client_arc = self.db_client.clone();
42+
let cursor_name = self.cursor_name.clone();
43+
let fetch_number = self.fetch_number;
44+
45+
let future = rustengine_future(py, async move {
46+
let db_client_guard = db_client_arc.read().await;
47+
let result = db_client_guard
48+
.query(
49+
format!("FETCH {fetch_number} FROM {cursor_name}").as_str(),
50+
&[],
51+
)
52+
.await?;
53+
54+
if result.is_empty() {
55+
return Err(PyStopAsyncIteration::new_err("Error").into());
56+
};
57+
58+
Ok(PSQLDriverPyQueryResult::new(result))
59+
});
60+
61+
Ok(Some(future?.into()))
62+
}
63+
3564
/// Fetch data from cursor.
3665
///
3766
/// It's possible to specify fetch number.
@@ -190,33 +219,26 @@ impl Cursor {
190219
})
191220
}
192221

193-
#[must_use]
194-
pub fn __aiter__(slf: PyRef<'_, Self>) -> PyRef<'_, Self> {
195-
slf
196-
}
197-
198-
pub fn __anext__(&self, py: Python<'_>) -> RustPSQLDriverPyResult<Option<PyObject>> {
222+
/// Fetch relative row from cursor.
223+
///
224+
/// Execute FETCH RELATIVE<absolute_number>.
225+
///
226+
/// # Errors
227+
/// May return Err Result if cannot execute query.
228+
pub fn fetch_forward_all<'a>(&'a self, py: Python<'a>) -> RustPSQLDriverPyResult<&PyAny> {
199229
let db_client_arc = self.db_client.clone();
200230
let cursor_name = self.cursor_name.clone();
201-
let fetch_number = self.fetch_number;
202231

203-
let future = rustengine_future(py, async move {
232+
rustengine_future(py, async move {
204233
let db_client_guard = db_client_arc.read().await;
205234
let result = db_client_guard
206235
.query(
207-
format!("FETCH {fetch_number} FROM {cursor_name}").as_str(),
236+
format!("FETCH FORWARD ALL FROM {cursor_name}").as_str(),
208237
&[],
209238
)
210239
.await?;
211-
212-
if result.is_empty() {
213-
return Err(PyStopAsyncIteration::new_err("Error").into());
214-
};
215-
216240
Ok(PSQLDriverPyQueryResult::new(result))
217-
});
218-
219-
Ok(Some(future?.into()))
241+
})
220242
}
221243

222244
/// Close cursor.

0 commit comments

Comments
 (0)