简体   繁体   English

匹配所有键/值对

[英]match all key/val pairs

Original query: 原始查询:

-- :name select*-list
-- :command :query
-- :result :raw
-- :doc Select all lists.
-- parameters()
SELECT * FROM list;

I want to pass in arbitrary key/val pairs and get matching results. 我想传递任意键/值对并获取匹配结果。 For example: 例如:

(select*-list db-spec {:name "Fruit" :type "Foo"})

should result in: 应导致:

SELECT * FROM list 
WHERE name = 'Fruit'
AND type = 'Foo';

I can think of a few ugly ways to accomplish this but it's likely I'm overlooking some nice way to do this. 我可以想到一些丑陋的方法来完成此操作,但是可能我忽略了一些不错的方法来完成此操作。

JDBC has some great shortcuts out from the box. JDBC提供了一些很棒的捷径。 One of them is find-by-keys . 其中之一是按键查找 It does exactly what you want: takes a map of key/value pairs and composes a set of WHERE clauses connected with AND : 它完全满足您的要求:取得键/值对的映射,并组成一组与AND连接的WHERE子句:

(jdbc/find-by-keys db-spec :users {:name "John" :age 42 :city "Chita"})

will turn out to 会变成

select from users
where
  name = 'John'
  and age = 42
  and city = 'Chita';

Here is an example java-jdbc.sql 这是一个示例java-jdbc.sql

(require '[java-jdbc.sql :as sql])

(jdbc/query db-spec
  (sql/select * :fruit (sql/where {:appearance "ripe"})))
;; -> ({:grade 8.4, :unit "carton", :cost 12, :appearance "ripe", :name "Plum"})

Please see: 请参见:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM