Received: from magus.postgresql.org ([87.238.57.229]) by malur.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TOYCg-0007j9-Dy for pgsql-jdbc@postgresql.org; Wed, 17 Oct 2012 18:18:50 +0000 Received: from nm15-vm0.bullet.mail.ac4.yahoo.com ([98.139.52.236]) by magus.postgresql.org with esmtp (Exim 4.72) (envelope-from ) id 1TOYCd-0008UH-7s for pgsql-jdbc@postgresql.org; Wed, 17 Oct 2012 18:18:50 +0000 Received: from [98.139.52.191] by nm15.bullet.mail.ac4.yahoo.com with NNFMP; 17 Oct 2012 18:18:45 -0000 Received: from [76.13.13.45] by tm4.bullet.mail.ac4.yahoo.com with NNFMP; 17 Oct 2012 18:18:45 -0000 Received: from [127.0.0.1] by smtp106.prem.mail.ac4.yahoo.com with NNFMP; 17 Oct 2012 18:18:45 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s1024; t=1350497925; bh=lpPHmxkLHyyhwnOaY44DDhAJjLCnKhWJduIsb5Uw3hU=; h=X-Yahoo-Newman-Id:X-Yahoo-Newman-Property:X-YMail-OSG:X-Yahoo-SMTP:Received:From:To:References:In-Reply-To:Subject:Date:Message-ID:MIME-Version:Content-Type:X-Mailer:Thread-Index:Content-Language; b=Wg0v1lLNJxMrz0P+FocMMtYeGj/T5yzSqTczpFr/AVGG7aaojODQyeY2KggSWw7/fyHCzLrYWrJk8XSKmOxh8MCggOvfp446LxtFE32cdrN0AH2w1wT/zfeQs0Bt/QDcPgri6JUMjgn9oCeTO25ECtF0Mp4EoumJNB0217NRaME= X-Yahoo-Newman-Id: 684507.79170.bm@smtp106.prem.mail.ac4.yahoo.com X-Yahoo-Newman-Property: ymail-3 X-YMail-OSG: U3eaPbEVM1nBHRi9fFtqIQoQptLr_gAKJ_cbHjLX1ZxlggZ 5HH5UQD1.JqfS6CUQjsie9L2CnBbkWKbDQwRuKTQS8SWlOvPxNM8_U32nrTV 9MROl5EyH.wVnNO3t_QdRtyAJQMkaoWRB8oaaXT9um5u632.6sT4iDXAhPkt 8t5Utqfjxf1XuSYnY9Ol2SZ8mR1neQ46cI78JZZLVEPZAPRGKWrHrLrwgdzw Rftl7f3T9l40.3t1ivNc6Sh2fm1g9JkhH80hHMpF1ghkQNI1CnZ_9V.JKgyd u9Hpr1IDiji7K8Z4kUnf5lT3Sjzfue5KO1RVODkVXexwv5l.oYWk0XdT_N0u m_1hr3fw9mgOAvDm9En5Q7LombrAXzGv_Z.EqksJhY.qkS.5JnSvdcqYcT.M 5XwbNuCUIrKJ3zdr0aROUCsc7_q9bU.dsT49_S1T732oYRCjlImrUTwZqNFW NewMmJ06CQfcmGelh51PUIkHfYleFl6U1L8oyhQfgRbnjBPJSIq_0GnGq50l 7WBLBWDY- X-Yahoo-SMTP: mpGJl6eswBD2IBufoVEg0Pa8gg-- Received: from WolfDog (polobo@24.93.23.188 with login) by smtp106.prem.mail.ac4.yahoo.com with SMTP; 17 Oct 2012 11:18:45 -0700 PDT From: "David Johnston" To: "'Martin'" , References: In-Reply-To: Subject: Re: Parameters are not being setted in a copy statement Date: Wed, 17 Oct 2012 14:17:47 -0400 Message-ID: <034201cdac93$b68bd600$23a38200$@yahoo.com> MIME-Version: 1.0 Content-Type: multipart/alternative; boundary="----=_NextPart_000_0343_01CDAC72.2F7EC9E0" X-Mailer: Microsoft Outlook 14.0 Thread-Index: AQI5x/amq+yGUGxbUCoZ11DE4ixgeZbllM/g Content-Language: en-us X-Pg-Spam-Score: -2.4 (--) X-Archive-Number: 201210/73 X-Sequence-Number: 23397 This is a multipart message in MIME format. ------=_NextPart_000_0343_01CDAC72.2F7EC9E0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable From: pgsql-jdbc-owner@postgresql.org = [mailto:pgsql-jdbc-owner@postgresql.org] On Behalf Of Martin Sent: Wednesday, October 17, 2012 11:52 AM To: pgsql-jdbc@postgresql.org Subject: [JDBC] Parameters are not being setted in a copy statement =20 Hi all =20 We need to copy from an arbitray query to a csv. We are using Postgresql = 9.1 an we would use the copy statement We need to trigger this query from a java application. We are using = Hibernate but you could find the problem to jdbc level =20 It's work with query with no parameters but if you try to set = parameters you get a there is no parameter $1 Error =20 Do you know if this is a bug and if there is a workarround to lead with = this? I wirite a test to show this=20 =20 SQL :=20 create schema test; create table test.test_copy ( id serial primary key ,data1 varchar( 100) ,data2 varchar( 100) ); =20 insert into test.test_copy(data1,data2) values = ('test1','test2'); insert into test.test_copy(data1,data2) values = ('test3','test4'); insert into test.test_copy(data1,data2) values = ('test5','test6'); insert into test.test_copy(data1,data2) values = ('test7','test8'); =20 =20 Java test =20 import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; =20 import org.testng.annotations.Test; =20 public class PostgressTest { =20 =20 private String DOES_NOT_WORK =3D "COPY (SELECT * " + "FROM test.test_copy t "+ "WHERE data1 like (?) OR data2 ilike (?) " + ") TO '/data/export/fail.csv' WITH CSV HEADER DELIMITER = ';'"; =20 private String WORK =3D "COPY (SELECT * " + "FROM test.test_copy t "+ "WHERE data1 like ('test1') OR data2 ilike ('test4') " + ") TO '/data/export/work.csv' WITH CSV HEADER DELIMITER = ';'"; private Connection connection; =20 =20 public PostgressTest() throws Exception { Class.forName("org.postgresql.Driver"); connection =3D = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/test", = "my_user", "my_pass"); } @Test public void testDoesNotWork() throws Exception { =20 PreparedStatement statement =3D connection .prepareStatement(DOES_NOT_WORK); statement.setString(1, "test1"); statement.setString(2, "test4"); statement.execute(); } @Test public void testWork() throws Exception { =20 PreparedStatement statement =3D connection .prepareStatement(WORK); statement.execute(); } } =20 =20 Thanks!! Martin =20 I am pretty sure this is PostgreSQL=E2=80=99s limitation and not JDBC = but my quick search for confirmation in the documentation (under COPY, = PREPARE, or EXECUTE) did not turn up anything. =20 Much like =E2=80=9CCREATE TABLE=E2=80=9D and similar statements do not = provide for =E2=80=9Cparameters=E2=80=9D the COPY command does not at = well. Thus in order to do what you want you need to build a = non-parameterized (i.e., no =E2=80=9C?=E2=80=9D) statement and execute = that as-is; making sure you deal with SQL-injection attack vectors. =20 My personal option would be to create a function to encapsulate the = desired query: =20 =E2=80=9CCOPY (SELECT * FROM some_function(=E2=80=9C & param_1_value & = =E2=80=9C , =E2=80=9D & param_2_value & =E2=80=9C ) TO = =E2=80=A6=E2=80=9D =20 Making sure the values of the parameters can be safely inserted as = literals into the query (including adding single-quotes if necessary). =20 David J. =20 ------=_NextPart_000_0343_01CDAC72.2F7EC9E0 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable

