Skip to content

Commit

Permalink
Add Cursor.fetchval method (#433)
Browse files Browse the repository at this point in the history
* Added ``Cursor.fetchval`` method

* Added test for ``Cursor.fetchval``

---------

Co-authored-by: Haruka3399 <57554044+Saratoga-CV6@users.noreply.github.com>
  • Loading branch information
Serious-senpai and Serious-senpai authored Oct 28, 2023
1 parent ecc52a6 commit 54efbd8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
10 changes: 10 additions & 0 deletions aioodbc/cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,16 @@ def fetchone(self):
fut = self._run_operation(self._impl.fetchone)
return fut

def fetchval(self):
"""Returns the first column of the first row if there are results.
A ProgrammingError exception is raised if no SQL has been executed
or if it did not return a result set (e.g. was not a SELECT
statement).
"""
fut = self._run_operation(self._impl.fetchval)
return fut

def fetchall(self):
"""Returns a list of all remaining rows.
Expand Down
12 changes: 12 additions & 0 deletions tests/test_cursor.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,18 @@ async def test_fetchone(conn, table):
await cur.close()


@pytest.mark.parametrize("db", pytest.db_list)
@pytest.mark.asyncio
async def test_fetchval(conn, table):
cur = await conn.cursor()
await cur.execute("SELECT * FROM t1;")
resp = await cur.fetchval()
expected = 1

assert expected == resp
await cur.close()


@pytest.mark.parametrize("db", ["sqlite"])
@pytest.mark.asyncio
async def test_tables(conn, table):
Expand Down

0 comments on commit 54efbd8

Please sign in to comment.