Re: type SERIAL in C host-struct - Mailing list pgsql-general

From Matthias Apitz
Subject Re: type SERIAL in C host-struct
Date
Msg-id 20191107191447.GA2407@c720-r342378
Whole thread Raw
In response to type SERIAL in C host-struct  (Matthias Apitz <guru@unixarea.de>)
Responses Re: type SERIAL in C host-struct  ("Peter J. Holzer" <hjp-pgsql@hjp.at>)
List pgsql-general
El día jueves, noviembre 07, 2019 a las 12:39:39p. m. +0100, Matthias Apitz escribió:

>
> Hello,
>
> We're struggling with the following problem (here show in a simplified
> case).
>
> We have in a PG 11.4 database a table with two columns: SERIAL, VARCHAR(11).
>
> In the ESQL/C pgm the code is:
>
>         EXEC SQL BEGIN DECLARE SECTION;
>         ...
>         struct {
>                 int ser;
>                 char name [11];
>         } host_struct;
>         EXEC SQL END DECLARE SECTION;
>
> an INSERT with
>
>         strcpy(host_struct.name, "Sigrid");
>         host_struct.ser = 0;
>
>         EXEC SQL INSERT INTO lina VALUES (:host_struct);
>
> works but, sets the SERIAL column to 0;
>
> an INSERT with
>
>         EXEC SQL INSERT INTO lina VALUES (DEFAULT, :host_struct.name);
>
> works correctly and increments the SERIAL on every INSERT:

At the end of the day we came up with the following solution:

        strcpy(host_struct.name, "Sigrid");
        EXEC SQL select nextval('lina_lid_seq') into :host_struct.ser;

        EXEC SQL INSERT INTO lina VALUES ( :host_struct );

which seems to work fine. Any comments about side effects?
The layout of the table 'lina' is ( serial lid, varchar name )

Thanks,

    matthias
--
Matthias Apitz, ✉ guru@unixarea.de, http://www.unixarea.de/ +49-176-38902045
Public GnuPG key: http://www.unixarea.de/key.pub

"Glaube wenig, hinterfrage alles, denke selbst: Wie man Manipulationen durchschaut"
"Believe little, scrutinise all, think by your own: How see through manipulations"
ISBN-10: 386489218X

Attachment

pgsql-general by date:

Previous
From: "Brad Nicholson"
Date:
Subject: Re: AW: AW: AW: broken backup trail in case of quickly patroni switchbackand forth
Next
From: Bruce Momjian
Date:
Subject: Re: logging proxy