Skip to content

Commit

Permalink
[#825] Replace % with %% to escape it in sql statements
Browse files Browse the repository at this point in the history
  • Loading branch information
domoritz authored and amercader committed Apr 25, 2013
1 parent 896f7fd commit 34c3522
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
2 changes: 1 addition & 1 deletion ckanext/datastore/db.py
Expand Up @@ -1108,7 +1108,7 @@ def search_sql(context, data_dict):
context['connection'].execute(
u'SET LOCAL statement_timeout TO {0}'.format(timeout))
results = context['connection'].execute(
data_dict['sql']
data_dict['sql'].replace('%', '%%')
)
return format_results(context, results, data_dict)

Expand Down
12 changes: 12 additions & 0 deletions ckanext/datastore/tests/test_search.py
Expand Up @@ -502,6 +502,18 @@ def test_select_basic(self):

assert result['records'] == res_dict_alias['result']['records']

def test_select_where_like_with_percent(self):
query = 'SELECT * FROM public."{0}" WHERE "author" LIKE \'tol%\''.format(self.data['resource_id'])
data = {'sql': query}
postparams = json.dumps(data)
auth = {'Authorization': str(self.sysadmin_user.apikey)}
res = self.app.post('/api/action/datastore_search_sql', params=postparams,
extra_environ=auth)
res_dict = json.loads(res.body)
assert res_dict['success'] is True
result = res_dict['result']
assert result['records'] == self.expected_records

def test_self_join(self):
query = '''
select a._id as first, b._id as second
Expand Down

0 comments on commit 34c3522

Please sign in to comment.