Move check_id sql generation into CancelableQuery class method