Skip to content

Commit cce29df

Browse files
Apply string interpolation in mogrify regardless of args presence
1 parent be7a773 commit cce29df

File tree

3 files changed

+25
-0
lines changed

3 files changed

+25
-0
lines changed

singlestoredb/mysql/cursors.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,8 @@ def mogrify(self, query, args=None):
183183

184184
if args:
185185
query = query % self._escape_args(args, conn)
186+
else:
187+
query = query % ()
186188

187189
return query
188190

singlestoredb/tests/test.sql

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -708,5 +708,10 @@ INSERT INTO bool_data_with_nulls SET id='nt', bool_a=NULL, bool_b=TRUE;
708708
INSERT INTO bool_data_with_nulls SET id='nn', bool_a=NULL, bool_b=NULL;
709709
INSERT INTO bool_data_with_nulls SET id='ff', bool_a=FALSE, bool_b=FALSE;
710710

711+
CREATE TABLE IF NOT EXISTS test_val_with_percent (
712+
i VARCHAR(16)
713+
);
714+
-- Double percent sign for execution from python
715+
INSERT INTO test_val_with_percent VALUES ('a%%a');
711716

712717
COMMIT;

singlestoredb/tests/test_connection.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3144,6 +3144,24 @@ def test_f16_vectors(self):
31443144
decimal=2,
31453145
)
31463146

3147+
def test_mogrify_val_with_percent(self):
3148+
val_with_percent = 'a%a'
3149+
self.cur.execute(
3150+
'''SELECT REPLACE(i, "%%", "\\%%")
3151+
FROM test_val_with_percent''',
3152+
)
3153+
res1 = self.cur.fetchall()
3154+
assert res1[0][0] == 'a\\%a'
3155+
3156+
self.cur.execute(
3157+
'''SELECT REPLACE(i, "%%", "\\%%")
3158+
FROM test_val_with_percent
3159+
WHERE i = %s''',
3160+
(val_with_percent,),
3161+
)
3162+
res2 = self.cur.fetchall()
3163+
assert res2[0][0] == 'a\\%a'
3164+
31473165

31483166
if __name__ == '__main__':
31493167
import nose2

0 commit comments

Comments
 (0)