Added a first stab at a general text search query
This commit is contained in:
parent
05623f0168
commit
955fb20acc
|
@ -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]
|
||||
(if
|
||||
(has-primary-key? entity-map)
|
||||
|
@ -272,6 +307,7 @@
|
|||
(merge
|
||||
(select-query entity-map)
|
||||
(list-query entity-map)
|
||||
(search-query entity-map)
|
||||
(foreign-queries entity-map entities-map)))))
|
||||
|
||||
|
||||
|
@ -292,11 +328,13 @@
|
|||
(doall
|
||||
(map
|
||||
#(:query %)
|
||||
(sort
|
||||
#(compare (:name %1) (:name %2))
|
||||
(vals
|
||||
(apply
|
||||
merge
|
||||
(map
|
||||
#(queries % adl-struct)
|
||||
(vals adl-struct))))))))]
|
||||
(vals adl-struct)))))))))]
|
||||
(spit output file-content)
|
||||
file-content)))
|
||||
|
|
|
@ -42,7 +42,7 @@
|
|||
(defn file-header [parent-name this-name]
|
||||
(list
|
||||
'ns
|
||||
(symbol (str parent-name "." this-name))
|
||||
(symbol (str parent-name ".routes." this-name))
|
||||
(str "JSON routes for " parent-name
|
||||
" auto-generated by [squirrel-parse](https://github.com/simon-brooke/squirrel-parse) at "
|
||||
(f/unparse (f/formatters :basic-date-time) (t/now)))
|
||||
|
@ -102,27 +102,27 @@
|
|||
:delete-1
|
||||
(generate-handler-src
|
||||
handler-name query :post
|
||||
(str "delete one record from the "
|
||||
(str "delete one record from the `"
|
||||
(-> 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))
|
||||
"."))
|
||||
"`."))
|
||||
:insert-1
|
||||
(generate-handler-src
|
||||
handler-name query :post
|
||||
(str "insert one record to the "
|
||||
(str "insert one record to the `"
|
||||
(-> 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))
|
||||
". Returns a map containing the keys "
|
||||
"`. Returns a map containing the keys `"
|
||||
(pr-str (-> query :entity :content :key :content keys))
|
||||
" identifying the record created."))
|
||||
"` identifying the record created."))
|
||||
:update-1
|
||||
(generate-handler-src
|
||||
handler-name query :post
|
||||
(str "update one record in the "
|
||||
(str "update one record in the `"
|
||||
(-> 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
|
||||
(distinct
|
||||
(sort
|
||||
|
@ -130,15 +130,15 @@
|
|||
(cons
|
||||
(-> query :entity :content :properties keys)
|
||||
(-> query :entity :content :key :content keys))))))
|
||||
"."))
|
||||
"`."))
|
||||
:select-1
|
||||
(generate-handler-src
|
||||
handler-name query :post
|
||||
(str "select one record from the "
|
||||
(str "select one record from the `"
|
||||
(-> 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))
|
||||
". Returns a map containing the following keys: "
|
||||
"`. Returns a map containing the following keys: `"
|
||||
(pr-str
|
||||
(distinct
|
||||
(sort
|
||||
|
@ -146,13 +146,13 @@
|
|||
(cons
|
||||
(-> query :entity :content :properties keys)
|
||||
(-> query :entity :content :key :content keys))))))
|
||||
"."))
|
||||
"`."))
|
||||
:select-many
|
||||
(generate-handler-src
|
||||
handler-name query :get
|
||||
(str "select all records from the "
|
||||
(str "select all records from the `"
|
||||
(-> 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
|
||||
(distinct
|
||||
(sort
|
||||
|
@ -160,8 +160,21 @@
|
|||
(cons
|
||||
(-> query :entity :content :properties 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-one-to-many)
|
||||
(hash-map :method :get
|
||||
|
@ -170,7 +183,7 @@
|
|||
;; default
|
||||
(hash-map
|
||||
: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]
|
||||
|
|
Loading…
Reference in a new issue