BUG #4550: ecpg problem with copy command and hostvar

Started by Clemens Fischerover 17 years ago3 messagesbugs
Jump to latest
#1Clemens Fischer
cfi@mbs-software.de

The following bug has been logged online:

Bug reference: 4550
Logged by: Clemens Fischer
Email address: cfi@mbs-software.de
PostgreSQL version: 8.3.5
Operating system: QNX 6.3
Description: ecpg problem with copy command and hostvar
Details:

Hello List,

Don't know whether anyone here can help but...
We have some code that has compiled and ran just fine from postgresql 7.4.x
thru 8.2.9.
It uses embedded sql.
With the new port - 8.3.5 - this code will compile but the program generates
an error at runtime.
The copy command can't be used with a hostvar.
Below is a standalone code, which shows you the details. Any help or ideas
would be appreciated.

-- snip ecpgtest-copy.pgc begin --
#include <stdio.h>
#include <stdlib.h>

EXEC SQL include sqlca;
EXEC SQL WHENEVER SQLERROR sqlprint;
EXEC SQL WHENEVER SQLWARNING sqlprint;
EXEC SQL WHENEVER NOT FOUND continue;

int main( void )
{
EXEC SQL begin declare section;
int no;
VARCHAR name[30];
char export_file[50];
EXEC SQL end declare section;

ECPGdebug(1, stderr);
// EXEC SQL SET autocommit TO ON; // OFF
EXEC SQL connect to postgres user postgres using postgres;

EXEC SQL DROP TABLE IF EXISTS ecpgtest;
EXEC SQL CREATE TABLE ecpgtest (no INTEGER PRIMARY KEY,
name VARCHAR(30) NOT NULL);
EXEC SQL INSERT INTO ecpgtest VALUES (1, 'first');
EXEC SQL INSERT INTO ecpgtest VALUES (2, 'second');
EXEC SQL INSERT INTO ecpgtest VALUES (3, 'third');

// test 1
EXEC SQL COPY ecpgtest TO '/tmp/ecpgtest_export1';

// test 2
strcpy(export_file, "/tmp/ecpgtest_export2");
EXEC SQL COPY ecpgtest TO :export_file;

EXEC SQL DISCONNECT ALL;
ECPGdebug(0, stderr);

return( EXIT_SUCCESS );
}
-- snip ecpgtest-copy.pgc end --

Running the compiled program generates the following output:

-- snip ecpgtest-copy output begin --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
<DEFAULT> for user postgres
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
0 parameter on connection postgres
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
primary key , name varchar ( 30 ) not null ) with 0 parameter on
connection postgres
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
'first' ) with 0 parameter on connection postgres
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
'second' ) with 0 parameter on connection postgres
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
'third' ) with 0 parameter on connection postgres
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
'/tmp/ecpgtest_export1' with 0 parameter on connection postgres
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
parameter on connection postgres
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
near "$1"
LINE 1: copy ecpgtest to $1
^
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
error at or near "$1"' in line 33.'.
sql error 'syntax error at or near "$1"' in line 33.
[11395125]: ecpg_finish: Connection postgres closed. -- snip ecpgtest-copy output end --
-- snip ecpgtest-copy output end --

-- snip ecpgtest-copy.c code fragment begin, processed by ecpg 4.4.1 --
// test 1
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "copy ecpgtest to
'/tmp/ecpgtest_export1' ", ECPGt_EOIT, ECPGt_EORT);
#line 29 "ecpgtest-copy.pgc"

if (sqlca.sqlwarn[0] == 'W') sqlprint();
#line 29 "ecpgtest-copy.pgc"

if (sqlca.sqlcode < 0) sqlprint();}
#line 29 "ecpgtest-copy.pgc"

// test 2
strcpy(export_file, "/tmp/ecpgtest_export2");
{ ECPGdo(__LINE__, 0, 1, NULL, 0, ECPGst_normal, "copy ecpgtest to $1
",
ECPGt_char,(export_file),(long)50,(long)1,(50)*sizeof(char),
ECPGt_NO_INDICATOR, NULL , 0L, 0L, 0L, ECPGt_EOIT, ECPGt_EORT);
#line 33 "ecpgtest-copy.pgc"

if (sqlca.sqlwarn[0] == 'W') sqlprint();
#line 33 "ecpgtest-copy.pgc"
-- snip ecpgtest-copy.c code fragment end --

I think the hostvar is correctly detected (parameter 1 =
/tmp/ecpgtest_export2).

Hope that helps.
Best regards
Clemens Fischer

#2Michael Meskes
meskes@postgresql.org
In reply to: Clemens Fischer (#1)
Re: BUG #4550: ecpg problem with copy command and hostvar

On Tue, Nov 25, 2008 at 12:28:38PM +0000, Clemens Fischer wrote:

The copy command can't be used with a hostvar.

This is essentially the answer. The major difference between the ecpg version
of PostgreSQL 8.2 and 8.3 is that the 8.3 version uses the backend supplied
prepare/execute facility instead of just simulating this feature. Part of this
change was to change the variable handling and no longer construct strings on
the client side but pass the variables to the backend directly. But there is no
way to pass a variable filename for the copy command AFAIK. Or in other words,
the old versions had a bug in that they accepted a variable there.

Can you use EXECUTE and put the whole statement into a variable instead?

Michael

--
Michael Meskes
Michael at Fam-Meskes dot De, Michael at Meskes dot (De|Com|Net|Org)
Michael at BorussiaFan dot De, Meskes at (Debian|Postgresql) dot Org
ICQ: 179140304, AIM/Yahoo: michaelmeskes, Jabber: meskes@jabber.org
Go VfL Borussia! Go SF 49ers! Use Debian GNU/Linux! Use PostgreSQL!

#3Clemens A. Fischer
clemens.fischer@mbs-software.de
In reply to: Michael Meskes (#2)
Re: BUG #4550: ecpg problem with copy command and hostvar

Hallo Michael,

Am Mittwoch, den 26.11.2008, 14:49 +0100 schrieb Michael Meskes:

On Tue, Nov 25, 2008 at 12:28:38PM +0000, Clemens Fischer wrote:

The copy command can't be used with a hostvar.

This is essentially the answer. The major difference between the ecpg version
of PostgreSQL 8.2 and 8.3 is that the 8.3 version uses the backend supplied
prepare/execute facility instead of just simulating this feature. Part of this
change was to change the variable handling and no longer construct strings on
the client side but pass the variables to the backend directly. But there is no
way to pass a variable filename for the copy command AFAIK. Or in other words,
the old versions had a bug in that they accepted a variable there.

Can you use EXECUTE and put the whole statement into a variable instead?

I switched to EXECUTE yesterday. It works.

Michael

Thank you

Clemens