在 pq.Array 中无法传递值。

huangapple go评论89阅读模式
英文:

IN pq.Array fails to pass values

问题

我有以下代码:

package main

import (
    "database/sql"
    "fmt"
    "github.com/lib/pq"
)

const (
    DB_USER     = "<username>"
    DB_PASSWORD = "<password>"
    DB_NAME     = "<db>"
)

func main() {
    dbinfo := fmt.Sprintf("user=%s password=%s dbname=%s sslmode=disable",
    DB_USER, DB_PASSWORD, DB_NAME)

    db, err := sql.Open("postgres", dbinfo)
    checkErr(err)
    defer db.Close()

    fmt.Println("# 使用 SQL 进行黑名单查询")
    rows, err := db.Query(`
    SELECT * FROM(
        SELECT x from (values ('A'), ('B'), ('C') ) s(x)
    ) As Res1 WHERE x NOT IN ('A');
`)
    checkErr(err)

    for rows.Next() {
        var str string
        err = rows.Scan(&str)
        fmt.Println(str) // 输出 B,C
    }

    fmt.Println("使用 Golang 进行黑名单查询")
    blacklist := []string{"A"}

    q := `
    SELECT * FROM(
        SELECT x from (values ('A'), ('B'), ('C') ) s(x)
    ) As Res1 WHERE x NOT IN ($1);

`
    rows, err = db.Query(q, pq.Array(blacklist))
    checkErr(err)

    for rows.Next() {
        var str string
        err = rows.Scan(&str)
        fmt.Println(str) // 输出 B,C
    }
}

func checkErr(err error) {
    if err != nil {
        panic(err)
    }
}

在这段代码中,我将 pq.Array 作为参数传递给了 Golang 的 Postgres 格式字符串 $1。然而,参数未能传递。当我期望输出 B,C 时,程序却打印出了 A,B,C

# 使用 SQL 进行黑名单查询
B
C
使用 Golang 进行黑名单查询
A
B
C

希望对你有帮助!

英文:

I have the following code:

package main
import (
&quot;database/sql&quot;
&quot;fmt&quot;
&quot;github.com/lib/pq&quot;
)
const (
DB_USER     = &quot;&lt;username&gt;&quot;
DB_PASSWORD = &quot;&lt;password&gt;&quot;
DB_NAME     = &quot;&lt;db&gt;&quot;
)
func main() {
dbinfo := fmt.Sprintf(&quot;user=%s password=%s dbname=%s sslmode=disable&quot;,
DB_USER, DB_PASSWORD, DB_NAME)
db, err := sql.Open(&quot;postgres&quot;, dbinfo)
checkErr(err)
defer db.Close()
fmt.Println(&quot;# Querying with blacklist in SQL&quot;)
rows, err := db.Query(`
SELECT * FROM(
SELECT x from (values (&#39;A&#39;), (&#39;B&#39;), (&#39;C&#39;) ) s(x)
) As Res1 WHERE x NOT IN (&#39;A&#39;);
`)
checkErr(err)
for rows.Next() {
var str string
err = rows.Scan(&amp;str)
fmt.Println(str) // Prints B,C
}
fmt.Println(&quot;Querying with blacklist in Golang&quot;)
blacklist := []string{&quot;A&quot;}
q := `
SELECT * FROM(
SELECT x from (values (&#39;A&#39;), (&#39;B&#39;), (&#39;C&#39;) ) s(x)
) As Res1 WHERE x NOT IN ($1);
`
rows, err = db.Query(q, pq.Array(blacklist))
checkErr(err)
for rows.Next() {
var str string
err = rows.Scan(&amp;str)
fmt.Println(str) // Prints A, B, C
}
}
func checkErr(err error) {
if err != nil {
panic(err)
}
}

where I pass a pq.Array as a parameter to a Golang Postgres format string $1. However the parameter fails to get passed. When I expect an output of B,C, the program is printing A,B,C.

# Querying with blacklist in SQL
B
C
Querying with blacklist in Golang
A
B
C

答案1

得分: 3

Postgres的IN不接受类似ANYALL的数组,而在这些情况下可以使用pq.Array

你可以使用x <> ALL($1)来替代使用x NOT IN($1)

来自ALL的文档:

左侧表达式被评估,并使用给定的运算符与数组的每个元素进行比较,该运算符必须产生布尔结果。如果所有比较都返回true(包括数组没有元素的情况),则ALL的结果为“true”。如果找到任何false结果,则结果为“false”。

英文:

Postgres's IN does not take an array like for example ANY or ALL which is where you can use pq.Array.

Instead of using x NOT IN($1) you can use x &lt;&gt; ALL($1).

From ALL's docs.

> The left-hand expression is evaluated and compared to each element of
> the array using the given operator, which must yield a Boolean result.
> The result of ALL is "true" if all comparisons yield true (including
> the case where the array has zero elements). The result is "false" if
> any false result is found.

huangapple
  • 本文由 发表于 2017年4月14日 15:57:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/43407537.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定