Skip to content

Commit 0685446

Browse files
Add ability to return Postgres cursor description
1 parent 9886727 commit 0685446

File tree

5 files changed

+28
-4
lines changed

5 files changed

+28
-4
lines changed

.python-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
3.9.16
1+
3.13.1

CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
# Changelog
2+
## v1.6.5 3/24/25
3+
- Add capability to return PostgreSQL cursor description
4+
25
## v1.6.4 1/30/25
36
- Update pyproject.toml to reflect CL changes (since omitted in last release)
47

pyproject.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
44

55
[project]
66
name = "nypl_py_utils"
7-
version = "1.6.4"
7+
version = "1.6.5"
88
authors = [
99
{ name="Aaron Friedman", email="[email protected]" },
1010
]

src/nypl_py_utils/classes/postgresql_client.py

+7-2
Original file line numberDiff line numberDiff line change
@@ -55,14 +55,18 @@ def connect(self, retry_count=0, backoff_factor=5, **kwargs):
5555
'Error connecting to {name} database: {error}'.format(
5656
name=self.database, error=e)) from None
5757

58-
def execute_query(self, query, query_params=None, **kwargs):
58+
def execute_query(
59+
self, query, return_desc=False, query_params=None, **kwargs):
5960
"""
6061
Executes an arbitrary query against the given database connection.
6162
6263
Parameters
6364
----------
6465
query: str
6566
The query to execute
67+
return_desc: bool, optional
68+
Whether or not to return the cursor description in addition to the
69+
results
6670
query_params: sequence, optional
6771
The values to be used in a parameterized query. The values can be
6872
for a single insert query -- e.g. execute_query(
@@ -92,7 +96,8 @@ def execute_query(self, query, query_params=None, **kwargs):
9296
else:
9397
cursor.execute(query, query_params, **kwargs)
9498
self.conn.commit()
95-
return None if cursor.description is None else cursor.fetchall()
99+
results = None if cursor.description is None else cursor.fetchall()
100+
return (results, cursor.description) if return_desc else results
96101
except Exception as e:
97102
self.conn.rollback()
98103
cursor.close()

tests/test_postgresql_client.py

+16
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,22 @@ def test_execute_read_query(self, mock_pg_conn, test_instance, mocker):
5050
test_instance.conn.commit.assert_called_once()
5151
mock_cursor.close.assert_called_once()
5252

53+
def test_execute_read_query_with_desc(self, mock_pg_conn, test_instance,
54+
mocker):
55+
test_instance.connect()
56+
57+
mock_cursor = mocker.MagicMock()
58+
mock_cursor.description = [('description', None, None)]
59+
mock_cursor.execute.return_value = mock_cursor
60+
mock_cursor.fetchall.return_value = [(1, 2, 3), ('a', 'b', 'c')]
61+
test_instance.conn.cursor.return_value = mock_cursor
62+
63+
assert test_instance.execute_query('test query', return_desc=True) == (
64+
[(1, 2, 3), ('a', 'b', 'c')], [('description', None, None)])
65+
mock_cursor.execute.assert_called_once_with('test query', None)
66+
test_instance.conn.commit.assert_called_once()
67+
mock_cursor.close.assert_called_once()
68+
5369
def test_execute_write_query(self, mock_pg_conn, test_instance, mocker):
5470
test_instance.connect()
5571

0 commit comments

Comments
 (0)