def test_query_interpolation(self): self.maxDiff = None # tests that interpolation replaces longer keys first self.assertEqual( interpolate(*query( "select * from foo", a__not='b', b__in='select * from blah where c=:$c', d__any={ 'one__like': 'o', 'two': 2 }, a0=3, a00=1, a00a=2, a00aa=4, # <-- breaks without correct interpolation key order ahash=memoryview(sha256(b'hello world')), limit=10, order_by='b', **{'$c': 3})), "select * from foo WHERE a != 'b' AND " "b IN (select * from blah where c=3) AND " "(one LIKE 'o' OR two = 2) AND " "a0 = 3 AND a00 = 1 AND a00a = 2 AND a00aa = 4 " "AND ahash = X'b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9' " "ORDER BY b LIMIT 10", )
def test_query_interpolation(self): self.maxDiff = None # tests that interpolation replaces longer keys first self.assertEqual( interpolate(*query( "select * from foo", a__not='b', b__in='select * from blah where c=:$c', d__any={ 'one__like': 'o', 'two': 2 }, a0=3, a00=1, a00a=2, a00aa=4, # <-- breaks without correct interpolation key order ahash=memoryview(sha256(b'hello world')), limit=10, order_by='b', **{'$c': 3})), "select * from foo WHERE a != 'b' AND " "b IN (select * from blah where c=3) AND " "(one LIKE 'o' OR two = 2) AND " "a0 = 3 AND a00 = 1 AND a00a = 2 AND a00aa = 4 " "AND ahash = e9cdefe2acf78890ee80537ae3ef84c4faab7ddad7522ea5083e4d93b9274db9 " "ORDER BY b LIMIT 10", )
def execute_query(sql, values) -> List: context = ctx.get() context.set_query_timeout() try: return context.db.execute(sql, values).fetchall() except sqlite3.OperationalError as err: plain_sql = interpolate(sql, values) if context.is_tracking_metrics: context.metrics['execute_query'][-1]['sql'] = plain_sql if str(err) == "interrupted": context.log.warning("interrupted slow sqlite query:\n%s", plain_sql) raise SQLiteInterruptedError(context.metrics) context.log.exception('failed running query', exc_info=err) raise SQLiteOperationalError(context.metrics)