+;; ## Request Format
+;;
+;; The single available route is `/resources?query=<query>`. The `query`
+;; parameter is a JSON array of query predicates in prefix form.
+;;
+;; ### Predicates
+;;
+;; #### =
+;;
+;; Resources tagged with "foo" (irrespective of other tags):
+;;
+;;     ["=" "tag" "foo"]
+;;
+;; Resources for the node "foo.example.com":
+;;
+;;     ["=" ["node" "<this value is ignored>"] "foo.example.com"]
+;;
+;; Resources whose owner parameter is "joe":
+;;
+;;     ["=" ["parameter" "owner"] "joe"]
+;;
+;; Resources whose title is "/etc/hosts"; "title" may be replaced with any legal column of the `resources` table, to query against that column:
+;;
+;;     ["=" "title" "/etc/hosts"]
+;;
+;; #### and
+;;
+;; Resources whose owner is "joe" and group is "people":
+;;
+;;     ["and" ["=" ["parameter" "owner"] "joe"]
+;;            ["=" ["parameter" "group"] "people"]]
+;;
+;; #### or
+;;
+;; Resources whose owner is "joe" or "jim":
+;;
+;;     ["or" ["=" ["parameter" "owner"] "joe"]
+;;           ["=" ["parameter" "owner"] "jim"]]
+;;
+;; #### not
+;;
+;; Resources whose owner is not "joe" AND is not "jim":
+;;
+;;     ["not" ["=" ["parameter" "owner"] "joe"]
+;;            ["=" ["parameter" "owner"] "jim"]]
+;;
+;; ## Response Format
+;;
+;; The response is a list of resource objects, returned in JSON form. Each
+;; resource object is a map of the following form:
+;;
+;;     {:hash       "the resource's unique hash"
+;;      :type       "File"
+;;      :title      "/etc/hosts"
+;;      :exported   "true"
+;;      :sourcefile "/etc/puppet/manifests/site.pp"
+;;      :sourceline "1"
+;;      :parameters {<parameter> <value>
+;;                   <parameter> <value>
+;;                   ...}}
+
 (ns com.puppetlabs.cmdb.query.resource
   (:require [com.puppetlabs.utils :as utils]
             [clojure.contrib.logging :as log]



(defmulti compile-query->sql
  "Recursively compile a query into a collection of SQL operations"
  (fn [db query]
    (string/lower-case (first query))))



(defn resource-list-as-json
  "Fetch a list of resources from the database, formatting them as a
JSON array, and returning them as the body of the response."
  [request graphdata]
  (let [db (get-in request [:globals :scf-db])]
    (json/generate-string (or (vec (query-resources db (:query graphdata)))
                              []))))

(defhandler resource-list-handler
  :allowed-methods        (constantly #{:get})
