pg.ddx.io  pgsql-jdbc@postgresql.org mailing list archive  
help / color / mirror / Atom feed
JDBC CallableStatement bug on functions with return parameter
6+ messages / 3 participants
[nested] [flat]

* JDBC CallableStatement bug on functions with return parameter
@ 2011-02-04 22:12 John LH <johnlh@aquafold.com>
  0 siblings, 0 replies; 6+ messages in thread

From: John LH @ 2011-02-04 22:12 UTC (permalink / raw)
  To: pgsql-jdbc

  Hi,

I believe there is a bug in the PostgreSQL JDBC driver.  I have a 
function "sales_tax (in subtotal money) RETURNS money" with DDL below.  
I am trying to execute the function with a CallableStatement.  I use the 
SQL of :

{ ? = CALL "public"."sales_tax"(?) }

I register the parameters and then execute the statement, but it fails 
with the error message :

ERROR: function public.sales_tax(double precision) does not exist

I trace the code down to the 
org.postgresql.jdbc2.AbstractJdbc2Statement.modifyJdbcCall(String 
p_sql).  At the end of this function on line 2384-2410 (Pasted below) it 
makes a conversion.  It seems that the CallableStatement converts the 
original SQL above to :

select * from "public"."sales_tax"(?, ?) as result

If you execute this statement with substituted variable a terminal 
window into PG, the statement will fail.  The syntax seems to be 
incorrect.  But the SQL will execute correctly if you execute :

select * from "public"."sales_tax"(?) as result

I am not sure what is the correct behavior, but this doesn't seem to be 
correct as I can't execute a function with a return value.

Does anyone know if this is a bug or a known limitation? ... or if I am 
doing something wrong?

thanks
-John

== PROCEDURE DDL ==============================================

CREATE OR REPLACE FUNCTION public.sales_tax (in subtotal money) RETURNS 
money AS
$BODY$
BEGIN
     RETURN subtotal * 0.06;
END;
$BODY$
LANGUAGE 'plpgsql'
GO

================================================================
== AbstractJdbc2Statement.modifyJdbcCall(String p_sql) ===============
         if (connection.haveMinimumServerVersion("8.1") && 
((AbstractJdbc2Connection)connection).getProtocolVersion() == 3)
         {
             String s = p_sql.substring(startIndex, endIndex );
             StringBuffer sb = new StringBuffer(s);
             if ( outParmBeforeFunc )
             {
                     // move the single out parameter into the function call
                     // so that it can be treated like all other parameters
                     boolean needComma=false;

                     // have to use String.indexOf for java 2
                     int opening = s.indexOf('(')+1;
                     int closing = s.indexOf(')');
                     for ( int j=opening; j< closing;j++ )
                     {
                         if ( !Character.isWhitespace(sb.charAt(j)) )
                         {
                             needComma = true;
                             break;
                         }
                     }
                     if ( needComma )
                     {
                         sb.insert(opening, "?,");
                     }
                     else
                     {
                         sb.insert(opening, "?");
                     }

             }
             return "select * from " + sb.toString() + " as result";
         }
