Skip to content

Npgsql doesn't support pass parameters by name to functions #305

Closed

Description

@franciscojunior

Open by Alexander Koval
From pgfoundry: http://pgfoundry.org/tracker/index.php?func=detail&aid=1010781&group_id=1000140&atid=590

Table

CREATE TABLE "Image"
(
"Name" character varying(50),
"Description" character varying(250),
"ImageID" serial NOT NULL,
CONSTRAINT pk_image PRIMARY KEY ("ImageID")
)

I have the next function

CREATE OR REPLACE FUNCTION "ImageInsert"(pname character varying, pdescription character varying)
RETURNS "Image" AS
$BODY$

DECLARE
res "Image"%ROWTYPE;
pImageID "Image"."ImageID"%type;

BEGIN
pImageID:= nextval('"Image_ImageID_seq"');
insert into "Image"("ImageID", "Name", "Description") values(pImageID, pName, pDescription);

select * into res from "Image" where "ImageID" = pImageID;
return res;

END;
$BODY$
LANGUAGE 'plpgsql'

Then I do these actions

NpgsqlCommand cmd1 = CreateNpgsqlCommand();
cmd1.CreateParameter("pname","image name1");
cmd1.CreateParameter("pdescription","image description1");

NpgsqlCommand cmd2 = CreateNpgsqlCommand();
cmd2.CreateParameter("pdescription","image description2");
cmd2.CreateParameter("pname","image name2");

Let's look at the data

select * from "Image"

Surprise!

"image name1";"image description1";1;
"image description2";"image name 2";2;

Activity

  1. added this to the milestone on Aug 1, 2014
  2. roji commented on Oct 28, 2014

    @roji
    Member

    A look at the Postgresql docs shows that Postgresql itself doesn't support named parameters, only positional ones. Note that supporting named parameters isn't mandated by the ADO.NET contract, different data providers support different things.

    Note that Postgresql's ParameterDescription message, which returns information on the parameters of a parsed query, doesn't return names. This makes it pretty much impossible to do named parameters unless I'm missing something.

    Will keep open for a bit more for comments, then close.

  3. Emill commented on Oct 28, 2014

    @Emill
    Contributor

    Even though not stated in the issue, I assume this refers only to the case where CommandType is set to StoredProcedure?

    If we leave everything as it is, we should add in the documentation, that when having CommandType set to StoredProcedure, the parameters in the parameter collection must be in the same order as in the function declaration.

    But, see section "4.3.2 Using Named Notation" http://www.postgresql.org/docs/9.1/static/sql-syntax-calling-funcs.html. It's actually possible to call a function with named parameters.

  4. roji commented on Oct 28, 2014

    @roji
    Member

    Ah, I misunderstood that entirely. Thanks for commenting.

    So this would be a purely Npgsql-internal feature to generate the correct SELECT * FROM function_call(...) query text with the Postgresql named notation. That's definitely doable.

    Note that SqlClient really does support named parameters for non-stored procedures - in fact it seems to be the only way to specify query parameters. And this we can't provide...

  5. Emill commented on Oct 28, 2014

    @Emill
    Contributor

    Ah, I misunderstood that entirely. Thanks for commenting.

    So this would be a purely Npgsql-internal feature to generate the correct SELECT * FROM function_call(...) query text with the Postgresql named notation. That's definitely doable.

    Note that SqlClient really does support named parameters for non-stored procedures - in fact it seems to be the only way to specify query parameters. And this we can't provide...

    But this is only for referring to what parameter holder, right? I.e what we support right now with the query rewrite process?

  6. roji commented on Oct 28, 2014

    @roji
    Member

    Well, the way I understand it there are two different problems: function calls and parameterized queries. In function calls the user doesn't specify placeholders in the query text: they just specify the function name. Npgsql then needs to generate the SQL with the placeholders. What we're discussing in this issue is only related to how Npgsql generates the SQL from the user's input - it doesn't touch the question of what happens in parameterized queries (where placeholders are exposed to the user).

  7. Emill commented on Oct 28, 2014

    @Emill
    Contributor

    Yes. So this means the named parameter notation approach would work, since we do this only when having CommandType set to StoredProcedure?

  8. roji commented on Oct 28, 2014

    @roji
    Member

    That's right. It's a feature for StoredProcedure only.

    Of course the specifics need to be worked out. Note that Postgresql supports mixed notation, where positional arguments come first, followed by named arguments. We'd have to implement it properly etc.

  9. removed this from the milestone on Mar 29, 2015
  10. shimole commented on Aug 13, 2015

    @shimole

    Guys, any updates on this? I'm moving my backend from SQL to postgres and suffering a lot since I should recheck all 100+ backend schema to be matching DB functions' params order.

  11. roji commented on Aug 13, 2015

    @roji
    Member

    I'm sorry, with everything that's happening right now in Npgsql (v3, EF7...) there just hasn't been any spare time for this yet...

  12. self-assigned this
    on Aug 23, 2015
  13. changed the title [-][#1010781] Npgsql doesn't support pass parameters by name[/-] [+]Npgsql doesn't support pass parameters by name to functions[/+] on Aug 23, 2015
  14. added this to the milestone on Aug 23, 2015
  15. Emill commented on Aug 23, 2015

    @Emill
    Contributor

    I changed so that parameters with a name are now passed using their name. Parameters without names entered by the user will now be sent normally using Positional Notation. In case there are some parameters with names and some without, Mixed Notation is used.

    I hope this won't affect people that has previously set names that don't correspond to the PostgreSQL parameter name...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions