pltcl - lastoid

Started by Nonameover 24 years ago6 messages
#1Noname
bob@redivi.com

I noticed that pltcl didn't have any way to get to SPI_lastoid like plpgsql does.. I started using pltcl a lot because I like to decide when and how my queries get planned.. so I put one together really quick

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

these changes are for src/pl/tcl/pltcl.c

/* start C code */

/* forward declaration */
static int pltcl_SPI_lastoid(ClientData cdata, Tcl_Interp *interp,
int argc, char *argv[]);

/* this needs to go in in pltcl_init_interp with the rest of 'em */
Tcl_CreateCommand(interp, "spi_lastoid",
pltcl_SPI_lastoid, NULL, NULL);

/**********************************************************************
* 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,"%d",SPI_lastoid);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return TCL_OK;
}

/* end C code */

-bob

#2Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Noname (#1)
1 attachment(s)
Re: pltcl - lastoid

Attached is the patch you suggested, with a documentation addition. Is
this correct?

I noticed that pltcl didn't have any way to get to SPI_lastoid like plpgsql does.. I started using pltcl a lot because I like to decide when and how my queries get planned.. so I put one together really quick

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

these changes are for src/pl/tcl/pltcl.c

/* start C code */

/* forward declaration */
static int pltcl_SPI_lastoid(ClientData cdata, Tcl_Interp *interp,
int argc, char *argv[]);

/* this needs to go in in pltcl_init_interp with the rest of 'em */
Tcl_CreateCommand(interp, "spi_lastoid",
pltcl_SPI_lastoid, NULL, NULL);

/**********************************************************************
* 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,"%d",SPI_lastoid);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return TCL_OK;
}

/* end C code */

-bob

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Attachments:

/bjm/difftext/plainDownload
Index: doc/src/sgml/pltcl.sgml
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v
retrieving revision 2.11
diff -c -r2.11 pltcl.sgml
*** doc/src/sgml/pltcl.sgml	2001/06/09 02:19:07	2.11
--- doc/src/sgml/pltcl.sgml	2001/08/01 19:32:04
***************
*** 395,400 ****
--- 395,412 ----
       </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>
         <para>
Index: src/pl/tcl/pltcl.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/pl/tcl/pltcl.c,v
retrieving revision 1.37
diff -c -r1.37 pltcl.c
*** src/pl/tcl/pltcl.c	2001/06/09 02:19:07	1.37
--- src/pl/tcl/pltcl.c	2001/08/01 19:32:09
***************
*** 144,149 ****
--- 144,151 ----
  					   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,257 ****
  					  pltcl_SPI_prepare, NULL, NULL);
  	Tcl_CreateCommand(interp, "spi_execp",
  					  pltcl_SPI_execp, NULL, NULL);
! 
  #ifdef ENABLE_PLTCL_UNKNOWN
  	/************************************************************
  	 * Try to load the unknown procedure from pltcl_modules
--- 253,261 ----
  					  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
***************
*** 2270,2275 ****
--- 2274,2294 ----
  	 ************************************************************/
  	memcpy(&Warn_restart, &save_restart, sizeof(Warn_restart));
  	sprintf(buf, "%d", ntuples);
+ 	Tcl_SetResult(interp, buf, TCL_VOLATILE);
+ 	return TCL_OK;
+ }
+ 
+ 
+ /**********************************************************************
+  * 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,"%d",SPI_lastoid);
  	Tcl_SetResult(interp, buf, TCL_VOLATILE);
  	return TCL_OK;
  }
#3Tom Lane
tgl@sss.pgh.pa.us
In reply to: Bruce Momjian (#2)
Re: pltcl - lastoid

Kindly format OIDs with %u not %d ... otherwise it looks reasonable...

regards, tom lane

#4Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Tom Lane (#3)
Re: pltcl - lastoid

Kindly format OIDs with %u not %d ... otherwise it looks reasonable...

Change made.

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026
#5Hiroshi Inoue
Inoue@tpf.co.jp
In reply to: Noname (#1)
Re: pltcl - lastoid

bob@redivi.com wrote:

I noticed that pltcl didn't have any way to get to SPI_lastoid like plpgsql does.. I started using pltcl a lot because I like to decide when and how my queries get planned.. so I put one together really quick

Please note that OIDs may be optional in 7.2 though
it doesn't seem a problem for you.

regards,
Hiroshi Inoue

#6Bruce Momjian
pgman@candle.pha.pa.us
In reply to: Noname (#1)
1 attachment(s)
Re: [HACKERS] pltcl - lastoid

Patch applied. Thanks.

I noticed that pltcl didn't have any way to get to SPI_lastoid like plpgsql does.. I started using pltcl a lot because I like to decide when and how my queries get planned.. so I put one together really quick

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

these changes are for src/pl/tcl/pltcl.c

/* start C code */

/* forward declaration */
static int pltcl_SPI_lastoid(ClientData cdata, Tcl_Interp *interp,
int argc, char *argv[]);

/* this needs to go in in pltcl_init_interp with the rest of 'em */
Tcl_CreateCommand(interp, "spi_lastoid",
pltcl_SPI_lastoid, NULL, NULL);

/**********************************************************************
* 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,"%d",SPI_lastoid);
Tcl_SetResult(interp, buf, TCL_VOLATILE);
return TCL_OK;
}

/* end C code */

-bob

---------------------------(end of broadcast)---------------------------
TIP 1: subscribe and unsubscribe commands go to majordomo@postgresql.org

-- 
  Bruce Momjian                        |  http://candle.pha.pa.us
  pgman@candle.pha.pa.us               |  (610) 853-3000
  +  If your life is a hard drive,     |  830 Blythe Avenue
  +  Christ can be your backup.        |  Drexel Hill, Pennsylvania 19026

Attachments:

/pgpatches/pltcltext/plainDownload
Index: doc/src/sgml/pltcl.sgml
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/doc/src/sgml/pltcl.sgml,v
retrieving revision 2.11
diff -c -r2.11 pltcl.sgml
*** doc/src/sgml/pltcl.sgml	2001/06/09 02:19:07	2.11
--- doc/src/sgml/pltcl.sgml	2001/08/01 19:32:04
***************
*** 395,400 ****
--- 395,412 ----
       </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>
         <para>
Index: src/pl/tcl/pltcl.c
===================================================================
RCS file: /home/projects/pgsql/cvsroot/pgsql/src/pl/tcl/pltcl.c,v
retrieving revision 1.37
diff -c -r1.37 pltcl.c
*** src/pl/tcl/pltcl.c	2001/06/09 02:19:07	1.37
--- src/pl/tcl/pltcl.c	2001/08/01 19:32:09
***************
*** 144,149 ****
--- 144,151 ----
  					   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,257 ****
  					  pltcl_SPI_prepare, NULL, NULL);
  	Tcl_CreateCommand(interp, "spi_execp",
  					  pltcl_SPI_execp, NULL, NULL);
! 
  #ifdef ENABLE_PLTCL_UNKNOWN
  	/************************************************************
  	 * Try to load the unknown procedure from pltcl_modules
--- 253,261 ----
  					  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
***************
*** 2270,2275 ****
--- 2274,2294 ----
  	 ************************************************************/
  	memcpy(&Warn_restart, &save_restart, sizeof(Warn_restart));
  	sprintf(buf, "%d", ntuples);
+ 	Tcl_SetResult(interp, buf, TCL_VOLATILE);
+ 	return TCL_OK;
+ }
+ 
+ 
+ /**********************************************************************
+  * 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;
  }