Added a first stab at a general text search query

This commit is contained in:
Simon Brooke 2018-03-17 23:59:09 +00:00
parent 05623f0168
commit 955fb20acc
2 changed files with 89 additions and 38 deletions

View file

@ -110,6 +110,41 @@
{})) {}))
(defn search-query [entity-map]
(let [entity-name (:name (:attrs entity-map))
pretty-name (singularise entity-name)
query-name (str "search-strings-" pretty-name)
signature ":? :1"
string-fields (filter
#(= (-> % :attrs :type) "string")
(-> entity-map :content :properties vals))]
(if
(empty? string-fields)
{}
(hash-map
(keyword query-name)
{:name query-name
:signature signature
:entity entity-map
:type :text-search
:query
(str "-- :name " query-name " " signature "\n"
"-- :doc selects existing " entity-name " records having any string field matching `:pattern` by substring match\n"
"SELECT * FROM " entity-name "\n"
"WHERE "
(s/join
"\n\tOR "
(map
#(str (-> % :attrs :name) " LIKE '%:pattern%'")
string-fields))
"\n"
(order-by-clause entity-map)
"\n"
"--~ (if (:offset params) \"OFFSET :offset \") \n"
"--~ (if (:limit params) \"LIMIT :limit\" \"LIMIT 100\")"
"\n\n")}))))
(defn select-query [entity-map] (defn select-query [entity-map]
(if (if
(has-primary-key? entity-map) (has-primary-key? entity-map)
@ -272,6 +307,7 @@
(merge (merge
(select-query entity-map) (select-query entity-map)
(list-query entity-map) (list-query entity-map)
(search-query entity-map)
(foreign-queries entity-map entities-map))))) (foreign-queries entity-map entities-map)))))
@ -292,11 +328,13 @@
(doall (doall
(map (map
#(:query %) #(:query %)
(sort
#(compare (:name %1) (:name %2))
(vals (vals
(apply (apply
merge merge
(map (map
#(queries % adl-struct) #(queries % adl-struct)
(vals adl-struct))))))))] (vals adl-struct)))))))))]
(spit output file-content) (spit output file-content)
file-content))) file-content)))

View file

@ -42,7 +42,7 @@
(defn file-header [parent-name this-name] (defn file-header [parent-name this-name]
(list (list
'ns 'ns
(symbol (str parent-name "." this-name)) (symbol (str parent-name ".routes." this-name))
(str "JSON routes for " parent-name (str "JSON routes for " parent-name
" auto-generated by [squirrel-parse](https://github.com/simon-brooke/squirrel-parse) at " " auto-generated by [squirrel-parse](https://github.com/simon-brooke/squirrel-parse) at "
(f/unparse (f/formatters :basic-date-time) (t/now))) (f/unparse (f/formatters :basic-date-time) (t/now)))
@ -102,27 +102,27 @@
:delete-1 :delete-1
(generate-handler-src (generate-handler-src
handler-name query :post handler-name query :post
(str "delete one record from the " (str "delete one record from the `"
(-> query :entity :attrs :name) (-> query :entity :attrs :name)
" table. Expects the following key(s) to be present in `params`: " "` table. Expects the following key(s) to be present in `params`: `"
(doall (-> query :entity :content :key :content keys)) (doall (-> query :entity :content :key :content keys))
".")) "`."))
:insert-1 :insert-1
(generate-handler-src (generate-handler-src
handler-name query :post handler-name query :post
(str "insert one record to the " (str "insert one record to the `"
(-> query :entity :attrs :name) (-> query :entity :attrs :name)
" table. Expects the following key(s) to be present in `params`: " "` table. Expects the following key(s) to be present in `params`: `"
(pr-str (-> query :entity :content :properties keys)) (pr-str (-> query :entity :content :properties keys))
". Returns a map containing the keys " "`. Returns a map containing the keys `"
(pr-str (-> query :entity :content :key :content keys)) (pr-str (-> query :entity :content :key :content keys))
" identifying the record created.")) "` identifying the record created."))
:update-1 :update-1
(generate-handler-src (generate-handler-src
handler-name query :post handler-name query :post
(str "update one record in the " (str "update one record in the `"
(-> query :entity :attrs :name) (-> query :entity :attrs :name)
" table. Expects the following key(s) to be present in `params`: " "` table. Expects the following key(s) to be present in `params`: `"
(pr-str (pr-str
(distinct (distinct
(sort (sort
@ -130,15 +130,15 @@
(cons (cons
(-> query :entity :content :properties keys) (-> query :entity :content :properties keys)
(-> query :entity :content :key :content keys)))))) (-> query :entity :content :key :content keys))))))
".")) "`."))
:select-1 :select-1
(generate-handler-src (generate-handler-src
handler-name query :post handler-name query :post
(str "select one record from the " (str "select one record from the `"
(-> query :entity :attrs :name) (-> query :entity :attrs :name)
" table. Expects the following key(s) to be present in `params`: " "` table. Expects the following key(s) to be present in `params`: `"
(pr-str (-> query :entity :content :key :content keys)) (pr-str (-> query :entity :content :key :content keys))
". Returns a map containing the following keys: " "`. Returns a map containing the following keys: `"
(pr-str (pr-str
(distinct (distinct
(sort (sort
@ -146,13 +146,13 @@
(cons (cons
(-> query :entity :content :properties keys) (-> query :entity :content :properties keys)
(-> query :entity :content :key :content keys)))))) (-> query :entity :content :key :content keys))))))
".")) "`."))
:select-many :select-many
(generate-handler-src (generate-handler-src
handler-name query :get handler-name query :get
(str "select all records from the " (str "select all records from the `"
(-> query :entity :attrs :name) (-> query :entity :attrs :name)
" table. If the keys (:limit :offset) are present in the request then they will be used to page through the data. Returns a sequence of maps each containing the following keys: " "` table. If the keys `(:limit :offset)` are present in the request then they will be used to page through the data. Returns a sequence of maps each containing the following keys: `"
(pr-str (pr-str
(distinct (distinct
(sort (sort
@ -160,8 +160,21 @@
(cons (cons
(-> query :entity :content :properties keys) (-> query :entity :content :properties keys)
(-> query :entity :content :key :content keys)))))) (-> query :entity :content :key :content keys))))))
".")) "`."))
:text-search
(generate-handler-src
handler-name query :get
(str "select all records from the `"
(-> query :entity :attrs :name)
"` table with any text field matching the value of the key `:pattern` which should be in the request. If the keys `(:limit :offset)` are present in the request then they will be used to page through the data. Returns a sequence of maps each containing the following keys: `"
(pr-str
(distinct
(sort
(flatten
(cons
(-> query :entity :content :properties keys)
(-> query :entity :content :key :content keys))))))
"`."))
(:select-many-to-many (:select-many-to-many
:select-one-to-many) :select-one-to-many)
(hash-map :method :get (hash-map :method :get
@ -170,7 +183,7 @@
;; default ;; default
(hash-map (hash-map
:src :src
(str ";; don't know what to do with query " :key " of type " (:type query)))))))) (str ";; don't know what to do with query `" :key "` of type `" (:type query) "`.")))))))
(defn defroutes [handlers-map] (defn defroutes [handlers-map]