calling currval() before nextval() patch adding currval_isset()

Started by John Hansenalmost 22 years ago7 messagespatches
Jump to latest
#1John Hansen
john@geeknet.com.au

Hi list,

attached, cvs context diff that adds currval_isset('sequence_name');

This avoids the warning messages you get when calling currval before
nextval in situations where the program flow does not allow you to
predetermine if the current session has called nexval yet.

I have for example, a general db_insert function, that needs to return
the currval value, or 0 if the table that was inserted on doesn't have a
sequence (sortof emulating mysql's last_insert_id function).

With this patch, I can now call currval_isset to determine if I need to
call currval. Previously, I just called currval, but with the result of
filling up the server log with warnings.

Kind Regards,

John Hansen

PS I picked OID 1295 for this function, as it's currently available
according to unused_oids.

Attachments:

currval_isset.difftext/x-patch; charset=ISO-8859-1; name=currval_isset.diffDownload+32-0
#2John Hansen
john@geeknet.com.au
In reply to: John Hansen (#1)
Re: calling currval() before nextval() patch adding currval_isset()

Hi list,

attached, cvs context diff that adds currval_isset('sequence_name');

Updated patch attached, didn't think to update the docs. Thanks oicu!

Kind Regards,

John Hansen

Attachments:

currval_isset.difftext/x-patch; charset=iso-8859-1; name=currval_isset.diffDownload+55-3
#3Rod Taylor
rbt@rbt.ca
In reply to: John Hansen (#1)
Re: calling currval() before nextval() patch adding

On Sun, 2004-11-07 at 17:21, John Hansen wrote:

Hi list,

attached, cvs context diff that adds currval_isset('sequence_name');

With this patch, I can now call currval_isset to determine if I need to
call currval. Previously, I just called currval, but with the result of
filling up the server log with warnings.

This might do what you're looking for on 8.0.

CREATE OR REPLACE FUNCTION currval_isset(text) RETURNS bigint AS '
DECLARE
var integer;
BEGIN
SELECT currval($1) INTO var;

RETURN var;

EXCEPTION
WHEN OBJECT_NOT_IN_PREREQUISITE_STATE THEN
RETURN 0;
END;
' LANGUAGE plpgsql;

SELECT isset('tst_seq');

#4Tom Lane
tgl@sss.pgh.pa.us
In reply to: John Hansen (#1)
Re: calling currval() before nextval() patch adding currval_isset()

John Hansen <john@geeknet.com.au> writes:

attached, cvs context diff that adds currval_isset('sequence_name');

This avoids the warning messages you get when calling currval before
nextval in situations where the program flow does not allow you to
predetermine if the current session has called nexval yet.

I would argue that a program written that way is broken by definition,
and we should not encourage programmers to write broken applications.

regards, tom lane

#5John Hansen
john@geeknet.com.au
In reply to: Rod Taylor (#3)
Re: calling currval() before nextval() patch adding

This might do what you're looking for on 8.0.

CREATE OR REPLACE FUNCTION currval_isset(text) RETURNS bigint AS '
DECLARE
var integer;
BEGIN
SELECT currval($1) INTO var;

RETURN var;

EXCEPTION
WHEN OBJECT_NOT_IN_PREREQUISITE_STATE THEN
RETURN 0;
END;
' LANGUAGE plpgsql;

SELECT isset('tst_seq');

k, i don't actually have an 8.0 installation to play with here,. but
won't this still produce a warning?

#6John Hansen
john@geeknet.com.au
In reply to: Tom Lane (#4)
Re: calling currval() before nextval() patch adding

I would argue that a program written that way is broken by definition,
and we should not encourage programmers to write broken applications.

Hmmm,.... is mysql's last_insert_id behaviour really that much to ask
for?

This is basically what I'm trying to emulate, to make porting mysql
applications easier. That is, to ADD postgresql support to applications,
that have backend specific code abstracted.... Many such applications
use this particular method of getting the id. the function doing the
insert only knows the tablename (by parsing the query string).

Agreed those applications should probably be completely rewritten, but
for many, including myself, that would be an effort that goes in to the
too hard basket, and thus will never see support for postgresql. Which
in my opinion is a shame.

Should there maybe instead be a .conf option that allows you to supress
the warning message given by a call to currval() before nextval() ?

currval_no_warning = true ?

... John

#7John Hansen
john@geeknet.com.au
In reply to: John Hansen (#6)
Re: calling currval() before nextval() patch adding

Hmmm,.... is mysql's last_insert_id behaviour really that much to ask
for?

Come to think of it,.... maybe the attached last_insert_id() patch is a
better option. Actually copying the behaviour of mysql's counterpart.

... John

Attachments:

last_insert_id.difftext/x-patch; charset=iso-8859-1; name=last_insert_id.diffDownload+35-6