Query middleware functions