简体   繁体   English

MarkLogic Optic API from-sql 绑定参数

[英]MarkLogic Optic API from-sql binding parameters

I'm trying to use the Optic API using from-sql and XQuery.我正在尝试使用 from-sql 和 XQuery 来使用 Optic API。 I'm finding it's a great way to get the data I need but I'm trying to find clear examples of passing parameters to placeholders using op:from-sql.我发现这是获取我需要的数据的好方法,但我试图找到使用 op:from-sql 将参数传递给占位符的清晰示例。

For example例如

let $result := op:from-sql('
                  SELECT name, sum(value) 
                  FROM db.namevalue
                  WHERE client=''IBM''
                    and department in (''IT'', ''ACCOUNTS'')
                  GROUP BY name
                  ORDER BY 2 DESC
                  limit 30
               ')
             => op:result()
return $result 

works nicely and gives the results I expect.效果很好,并给出了我期望的结果。

What I need though it to parameterize so i can use variables instead of literals.我需要它来参数化以便我可以使用变量而不是文字。 I have seen the op:param but I suspect it isn't used to specify placeholders like the ?我见过 op:param 但我怀疑它不用于指定占位符,如 ? in traditional SQL environments.在传统的 SQL 环境中。

eg which I know is wrong;例如,我知道这是错误的;

let $client  = "IBM
let $dept    = ("IT", "ACCOUNT")

let $result := op:from-sql('
                  SELECT name, sum(value) 
                  FROM db.namevalue
                  WHERE client=?
                    and department in (?)
                  GROUP BY name
                  ORDER BY 2 DESC
                  limit 30
               ')
             => op:result((), map:entry("$1", $client) => map:entry("$2", $dept))
return $result 

So is there an clear example of how i can do this?那么有没有一个明确的例子来说明我如何做到这一点?

Thanks,谢谢,

Stephen斯蒂芬

Does it work to use @ as the prefix in the SQL string?在 SQL 字符串中使用 @ 作为前缀是否有效? As in:如:

op:from-sql('
              SELECT name, sum(value) 
              FROM db.namevalue
              WHERE client=@client and department in (@department)
              GROUP BY name
              ORDER BY 2 DESC
              limit 30
           ')
         => op:result((),
             map:entry("client", $client)
             => map:with("department", $dept))

An alternative would be to express the fixed structure with Optic builder methods and to parameterize the entire SQL condition along the lines of the following untested sketch:另一种方法是使用 Optic 构建器方法表达固定结构,并沿着以下未经测试的草图参数化整个 SQL 条件:

op:from-view('db', 'namevalue')
    => op:where(op:sql-condition(op:param("condition")))
    => op:group-by("name", op:sum("valueSum", "value"))
    => op:order-by(op:desc("valueSum"))
    => op:limit(30)
    => op:result((), map:entry("condition", $condition))

Hoping that helps,希望有所帮助,

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

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