================================================================



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JDBC CallableStatement bug on functions with return parameter
@ 2011-02-05 15:23 Kevin Grittner <Kevin.Grittner@wicourts.gov>
  2011-02-05 16:21 ` Re: JDBC CallableStatement bug on functions with return	 parameter John LH <johnlh@aquafold.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Kevin Grittner @ 2011-02-05 15:23 UTC (permalink / raw)
  To: johnlh@aquafold.com; pgsql-jdbc

John LH  wrote:
 
> I have a function "sales_tax (in subtotal money) RETURNS money"
 
> I register the parameters and then execute the statement, but it
> fails with the error message :
> 
> ERROR: function public.sales_tax(double precision) does not exist
 
What class of Java object are you trying to use for the parameter?
 
-Kevin



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JDBC CallableStatement bug on functions with return	 parameter
  2011-02-05 15:23 Re: JDBC CallableStatement bug on functions with return parameter Kevin Grittner <Kevin.Grittner@wicourts.gov>
@ 2011-02-05 16:21 ` John LH <johnlh@aquafold.com>
  2011-02-06 16:24   ` Re: JDBC CallableStatement bug on functions with return  parameter Lew <noone@lewscanon.com>
  0 siblings, 1 reply; 6+ messages in thread

From: John LH @ 2011-02-05 16:21 UTC (permalink / raw)
  To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: pgsql-jdbc

  Hi,

I create the CallableStatement with this :

callStatement = conn.prepareCall("{ ? = CALL "public"."sales_tax"(?) }");

For the return parameter I use :

callStatement.registerOutParameter(1, java.sql.Types.FLOAT);

For the function parameter I use :

callStatement.setObject(2, new Float(25.25));

After this, the Jdbc4CallableStatement SQL is :

select * from "public"."sales_tax"(null,25.25)  as result

When I callStatement.execute() I get this :

org.postgresql.util.PSQLException: ERROR: function 
public.sales_tax(double precision) does not exist

I've also tried the combination and results of ...

===
java.sql.Types.DOUBLE/Double
org.postgresql.util.PSQLException: ERROR: function 
public.sales_tax(double precision) does not exist
===
java.sql.Types.NUMERIC/BigDecimal
org.postgresql.util.PSQLException: ERROR: function 
public.sales_tax(numeric) does not exist

thanks
-John



On 2/5/2011 7:23 AM, Kevin Grittner wrote:
> John LH  wrote:
>
>> I have a function "sales_tax (in subtotal money) RETURNS money"
>
>> I register the parameters and then execute the statement, but it
>> fails with the error message :
>>
>> ERROR: function public.sales_tax(double precision) does not exist
>
> What class of Java object are you trying to use for the parameter?
>
> -Kevin
>



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JDBC CallableStatement bug on functions with return  parameter
  2011-02-05 15:23 Re: JDBC CallableStatement bug on functions with return parameter Kevin Grittner <Kevin.Grittner@wicourts.gov>
  2011-02-05 16:21 ` Re: JDBC CallableStatement bug on functions with return	 parameter John LH <johnlh@aquafold.com>
@ 2011-02-06 16:24   ` Lew <noone@lewscanon.com>
  0 siblings, 0 replies; 6+ messages in thread

From: Lew @ 2011-02-06 16:24 UTC (permalink / raw)
  To: pgsql-jdbc

(Please do not top-post.)

John LH wrote:
>
> I create the CallableStatement with this :
>
> callStatement = conn.prepareCall("{ ? = CALL "public"."sales_tax"(?) }");
>
> For the return parameter I use :
>
> callStatement.registerOutParameter(1, java.sql.Types.FLOAT);
>
> For the function parameter I use :
>
> callStatement.setObject(2, new Float(25.25));

You do realize that 'float' ('Float') is pretty much the worst type to use for 
monetary facts, right?


-- 
Lew
Ceci n'est pas une fenĂȘtre.
.___________.
|###] | [###|
|##/  | *\##|
|#/ * |   \#|
|#----|----#|
||    |  * ||
|o *  |    o|
|_____|_____|
|===========|



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JDBC CallableStatement bug on functions with return	 parameter
@ 2011-02-05 17:43 Kevin Grittner <Kevin.Grittner@wicourts.gov>
  2011-02-05 18:00 ` Re: JDBC CallableStatement bug on functions with	 return parameter John LH <johnlh@aquafold.com>
  0 siblings, 1 reply; 6+ messages in thread

From: Kevin Grittner @ 2011-02-05 17:43 UTC (permalink / raw)
  To: johnlh@aquafold.com; +Cc: pgsql-jdbc

John LH  wrote:
 
> callStatement.registerOutParameter(1, java.sql.Types.FLOAT);
 
> callStatement.setObject(2, new Float(25.25));
 
PostgreSQL doesn't support using approximate data types (like Float)
for money.  Try compiling and running this to see why:
 
import java.math.BigDecimal;
public class ExactnessTest
{
    public static void main(String[] args)
    {
        double approximate;
        BigDecimal exact;

        // Set an approximation of a penny.
        approximate = 0.01;
        // Put the actual value into an exact class.
        exact = new BigDecimal(approximate);
        // Show the actual value assigned to the double.
        System.out.println(exact);
        // Create and show an exact decimal penny.
        exact = new BigDecimal("0.01");
        System.out.println(exact);
    }
}
 
