Skip to content

parameter parsing fails (regression) #296

Closed

Description

@kobruleht

Newer npgsql versions does not parse :p parameters properly.
In 2.2.0-beta1 code below produces error

syntax error at or near ":"

shown in comment. npgsql sends :p6 to server without parsing it.

        using (var connection = new NpgsqlConnection(ConnectionString()))
        {
            connection.Open();
            DbCommand command = (DbCommand)connection.CreateCommand();
           command.CommandText = @"
 SELECT 1
 WHERE ''= 'type(''m.response'')#''O''%' AND :p6
            ";

           for (int i = 0; i < 7; i++)
           {
               IDbDataParameter param = command.CreateParameter();
               param.ParameterName = "p" + i.ToString();
               var value = DBNull.Value;
               command.Parameters.Add(param);
           }
           // syntax error at or near ":"
            command.ExecuteReader();
         }

    static string ConnectionString()
    {
        NpgsqlConnectionStringBuilder csb = new NpgsqlConnectionStringBuilder()
        {
            SearchPath = "public",
            Host = "localhost",
            Database = "postgres",
            UserName = "postgres",
            Password = "secret"
        };
        return csb.ConnectionString;
    }

Activity

  1. added this to the milestone on Jul 26, 2014
  2. roji commented on Jul 26, 2014

    @roji
    Member

    Should be resolved for 2.2...

  3. roji commented on Jul 26, 2014

    @roji
    Member

    @kobruleht, any way you could send SQL to create the schema for this query, to help with reproducing?

  4. kobruleht commented on Jul 26, 2014

    @kobruleht
    Author

    This occurs with any schema. No need to create schema for reproducing.

    Error is reported from Postgres server during parsing :p6 and before tables are accessed. I verified it by specifying standard postgres database as database name.
    I updated issue and added ConnectionString() method.

  5. kobruleht commented on Jul 26, 2014

    @kobruleht
    Author

    @roji I updated testcase so it does not reference any tables.

  6. added a commit that references this issue on Jul 26, 2014
  7. roji commented on Jul 26, 2014

    @roji
    Member

    Thanks for reporting @kobruleht, I can indeed reproduce this.

    @glenebob, I think this is in your area of expertise... I've made a commit with a repro test: roji@b1a0378. Running it clearly shows that the string :p6 gets sent to the backend instead of getting replaced in AppendCommandReplacingParameterValues. Do you think you can take a look?

  8. kobruleht commented on Jul 26, 2014

    @kobruleht
    Author

    @roji my updated testcase contains 2 lines of sql to reproduce the issue. You can replace big sql in your commit with those 2 lines. It looke like AppendCommandReplacingParameterValues does not work properly if sql string contains '' which represent single quote.
    In earlier versions of npgsql it worked OK.

  9. franciscojunior commented on Jul 29, 2014

    @franciscojunior
    Member

    Hi, @glenebob ! Were you able to have a look at this issue? It is a pretty important regression.
    Thanks in advance!

  10. glenebob commented on Jul 29, 2014

    @glenebob
    Contributor

    Ah, no, I missed it (was out on vacation all last week). I'll try to look
    at it in the next couple days.

    -Glen

    On Tue, Jul 29, 2014 at 9:55 AM, Francisco Figueiredo Jr. <
    [email protected]> wrote:

    Hi, @glenebob https://github.com/glenebob ! Were you able to have a
    look at this issue? It is a pretty important regression.
    Thanks in advance!

    —
    Reply to this email directly or view it on GitHub
    #296 (comment).

  11. franciscojunior commented on Jul 29, 2014

    @franciscojunior
    Member

    Ah, no, I missed it (was out on vacation all last week). I'll try to look at it in the next couple days.

    Thank you very much, Glen!

  12. franciscojunior commented on Aug 1, 2014

    @franciscojunior
    Member

    I think this problem was also reported in #240 .

  13. glenebob commented on Aug 2, 2014

    @glenebob
    Contributor

    @roji, I took your repro test and simplified the heck out of it :) I should have this fixed shortly.

    -Glen

  14. 9 remaining items

  15. added a commit that references this issue on Aug 4, 2014
  16. kobruleht commented on Aug 13, 2014

    @kobruleht
    Author

    Ca we add empty string

    select ''

    to unit test. It also stopped parameter parsing.

  17. franciscojunior commented on Aug 17, 2014

    @franciscojunior
    Member

    This is fixed in Npgsql 2.2.0-rc1. Please, give it a try and let us know if you find any other problem.

  18. franciscojunior commented on Aug 17, 2014

    @franciscojunior
    Member

    @kobruleht , can you confirm if the empty string is still an issue? It was supposed to be fixed by now. I'm closing this issue now. If you still find problems with it, let us know.

  19. FransBouma commented on Oct 23, 2014

    @FransBouma
    Contributor

    Still fails in 2.2.1 with an output parameter.

    proc:
    CREATE OR REPLACE FUNCTION pr_getcustomeramount(IN pcountry character varying, OUT pcustomercount integer)
    RETURNS integer AS
    $BODY$
    BEGIN
    SELECT COUNT(*) INTO pCustomerCount FROM Customers WHERE Country = pCountry;
    END;
    $BODY$
    LANGUAGE plpgsql VOLATILE
    COST 100;
    ALTER FUNCTION pr_getcustomeramount(character varying)
    OWNER TO postgres;

    (on northwind DB on postgres).

    Call: "Northwind"."public"."pr_getcustomeramount"(:pcountry, :pcustomercount)"

    Fails with the same error at ':'. See for more info:
    http://www.llblgen.com/tinyforum/GotoMessage.aspx?MessageID=130522&ThreadID=22680

    I'll see if I have time later today whether I can repro it in a unit test with the latest trunk code of npgsql. I can reproduce it on the released 2.2.1 assembly (.net 4.0 build)

  20. Emill commented on Oct 23, 2014

    @Emill
    Contributor

    It would be good with a unit test that shows the problem. I don't know how llblgen calls Npgsql. For example, is command type set to stored procedure or text.

  21. FransBouma commented on Oct 23, 2014

    @FransBouma
    Contributor

    Sorry, should have given that info. It creates a DbCommand using the factory and sets the commandtype to StoredProcedure. Input parameters only calls work fine, output parameters don't. I'll write a test which uses native commands to illustrate the problem.

  22. franciscojunior commented on Oct 23, 2014

    @franciscojunior
    Member

    Hi, @FransBouma !

    When working with parameter in a NpgsqlCommand whose commandtype is StoredProcedure, you don't need to specify them in the CommandText. You just need to add the parameters as usual in the Parameters collection and let Npgsql take care of them for you.

    In the case of output parameters, Postgresql return them as a resultset after the function is executed. Them, Npgsql will map them to any output parameter specified in your Parameters collection. If you add the output parameter marker in the commandtext, Npgsql will ignore it when calling the function (because it is output parameter) and then you get the error.
    In the case of inputparameters, Npgsql will use them when calling the function. That's why you don't get errors when using input parameters only.
    In order to be consistent and don't have this problem again, I'd suggest you to not add any parameter markers at all in your commandtext.

    Check here a test we have about output parameter in our testsuite: (you can find a lot of other output parameters usage in our testsuite)

    [Test]
            public void TestBug1006158OutputParameters()
            {
                const string createFunction =
                    @"CREATE OR REPLACE FUNCTION more_params(OUT a integer, OUT b boolean) AS
                $BODY$DECLARE
                    BEGIN
                        a := 3;
                        b := true;
                    END;$BODY$
                  LANGUAGE 'plpgsql' VOLATILE;";
    
                var command = new NpgsqlCommand(createFunction, Conn);
                command.ExecuteNonQuery();
    
                command = new NpgsqlCommand("more_params", Conn);
                command.CommandType = CommandType.StoredProcedure;
    
                command.Parameters.Add(new NpgsqlParameter("a", DbType.Int32));
                command.Parameters[0].Direction = ParameterDirection.Output;
                command.Parameters.Add(new NpgsqlParameter("b", DbType.Boolean));
                command.Parameters[1].Direction = ParameterDirection.Output;
    
                var result = command.ExecuteScalar();
    
                Assert.AreEqual(3, command.Parameters[0].Value);
                Assert.AreEqual(true, command.Parameters[1].Value);
            }

    Note that only the name is specified in the commandtext and output parameters are added to the Parameters collection and have their value populated correctly.

    I hope it helps.

  23. FransBouma commented on Oct 23, 2014

    @FransBouma
    Contributor

    Ok that helps :) I'll strip out generating the output proc names and leave the input proc names in, as I do recall they had to be there at some point in old(er) versions of npgsql? (removing all parameters from the header then would break code for ppl who still use older npgsql versions, but I don't have exact info on this)

    (edit) omitting the output parameters works indeed. (on 2.2.1)

  24. Emill commented on Oct 23, 2014

    @Emill
    Contributor
  25. franciscojunior commented on Oct 23, 2014

    @franciscojunior
    Member

    Ok that helps :) I'll strip out generating the output proc names and leave the input proc names in, as I do recall they had to be there at some point in old(er) versions of npgsql? (removing all parameters from the header then would break code for ppl who still use older npgsql versions, but I don't have exact info on this)

    Yeah. If I recall correctly, only very very old Npgsql versions would need the parameter markers when calling functions, so you are right to have them there. I think that for 2.0.x and above you can omit them without fear.

    (edit) omitting the output parameters works indeed. (on 2.2.1)

    Excellent!

    Please, let us know if you find any other problem, Frans!

    I'm a big fan of your work and I'm glad to know Npgsql is being helpful to bring support of your tool to Postgresql!

  26. FransBouma commented on Oct 23, 2014

    @FransBouma
    Contributor

    :) Thanks Francisco! Yes stripping everything off (the brackets and all parameter markers) works fine, which makes it easy to fix this. If v2.x.y supports this, it's fine by me, older versions are too old anyway. So consider this solved! :)

  27. franciscojunior commented on Oct 23, 2014

    @franciscojunior
    Member

    :) Thanks Francisco! Yes stripping everything off (the brackets and all parameter markers) works fine, which makes it easy to fix this. If v2.x.y supports this, it's fine by me, older versions are too old anyway. So consider this solved! :)

    Awesome! I'm glad to hear that. :)

  28. added theissue type on Jun 18, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions