Skip to content
This repository has been archived by the owner on Feb 22, 2020. It is now read-only.

Commit

Permalink
Merge pull request #23 from 4031651/master
Browse files Browse the repository at this point in the history
Fixed raising ValueError for queries that contain the%%.
  • Loading branch information
Jonas Tärnström committed Oct 1, 2012
2 parents a14b3d4 + 4e9dcf8 commit 09e4b36
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 2 deletions.
8 changes: 7 additions & 1 deletion python/umysql.c
Original file line number Diff line number Diff line change
Expand Up @@ -1081,13 +1081,19 @@ PyObject *EscapeQueryArguments(Connection *self, PyObject *inQuery, PyObject *it

iptr ++;

if (*iptr != 's')
if (*iptr != 's' && *iptr != '%')
{
Py_DECREF(iterator);
if (heap) PyObject_Free(obuffer);
return PyErr_Format (PyExc_ValueError, "Found character %c expected %%", *iptr);
}

if (*iptr == '%')
{
*(optr++) = *(iptr)++;
break;
}

iptr ++;

arg = PyIter_Next(iterator);
Expand Down
17 changes: 16 additions & 1 deletion tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,7 +715,22 @@ def testTextCharsets(self):
self.assertEquals(result, expected)

cnn.close()


def testPercentEscaping(self):
cnn = umysql.Connection()
cnn.connect (DB_HOST, 3306, DB_USER, DB_PASSWD, DB_DB)
rs = cnn.query("SELECT * FROM `tblautoincint` WHERE `test_id` LIKE '%%10%%'")
self.assertEquals([(100, u'piet'), (101, u'piet')], rs.rows)

rs = cnn.query("SELECT * FROM `tblautoincint` WHERE `test_id` LIKE '%%%s%%'", [10])
self.assertEquals([(100, u'piet'), (101, u'piet')], rs.rows)

# SqlAlchemy query style
rs = cnn.query("SELECT * FROM `tblautoincint` WHERE `test_id` LIKE concat(concat('%%', %s), '%%')", [10])
self.assertEquals([(100, u'piet'), (101, 'piet')], rs.rows)

cnn.close()

if __name__ == '__main__':
from guppy import hpy
hp = hpy()
Expand Down

0 comments on commit 09e4b36

Please sign in to comment.