From:= = pgsql-jdbc-owner@postgresql.org [mailto:pgsql-jdbc-owner@postgresql.org] = On Behalf Of Martin
Sent: Wednesday, October 17, 2012 = 11:52 AM
To: pgsql-jdbc@postgresql.org
Subject: = [JDBC] Parameters are not being setted in a copy = statement

 

Hi = all

 

We need to copy from an arbitray query to a csv. We = are using Postgresql 9.1 an we would use the copy = statement

We need to = trigger this query from a java application. We are using Hibernate but = you could find the problem to jdbc level

 

 It's work with query with no parameters but if = you try to set parameters you get a  there is no parameter = $1 Error

 

Do you know if this is a bug and if there is a = workarround to lead with this?

I wirite a test to show = this 

 

SQL : 

    create schema = test;

    create = table test.test_copy (

            id serial = primary key

    =         ,data1 varchar( = 100)

      =       ,data2 varchar( 100)

            = );

 

            insert into = test.test_copy(data1,data2) values = ('test1','test2');

  =           insert into = test.test_copy(data1,data2) values = ('test3','test4');

  =           insert into = test.test_copy(data1,data2) values = ('test5','test6');

  =           insert into = test.test_copy(data1,data2) values = ('test7','test8');

 

 

Java test

 

import = java.sql.Connection;

