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.

===================================================================
RCS file: /projects/cvsroot/pgsql/src/backend/commands/sequence.c,v
retrieving revision 1.117
diff -c -r1.117 sequence.c
*** src/backend/commands/sequence.c     16 Sep 2004 16:58:28 -0000      1.117
--- src/backend/commands/sequence.c     7 Nov 2004 22:05:01 -0000
***************
*** 608,613 ****
--- 608,642 ----
        PG_RETURN_INT64(result);
  }
  
+ Datum
+ currval_isset(PG_FUNCTION_ARGS)
+ {
+       text       *seqin = PG_GETARG_TEXT_P(0);
+       RangeVar   *sequence;
+       SeqTable        elm;
+       Relation        seqrel;
+ 
+       sequence = makeRangeVarFromNameList(textToQualifiedNameList(seqin,
+                                                                                                                        "currval"));
+ 
+       /* open and AccessShareLock sequence */
+       init_sequence(sequence, &elm, &seqrel);
+ 
+       if (pg_class_aclcheck(elm->relid, GetUserId(), ACL_SELECT) != ACLCHECK_OK)
+               ereport(ERROR,
+                               (errcode(ERRCODE_INSUFFICIENT_PRIVILEGE),
+                                errmsg("permission denied for sequence %s",
+                                               sequence->relname)));
+ 
+       if (elm->increment == 0) {      /* nextval/read_info were not called */
+           relation_close(seqrel, NoLock);
+           PG_RETURN_BOOL(false);
+       }
+ 
+       relation_close(seqrel, NoLock);
+       PG_RETURN_BOOL(true);
+ }
+ 
  /*
   * Main internal procedure that handles 2 & 3 arg forms of SETVAL.
   *
Index: src/include/catalog/pg_proc.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/catalog/pg_proc.h,v
retrieving revision 1.348
diff -c -r1.348 pg_proc.h
*** src/include/catalog/pg_proc.h       7 Oct 2004 18:38:50 -0000       1.348
--- src/include/catalog/pg_proc.h       7 Nov 2004 22:05:02 -0000
***************
*** 2034,2039 ****
--- 2034,2041 ----
  DESCR("sequence next value");
  DATA(insert OID = 1575 (  currval                     PGNSP PGUID 12 f f t f v 1 20 "25" _null_       currval - _null_ ));
  DESCR("sequence current value");
+ DATA(insert OID = 1295 (  currval_isset                       PGNSP PGUID 12 f f t f v 1 16 "25" _null_       currval_isset - _null_ ));
+ DESCR("sequence has current value");
  DATA(insert OID = 1576 (  setval                      PGNSP PGUID 12 f f t f v 2 20 "25 20" _null_  setval - _null_ ));
  DESCR("set sequence value");
  DATA(insert OID = 1765 (  setval                      PGNSP PGUID 12 f f t f v 3 20 "25 20 16" _null_ setval_and_iscalled - _null_ ));
Index: src/include/commands/sequence.h
===================================================================
RCS file: /projects/cvsroot/pgsql/src/include/commands/sequence.h,v
retrieving revision 1.29
diff -c -r1.29 sequence.h
*** src/include/commands/sequence.h     29 Aug 2004 04:13:05 -0000      1.29
--- src/include/commands/sequence.h     7 Nov 2004 22:05:02 -0000
***************
*** 82,87 ****
--- 82,88 ----
  
  extern Datum nextval(PG_FUNCTION_ARGS);
  extern Datum currval(PG_FUNCTION_ARGS);
+ extern Datum currval_isset(PG_FUNCTION_ARGS);
  extern Datum setval(PG_FUNCTION_ARGS);
  extern Datum setval_and_iscalled(PG_FUNCTION_ARGS);
  
---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
      subscribe-nomail command to [EMAIL PROTECTED] so that your
      message can get through to the mailing list cleanly

Reply via email to