在Go中为PGSQL函数提供参数

时间:2014-08-12 05:15:37

标签: sql postgresql go

我试图用db.QueryRow()将参数传递给Golang中的以下函数,但它似乎不接受通常的吗?。

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;

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

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

我该如何正确地做到这一点?

1 个答案:

答案 0 :(得分:1)

Postgres驱动程序对参数使用位置表示法,$ 1,$ 2,......所以只需SELECT * FROM upsertplatform($1)