From 66602978710966c222ae41236ee13bb39327a7e0 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 21 Jul 2017 17:10:03 +0200 Subject: [PATCH 1/3] Fix new completion with web-history-max-items set to 0 We get no last_atime limit at all otherwise: qutebrowser.misc.sql.SqlException: Failed to prepare query "SELECT url, title, strftime('%Y-%m-%d', last_atime, 'unixepoch', 'localtime') FROM CompletionHistory WHERE (url LIKE :pat escape '\' or title LIKE :pat escape '\') AND last_atime >= ORDER BY last_atime DESC": "near "ORDER": syntax error Unable to execute statement" --- qutebrowser/completion/models/histcategory.py | 9 +++++---- tests/unit/completion/test_histcategory.py | 7 ++++++- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/qutebrowser/completion/models/histcategory.py b/qutebrowser/completion/models/histcategory.py index fa8443a60..c87ba026c 100644 --- a/qutebrowser/completion/models/histcategory.py +++ b/qutebrowser/completion/models/histcategory.py @@ -60,7 +60,7 @@ class HistoryCategory(QSqlQueryModel): def _atime_expr(self): """If max_items is set, return an expression to limit the query.""" max_items = config.get('completion', 'web-history-max-items') - if max_items < 0: + if max_items <= 0: return '' min_atime = sql.Query(' '.join([ @@ -83,9 +83,10 @@ class HistoryCategory(QSqlQueryModel): # treat spaces as wildcards to match any of the typed words pattern = re.sub(r' +', '%', pattern) pattern = '%{}%'.format(pattern) - with debug.log_time('sql', 'Running completion query'): - self._query.run(pat=pattern) - self.setQuery(self._query) + if config.get('completion', 'web-history-max-items') != 0: + with debug.log_time('sql', 'Running completion query'): + self._query.run(pat=pattern) + self.setQuery(self._query) def delete_cur_item(self, index): """Delete the row at the given index.""" diff --git a/tests/unit/completion/test_histcategory.py b/tests/unit/completion/test_histcategory.py index 0b5fcb915..bfd9d4aa5 100644 --- a/tests/unit/completion/test_histcategory.py +++ b/tests/unit/completion/test_histcategory.py @@ -117,7 +117,12 @@ def test_set_pattern(pattern, before, after, model_validator, hist): ], [ ('b', 'b', '2017-06-16'), ('c', 'c', '2017-05-16'), - ]) + ]), + (0, [ + ('a', 'a', '2017-04-16'), + ('b', 'b', '2017-06-16'), + ('c', 'c', '2017-05-16'), + ], []), ]) def test_sorting(max_items, before, after, model_validator, hist, config_stub): """Validate the filtering and sorting results of set_pattern.""" From 544094ba722ad3c9b2ee53977d55cdf4805cc00f Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 21 Jul 2017 17:55:47 +0200 Subject: [PATCH 2/3] Use simpler way of preventing History completion --- qutebrowser/completion/models/histcategory.py | 12 +++++++----- qutebrowser/completion/models/urlmodel.py | 6 ++++-- tests/unit/completion/test_histcategory.py | 7 +------ tests/unit/completion/test_models.py | 10 ++++++++++ 4 files changed, 22 insertions(+), 13 deletions(-) diff --git a/qutebrowser/completion/models/histcategory.py b/qutebrowser/completion/models/histcategory.py index c87ba026c..612eb0bf4 100644 --- a/qutebrowser/completion/models/histcategory.py +++ b/qutebrowser/completion/models/histcategory.py @@ -60,7 +60,10 @@ class HistoryCategory(QSqlQueryModel): def _atime_expr(self): """If max_items is set, return an expression to limit the query.""" max_items = config.get('completion', 'web-history-max-items') - if max_items <= 0: + # HistoryCategory should not be added to the completion in that case. + assert max_items != 0 + + if max_items < 0: return '' min_atime = sql.Query(' '.join([ @@ -83,10 +86,9 @@ class HistoryCategory(QSqlQueryModel): # treat spaces as wildcards to match any of the typed words pattern = re.sub(r' +', '%', pattern) pattern = '%{}%'.format(pattern) - if config.get('completion', 'web-history-max-items') != 0: - with debug.log_time('sql', 'Running completion query'): - self._query.run(pat=pattern) - self.setQuery(self._query) + with debug.log_time('sql', 'Running completion query'): + self._query.run(pat=pattern) + self.setQuery(self._query) def delete_cur_item(self, index): """Delete the row at the given index.""" diff --git a/qutebrowser/completion/models/urlmodel.py b/qutebrowser/completion/models/urlmodel.py index 0c5fbeacc..fbb9661ec 100644 --- a/qutebrowser/completion/models/urlmodel.py +++ b/qutebrowser/completion/models/urlmodel.py @@ -22,6 +22,7 @@ from qutebrowser.completion.models import (completionmodel, listcategory, histcategory) from qutebrowser.utils import log, objreg +from qutebrowser.config import config _URLCOL = 0 @@ -65,6 +66,7 @@ def url(): model.add_category(listcategory.ListCategory( 'Bookmarks', bookmarks, delete_func=_delete_bookmark)) - hist_cat = histcategory.HistoryCategory(delete_func=_delete_history) - model.add_category(hist_cat) + if config.get('completion', 'web-history-max-items') != 0: + hist_cat = histcategory.HistoryCategory(delete_func=_delete_history) + model.add_category(hist_cat) return model diff --git a/tests/unit/completion/test_histcategory.py b/tests/unit/completion/test_histcategory.py index bfd9d4aa5..0b5fcb915 100644 --- a/tests/unit/completion/test_histcategory.py +++ b/tests/unit/completion/test_histcategory.py @@ -117,12 +117,7 @@ def test_set_pattern(pattern, before, after, model_validator, hist): ], [ ('b', 'b', '2017-06-16'), ('c', 'c', '2017-05-16'), - ]), - (0, [ - ('a', 'a', '2017-04-16'), - ('b', 'b', '2017-06-16'), - ('c', 'c', '2017-05-16'), - ], []), + ]) ]) def test_sorting(max_items, before, after, model_validator, hist, config_stub): """Validate the filtering and sorting results of set_pattern.""" diff --git a/tests/unit/completion/test_models.py b/tests/unit/completion/test_models.py index 5b632eb3a..83c3d3e7d 100644 --- a/tests/unit/completion/test_models.py +++ b/tests/unit/completion/test_models.py @@ -396,6 +396,16 @@ def test_url_completion_delete_history(qtmodeltester, assert 'https://python.org' not in web_history_populated +def test_url_completion_zero_limit(config_stub, web_history, quickmarks, + bookmarks): + """Make sure there's no history if the limit was set to zero.""" + config_stub.data['completion']['web-history-max-items'] = 0 + model = urlmodel.url() + model.set_pattern('') + category = model.index(2, 0) # "History" normally + assert model.data(category) is None + + def test_session_completion(qtmodeltester, session_manager_stub): session_manager_stub.sessions = ['default', '1', '2'] model = miscmodels.session() From 118a7942a521ee711608ea5da84a2b975c20c228 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Fri, 21 Jul 2017 18:30:12 +0200 Subject: [PATCH 3/3] Add maximum bound for web-history-max-items sqlite can't handle values bigger than uint64_t for LIMIT. --- qutebrowser/config/configdata.py | 2 +- tests/unit/completion/test_histcategory.py | 9 +++++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/qutebrowser/config/configdata.py b/qutebrowser/config/configdata.py index 23d3efb67..92878d931 100644 --- a/qutebrowser/config/configdata.py +++ b/qutebrowser/config/configdata.py @@ -503,7 +503,7 @@ def data(readonly=False): "0: no history / -1: unlimited"), ('web-history-max-items', - SettingValue(typ.Int(minval=-1), '-1'), + SettingValue(typ.Int(minval=-1, maxval=MAXVALS['int64']), '-1'), "How many URLs to show in the web history.\n\n" "0: no history / -1: unlimited"), diff --git a/tests/unit/completion/test_histcategory.py b/tests/unit/completion/test_histcategory.py index 0b5fcb915..c53c86ee1 100644 --- a/tests/unit/completion/test_histcategory.py +++ b/tests/unit/completion/test_histcategory.py @@ -110,6 +110,15 @@ def test_set_pattern(pattern, before, after, model_validator, hist): ('c', 'c', '2017-05-16'), ('a', 'a', '2017-04-16'), ]), + (2 ** 63 - 1, [ # Maximum value sqlite can handle for LIMIT + ('a', 'a', '2017-04-16'), + ('b', 'b', '2017-06-16'), + ('c', 'c', '2017-05-16'), + ], [ + ('b', 'b', '2017-06-16'), + ('c', 'c', '2017-05-16'), + ('a', 'a', '2017-04-16'), + ]), (2, [ ('a', 'a', '2017-04-16'), ('b', 'b', '2017-06-16'),