question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

NpgsqlCommand.DeriveParametersForFunction

See original GitHub issue

I’m trying to integrate Npgsql to EntLib here https://github.com/Chavoshi/EnterpriseLibrary.NetCore/pull/27 and EntLib tries to match the number of parameter provided by the user with the number of the parameters detected on the database.

Looks like the query used at NpgsqlCommand.DeriveParametersForFunction is not returning good results for functions that RETURNS SETOF table.

The code is creating one output parameter for each column on the table due to the following code:

                    if (names != null && i < names.Length)
                        param.ParameterName = names[i];
                    else
                        param.ParameterName = "parameter" + (i + 1);

Example function:

CREATE OR REPLACE FUNCTION public.obtercamera(
	pcameraid integer)
    RETURNS SETOF camera 
    LANGUAGE 'plpgsql'

    COST 100
    VOLATILE 
    ROWS 1000
AS $BODY$

BEGIN

	RETURN QUERY
	SELECT CameraId, CameraUrl, Ativo, Criacao, Atualizacao 
	FROM Camera 
	WHERE CameraId = pCameraId OR pCameraId IS NULL;
					  
END

$BODY$;

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jrochacommented, Oct 18, 2018

Got it. Since DAAB is no longer under active development I think the way I managed this behavior to work both ways in DAAB adapter is the best approach.

Nevertheless, IMHO, I don’t think the way Npgsql does it today is semantically consistent. If in the future PostgreSQL starts to support IN, OUT and RETURNS SETOF record (like obtercamera2) it would became confusing.

– Edit: obtercamera2 signature works.

1reaction
Brarcommented, Oct 18, 2018

Ok, thank you.

So what I meant to say was that the following 3 function signatures are functionally equivalent in PostgreSQL and Npgsql:

CREATE FUNCTION obtercamera1(cameraid integer) RETURNS SETOF camera AS ...;

CREATE FUNCTION obtercamera2(
	INOUT cameraid integer,
	OUT cameraurl character varying(255),
	OUT ativo boolean,
	OUT criacao timestamp without time zone,
	OUT alteracao timestamp without time zone
) RETURNS SETOF record AS ...;

CREATE FUNCTION obtercamera3(cameraid integer) RETURNS TABLE(
    cameraid integer,
    cameraurl character varying(255),
    ativo boolean,
    criacao timestamp without time zone,
    alteracao timestamp without time zone
) AS ...;

Edit: edited demo signatures after thinking twice what the correct signature would have to look like.

All 3 signatures should (I haven’t actually tested it today) return the same result set in PostgreSQL and derive the same one IN and 4 5 OUT parameters in Npgsql.

I understand that this isn’t what the Entlib DAAB is expecting but that’s how it is currently implemented.

Changing this would certainly need a broader discussion as it is a change that will break some peoples’ code. Especially given the fact that Entlib is no longer under active development.

From a purity point of view I’d support the argument that obtercamera1 and obtercamera3 can bee seen as different from obtercamera2 from the frontends perspective and shouldn’t derive any OUT parameters.

cc: @roji @austindrenski @YohDeadfall

Read more comments on GitHub >

github_iconTop Results From Across the Web

Cannot derive function parameters for functions returning ...
DeriveParameters (NpgsqlCommand command) for a function which returns a TABLE result causes a NotImplementedException.
Read more >
Class NpgsqlCommandBuilder
DeriveParameters (NpgsqlCommand). This method is responsible to derive the command parameter list with values obtained from function definition.
Read more >
Npgsql failing on CommandBuilder DeriveParameters
The basic problem is that the interface to the PostgreSQL stored function can only use DbTypes specified by the NpgsqlDbType enumeration. The ...
Read more >
DeriveParameters - Oracle Database
This method queries for the parameters of a stored procedure or function, represented by a specified OracleCommand , and populates the OracleParameterCollection ...
Read more >
DB2Command.DeriveParameters Method
Retrieves parameter information for the command that is specified in the current DB2Command object (this) and populates the Parameters collection.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found