Use a prepared query for historyContains.
This is called often, hopefully a prepared query will speed it up. This also modifies Query.run to return self for easier chaining, so you can use `query.run.value()` instead of `query.run` ; query.value()`.
This commit is contained in:
parent
5b827cf86a
commit
478a719f77
@ -79,6 +79,7 @@ class WebHistory(sql.SqlTable):
|
|||||||
super().__init__("History", ['url', 'title', 'atime', 'redirect'],
|
super().__init__("History", ['url', 'title', 'atime', 'redirect'],
|
||||||
parent=parent)
|
parent=parent)
|
||||||
self.create_index('HistoryIndex', 'url', where='not redirect')
|
self.create_index('HistoryIndex', 'url', where='not redirect')
|
||||||
|
self._contains_query = self.contains_query('url')
|
||||||
self._between_query = sql.Query('SELECT * FROM History '
|
self._between_query = sql.Query('SELECT * FROM History '
|
||||||
'where not redirect '
|
'where not redirect '
|
||||||
'and not url like "qute://%" '
|
'and not url like "qute://%" '
|
||||||
@ -97,7 +98,7 @@ class WebHistory(sql.SqlTable):
|
|||||||
return utils.get_repr(self, length=len(self))
|
return utils.get_repr(self, length=len(self))
|
||||||
|
|
||||||
def __contains__(self, url):
|
def __contains__(self, url):
|
||||||
return self.contains('url', url)
|
return self._contains_query.run([url]).value()
|
||||||
|
|
||||||
def _add_entry(self, entry):
|
def _add_entry(self, entry):
|
||||||
"""Add an entry to the in-memory database."""
|
"""Add an entry to the in-memory database."""
|
||||||
|
@ -83,6 +83,7 @@ class Query(QSqlQuery):
|
|||||||
if not self.exec_():
|
if not self.exec_():
|
||||||
raise SqlException('Failed to exec query "{}": "{}"'.format(
|
raise SqlException('Failed to exec query "{}": "{}"'.format(
|
||||||
self.lastQuery(), self.lastError().text()))
|
self.lastQuery(), self.lastError().text()))
|
||||||
|
return self
|
||||||
|
|
||||||
def value(self):
|
def value(self):
|
||||||
"""Return the result of a single-value query (e.g. an EXISTS)."""
|
"""Return the result of a single-value query (e.g. an EXISTS)."""
|
||||||
@ -140,17 +141,14 @@ class SqlTable(QObject):
|
|||||||
q.run()
|
q.run()
|
||||||
return iter(q)
|
return iter(q)
|
||||||
|
|
||||||
def contains(self, field, value):
|
def contains_query(self, field):
|
||||||
"""Return whether the table contains the matching item.
|
"""Return a prepared query that checks for the existence of an item.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
field: Field to match.
|
field: Field to match.
|
||||||
value: Value to check for the given field.
|
|
||||||
"""
|
"""
|
||||||
q = Query("Select EXISTS(SELECT * FROM {} where {} = ?)"
|
return Query("Select EXISTS(SELECT * FROM {} where {} = ?)"
|
||||||
.format(self._name, field))
|
.format(self._name, field))
|
||||||
q.run([value])
|
|
||||||
return q.value()
|
|
||||||
|
|
||||||
def __len__(self):
|
def __len__(self):
|
||||||
"""Return the count of rows in the table."""
|
"""Return the count of rows in the table."""
|
||||||
|
@ -94,15 +94,20 @@ def test_contains():
|
|||||||
table.insert(['one', 1, False])
|
table.insert(['one', 1, False])
|
||||||
table.insert(['nine', 9, False])
|
table.insert(['nine', 9, False])
|
||||||
table.insert(['thirteen', 13, True])
|
table.insert(['thirteen', 13, True])
|
||||||
assert table.contains('name', 'one')
|
|
||||||
assert table.contains('name', 'thirteen')
|
name_query = table.contains_query('name')
|
||||||
assert table.contains('val', 9)
|
val_query = table.contains_query('val')
|
||||||
assert table.contains('lucky', False)
|
lucky_query = table.contains_query('lucky')
|
||||||
assert table.contains('lucky', True)
|
|
||||||
assert not table.contains('name', 'oone')
|
assert name_query.run(['one']).value()
|
||||||
assert not table.contains('name', 1)
|
assert name_query.run(['thirteen']).value()
|
||||||
assert not table.contains('name', '*')
|
assert val_query.run([9]).value()
|
||||||
assert not table.contains('val', 10)
|
assert lucky_query.run([False]).value()
|
||||||
|
assert lucky_query.run([True]).value()
|
||||||
|
assert not name_query.run(['oone']).value()
|
||||||
|
assert not name_query.run([1]).value()
|
||||||
|
assert not name_query.run(['*']).value()
|
||||||
|
assert not val_query.run([10]).value()
|
||||||
|
|
||||||
|
|
||||||
def test_delete_all(qtbot):
|
def test_delete_all(qtbot):
|
||||||
|
Loading…
Reference in New Issue
Block a user