Dapper parameters not working with PostgreSQL through npgsql connection, is postgres anonymous function parameterization supported?

二次信任 提交于 2019-12-02 07:22:11

问题


I'm trying to use dapper to parameterize a postgres anonymous function i've written to do an upsert. Here's the code:

private static int UpsertProductPrice(
        IDbConnection connection,
        Data.ProductPrice price,
        List<Data.ProductPriceTier> priceTiers)
    {
        string postgres = @"DO $$
        BEGIN 
        UPDATE product_price 
            SET start_date = @StartDate, end_date = @EndDate, price_tier_type = @PriceTierType
            WHERE product_price_external_id = @Id;
        IF found THEN
            RETURN;
        END IF;
        BEGIN
            INSERT INTO product_price(product_price_external_id, start_date, end_date, price_tier_type) 
            VALUES (@Id, @StartDate, @EndDate, @PriceTierType);
            RETURN;
        EXCEPTION WHEN unique_violation THEN END; END$$;";

        int productPriceId = connection.Execute(
            postgres,
            new { Id = price.product_price_external_id, StartDate = price.start_date, EndDate = price.end_date, PriceTierType = price.price_tier_type });

Postgres logs the query as unparameterized: there are still @ signs and no values were inserted. Here's the log.

2015-07-15 14:57:58.570 EDT,"user","TestDb",8496,"::1:53953",55a6ad36.2130,1,"DO",2015-07-15 14:57:58 EDT,9/42,0,ERROR,42703,"column ""startdate"" does not exist",,,"UPDATE product_price 

            SET start_date = @StartDate, end_date = @EndDate, price_tier_type = @PriceTierType

            WHERE product_price_external_id = @Id",52,"PL/pgSQL function inline_code_block line 3 at SQL statement","DO $$

        BEGIN 

        UPDATE product_price 

            SET start_date = @StartDate, end_date = @EndDate, price_tier_type = @PriceTierType

            WHERE product_price_external_id = @Id;

        IF found THEN

            RETURN;

        END IF;

        BEGIN

            INSERT INTO product_price(product_price_external_id, start_date, end_date, price_tier_type) 

            VALUES (@Id, @StartDate, @EndDate, @PriceTierType);

            RETURN;

        EXCEPTION WHEN unique_violation THEN END; END$$;",,,""

I'm I doing something wrong, or is this not supported? I couldn't find anyone trying to do this online.


回答1:


Parameters in anonymous code blocks aren't supported, this would be a PostgreSQL feature rather than an Npgsql feature. See discussion here: https://github.com/npgsql/npgsql/issues/629



来源:https://stackoverflow.com/questions/31439352/dapper-parameters-not-working-with-postgresql-through-npgsql-connection-is-post

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!