Make sure queries don't have any missing bindings

This commit is contained in:
Florian Bruhin 2018-08-31 23:35:00 +02:00
parent 1d6282fd6c
commit 6b719fb218
2 changed files with 9 additions and 0 deletions

View File

@ -171,9 +171,13 @@ class Query(QSqlQuery):
def run(self, **values):
"""Execute the prepared query."""
log.sql.debug('Running SQL query: "{}"'.format(self.lastQuery()))
for key, val in values.items():
self.bindValue(':{}'.format(key), val)
log.sql.debug('query bindings: {}'.format(self.boundValues()))
if any(val is None for val in self.boundValues().values()):
raise SqlError("Missing bound values!")
if not self.exec_():
raise SqliteError.from_query('exec', self.lastQuery(),
self.lastError())

View File

@ -263,6 +263,11 @@ class TestSqlQuery:
q.run(answer=42)
assert q.value() == 42
def test_run_missing_binding(self):
q = sql.Query('SELECT :answer')
with pytest.raises(sql.SqlError, match='Missing bound values!'):
q.run()
def test_value_missing(self):
q = sql.Query('SELECT 0 WHERE 0')
q.run()