Skip to content

Commit 0bcf420

Browse files
committed
Added more cursor methods and more info to README
1 parent 29684fe commit 0bcf420

File tree

3 files changed

+75
-2
lines changed

3 files changed

+75
-2
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,8 @@ Available cursor operations:
309309
- FETCH ABSOLUTE - `cursor.fetch_absolute(absolute_number=)`
310310
- FETCH RELATIVE - `cursor.fetch_relative(relative_number=)`
311311
- FETCH FORWARD ALL - `cursor.fetch_forward_all()`
312+
- FETCH BACKWARD backward_count - `cursor.fetch_backward(backward_count=)`
313+
- FETCH BACKWARD ALL - `cursor.fetch_backward_all()`
312314

313315
## Extra Types
314316
Sometimes it's impossible to identify which type user tries to pass as a argument. But Rust is a strongly typed programming language so we have to help.

python/psqlpy/_internal/__init__.pyi

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,29 @@ class Cursor:
127127
### Returns:
128128
result as `QueryResult`.
129129
"""
130+
131+
async def fetch_backward(
132+
self: Self,
133+
backward_count: int,
134+
) -> QueryResult:
135+
"""Fetch backward rows.
136+
137+
Execute FETCH BACKWARD <backward_count>.
138+
139+
### Returns:
140+
result as `QueryResult`.
141+
"""
142+
143+
async def fetch_backward_all(
144+
self: Self,
145+
) -> QueryResult:
146+
"""Fetch backward all rows.
147+
148+
Execute FETCH BACKWARD ALL.
149+
150+
### Returns:
151+
result as `QueryResult`.
152+
"""
130153

131154
async def close(self: Self) -> None:
132155
"""Close the cursor.

src/driver/cursor.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,9 @@ impl Cursor {
219219
})
220220
}
221221

222-
/// Fetch relative row from cursor.
222+
/// Fetch forward all from cursor.
223223
///
224-
/// Execute FETCH RELATIVE<absolute_number>.
224+
/// Execute FORWARD ALL.
225225
///
226226
/// # Errors
227227
/// May return Err Result if cannot execute query.
@@ -241,6 +241,54 @@ impl Cursor {
241241
})
242242
}
243243

244+
/// Fetch backward from cursor.
245+
///
246+
/// Execute BACKWARD <backward_count>.
247+
///
248+
/// # Errors
249+
/// May return Err Result if cannot execute query.
250+
pub fn fetch_backward<'a>(
251+
&'a self,
252+
py: Python<'a>,
253+
backward_count: i64,
254+
) -> RustPSQLDriverPyResult<&PyAny> {
255+
let db_client_arc = self.db_client.clone();
256+
let cursor_name = self.cursor_name.clone();
257+
258+
rustengine_future(py, async move {
259+
let db_client_guard = db_client_arc.read().await;
260+
let result = db_client_guard
261+
.query(
262+
format!("FETCH BACKWARD {backward_count} FROM {cursor_name}").as_str(),
263+
&[],
264+
)
265+
.await?;
266+
Ok(PSQLDriverPyQueryResult::new(result))
267+
})
268+
}
269+
270+
/// Fetch backward from cursor.
271+
///
272+
/// Execute BACKWARD <backward_count>.
273+
///
274+
/// # Errors
275+
/// May return Err Result if cannot execute query.
276+
pub fn fetch_backward_all<'a>(&'a self, py: Python<'a>) -> RustPSQLDriverPyResult<&PyAny> {
277+
let db_client_arc = self.db_client.clone();
278+
let cursor_name = self.cursor_name.clone();
279+
280+
rustengine_future(py, async move {
281+
let db_client_guard = db_client_arc.read().await;
282+
let result = db_client_guard
283+
.query(
284+
format!("FETCH BACKWARD ALL FROM {cursor_name}").as_str(),
285+
&[],
286+
)
287+
.await?;
288+
Ok(PSQLDriverPyQueryResult::new(result))
289+
})
290+
}
291+
244292
/// Close cursor.
245293
///
246294
/// # Errors

0 commit comments

Comments
 (0)