(PDB-657) Query logic refactor

First cut at creating an intermediate query language for PuppetDB. The
initial user query is expanded from the more sugared syntax to a smaller
set of basic operators and subqueries. The query then goes from the
"expanded form" to a query plan and then ultimately down to sql. The
user query is manipulated in the context of a "source query", specific
to each endpoint. This commit supports resources, but will next expand
to the other endpoints.
Merge pull request #976 from senior/ticket/master/pdb-657-query-logic-refactor

(PDB-657) Query logic refactor
(PDB-660) Enable event streaming and provide other streaming cleanups

This provides streaming for the events end-point, thus removing the need for
event-resource-limit setting as well.

Some other cleanups around streaming have been provided also:

* stream-json-results is better documented
* remove-all-environments/remove-environment has been moved into query to
  align with how it is used in src/
* Positioned with-transacted-connection closer to count query and removed it
  from streamed-query-result so that the two db connections were clearly marked
  out in one place (the http code).
* lots of cases of :use converted to :require
* streamed-query-result no longer returns a function, never really needed to.
  The code is somewhat simpler now.
* munge-result-rows for events has now been moved into query so there is only 1
  place where munging is performed. Same for resources.

Signed-off-by: Ken Barber <ken@bob.sh>