Skip to content

Commit 88667d7

Browse files
authored
Merge pull request #505 from dimitri-yatsenko/master
Fix #376 and #488
2 parents 4fee300 + b1e007a commit 88667d7

File tree

4 files changed

+30
-9
lines changed

4 files changed

+30
-9
lines changed

datajoint/connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ def connect(self):
9595
self._conn = client.connect(
9696
init_command=self.init_fun,
9797
sql_mode="NO_ZERO_DATE,NO_ZERO_IN_DATE,ERROR_FOR_DIVISION_BY_ZERO,"
98-
"STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION",
98+
"STRICT_ALL_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION",
9999
charset=config['connection.charset'],
100100
**self.conn_info)
101101

datajoint/query.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ def _make_condition(self, arg):
117117
:param arg: any valid restriction object.
118118
:return: an SQL condition string. It may also be a boolean that is intended to be treated as a string.
119119
"""
120+
def prep_value(v):
121+
return str(v) if isinstance(v, (datetime.date, datetime.datetime, datetime.time, decimal.Decimal)) else v
122+
120123
negate = False
121124
while isinstance(arg, Not):
122125
negate = not negate
@@ -125,7 +128,7 @@ def _make_condition(self, arg):
125128

126129
# restrict by string
127130
if isinstance(arg, str):
128-
return template % arg.strip()
131+
return template % arg.strip().replace("%", "%%") # escape % in strings, see issue #376
129132

130133
# restrict by AndList
131134
if isinstance(arg, AndList):
@@ -148,15 +151,12 @@ def _make_condition(self, arg):
148151
# restrict by a mapping such as a dict -- convert to an AndList of string equality conditions
149152
if isinstance(arg, collections.abc.Mapping):
150153
return template % self._make_condition(
151-
AndList('`%s`=%r' % (k, (v if not isinstance(v, (
152-
datetime.date, datetime.datetime, datetime.time, decimal.Decimal)) else str(v)))
153-
for k, v in arg.items() if k in self.heading))
154+
AndList('`%s`=%r' % (k, prep_value(v)) for k, v in arg.items() if k in self.heading))
154155

155156
# restrict by a numpy record -- convert to an AndList of string equality conditions
156157
if isinstance(arg, np.void):
157158
return template % self._make_condition(
158-
AndList(('`%s`='+('%s' if self.heading[k].numeric else '"%s"')) % (k, arg[k])
159-
for k in arg.dtype.fields if k in self.heading))
159+
AndList(('`%s`=%r' % (k, prep_value(arg[k])) for k in arg.dtype.fields if k in self.heading)))
160160

161161
# restrict by a Relation class -- triggers instantiation
162162
if inspect.isclass(arg) and issubclass(arg, Query):

tests/schema.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,13 @@ class Test2(dj.Manual):
2929
value : int # value
3030
"""
3131

32-
32+
@schema
33+
class Test3(dj.Manual):
34+
definition = """
35+
key : int
36+
---
37+
value : varchar(300)
38+
"""
3339

3440
@schema
3541
class TestExtra(dj.Manual):

tests/test_relational_operand.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
import datajoint as dj
88
from .schema_simple import A, B, D, E, L, DataA, DataB, TestUpdate, IJ, JI, ReservedWord
9-
from .schema import Experiment
9+
from .schema import Experiment, Test3
1010

1111

1212
def setup():
@@ -129,6 +129,21 @@ def test_join():
129129
y = (A & 'cond_in_a=1').proj(a2='id_a')
130130
assert_equal(len(rel), len(x * y))
131131

132+
133+
@staticmethod
134+
def test_issue_376():
135+
tab = Test3()
136+
tab.delete_quick()
137+
tab.insert((
138+
(1, '%%%'),
139+
(2, 'one%'),
140+
(3, 'one')
141+
))
142+
assert_equal(len(tab & 'value="%%%"'), 1)
143+
assert_equal(len(tab & {'value': "%%%"}), 1)
144+
assert_equal(len(tab & 'value like "o%"'), 2)
145+
assert_equal(len(tab & 'value like "o%%"'), 2)
146+
132147
@staticmethod
133148
def test_issue_463():
134149
assert_equal(((A & B) * B).fetch().size, len(A * B))

0 commit comments

Comments
 (0)