根据键类型解析地图和过滤的向量
我有一个地图向量,我正在尝试将这些地图解析为一个将在RAW SQL查询中使用的字符串。
(def params [{:fieldName "salary" :comparator "greater" :inputValue "10000" :inputType "number"}
{:fieldName "name" :comparator "include" :inputValue "Ros" :inputType "text"}
{:fieldName "start_date" :comparator "between" :inputValue "2022-01-01" :maxInputValue "2022-06-01" :inputType "date"}])
我需要从每张映射中提取的内容是基于的类型:比较器
and :fieldName
我创建一个strinng,一旦我对其进行解析,请放置所有字符串在一个字符串中。每个fieldName
将在向量中仅重复一次。我在下面写了一些东西,但我试图做得更好,而且它仅适用于一种类型,这将使所有比较器
and field> fieldname
(defn convert
[params]
(cond-> nil
(= "equal" (->> params
(filter #(= (:fieldName %) "salary"))
(first)
(:comparator))) (conj " AND salary= ?")
(= "not-equal" (->> params
(filter #(= (:fieldName %) "salary"))
(first)
(:comparator))) (conj " AND salary!= ?")
(= "greater" (->> params
(filter #(= (:fieldName %) "salary"))
(first)
(:comparator))) (conj " AND salary > ?")))
我想这样做
"AND salary > ?
AND name LIKE 'Ros%'
AND start BETWEEN between '2022-01-01' AND '2022-06-01' "
I have a vector of maps, and I am trying to parse this maps into a string which will be used in a raw SQL query.
(def params [{:fieldName "salary" :comparator "greater" :inputValue "10000" :inputType "number"}
{:fieldName "name" :comparator "include" :inputValue "Ros" :inputType "text"}
{:fieldName "start_date" :comparator "between" :inputValue "2022-01-01" :maxInputValue "2022-06-01" :inputType "date"}])
What I need to extract from each map is that based on the type of :comparator
and :fieldName
I create a strinng and once I have parsed them all, put all the strings in one string. Each fieldName
will be repeated only once in the vector. I have written something below but I am trying to do it better, plus it is only for one type which will make it longer and more difficult to read for all the comparator
and fieldName
(defn convert
[params]
(cond-> nil
(= "equal" (->> params
(filter #(= (:fieldName %) "salary"))
(first)
(:comparator))) (conj " AND salary= ?")
(= "not-equal" (->> params
(filter #(= (:fieldName %) "salary"))
(first)
(:comparator))) (conj " AND salary!= ?")
(= "greater" (->> params
(filter #(= (:fieldName %) "salary"))
(first)
(:comparator))) (conj " AND salary > ?")))
I want to have somthing like this
"AND salary > ?
AND name LIKE 'Ros%'
AND start BETWEEN between '2022-01-01' AND '2022-06-01' "
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不确定您想在这里拥有的所有选项,所以这是我的第一次尝试(请注意,我将名称更改为kebab-case):
I'm not sure about all options you want to have here, so this is my first try (note that I changed names to kebab-case):
这当然是一个不错的用例
对于多途径可能是这样实现的,
。像这样:
其中最好的部分是,您始终可以添加新的类型 /选择器,而无需触摸现有的(大概是经过良好测试)代码。例如,您想添加
in-list
操作员:This one certainly looks like a nice use case for the multimethods
Could be possibly implemented like this:
and use it somehow like this:
the best part of it, is that you can always add new types / selectors without touching the existing (and presumably well tested) code at all. Like for example you want to add the
in-list
operator: