From 99fd5cbd410c29381d2ffdfd98705c68fe99fbe3 Mon Sep 17 00:00:00 2001 From: Tom Lane Date: Thu, 29 Aug 2002 04:12:03 +0000 Subject: [PATCH] FOUND patch was a bit over-enthusiastic: SQL commands that are not INSERT, UPDATE, or DELETE shouldn't change FOUND. IMHO anyway. Also, try to make documentation a little clearer. --- doc/src/sgml/plpgsql.sgml | 110 ++++++++++++++++++++++--------------------- src/pl/plpgsql/src/pl_exec.c | 16 ++----- 2 files changed, 62 insertions(+), 64 deletions(-) diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 51b85b9d85..acdd8a4f4d 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -1,5 +1,5 @@ @@ -852,58 +852,10 @@ SELECT INTO target expressions - There is a special variable named FOUND of - type boolean. The initial value of - FOUND is false; it is set to true when one of - the following events occurs: - - - - A SELECT INTO statement is executed, and it returns one or - more rows. - - - - - A UPDATE, INSERT, or DELETE statement is executed, and it - affects one or more rows. - - - - - A PERFORM statement is executed, and it discards one or more - rows. - - - - - A FETCH statement is executed, and it returns an additional - row. - - - - - A FOR statement is executed, and it iterates one or more - times. This applies to all three variants of the FOR statement - (integer FOR loops, record-set FOR loops, and dynamic - record-set FOR loops). FOUND is only set - when the FOR loop exits: inside the execution of the loop, - FOUND is not modified, although it may be - set by the execution of other statements. - - - - If none of these events occur, FOUND is set to - false. FOUND is a local variable; any changes - to it effect only the current PL/pgSQL - function. - - - - You can use FOUND immediately after a SELECT - INTO statement to determine whether the assignment was successful - (that is, at least one row was was returned by the SELECT - statement). For example: + You can use FOUND immediately after a SELECT + INTO statement to determine whether the assignment was successful + (that is, at least one row was was returned by the SELECT + statement). For example: SELECT INTO myrec * FROM EMP WHERE empname = myname; @@ -1116,6 +1068,58 @@ GET DIAGNOSTICS variable = item + + + There is a special variable named FOUND of + type boolean. FOUND starts out + false within each PL/pgSQL function. + It is set by each of the following types of statements: + + + + A SELECT INTO statement sets FOUND + true if it returns a row, false if no row is returned. + + + + + A PERFORM statement sets FOUND + true if it produces (discards) a row, false if no row is + produced. + + + + + UPDATE, INSERT, and DELETE statements set + FOUND true if at least one row is + affected, false if no row is affected. + + + + + A FETCH statement sets FOUND + true if it returns a row, false if no row is returned. + + + + + A FOR statement sets FOUND + true if it iterates one or more times, else false. + This applies to all three variants of the FOR statement + (integer FOR loops, record-set FOR loops, and dynamic + record-set FOR loops). FOUND is only set + when the FOR loop exits: inside the execution of the loop, + FOUND is not modified by the FOR statement, + although it may be changed by the execution of other + statements within the loop body. + + + + FOUND is a local variable; any changes + to it affect only the current PL/pgSQL + function. + + diff --git a/src/pl/plpgsql/src/pl_exec.c b/src/pl/plpgsql/src/pl_exec.c index c6522c35b9..363a04839a 100644 --- a/src/pl/plpgsql/src/pl_exec.c +++ b/src/pl/plpgsql/src/pl_exec.c @@ -3,7 +3,7 @@ * procedural language * * IDENTIFICATION - * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.58 2002/08/24 15:00:47 tgl Exp $ + * $Header: /cvsroot/pgsql/src/pl/plpgsql/src/pl_exec.c,v 1.59 2002/08/29 04:12:03 tgl Exp $ * * This software is copyrighted by Jan Wieck - Hamburg. * @@ -1845,11 +1845,6 @@ exec_stmt_execsql(PLpgSQL_execstate * estate, bool isnull; /* - * Set magic FOUND variable to false - */ - exec_set_found(estate, false); - - /* * On the first call for this expression generate the plan */ if (expr->plan == NULL) @@ -1927,16 +1922,15 @@ exec_stmt_execsql(PLpgSQL_execstate * estate, case SPI_OK_SELINTO: break; + case SPI_OK_INSERT: + case SPI_OK_DELETE: + case SPI_OK_UPDATE: /* * If the INSERT, DELETE, or UPDATE query affected at least * one tuple, set the magic 'FOUND' variable to true. This * conforms with the behavior of PL/SQL. */ - case SPI_OK_INSERT: - case SPI_OK_DELETE: - case SPI_OK_UPDATE: - if (SPI_processed > 0) - exec_set_found(estate, true); + exec_set_found(estate, (SPI_processed != 0)); break; case SPI_OK_SELECT: -- 2.11.0