import = java.sql.DriverManager;

import = java.sql.PreparedStatement;

 

import = org.testng.annotations.Test;

 

public class PostgressTest = {

 

   

    private String DOES_NOT_WORK =3D = "COPY (SELECT * " +

            "FROM = test.test_copy t "+

            "WHERE = data1 like (?) OR data2  ilike (?)  " = +

      =       ") TO '/data/export/fail.csv' WITH CSV HEADER = DELIMITER ';'";

    

    private String WORK =3D "COPY = (SELECT * " +

  =           "FROM test.test_copy t = "+

    =         "WHERE data1 like ('test1') OR data2 =  ilike ('test4')  " +

            ") TO = '/data/export/work.csv' WITH CSV HEADER DELIMITER = ';'";

    = private Connection connection;

 

    

    public PostgressTest() throws Exception = {

      =   = Class.forName("org.postgresql.Driver");

        connection =3D = DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/test", = "my_user",

              =   "my_pass");

    }

    @Test

    public void testDoesNotWork() throws = Exception {

 

        PreparedStatement = statement =3D connection

              =   .prepareStatement(DOES_NOT_WORK);

        statement.setString(1, = "test1");

  =       statement.setString(2, = "test4");

  =       statement.execute();

    }

    @Test

    public void testWork() throws Exception = {

 

        PreparedStatement = statement =3D connection

              =   .prepareStatement(WORK);

        = statement.execute();

    }

}

 

 

Thanks!!

Martin

 

I am pretty sure this is PostgreSQL=E2=80=99s limitation and not JDBC = but my quick search for confirmation in the documentation (under COPY, = PREPARE, or EXECUTE) did not turn up anything.

 

Much like =E2=80=9CCREATE TABLE=E2=80=9D and similar statements do = not provide for =E2=80=9Cparameters=E2=80=9D the COPY command does not = at well.=C2=A0 Thus in order to do what you want you need to build a = non-parameterized (i.e., no =E2=80=9C?=E2=80=9D) statement and execute = that as-is; making sure you deal with SQL-injection attack = vectors.

 

My personal option would be to create a function to encapsulate the = desired query:

 

=E2=80=9CCOPY (SELECT * FROM some_function(=E2=80=9C & = param_1_value & =E2=80=9C , =E2=80=9D & param_2_value & = =E2=80=9C ) TO =E2=80=A6=E2=80=9D

 

Making sure the values of the parameters can be safely inserted as = literals into the query (including adding single-quotes if = necessary).

 

David J.

 

------=_NextPart_000_0343_01CDAC72.2F7EC9E0--