英文:
How to run a delete query in golang that returns the id of deleted entity
问题
如何在我的 Golang 代码中运行这个查询?我尝试了以下代码:
resp, err := pgConnPool.Exec(context.Background(), deleteQuery)
但是它返回的是 pgConn.CommandTag
(可以用来获取行数,但不是实际的行)。
英文:
Query :
DELETE FROM *tableName* RETURNING *id*
How to run this query in my golang code?
I tried
resp, err := pgConnPool.Exec(context.Background(), deleteQuery)
but it gives pgConn.CommandTag(can be used to get rowsCount but not actual rows)
答案1
得分: 1
查询是正确的(只需确保以RETURNING *id*
结尾)。
假设您只删除一个实体,您可以使用QueryRow
,如@mkopriva建议的那样。
package yourpackage
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
)
func YourMethod() {
// ...
var recordID uuid.UUID
err := pgConnPool.QueryRow(context.Background(), deleteQuery).Scan(&recordID)
if err != nil {
// 处理错误。
}
// recordID 现在包含已删除记录的ID。
}
或者包装在处理程序中
package yourpackage
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
)
func DeleteRecord(/*your args*/) (uuid.UUID, error) {
var recordID uuid.UUID
// 构建您的 deleteQuery 对象。
err := pgConnPool.QueryRow(context.Background(), deleteQuery).Scan(&recordID)
return recordID, err
}
如果您想要添加一次删除多个记录的可能性
package yourpackage
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
)
func DeleteRecords(/*your args*/) ([]uuid.UUID, error) {
var recordIDs []uuid.UUID
// 构建您的 deleteQuery 对象。
rows, err := pgConnPool.Query(context.Background(), deleteQuery)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var recordID uuid.UUID
if err := rows.Scan(&recordID); err != nil {
return nil, err
}
recordIDs = append(recordIDs, recordID)
}
return recordIDs, rows.Err() // 确保没有发生读取错误
}
英文:
The query is OK (just ensure it ends with RETURNING *id*
).
Assuming you are only deleting one entity, you can use QueryRow
as suggested by @mkopriva
package yourpackage
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
)
func YourMethod() {
// ...
var recordID uuid.UUID
err := pgConnPool.QueryRow(context.Background(), deleteQuery).Scan(&recordID)
if err != nil {
// handle error.
}
// recordID now contains the id of the deleted record.
}
Or wrapped in a handler
package yourpackage
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
)
func DeleteRecord(/*your args*/) (uuid.UUID, error) {
var recordID uuid.UUID
// build your deleteQuery object.
err := pgConnPool.QueryRow(context.Background(), deleteQuery).Scan(&recordID)
return recordID, err
}
If you want to add the possibility to delete multiple records at once
package yourpackage
import (
"context"
"github.com/google/uuid"
"github.com/jackc/pgx/v4"
)
func DeleteRecords(/*your args*/) ([]uuid.UUID, error) {
var recordIDs []uuid.UUID
// build your deleteQuery object.
rows, err := pgConnPool.Query(context.Background(), deleteQuery)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var recordID uuid.UUID
if err := rows.Scan(&recordID); err != nil {
return nil, err
}
recordIDs = append(recordIDs, recordID)
}
return recordIDs, rows.Err() // Ensure no reading error occurred
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论