Skip to content

Commit

Permalink
Merge pull request #10 from IdanHaim/master
Browse files Browse the repository at this point in the history
Fix escaping and quoting special characters
  • Loading branch information
IdanHaim authored Mar 5, 2017
2 parents 474938d + 3f2c900 commit 0ca4dff
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 10 deletions.
7 changes: 3 additions & 4 deletions pyravendb/store/session_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ def where_not_none(self, field_name):
if len(self.query_builder) > 0:
self.query_builder += ' '
self.query_builder += '('
self.where_equals(field_name, '*').and_also().add_not().where_equals(field_name, None)
self.where_equals(field_name, '*', escape_query_options=EscapeQueryOptions.RawQuery).and_also().add_not().where_equals(field_name, None)
self.query_builder += ')'
return self

Expand Down Expand Up @@ -321,8 +321,7 @@ def boost(self, value):
def _execute_query(self):
self.session.increment_requests_count()
conventions = self.session.conventions
start_time = time.time()
end_time = start_time + conventions.timeout
end_time = time.time() + conventions.timeout
while True:
response = self.session.database_commands. \
query(self.index_name, IndexQuery(self.query_builder, default_operator=self.using_default_operator,
Expand All @@ -331,7 +330,7 @@ def _execute_query(self):
wait_for_non_stale_results=self.wait_for_non_stale_results),
includes=self.includes)
if response["IsStale"] and self.wait_for_non_stale_results:
if start_time > end_time:
if time.time() > end_time:
raise ErrorResponseException("The index is still stale after reached the timeout")
time.sleep(0.1)
continue
Expand Down
21 changes: 21 additions & 0 deletions pyravendb/tests/session_tests/test_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ def setUpClass(cls):
{"name": "withNesting", "product": {"name": "testing_order", "key": 4, "order": None}},
{"Raven-Entity-Name": "Companies"})
cls.db.put("specials/1", {"url": "https://ravendb.net/"})
cls.db.put("specials/2",
{"url": "http://www.example.com/ebx/LinkResolverServlet?classofcontent=Standard&id=63935",
"date": "2017.03.05T00:00:00.000"})
cls.db.put("specials/3",
{"wild_cards": "%~#+!$,;'*[]?*",
"date": "2017.03.05T00:00:01.000"})
cls.document_store = documentstore(cls.default_url, cls.default_database)
cls.document_store.initialize()

Expand Down Expand Up @@ -94,6 +100,21 @@ def test_query_with_special_characters(self):
self.assertTrue(len(query_result) > 0)
self.assertTrue(query_result[0].url == url)

def test_query_with_wild_cards_and_select(self):
with self.document_store.open_session() as session:
url = "http://www.example.com/ebx/LinkResolverServlet?classofcontent=Standard&id=63935"
query_result = list(session.query().where_equals("url", url).select("date"))
self.assertTrue(len(query_result) > 0)
self.assertIsNotNone(query_result[0].date)
self.assertFalse(hasattr(query_result[0], "url"))

def test_query_with_only_wild_cards(self):
with self.document_store.open_session() as session:
wild_cards = "%~#+!$,;'*[]?*"
query_result = list(session.query().where_equals("wild_cards", wild_cards))
self.assertTrue(len(query_result) > 0)
self.assertIsNotNone(query_result[0].wild_cards)

def test_query_success_with_where(self):
with self.document_store.open_session() as session:
query_results = list(session.query().where(name="test101", key=[4, 6, 90]))
Expand Down
9 changes: 4 additions & 5 deletions pyravendb/tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,12 @@ def empty_etag():

@staticmethod
def quote_key(key):
reserved = '%:=&?~#+!$,;\'*[]'
if key:
# To be able to work on python 2.x and 3.x
if sys.version_info.major > 2:
return urllib.parse.quote(key, safe=reserved)
return urllib.parse.quote(key)
else:
return urllib.quote(key, safe=reserved)
return urllib.quote(key)
else:
return ''

Expand Down Expand Up @@ -282,7 +281,7 @@ def timedelta_to_str(timedelta_obj):

@staticmethod
def escape(term, allow_wild_cards, make_phrase):
wild_cards = ['-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', ':', '\\']
wild_cards = ['-', '&', '|', '!', '(', ')', '{', '}', '[', ']', '^', '"', '~', ':', '\\', '?', '*']
if not term:
return "\"\""
start = 0
Expand All @@ -301,7 +300,7 @@ def escape(term, allow_wild_cards, make_phrase):

if ch in wild_cards:
if i > start:
builder += term[start:i - start]
builder += term[start:i]

builder += '\\{0}'.format(ch)
start = i + 1
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
setup(
name='pyravendb',
packages=find_packages(),
version='1.3.1.3',
version='3.5.3.3',
description='This is the official python client for RavenDB document database',
author='Idan Haim Shalom',
author_email='[email protected]',
Expand Down

0 comments on commit 0ca4dff

Please sign in to comment.