Extract query building functionality to expose it.