   "Support fact command versions"
   [:v4])
 
+(defn approx-facts-query []
+  "Get all fact names and their values converting values to text.
+   Returns a vector of maps following the format below:
+   [{:certname \"blarg\" :name \"fact1\" :value \"1\"}]"
+  (query-to-vec
+   "SELECT fp.path as name,
+           COALESCE(fv.value_string,
+                    cast(fv.value_integer as text),
+                    cast(fv.value_boolean as text),
+                    cast(fv.value_float as text),
+                    '') as value,
+           fs.certname
+    FROM factsets fs
+         INNER JOIN facts as f on fs.id = f.factset_id
+         INNER JOIN fact_values as fv on f.fact_value_id = fv.id
+         INNER JOIN fact_paths as fp on f.fact_path_id = fp.id
+    WHERE fp.depth = 0
+    ORDER BY name ASC"))
+
 (let [certname  "foo.example.com"
       facts     {:certname certname
                  :environment "DEV"

          (handle-message (queue/store-command q (facts->command-req (version-kwd->num version) command)))
          (is (= (query-to-vec "SELECT certname,deactivated FROM certnames")
                 [{:certname certname :deactivated nil}]))
          (is (= (query-to-vec
                  "SELECT fp.path as name,
                          COALESCE(fv.value_string,
                                   cast(fv.value_integer as text),
                                   cast(fv.value_boolean as text),
                                   cast(fv.value_float as text),
                                   '') as value,
                          fs.certname
                   FROM factsets fs
                     INNER JOIN facts as f on fs.id = f.factset_id
                     INNER JOIN fact_values as fv on f.fact_value_id = fv.id
                     INNER JOIN fact_paths as fp on f.fact_path_id = fp.id
                   WHERE fp.depth = 0
                   ORDER BY name ASC")
                 [{:certname certname :name "a" :value "1"}
                  {:certname certname :name "b" :value "2"}
                  {:certname certname :name "c" :value "3"}]))


          (is (= (query-to-vec "SELECT certname,deactivated FROM certnames")
                 [{:certname certname :deactivated tomorrow}]))
          (is (= (approx-facts-query)
                 [{:certname certname :name "a" :value "1"}
                  {:certname certname :name "b" :value "2"}
                  {:certname certname :name "c" :value "3"}]))
