简体   繁体   English

科尔马/字段可以采用一列列吗?

[英]Can korma/fields take an array of columns?

I would like to be able to pass a user-defined array of fields which contains a list of all the columns that need fetching. 我希望能够传递用户定义的字段数组,其中包含需要提取的所有列的列表。 Can korma/fields take an array of columns? 科尔马/字段可以采用一列列吗?

What I am essentially wanting to create is something like this: 我本质上想要创建的是这样的:

(defn fetch [fields]
(->
   (korma/select* foo)

   (as-> query
         (if (not-empty? fields)
           (korma/fields query fields)
           query))

   (korma/select)))

I am assuming this is because I have specified entity-fields in defentity. 我假设这是因为我已指定了指定的实体字段。 I am therefore wondering, if it is possible to override the entity-fields specified in defentity ? 因此,我想知道是否有可能覆盖defentity指定的实体字段?

I think your assumption is correct, and it's also possible to override this. 我认为您的假设是正确的,也有可能将其覆盖。 If you look at the map generated by (defentity foo) it has a :fields key with all the fields. 如果查看由(defentity foo)生成的映射,则它具有包含所有字段的:fields键。 korma.core/entity-fields doesn't replace what's already there, but this will: korma.core/entity-fields不会代替已经存在的东西,但是它将:

(-> foo
    (assoc :fields [:first])
    (korma/select*)
    (korma/as-sql))
=> "SELECT \"foo\".\"first\" FROM \"foo\""

I would like to be able to pass a user-defined array of fields which contains a list of all the columns that need fetching. 我希望能够传递用户定义的字段数组,其中包含需要提取的所有列的列表。

(defn fetch [& fields]
  (-> (korma/select* foo)
      (as-> query
        (if (seq fields)
          (assoc query :fields fields)
          query))
      (korma/select)))

I used variadic args in this example to mirror korma.core/entity-fields (and made fetch return the SQL string for my testing rather than execute the query): 在此示例中,我使用了可变参数args来镜像korma.core/entity-fields (并使fetch返回用于测试的SQL字符串,而不是执行查询):

(fetch :first :last)
=> "SELECT \"foo\".\"first\", \"foo\".\"last\" FROM \"foo\""
(fetch)
=> "SELECT \"foo\".\"id\", \"foo\".\"first\", \"foo\".\"last\" FROM \"foo\""

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

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