Thread: ECPG & 'exec sql connect to' question

ECPG & 'exec sql connect to' question

From
"Pavel PaJaSoft Janousek"
Date:
Hi all,

    I've one big problem. Last month I saw in this mailing-list one example, where was this line:

'exec sql connect to unix:postgresql://localhost:5432/mm; '

    I thought, that this example is for ECPG, but....

    I'm using PostgreSQL 6.4.2 - ECPG is from this release (without other patches, which are distributed in this
mailing-list).I try compile my program, but I got always the same error: <source>:<line>: parse error at or near
'postgresql'.

    So, I write small example:

All described this I make by psql monitor... I create database 'test'. Database 'test' is created by unix user 'paja'
('paja'is postgres user too). I create new postgres user 'petr' with password 'petr'. As 'paja', I connect to database
'test'and create table 'pom' with some cols, then execute this: 'grant insert on pom to petr;'. All is OK, but now, I
wantconnect to database 'test' by user 'petr'... of course, I cant' use simple command: 
'exec sql connect 'test';', because I want have only rights as user 'petr' (only inserting rows).
    My problem is, that I don't know, how I can connect to database (on local system or remote...) with specifiing user
andhis password. 
    I know, that I can use libpq directly, but I want do this by ECPG, not by using the PostgreSQL C library.

    Thank you for any ideas....

Pavel PaJaSoft Janousek
FoNet, spol. s r.o.

PS: A wrote this question in november 1998, but I didn't got some answer. Of course, I specify my question rather
diferent...


Re: [INTERFACES] ECPG & 'exec sql connect to' question

From
James Thompson
Date:
On Thu, 4 Mar 1999, Pavel PaJaSoft Janousek wrote:

>     Hi all,
>
>     I've one big problem. Last month I saw in this mailing-list one example, where was this line:
>
> 'exec sql connect to unix:postgresql://localhost:5432/mm; '
>

Here is a copy of the subroutine I use.  I'm pretty sure it worked fine
with 6.4.2 and the ecpg that came with it.  The obstrdup() is a simple
string copy function, obstrappend appends the second string arg to the
first.  You should be able to get rid of them easily, if not I'll send you
those as well.  Hope this helps.

int
db_connect(char *username, char *password, char *host)
{
  exec sql begin declare section;
  char *xpassword;
  char *xusername;
  char *xhost;
  exec sql end declare section;

  assert (username != NULL);
  assert (password != NULL);

  xpassword = obstrdup(password);
  xusername = obstrdup(username);
  if (host)
    {
      xhost = obstrdup("tcp:postgresql://");
      xhost = obstrappend(xhost,host);
      xhost = obstrappend(xhost,"/obe");
    } else {
      xhost = obstrdup("tcp:postgresql://127.0.0.1/obe");
    }

  /* <tcp|unix>:postgresql://server[:port][/dbname][?options] */

    EXEC SQL CONNECT TO :xhost USER :xusername IDENTIFIED BY :xpassword;

    return 0;
}



->->->->->->->->->->->->->->->->->->---<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<
James Thompson    138 Cardwell Hall  Manhattan, Ks   66506    785-532-0561
Kansas State University                          Department of Mathematics
->->->->->->->->->->->->->->->->->->---<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<




Re: [INTERFACES] ECPG & 'exec sql connect to' question

From
Michael Meskes
Date:
On Thu, Mar 04, 1999 at 05:01:05PM +0100, Pavel PaJaSoft Janousek wrote:
> 'exec sql connect to unix:postgresql://localhost:5432/mm; '
>
>     I thought, that this example is for ECPG, but....

It is. However, the same dbname syntax applies to all programs using libpq.

> All described this I make by psql monitor... I create database 'test'. Database 'test' is created by unix user 'paja'
('paja'is postgres user too). I create new postgres user 'petr' with password 'petr'. As 'paja', I connect to database
'test'and create table 'pom' with some cols, then execute this: 'grant insert on pom to petr;'. All is OK, but now, I
wantconnect to database 'test' by user 'petr'... of course, I cant' use simple command: 
> 'exec sql connect 'test';', because I want have only rights as user 'petr' (only inserting rows).
>     My problem is, that I don't know, how I can connect to database (on
>     local system or remote...) with specifiing user and his password. I
>     know, that I can use libpq directly, but I want do this by ECPG, not
>     by using the PostgreSQL C library.

