gpt4 book ai didi

node.js - 可以使用 QueryFile 添加动态 WHERE 子句吗?

转载 作者:行者123 更新时间:2023-11-29 12:49:26 26 4
gpt4 key购买 nike

我有一个存储在 SQL 文件中的复杂查询,我想将它重用于各种路由,但根据路由更改 WHERE 子句。这将代替在多个文件中进行大型复杂查询,唯一的区别是 WHERE 语句。

使用QueryFile时是否可以动态添加WHERE?下面的简化示例:

SELECT "id", "userId", "locationId", "title", "body",
(
SELECT row_to_json(sqUser)
FROM (
SELECT "id", "firstname", "lastname"
FROM "users"
WHERE "users"."id" = "todos"."userId"
) sqUser
) as "user"
FROM "todos"
const queryIndex = new pgp.QueryFile('sql/todos/index.pgsql', queryOptions);
// 1. Use as is to get a list of all todos
// 2. OR Append WHERE "locationId" = $1 to get list filtered by location
// 3. OR Append WHERE "id" = $1 to get a specific item
// without having three separate SQL files?

看起来(也许?)您可以在查询文件中添加以下内容,但感觉仍然有限(仍然需要两个文件用于 =LIKE 并且它仍然仅限于一个 WHERE 条件)。执行 WHERE 1 = 1 之类的操作来返回所有记录也感觉很奇怪。

WHERE $1 = $2

我很想听听人们对此的想法,或者是否有更好的方法。

最佳答案

您可以将动态条件注入(inject)到查询文件中,如 Raw Text :

SELECT "id", "userId", "locationId", "title", "body",
(
SELECT row_to_json(sqUser)
FROM (
SELECT "id", "firstname", "lastname"
FROM "users"
${condition:raw}
) sqUser
) as "user"
FROM "todos"

预格式化参数,基于条件:

// Generate a condition, based on the business logic:
const condition = pgp.as.format('WHERE col_1 = $1 AND col_2 = $2', [111, 222]);

执行你的查询文件:

await db.any(myQueryFile, {condition});

高级

以上是当您有一个您想要在代码中生成的简单动态条件时的场景。但有时您可能有想要交替的复杂静态条件。在这种情况下,您可以让主查询文件引用从查询文件中的条件(开箱即用地支持嵌套查询文件)。在这种情况下,您甚至不需要使用 :raw 过滤器,因为默认情况下查询文件作为原始文本注入(inject):

主查询:

SELECT * FROM table ${condition}

加载具有复杂条件的从属查询文件(当应用程序启动时):

const conditionQueryFile1 = new QueryFile(...);
const conditionQueryFile2 = new QueryFile(...);

根据业务逻辑选择正确的从属查询:

const condition = conditionQueryFile1; // some business logic here;

以从属作为参数执行主查询:

await db.any(myQueryFile, {condition});

关于node.js - 可以使用 QueryFile 添加动态 WHERE 子句吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57808220/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com