npgsql icon indicating copy to clipboard operation
npgsql copied to clipboard

Is there a way to pass parameter into XPath NpgsqlCommand?

Open stkash opened this issue 6 years ago • 5 comments

Steps to reproduce

Here is a code snippet:

string connstr = "PORT=5432;DATABASE=mydb;HOST=myhost;USER ID=myuser;PASSWORD=mypassword";
using (NpgsqlConnection connection = new NpgsqlConnection(connstr))
{
    connection.Open();

    string sql = "select xml_column from mytable where xmlexists('//Tag[text()=\"@value\"]' PASSING BY REF xml_column) = true";
    using (var cmd = connection.CreateCommand())
    {
        cmd.CommandText = sql;
        cmd.Parameters.AddWithValue("@value", "my value");
        using (var reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                reader[0].Dump();
            }
        }
    }

    connection.Close();
}

The issue

In SQL Server I can use sql:variable: string sql = "select * from mytable where xml_column.exist('//Tag[text()=sql:variable(\"@value\")]') = 1";

I was trying to find similar way to pass parameter into NpgsqlCommand. This one for example does not fail but also does not return any data: string sql = "select * from mytable where xmlexists('//Tag[text()=\"@value\"]' PASSING BY REF xml_column) = true";

Further technical details

Npgsql version: 4.0.2 PostgreSQL version: 9.6 Operating system: Windows 10

Crosspost to Stack Overflow: https://stackoverflow.com/questions/61644001/is-there-a-way-to-pass-parameter-into-xpath-npgsqlcommand

stkash avatar May 06 '20 20:05 stkash

There are two ways of doing so.

Using the first one you should pass the entire XPath query as a parameter, but in that case escape quotes on the client to prevent SQL injections:

cmd.CommandText = "select * from mytable where xmlexists(@value PASSING BY REF xml_column)";
cmd.Parameters.AddWithValue("@value", "//Tag[text()='my value']");

Or using string interpolation:

cmd.CommandText = "select * from mytable where xmlexists(@value PASSING BY REF xml_column)";
cmd.Parameters.AddWithValue($"@value", "//Tag[text()='{myArg.Replace("'", "''")}']");

An other way is to use the quote_literal function and the server side concatenation:

select * from mytable where xmlexists(('//Tag[text()=' || quote_literal(@value) || ']')
 PASSING BY REF xml_column)

Or using dollar quoted strings and the format function:

select * from mytable where xmlexists(format($$//Tag[text()=%s]$$, quote_literal(@value))
 PASSING BY REF xml_column)

YohDeadfall avatar May 06 '20 22:05 YohDeadfall

Thank you for the quick response. I tried the 3rd suggestion, with quote_literal function and the server side concatenation, but could not make it work, getting:

PostgresException 
42601: syntax error at or near "||"

stkash avatar May 07 '20 13:05 stkash

Add braces around concatenation expression or use the concat function instead:

select * from mytable where xmlexists(('//Tag[text()=' || quote_literal(@value) || ']') PASSING BY REF xml_column);
-- or
select * from mytable where xmlexists(concat('//Tag[text()=', quote_literal(@value), ']') PASSING BY REF xml_column);

YohDeadfall avatar May 07 '20 15:05 YohDeadfall

Thank you!

Another related question then. If I have <Tag>a'a</Tag>, this works: select * from mytable where xmlexists(('//Tag[text()="' || $$a'a$$ || '"]') PASSING BY REF xml_column) = true But for <Tag>a"a</Tag> this fails, saying DETAIL: Invalid predicate select * from mytable where xmlexists(('//Tag[text()="' || $$a"a$$ || '"]') PASSING BY REF xml_column) = true

stkash avatar May 07 '20 16:05 stkash

Unfortunately PostgreSQL supports XPath version 1.0, so escaping isn't possible, but there are some workarounds in the Internet.

YohDeadfall avatar May 07 '20 20:05 YohDeadfall