]> granicus.if.org Git - postgresql/commitdiff
I noticed that pltcl didn't have any way to get to SPI_lastoid like plpgsql does...
authorBruce Momjian <bruce@momjian.us>
Thu, 2 Aug 2001 15:45:55 +0000 (15:45 +0000)
committerBruce Momjian <bruce@momjian.us>
Thu, 2 Aug 2001 15:45:55 +0000 (15:45 +0000)
Sorry I don't have the original around to make a quick diff, but its a very small change... I think this should be in the next release, there's no reason not to have it.

its a function with no expected arguments, so you can use it like:
spi_exec "INSERT INTO mytable(columns...) VALUES(values..)"
set oid [spi_lastoid]
spi_exec "SELECT mytable_id from mytable WHERE oid=$oid"

It just didn't make sense for me to use plpgsql and pltcl, or just screw
them both and use SPI from C.

bob@redivi.com

doc/src/sgml/pltcl.sgml
src/pl/tcl/pltcl.c

index 0683739f1a403c547f882a904859f98d473d5c9b..756984676078c95e0c565265b02d3f61f354310d 100644 (file)
@@ -1,5 +1,5 @@
 <!--
-$Header: /cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v 2.11 2001/06/09 02:19:07 tgl Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v 2.12 2001/08/02 15:45:55 momjian Exp $
 -->
 
  <chapter id="pltcl">
@@ -394,6 +394,18 @@ CREATE TRIGGER trig_mytab_modcount BEFORE INSERT OR UPDATE ON mytab
       </listitem>
      </varlistentry>
 
+     <varlistentry>
+      <indexterm>
+       <primary>spi_lastoid</primary>
+      </indexterm>
+      <term>spi_lastoid</term>
+      <listitem>
+       <para>
+       Returns the OID of the last query if it was an INSERT.
+       </para>
+      </listitem>
+     </varlistentry>
+
      <varlistentry>
       <term>spi_exec ?-count <replaceable>n</replaceable>? ?-array <replaceable>name</replaceable>? <replaceable>query</replaceable> ?<replaceable>loop-body</replaceable>?</term>
       <listitem>
index 0f847a61bfed7dacf59122d3789ed433ee6148c4..3991d9dc1cf62f4213ad1f44e68ab0abb67fcfb4 100644 (file)
@@ -31,7 +31,7 @@
  *       ENHANCEMENTS, OR MODIFICATIONS.
  *
  * IDENTIFICATION
- *       $Header: /cvsroot/pgsql/src/pl/tcl/pltcl.c,v 1.37 2001/06/09 02:19:07 tgl Exp $
+ *       $Header: /cvsroot/pgsql/src/pl/tcl/pltcl.c,v 1.38 2001/08/02 15:45:55 momjian Exp $
  *
  **********************************************************************/
 
@@ -144,6 +144,8 @@ static void pltcl_set_tuple_values(Tcl_Interp *interp, char *arrayname,
                                           int tupno, HeapTuple tuple, TupleDesc tupdesc);
 static void pltcl_build_tuple_argument(HeapTuple tuple, TupleDesc tupdesc,
                                                   Tcl_DString *retval);
+static int pltcl_SPI_lastoid(ClientData cdata, Tcl_Interp *interp,
+                               int argc, char *argv[]);
 
 /*
  * This routine is a crock, and so is everyplace that calls it.  The problem
@@ -251,7 +253,9 @@ pltcl_init_interp(Tcl_Interp *interp)
                                          pltcl_SPI_prepare, NULL, NULL);
        Tcl_CreateCommand(interp, "spi_execp",
                                          pltcl_SPI_execp, NULL, NULL);
-
+       Tcl_CreateCommand(interp, "spi_lastoid",
+                                         pltcl_SPI_lastoid, NULL, NULL);
+                                         
 #ifdef ENABLE_PLTCL_UNKNOWN
        /************************************************************
         * Try to load the unknown procedure from pltcl_modules
@@ -2275,6 +2279,21 @@ pltcl_SPI_execp(ClientData cdata, Tcl_Interp *interp,
 }
 
 
+/**********************************************************************
+ * pltcl_SPI_lastoid()  - return the last oid. To
+ *        be used after insert queries
+ **********************************************************************/
+static int
+pltcl_SPI_lastoid(ClientData cdata, Tcl_Interp *interp,
+                                 int argc, char *argv[])
+{
+       char buf[64];
+       sprintf(buf,"%u",SPI_lastoid);
+       Tcl_SetResult(interp, buf, TCL_VOLATILE);
+       return TCL_OK;
+}
+
+
 /**********************************************************************
  * pltcl_set_tuple_values() - Set variables for all attributes
  *                               of a given tuple