the syntax is 'exec sql <dbname> user <username> identified by <password>'

> PS: A wrote this question in november 1998, but I didn't got some answer.
> Of course, I specify my question rather diferent...

Seems I missed it. Sorry.

Michael

--
Michael Meskes                         | Go SF 49ers!
Th.-Heuss-Str. 61, D-41812 Erkelenz    | Go Rhein Fire!
Tel.: (+49) 2431/72651                 | Use Debian GNU/Linux!
Email: Michael.Meskes@gmx.net          | Use PostgreSQL!

Odd program behaviour

From
James Thompson
Date:
I'm desperate here.  I've got two days invested in this and have not made
any progress at all.  I'm hoping someone has run into the following and
might be able to give me a clue.

I have a a bunch of C functions that access the database via ecpg.
I have used SWIG to wrap these functions so that they may used in TCL/TK
I have the front end written in TCL/TK.

Everything works fine when saving data but pulling data from the database
results in a segfault when using the TCL/TK front end.  The same functions
work fine when called from a C test program.  In addition the
customer_create function listed in the backtrace is used alot in the front
end without problems.  It only seems to bomb when I'm actively fetching
data from the database.

A backtrace on the core file gives me this.

#0  0x40248090 in ?? () from /lib/libc.so.6
#1  0x40247fa5 in ?? () from /lib/libc.so.6
#2  0x804ec5a in obmalloc (size=84) at memory.c:28
#3  0x804c896 in customer_create () at customer.ec:73
#4  0x8049ce2 in packet_create () at packet.ec:78
#5  0x804c475 in packet_query (object=0x80ecb60) at packet.ec:795
#6  0x80512c2 in _wrap_packet_query (clientData=0x0, interp=0x8058d~b8,
objc=2, objv=0x8059a48) at obe_wrap.c:1818
#7  0x400ca467 in ?? () from /usr/local/lib/libtcl.so

Now I'm fairly new to gdb but this looks like it's dying inside the
obmalloc which is simply...

obmalloc(size_t size)
{
  void *tmp;

  assert(size!=0);

  tmp = malloc(size);
  while (tmp == NULL) {
    fprintf(stderr, "obmalloc: memory exhausted. 2 second delay before
retry");
    sleep(2);
    tmp = malloc(size);
  }
  return tmp;
}

I'm at the point that I can't think straight anymore.  Does anyone have a
suggestion on why the code would work correctly from C but not TCL?

Oh, and if you wan't more info on what I'm trying to do visit

www.math.ksu.edu/~jamest/obe

I'd be willing to move the code up now if anyone wants to look at it.
I've mangled it up enough this weekedn it's probably good for a laugh.

Thanks in advance for any ideas you can provide.

->->->->->->->->->->->->->->->->->->---<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<
James Thompson    138 Cardwell Hall  Manhattan, Ks   66506    785-532-0561
Kansas State University                          Department of Mathematics
->->->->->->->->->->->->->->->->->->---<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<-<







Re: [INTERFACES] Odd program behaviour

From
Tom Lane
Date:
James Thompson <jamest@math.ksu.edu> writes:
> I'm desperate here.  I've got two days invested in this and have not made
> any progress at all.  I'm hoping someone has run into the following and
> might be able to give me a clue.
> Everything works fine when saving data but pulling data from the database
> results in a segfault when using the TCL/TK front end.
> [ segfault is evidently inside malloc ]

A fairly reliable rule is that a segfault inside malloc means your
program has trodden on malloc's data structures that keep track of free
and allocated space in the malloc arena.  Usually this means you have
written off the end of a chunk of malloc'd space.  (Whether this is a
bug in the code that calculated the amount of space to ask for or a bug
in the code using the space is for you to say, of course.)

If you don't have any direct insights into the problem, I'd suggest
building your app with a debugging malloc package that will detect
the corruption as soon as possible.  One I've had good luck with
for apps that don't require huge amounts of space is Electric Fence.
(No URL at hand, but poke around the web and you should find it.)

            regards, tom lane