gpt4 book ai didi

postgresql - 从存储过程中检索结果的惯用方法是什么?

转载 作者:IT王子 更新时间:2023-10-29 01:48:32 28 4
gpt4 key购买 nike

DROP function mytest();
CREATE OR REPLACE FUNCTION mytest()
RETURNS TABLE(name text, age int)
AS
$$
SELECT name, age FROM names
$$
LANGUAGE sql;

我见过的大多数存储过程示例都返回单行和单列,并且可以与 QueryRow 一起使用。这里我使用一个表作为输出。以上返回 4 行:

    mytest   
------------
(bob,12)
(fred,18)
(james,22)
(bill,27)
(4 rows)

在 Go 中,处理元组的惯用方式是什么:

rows, err := db.Query("SELECT mytest()")
if err != nil {
panic(err)
}

defer rows.Close()
for rows.Next() {
var items string

if err = rows.Scan(&items); err != nil {
panic(err)
}

log.Println(items) // items is a string...now what?

}
if err = rows.Err(); err != nil {
panic(err)
}

//打印一些元组:

(bob,12)
(fred,18)
(james,22)
(bill,27)

也许有一个包,但我没有找到它;(

最佳答案

您可以尝试 PostgreSQL typesafe1 ORM 而不是查询,例如 src-d/go-kallax ,这deals with tuples .

您可以在这个 blog post 中看到 kalax

  • the first priority of kallax is to provide type safety to the data access layer.
  • Another goal of kallax is to make sure all models are, first and foremost, Go structs without having to use database-specific types such as, for example, sql.NullInt64. Support for arrays and slices of all basic Go types and all JSON and arrays operators is provided as well.

关于postgresql - 从存储过程中检索结果的惯用方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46267073/

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