Skip to content

Commit 4fa6cad

Browse files
Add client_found_rows option to connect() (#32)
* Add client_found_rows option to connect() --------- Co-authored-by: Pavlo Mishchenko <[email protected]> Co-authored-by: Kevin D Smith <[email protected]>
1 parent 865fd3f commit 4fa6cad

File tree

4 files changed

+44
-0
lines changed

4 files changed

+44
-0
lines changed

singlestoredb/config.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,13 @@
103103
environ='SINGLESTOREDB_MULTI_STATEMENTS',
104104
)
105105

106+
register_option(
107+
'client_found_rows', 'bool', check_bool, False,
108+
'Should affected_rows in OK_PACKET indicate the '
109+
'number of matched rows instead of changed?',
110+
environ='SINGLESTOREDB_CLIENT_FOUND_ROWS',
111+
)
112+
106113
register_option(
107114
'ssl_key', 'str', check_str, None,
108115
'File containing SSL key',

singlestoredb/connection.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1298,6 +1298,7 @@ def connect(
12981298
program_name: Optional[str] = None,
12991299
conn_attrs: Optional[Dict[str, str]] = None,
13001300
multi_statements: Optional[bool] = None,
1301+
client_found_rows: Optional[bool] = None,
13011302
connect_timeout: Optional[int] = None,
13021303
nan_as_null: Optional[bool] = None,
13031304
inf_as_null: Optional[bool] = None,

singlestoredb/mysql/connection.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ def __init__( # noqa: C901
347347
driver=None, # internal use
348348
conn_attrs=None,
349349
multi_statements=None,
350+
client_found_rows=None,
350351
nan_as_null=None,
351352
inf_as_null=None,
352353
encoding_errors='strict',
@@ -380,6 +381,8 @@ def __init__( # noqa: C901
380381
client_flag |= CLIENT.LOCAL_FILES
381382
if multi_statements:
382383
client_flag |= CLIENT.MULTI_STATEMENTS
384+
if client_found_rows:
385+
client_flag |= CLIENT.FOUND_ROWS
383386

384387
if read_default_group and not read_default_file:
385388
if sys.platform.startswith('win'):

singlestoredb/tests/test_connection.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2776,6 +2776,39 @@ def test_multi_statements(self):
27762776
self.assertEqual([(2,)], list(cur))
27772777
self.assertIsNone(cur.nextset())
27782778

2779+
def test_client_found_rows(self):
2780+
if self.conn.driver not in ['http', 'https']:
2781+
with s2.connect(database=type(self).dbname, client_found_rows=False) as conn:
2782+
with conn.cursor() as cur:
2783+
tag = str(uuid.uuid4()).replace('-', '_')
2784+
table_name = f'test_client_found_rows_{tag}'
2785+
cur.execute(f"CREATE TABLE {table_name} (id BIGINT \
2786+
PRIMARY KEY, s TEXT DEFAULT 'def');")
2787+
cur.execute(f'INSERT INTO {table_name} (id) \
2788+
VALUES (1), (2), (3);')
2789+
cur.execute(f"UPDATE {table_name} SET s = 'def' \
2790+
WHERE id = 1;")
2791+
# UPDATE statement above is not changing any rows,
2792+
# so affected_rows is 0 if client_found_rows is False (default)
2793+
self.assertEqual(0, conn.affected_rows())
2794+
cur.execute(f'DROP TABLE {table_name};')
2795+
2796+
with s2.connect(database=type(self).dbname, client_found_rows=True) as conn:
2797+
with conn.cursor() as cur:
2798+
tag = str(uuid.uuid4()).replace('-', '_')
2799+
table_name = f'test_client_found_rows_{tag}'
2800+
cur.execute(f"CREATE TABLE {table_name} (id BIGINT \
2801+
PRIMARY KEY, s TEXT DEFAULT 'def');")
2802+
cur.execute(f'INSERT INTO {table_name} (id) \
2803+
VALUES (1), (2), (3);')
2804+
cur.execute(f"UPDATE {table_name} SET s = 'def' \
2805+
WHERE id = 1;")
2806+
# UPDATE statement above is not changing any rows,
2807+
# but affected_rows is 1 as 1 row is subject to update, and
2808+
# this is what affected_rows return when client_found_rows is True
2809+
self.assertEqual(1, conn.affected_rows())
2810+
cur.execute(f'DROP TABLE {table_name};')
2811+
27792812
def test_connect_timeout(self):
27802813
with s2.connect(database=type(self).dbname, connect_timeout=8) as conn:
27812814
with conn.cursor() as cur:

0 commit comments

Comments
 (0)