mirror of
https://github.com/postgres/postgres.git
synced 2025-07-28 23:42:10 +03:00
This patch improves the behavior of FOUND in PL/PgSQL. In Oracle,
FOUND is set whenever a SELECT INTO returns > 0 rows, *or* when an INSERT, UPDATE, or DELETE affects > 0 rows. We implemented the first part of this behavior, but not the second. I also improved the documentation on the various situations in which FOUND can be set (excluding inside FOR loops, which I still need to think about), and added some regression tests for this behavior. Neil Conway
This commit is contained in:
@ -1534,3 +1534,59 @@ SELECT recursion_test(4,3);
|
||||
4,3,2,1,3
|
||||
(1 row)
|
||||
|
||||
--
|
||||
-- Test the FOUND magic variable
|
||||
--
|
||||
CREATE TABLE found_test_tbl (a int);
|
||||
create function test_found ()
|
||||
returns boolean as '
|
||||
declare
|
||||
begin
|
||||
insert into found_test_tbl values (1);
|
||||
if FOUND then
|
||||
insert into found_test_tbl values (2);
|
||||
end if;
|
||||
|
||||
update found_test_tbl set a = 100 where a = 1;
|
||||
if FOUND then
|
||||
insert into found_test_tbl values (3);
|
||||
end if;
|
||||
|
||||
delete from found_test_tbl where a = 9999; -- matches no rows
|
||||
if not FOUND then
|
||||
insert into found_test_tbl values (4);
|
||||
end if;
|
||||
|
||||
for i in 1 .. 10 loop
|
||||
-- no need to do anything
|
||||
end loop;
|
||||
if FOUND then
|
||||
insert into found_test_tbl values (5);
|
||||
end if;
|
||||
|
||||
-- never executes the loop
|
||||
for i in 2 .. 1 loop
|
||||
-- no need to do anything
|
||||
end loop;
|
||||
if not FOUND then
|
||||
insert into found_test_tbl values (6);
|
||||
end if;
|
||||
return true;
|
||||
end;' language 'plpgsql';
|
||||
select test_found();
|
||||
test_found
|
||||
------------
|
||||
t
|
||||
(1 row)
|
||||
|
||||
select * from found_test_tbl;
|
||||
a
|
||||
-----
|
||||
2
|
||||
100
|
||||
3
|
||||
4
|
||||
5
|
||||
6
|
||||
(6 rows)
|
||||
|
||||
|
@ -1414,3 +1414,47 @@ BEGIN
|
||||
END;' LANGUAGE 'plpgsql';
|
||||
|
||||
SELECT recursion_test(4,3);
|
||||
|
||||
--
|
||||
-- Test the FOUND magic variable
|
||||
--
|
||||
CREATE TABLE found_test_tbl (a int);
|
||||
|
||||
create function test_found ()
|
||||
returns boolean as '
|
||||
declare
|
||||
begin
|
||||
insert into found_test_tbl values (1);
|
||||
if FOUND then
|
||||
insert into found_test_tbl values (2);
|
||||
end if;
|
||||
|
||||
update found_test_tbl set a = 100 where a = 1;
|
||||
if FOUND then
|
||||
insert into found_test_tbl values (3);
|
||||
end if;
|
||||
|
||||
delete from found_test_tbl where a = 9999; -- matches no rows
|
||||
if not FOUND then
|
||||
insert into found_test_tbl values (4);
|
||||
end if;
|
||||
|
||||
for i in 1 .. 10 loop
|
||||
-- no need to do anything
|
||||
end loop;
|
||||
if FOUND then
|
||||
insert into found_test_tbl values (5);
|
||||
end if;
|
||||
|
||||
-- never executes the loop
|
||||
for i in 2 .. 1 loop
|
||||
-- no need to do anything
|
||||
end loop;
|
||||
if not FOUND then
|
||||
insert into found_test_tbl values (6);
|
||||
end if;
|
||||
return true;
|
||||
end;' language 'plpgsql';
|
||||
|
||||
select test_found();
|
||||
select * from found_test_tbl;
|
||||
|
Reference in New Issue
Block a user