SCM

[#1010557] Backslash gets doubled when passed to a stored function in a varchar argument

View Trackers | Bugs | Download .csv | Monitor

Date:
2009-02-11 15:41
Priority:
3
State:
Open
Submitted by:
Vasily Romanov (vasiliy)
Assigned to:
Nobody (None)
Npgsql Version:
None
Category:
Group:
Resolution:
None
Summary:
Backslash gets doubled when passed to a stored function in a varchar argument

Detailed description
Dear npgsql developer,

A backslash contained in a stored function argument value gets doubled when passed to the DB via NpgsqlParameter.

Example:

1) Create a test function:

CREATE OR REPLACE FUNCTION test_backslash(arg character varying)
RETURNS character varying AS
$BODY$
BEGIN
RETURN arg;
END;
$BODY$
LANGUAGE 'plpgsql';

2) Invoke it from psql:

select test_backslash('test\str');

Result:

test_backslash
----------------
test\str
(1 row)

3) Invoke it using npgsql. The code below is written in VB.NET. Npgsql namespace is imported. Please note the argument value passed: test\str

Sub TestBackslash()

Dim conn As New NpgsqlConnection(...)
conn.Open()

Dim cmd As New NpgsqlCommand("test_backslash", conn)
cmd.CommandType = CommandType.StoredProcedure

Dim pp As NpgsqlParameterCollection = cmd.Parameters

Dim p As NpgsqlParameter = pp.Add("arg", NpgsqlTypes.NpgsqlDbType.Varchar)

p.Value = "test\str"

Dim res As String = cmd.ExecuteScalar()

MsgBox(res)

conn.Close()

End Sub

The message box will display test\\str

I think the described behavior is a bug. I didn't find a similar bug report in the project tracker. Should I post a new bug report or could you please do it? What should I do next in this respect? (We are working on porting an ERP system to a free platform, namely Mono+PostgreS.)

Thank you for your attention!

Vasily

Followup

Message
Date: 2009-02-16 15:05
Sender: Francisco Figueiredo jr.


Hi, Vasily!

I think this may be related about how vb.net handles strings. I'll have to investigate this more, but I don't use vb.net.
In C#, I would need to use a double backslash in the string in order to get a single backslash or use the @ string prefix: @"test\str".
I still have no clue about how vb.net is handling and translating the backslashes inside the string.

It would be very helpful if you and your team could do some tests with backslashes and strings in order to get what could be happening.

Enable Npgsql logging (see http://manual.npgsql.org for details) or enable postgresql logging and see what Npgsql is sending to server. On my tests it is sending the following:
select (E'test\\str'::text)

where you can see the double backslash which is translated by Npgsql inside the method ConvertToBackendPlainQuery in NpgsqlTypes\NpgsqlTypesHelper.cs file.

I hope it helps.
Date: 2009-02-12 23:25
Sender: Vasily Romanov

Thank you, Francisco!

I wonder if our team could be of any help here. Would you
recommend us analyze npgsql source code for a possible fix?
Which module do you think can be a suspect? Or perhaps the
problem is caused by a lower level, PostgreS client
library, not npgsql?

Vasily
Date: 2009-02-12 15:06
Sender: Francisco Figueiredo jr.


Hi, Vasily!

I'm changing your report to a bug report. We will work on this.

Thanks for your report!

Attached Files:

Changes:

No Changes Have Been Made to This Item

Powered By FusionForge