From Michael.Ansley@intec.co.za Mon Sep 13 13:03:02 1999 Received: from s-nath-exch1.nath-gpbry.co.za (mail.newaftech.com [209.212.104.161]) by hub.org (8.9.3/8.9.3) with ESMTP id JAA50900; Mon, 13 Sep 1999 09:03:02 -0400 (EDT) (envelope-from Michael.Ansley@intec.co.za) Received: by S-NATH-EXCH1 with Internet Mail Service (5.5.2448.0) id ; Mon, 13 Sep 1999 15:03:23 +0200 Message-ID: <1BF7C7482189D211B03F00805F8527F748C06B@S-NATH-EXCH2> From: "Ansley, Michael" To: "'Andreas Theofilu'" , Michael Meskes Cc: pgsql-interfaces@postgreSQL.org Subject: RE: [INTERFACES] ecpg and getting just assigned serial number Date: Mon, 13 Sep 1999 14:56:40 +0200 MIME-Version: 1.0 X-Mailer: Internet Mail Service (5.5.2448.0) Content-Type: text/plain; charset="iso-8859-1" It's would be pretty difficult to return the unique key, because that is defined by the user, and what if I had a table into which were placed values from two separate sequences (not great design, but possible), or a compound unique key, or multiple unique keys? The things that's necessary here is for the sqlca struct to contain the oid of the newly inserted row after the insertion. Of course, then there is a problem if more than one row is inserted. What does Informix do then? But why can't you just write a database function that performs the insert, and returns the value, and then call the function using embedded sql. The function can be written in plsql, and accept as parameters all the values that need to go into the new record. This is far better db design. The basic function looks like this (pseudocode): BEGIN get next sequence value into i; insert new record using parameters and i; if insert fails then return (0); else return (i); END This has the added benefit of being able to add further business rules into the function, which better encapsulates your process. Of course, this is design dependent, but it's a method I've used quite successfully in the past (although, admittedly, not in PG ;-) MikeA >> -----Original Message----- >> From: Andreas Theofilu [mailto:theofilu@eunet.at] >> Sent: Monday, September 13, 1999 2:11 PM >> To: Michael Meskes >> Cc: theofilu@eunet.at; pgsql-interfaces@postgreSQL.org >> Subject: Re: [INTERFACES] ecpg and getting just assigned >> serial number >> >> >> Am Mo, 13 Sep 1999 schrieben Sie: >> > On Sun, Sep 12, 1999 at 04:25:17PM +0200, Theofilu Andreas wrote: >> > > I'm using 'ecpg' to develop my application, because it's >> mostly compatible >> > > to Informix's 'esqlc'. However. I have a table with a >> field of type >> > > 'serial'. Now I've no problem to insert any sentences >> into this table, but >> > > immediately after inserting a new sentence I need the >> newly assigned number >> > > to the serial field in the table. How can I access this number? >> > > With Informix I got this number in a field of structure >> 'sqlca'. With >> > > PostgreSQL the same field in this structure exists but >> contains always 0, >> > > as documented. >> > >> > Where exactly does Informix return it? sqlca[?]. >> >> Informix returns this number in 'sqlca.sqlerrd[1]'. >> Currently this field >> is not used by ecpg. >> >> > Also does anyone know whether the backend returns that >> number somewhere? >> >> Now I know that the backend does not return this number >> anywhere. Instead >> you need two SQL commands to insert a sentence and get the >> number. You can >> do it with following commands: >> >> EXEC SQL nextval ('') into :variable; >> EXEC SQL insert into values (:variable, ...); >> >> Maybe ecpg can implement the first call behind the scenes >> and put the new >> serial number into 'sqlca.sqlerrd[1]'. >> In Informix I have to write: >> >> EXEC SQL insert into
values (0, ...); >> >> The '0' increments the serial counter, who is a simple int4 >> field in a >> system table, by one, inserts the sentence with new serial >> number into the >> table and returns the new number in 'sqlca.sqlerrd[1]'. >> It would be nice to have this feature exactly the same way >> with 'ecpg'. >> This would make it more compatible, at least against >> Informix. Don't know >> about Oracle. >> >> BTW: When I insert a sentence in Informix and the serial >> field is not 0, >> but contains a unique number and this number is higher than >> the current >> value of the serial counter, the serial counter is set to >> the manualy set >> number. This is important if one inserts sentences from a file. For >> example when I move data between two machines/databases. >> With PostgreSQL >> I've to set the counter into the sequence by hand, or it will not be >> incremented. >> -- >> Theofilu Andreas >> http://members.eunet.at/theofilu >> >> ------------------------------------------------- >> Enjoy the science of Linux! >> Genieße die Wissenschaft von Linux! >> ------------------------------------------------- >> >> ************ >> From theofilu@eunet.at Mon Sep 13 14:10:35 1999 Received: from pop-c.netway.at (pop-c.netway.at [195.96.0.132]) by hub.org (8.9.3/8.9.3) with ESMTP id KAA61066 for ; Mon, 13 Sep 1999 10:10:35 -0400 (EDT) (envelope-from theofilu@eunet.at) Received: from pop-d.netway.at (pop-d.netway.at [195.96.0.131]) by pop-c.netway.at (8.9.1/8.9.1) with ESMTP id QAA19642; Mon, 13 Sep 1999 16:10:27 +0200 Received: from theofilu ([195.96.30.177]) by pop-d.netway.at (8.9.1/8.9.1) with SMTP id QAA12454; Mon, 13 Sep 1999 16:10:27 +0200 From: Andreas Theofilu Organization: Raiffeisen Landesbank Steiermark To: pgsql-interfaces@postgreSQL.org Subject: RE: [INTERFACES] ecpg and getting just assigned serial number Date: Mon, 13 Sep 1999 15:46:51 +0200 X-Mailer: KMail [version 1.0.21] Content-Type: text/plain References: <1BF7C7482189D211B03F00805F8527F748C06B@S-NATH-EXCH2> Cc: theofilu@eunet.at MIME-Version: 1.0 Message-Id: <99091316123503.01664@theofilu> Content-Transfer-Encoding: 8bit Am Mo, 13 Sep 1999 schrieben Sie: > It's would be pretty difficult to return the unique key, because that is > defined by the user, and what if I had a table into which were placed values > from two separate sequences (not great design, but possible), or a compound > unique key, or multiple unique keys? The things that's necessary here is > for the sqlca struct to contain the oid of the newly inserted row after the > insertion. Of course, then there is a problem if more than one row is > inserted. What does Informix do then? Agree. It's difficult to do that with PostgreSQL. It works with Informix only because the type 'serial' is realy a seperate type and handled completely different. Beside this it's allowed to have only one field of type serial in a table. With SQL command 'insert' you can only insert one sentence at a time. To enter more than one sentence you must program a loop. So I can see no problem with the serial number. Beside this: The serial field of Informix is independant of any index. Therefore it doesn't matter how complicate an index may be. > But why can't you just write a database function that performs the insert, > and returns the value, and then call the function using embedded sql. The > function can be written in plsql, and accept as parameters all the values > that need to go into the new record. This is far better db design. I can write such a function, of course. But I will not break compatibility to Informix, because both databases, PostgreSQL _and_ Informix are supported in only a single source file. > The basic function looks like this (pseudocode): > > BEGIN > get next sequence value into i; > insert new record using parameters and i; > if insert fails then > return (0); > else > return (i); > END > > This has the added benefit of being able to add further business rules into > the function, which better encapsulates your process. Of course, this is > design dependent, but it's a method I've used quite successfully in the past > (although, admittedly, not in PG ;-) I've done exactly the same your example shows, but not in a seperate function. I simply added one SQL command to get the next number out of the sequence. This works well now and the extra line is ignored when compiling for Informix :-). -- Theofilu Andreas http://members.eunet.at/theofilu ------------------------------------------------- Enjoy the science of Linux! Genieße die Wissenschaft von Linux! ------------------------------------------------- From orsingerc@epg-gw1.lewis.army.mil Mon Sep 13 18:49:42 1999 Received: from epg-gw1.lewis.army.mil (epg-gw1.lewis.army.mil [150.192.56.10]) by hub.org (8.9.3/8.9.3) with SMTP id OAA44789 for ; Mon, 13 Sep 1999 14:49:42 -0400 (EDT) (envelope-from craig@epg-gw1.lewis.army.mil) Received: from delenn.eng by epg-gw1.lewis.army.mil with SMTP (1.37.109.4/15.6) id AA25099; Mon, 13 Sep 99 11:49:03 -0700 Received: by delenn.eng (8.7.6/16.2) id OAA07351; Mon, 13 Sep 1999 14:49:03 -0400 (EDT) Message-Id: X-Mailer: XFMail 1.3 [p0] on HP-UX (unknown) X-Priority: 3 (Normal) Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 8bit Mime-Version: 1.0 In-Reply-To: <99091316123503.01664@theofilu> Date: Mon, 13 Sep 1999 11:49:03 -0700 (PDT) Reply-To: Craig Orsinger Organization: EPG Ft. Lewis Field Office Sender: craig@epg-gw1.lewis.army.mil From: Craig Orsinger To: Andreas Theofilu Subject: RE: [INTERFACES] ecpg and getting just assigned serial number Cc: pgsql-interfaces@postgreSQL.org On 13-Sep-99 Andreas Theofilu wrote: > I can write such a function, of course. But I will not break compatibility > to Informix, because both databases, PostgreSQL _and_ Informix are > supported in only a single source file. I've tried to do this myself, and in the long run I think you'll be better off planning on having two different versions of embedded C, or at least, two different sets of functions that do certain operations. Informix ESQL/C and ecpg have significantly different syntax, especially regarding CURSORs and FETCHs. If you keep things simple, and have some way of converting database names between the two systems, you can undoubtedly do what you're trying to do. Anything else and it gets really difficult. While Informix supports EXEC SQL IFDEF, PostgreSQL does not appear to. An alternative might be to have a single source file that can be modified by a sed or awk script to conform to whatever embedded SQL you're using at the moment. I considered this approach, but didn't pursue it because I didn't have the time. We had a discussion here about this subject in June. The subject was "esql/c documentation". If you can't find it in the archive, I can e-mail some of the more interesting posts in that thread. ---------------------------------- Date: 13-Sep-99 Time: 10:55:47 Craig Orsinger (email: ) Logicon RDA Bldg. 8B28 "Just another megalomaniac with ideas above his 6th & F Streets station. The Universe is full of them." Ft. Lewis, WA 98433 - The Doctor ---------------------------------- From meskes@postgresql.org Tue Sep 14 18:51:00 1999 Received: from tanja.fam-meskes.de (root@p3E9D3DCD.dip0.t-ipconnect.de [62.157.61.205]) by hub.org (8.9.3/8.9.3) with ESMTP id OAA42611 for ; Tue, 14 Sep 1999 14:51:00 -0400 (EDT) (envelope-from michael@fam-meskes.de) Received: (from michael@localhost) by tanja.fam-meskes.de (8.9.3/8.9.0/Debian/GNU) id UAA05440; Tue, 14 Sep 1999 20:23:04 +0200 Date: Tue, 14 Sep 1999 20:23:04 +0200 From: Michael Meskes To: Craig Orsinger Cc: Andreas Theofilu , pgsql-interfaces@postgresql.org Subject: Re: [INTERFACES] ecpg and getting just assigned serial number Message-ID: <19990914202304.B5414@gmx.net> Mail-Followup-To: Craig Orsinger , Andreas Theofilu , pgsql-interfaces@postgresql.org References: <99091316123503.01664@theofilu> Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Mailer: Mutt 0.95.6i In-Reply-To: ; from Craig Orsinger on Mon, Sep 13, 1999 at 11:49:03AM -0700 On Mon, Sep 13, 1999 at 11:49:03AM -0700, Craig Orsinger wrote: > to do. Anything else and it gets really difficult. While Informix > supports EXEC SQL IFDEF, PostgreSQL does not appear to. Not yet, but it may make a nice addition. 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@Fam-Meskes.De | Use PostgreSQL!