doudan5136
2014-08-12 05:15
浏览 143

为Go中的PGSQL函数提供参数

I am attempting to pass arguments to the following function in Golang with db.QueryRow(), however it doesn't seem to accept the usual ?.

DROP FUNCTION IF EXISTS upsertplatform(pToken varchar);
CREATE FUNCTION upsertplatform(pToken varchar) RETURNS int as $$
    DECLARE
        _id int = NULL;
    BEGIN
        LOOP
            UPDATE public.platform SET platformToken = pToken WHERE userID = 3 AND platformID = 2 RETURNING id into _id;
            IF NOT FOUND THEN
                BEGIN
                    INSERT INTO public.platform (userID, platformID, platformToken) VALUES (3, 2, '1234-123-bbasd') RETURNING id into _id;
                EXCEPTION WHEN unique_violation THEN
                        UPDATE public.platform SET platformToken = '1234-123-bbasd' WHERE userID = 3 AND platformID = 2 RETURNING id into strict _id;
                END;
            END IF;
        EXIT WHEN _id IS NOT NULL;
        END LOOP;
    RETURN 1;
    END
$$ LANGUAGE PLPGSQL;

This is the query I am having issues with, I'm unsure how to pass arguments to it:

res := db.QueryRow(`SELECT * FROM upsertplatform( ? )`, "test123")

How do I do this correctly?

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用db.QueryRow()将参数传递给Golang中的以下函数,但是它没有 似乎接受了通常的?。

  DROP FUNCTION如果存在upsertplatform(pToken varchar); 
CREATE FUNCTION upsertplatform(pToken varchar)返回int为$$ 
 DECLARE 
 _id  int = NULL; 
开始
循环
更新public.platform SET platformToken = pToken WHERE用户ID = 3 AND platformID = 2将ID返回_id; 
如果未找到则开始
 BEGIN 
插入public.platform(  userID,platformID,platformToken)值(3,2,'1234-123-bbasd')返回id到_id; 
当unique_violation时,则例外
更新public.platform SET platformToken ='1234-123-bbasd'WHERE userID =  3 AND platformID = 2将id返回到严格的_id; 
 END; 
 END IF; 
 _id不为NULL时退出; 
 E  ND LOOP; 
 RETURN 1; 
 END 
 $$ LANGUAGE PLPGSQL; 
   
 
 

这是我遇到的查询,我不确定 将参数传递给它:

  res:= db.QueryRow(`SELECT * FROM upsertplatform(?  )`,“ test123”)
   
 
 

如何正确执行此操作?

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

1条回答 默认 最新

  • douqinlin5094 2014-08-12 06:51
    最佳回答

    The Postgres drivers use positional notation for parameters, $1, $2,... So just SELECT * FROM upsertplatform($1).

    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题