Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions singlestoredb/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,13 @@
environ='SINGLESTOREDB_ENCODING_ERRORS',
)

register_option(
'sql_mode', 'string', check_str, None,
'Specifies the sql_mode session variable to set when connecting. '
'This is only applied to MySQL protocol connections.',
environ='SINGLESTOREDB_SQL_MODE',
)

register_option(
'local_infile', 'bool', check_bool, False,
'Should it be possible to load local files?',
Expand Down
5 changes: 5 additions & 0 deletions singlestoredb/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -1382,6 +1382,7 @@ def connect(
parse_json: Optional[bool] = None,
interpolate_query_with_empty_args: Optional[bool] = None,
socket_options: Optional[Dict[int, Dict[int, Any]]] = None,
sql_mode: Optional[str] = None,
) -> Connection:
"""
Return a SingleStoreDB connection.
Expand Down Expand Up @@ -1475,6 +1476,10 @@ def connect(
socket level constants (e.g., socket.SOL_SOCKET) and the values should be
dictionaries mapping socket option constants (e.g., socket.SO_KEEPALIVE) to
the desired value for that option.
sql_mode : str, optional
Value of the ``sql_mode`` session variable to set when connecting.
This is not supported by HTTP connections and will be ignored
with a warning.

Examples
--------
Expand Down
9 changes: 9 additions & 0 deletions singlestoredb/http/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import os
import re
import time
import warnings
from base64 import b64decode
from collections.abc import Iterable
from collections.abc import Sequence
Expand Down Expand Up @@ -1040,6 +1041,13 @@ def __init__(self, **kwargs: Any):
'statements within a query',
)

if kwargs.get('sql_mode', None) is not None:
warnings.warn(
'The `sql_mode=` parameter is not supported by the Data API '
'and will be ignored for HTTP connections.',
RuntimeWarning,
)

self._version = kwargs.get('version', 'v2')
self.driver = kwargs.get('driver', 'https')

Expand Down Expand Up @@ -1268,5 +1276,6 @@ def connect(
track_env: Optional[bool] = None,
enable_extended_data_types: Optional[bool] = None,
vector_data_format: Optional[str] = None,
sql_mode: Optional[str] = None,
) -> Connection:
return Connection(**dict(locals()))
18 changes: 18 additions & 0 deletions singlestoredb/tests/test_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,24 @@ def test_connection_attr(self):
with self.conn as conn:
assert conn is self.conn

def test_sql_mode(self):
if self.conn.driver in ['http', 'https']:
with self.assertWarns(RuntimeWarning):
s2.connect(database=type(self).dbname, sql_mode='ANSI_QUOTES')
return

with s2.connect(database=type(self).dbname, sql_mode='ANSI_QUOTES') as conn:
with conn.cursor() as cur:
cur.execute('select @@sql_mode')
assert 'ANSI_QUOTES' in cur.fetchone()[0]

def test_sql_mode_param(self):
out = sc.build_params(host='me:p455w0rd@s2host.com/mydb?sql_mode=ANSI_QUOTES')
assert out['sql_mode'] == 'ANSI_QUOTES', out['sql_mode']

out = sc.build_params(host='s2host.com', sql_mode='PIPES_AS_CONCAT')
assert out['sql_mode'] == 'PIPES_AS_CONCAT', out['sql_mode']

def test_executemany(self):
# NOTE: Doesn't actually do anything since no rows match
self.cur.executemany(
Expand Down
Loading