@@ -165,15 +165,18 @@ class CollationErrorTests(MemoryDatabaseMixin, unittest.TestCase):
165165 def setUp (self ):
166166 super ().setUp ()
167167 self .events = []
168+ self .setUp_collation ()
169+ self .con .execute ("create table t(x, y)" )
170+ self .con .executemany ("insert into t values (?, ?)" ,
171+ [(1 , "a" ), (- 2 ** 63 , "boom" )])
172+
173+ def setUp_collation (self ):
168174 def mycoll (x , y ):
169175 if "boom" in (x , y ):
170176 self .events .append ("collation raised" )
171177 raise KeyError ("collation raised" )
172178 return (x > y ) - (x < y )
173179 self .con .create_collation ("mycoll" , mycoll )
174- self .con .execute ("create table t(x, y)" )
175- self .con .executemany ("insert into t values (?, ?)" ,
176- [(1 , "a" ), (- 2 ** 63 , "boom" )])
177180
178181 def assertCollationRaises (self ):
179182 return self .assertRaisesRegex (KeyError , "collation raised" )
@@ -284,7 +287,7 @@ def finalize(self):
284287 self .con .create_window_function ("win" , 1 , Window )
285288 # For this query, SQLite evaluates the filter for all rows before it
286289 # calls the window function. The filter is false for the first row,
287- # so the first call after the exception is value().
290+ # so the first call after the exception is value(), before step() .
288291 with self .assertCollationRaises ():
289292 self .con .execute ("""
290293 select win(x) filter (where y = 'q' collate mycoll)
@@ -322,21 +325,48 @@ def finalize(self):
322325 self .assertEqual (self .events_after_exception (), [])
323326
324327 def test_authorizer_not_called (self ):
325- def authorizer (* args ):
326- self .events .append ("authorizer" )
328+ def authorizer (action , * args ):
329+ self .events .append (action )
327330 return sqlite .SQLITE_OK
328- # SQLite prepares a statement for pragma_table_info() during the
329- # step, and it calls the authorizer for that statement.
331+ sql = """
332+ select t.x, p.name from t join pragma_table_info('t') p
333+ where t.y = 'q' collate mycoll
334+ """
330335 self .con .executemany ("insert into t values (?, ?)" , [(3 , "c" )])
331336 self .con .set_authorizer (authorizer )
337+ # Control: if the comparison is true, SQLite prepares a statement for
338+ # pragma_table_info() during the step, and it calls the authorizer.
339+ self .con .create_collation ("mycoll" , lambda x , y : 0 )
340+ self .con .execute (sql ).fetchall ()
341+ self .assertIn (sqlite .SQLITE_PRAGMA , self .events )
342+ self .events .clear ()
343+ self .setUp_collation ()
332344 with self .assertCollationRaises ():
333- self .con .execute ("""
334- select t.x, p.name from t join pragma_table_info('t') p
335- where t.y = 'q' collate mycoll
336- """ )
345+ self .con .execute (sql )
337346 self .con .set_authorizer (None )
338347 self .assertEqual (self .events_after_exception (), [])
339348
349+ def test_prepare (self ):
350+ # With STAT4, the query planner calls the collation when SQLite
351+ # prepares the statement.
352+ options = [o for (o ,) in self .con .execute ("pragma compile_options" )]
353+ if "ENABLE_STAT4" not in options :
354+ self .skipTest ("requires SQLite with SQLITE_ENABLE_STAT4" )
355+ fail = False
356+ def statcoll (x , y ):
357+ if fail :
358+ raise KeyError ("collation raised" )
359+ return (x > y ) - (x < y )
360+ self .con .create_collation ("statcoll" , statcoll )
361+ self .con .execute ("create table s(v text collate statcoll)" )
362+ self .con .execute ("create index s_v on s(v)" )
363+ self .con .executemany ("insert into s values (?)" ,
364+ [(str (i % 7 ),) for i in range (200 )])
365+ self .con .execute ("analyze" )
366+ fail = True
367+ with self .assertCollationRaises ():
368+ self .con .execute ("select * from s where v = '3'" )
369+
340370 def test_progress_handler_not_called (self ):
341371 def progress ():
342372 self .events .append ("progress" )
0 commit comments