Try using BigDecimal, which *is* capable of storing exact
representations of all decimal fractions.  Note that feeding an
approximate value, including an unquoted literal, to the BigDecimal
constructor will cause the BigDecimal value to be less precise than
you might expect.
 
-Kevin



^ permalink  raw  reply  [nested|flat] 6+ messages in thread

* Re: JDBC CallableStatement bug on functions with	 return parameter
  2011-02-05 17:43 Re: JDBC CallableStatement bug on functions with return	 parameter Kevin Grittner <Kevin.Grittner@wicourts.gov>
@ 2011-02-05 18:00 ` John LH <johnlh@aquafold.com>
  0 siblings, 0 replies; 6+ messages in thread

From: John LH @ 2011-02-05 18:00 UTC (permalink / raw)
  To: Kevin Grittner <Kevin.Grittner@wicourts.gov>; +Cc: pgsql-jdbc

  Hi,

 >> Try using BigDecimal, which *is* capable of storing exact 
representations of all decimal fractions.

In my previous email I mentioned that I also tried BigDecimal and it did 
not work.

 >> I've also tried the combination and results of ...
 >> ===
 >> java.sql.Types.DOUBLE/Double
 >> org.postgresql.util.PSQLException: ERROR: function 
public.sales_tax(double precision) does not exist
 >> ===
 >> java.sql.Types.NUMERIC/BigDecimal
 >> org.postgresql.util.PSQLException: ERROR: function 
public.sales_tax(numeric) does not exist

I am aware of the approximate datatype issue with Float and Double.  The 
reason I tried Float and Double is because if I call 
statement.execute("select * from tableWithMoney"), and then call 
ResultSet.getObject("money_column"), it will actually return a Double 
datatype.  Which is a possible bug in the JDBC driver.

I tried Float, Double and BigDecimal.  I can't find any documentation 
indicating what datatype to use.  Looking at the JDBC driver code it 
looks like it is incorrectly converting the SQL.

thanks
-John


On 2/5/2011 9:43 AM, Kevin Grittner wrote:
> John LH  wrote:
>
>> callStatement.registerOutParameter(1, java.sql.Types.FLOAT);
>
>> callStatement.setObject(2, new Float(25.25));
>
> PostgreSQL doesn't support using approximate data types (like Float)
> for money.  Try compiling and running this to see why:
>
> import java.math.BigDecimal;
> public class ExactnessTest
> {
>      public static void main(String[] args)
>      {
>          double approximate;
>          BigDecimal exact;
>
>          // Set an approximation of a penny.
>          approximate = 0.01;
>          // Put the actual value into an exact class.
>          exact = new BigDecimal(approximate);
>          // Show the actual value assigned to the double.
>          System.out.println(exact);
>          // Create and show an exact decimal penny.
>          exact = new BigDecimal("0.01");
>          System.out.println(exact);
>      }
> }
>
> Try using BigDecimal, which *is* capable of storing exact
> representations of all decimal fractions.  Note that feeding an
> approximate value, including an unquoted literal, to the BigDecimal
> constructor will cause the BigDecimal value to be less precise than
> you might expect.
>
> -Kevin
>



^ permalink  raw  reply  [nested|flat] 6+ messages in thread


end of thread, other threads:[~2011-02-06 16:24 UTC | newest]

Thread overview: 6+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2011-02-04 22:12 JDBC CallableStatement bug on functions with return parameter John LH <johnlh@aquafold.com>
2011-02-05 15:23 Re: JDBC CallableStatement bug on functions with return parameter Kevin Grittner <Kevin.Grittner@wicourts.gov>
2011-02-05 16:21 ` Re: JDBC CallableStatement bug on functions with return	 parameter John LH <johnlh@aquafold.com>
2011-02-06 16:24   ` Re: JDBC CallableStatement bug on functions with return  parameter Lew <noone@lewscanon.com>
2011-02-05 17:43 Re: JDBC CallableStatement bug on functions with return	 parameter Kevin Grittner <Kevin.Grittner@wicourts.gov>
2011-02-05 18:00 ` Re: JDBC CallableStatement bug on functions with	 return parameter John LH <johnlh@aquafold.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox