Skip to content

Commit 6441028

Browse files
committed
Add trivial NULL statement to plpgsql, for Oracle compatibility.
1 parent bc91389 commit 6441028

File tree

2 files changed

+77
-18
lines changed

2 files changed

+77
-18
lines changed

doc/src/sgml/plpgsql.sgml

Lines changed: 54 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.45 2004/08/08 22:40:46 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.46 2004/08/16 17:52:06 tgl Exp $
33
-->
44

55
<chapter id="plpgsql">
@@ -1098,6 +1098,52 @@ PERFORM create_mv('cs_session_page_requests_mv', my_query);
10981098
</para>
10991099
</sect2>
11001100

1101+
<sect2 id="plpgsql-statements-null">
1102+
<title>Doing Nothing At All</title>
1103+
1104+
<para>
1105+
Sometimes a placeholder statement that does nothing is useful.
1106+
For example, it can indicate that one arm of an if/then/else
1107+
chain is deliberately empty. For this purpose, use the
1108+
<command>NULL</command> statement:
1109+
1110+
<synopsis>
1111+
NULL;
1112+
</synopsis>
1113+
</para>
1114+
1115+
<para>
1116+
For example, the following two fragments of code are equivalent:
1117+
<programlisting>
1118+
BEGIN
1119+
y := x / 0;
1120+
EXCEPTION
1121+
WHEN division_by_zero THEN
1122+
NULL; -- ignore the error
1123+
END;
1124+
</programlisting>
1125+
1126+
<programlisting>
1127+
BEGIN
1128+
y := x / 0;
1129+
EXCEPTION
1130+
WHEN division_by_zero THEN -- ignore the error
1131+
END;
1132+
</programlisting>
1133+
Which is preferable is a matter of taste.
1134+
</para>
1135+
1136+
<note>
1137+
<para>
1138+
In Oracle's PL/SQL, empty statement lists are not allowed, and so
1139+
<command>NULL</> statements are <emphasis>required</> for situations
1140+
such as this. <application>PL/pgSQL</application> allows you to
1141+
just write nothing, instead.
1142+
</para>
1143+
</note>
1144+
1145+
</sect2>
1146+
11011147
<sect2 id="plpgsql-statements-executing-dyn">
11021148
<title>Executing Dynamic Commands</title>
11031149

@@ -1129,7 +1175,7 @@ EXECUTE <replaceable class="command">command-string</replaceable>;
11291175
<para>
11301176
When working with dynamic commands you will often have to handle escaping
11311177
of single quotes. The recommended method for quoting fixed text in your
1132-
function body is dollar quoting. If you have legacy code which does
1178+
function body is dollar quoting. If you have legacy code that does
11331179
not use dollar quoting, please refer to the
11341180
overview in <xref linkend="plpgsql-quote-tips">, which can save you
11351181
some effort when translating said code to a more reasonable scheme.
@@ -1158,14 +1204,15 @@ EXECUTE <replaceable class="command">command-string</replaceable>;
11581204
</para>
11591205

11601206
<para>
1161-
An example (this assumes that you are using dollar quoting, so the
1162-
quote marks need not be doubled):
1207+
An example (this assumes that you are using dollar quoting for the
1208+
function as a whole, so the quote marks need not be doubled):
11631209
<programlisting>
11641210
EXECUTE 'UPDATE tbl SET '
11651211
|| quote_ident(colname)
11661212
|| ' = '
11671213
|| quote_literal(newvalue)
1168-
|| ' WHERE ...';
1214+
|| ' WHERE key = '
1215+
|| quote_literal(keyvalue);
11691216
</programlisting>
11701217
</para>
11711218

@@ -1193,7 +1240,8 @@ EXECUTE 'UPDATE tbl SET '
11931240
|| quote_ident(colname)
11941241
|| ' = $$'
11951242
|| newvalue
1196-
|| '$$ WHERE ...';
1243+
|| '$$ WHERE key = '
1244+
|| quote_literal(keyvalue);
11971245
</programlisting>
11981246
because it would break if the contents of <literal>newvalue</>
11991247
happened to contain <literal>$$</>. The same objection would

src/pl/plpgsql/src/gram.y

Lines changed: 23 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
* procedural language
55
*
66
* IDENTIFICATION
7-
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.59 2004/07/31 23:04:56 tgl Exp $
7+
* $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.60 2004/08/16 17:52:06 tgl Exp $
88
*
99
* This software is copyrighted by Jan Wieck - Hamburg.
1010
*
@@ -132,7 +132,7 @@ static void check_assignable(PLpgSQL_datum *datum);
132132
%type <stmt> stmt_return stmt_return_next stmt_raise stmt_execsql
133133
%type <stmt> stmt_for stmt_select stmt_perform
134134
%type <stmt> stmt_dynexecute stmt_getdiag
135-
%type <stmt> stmt_open stmt_fetch stmt_close
135+
%type <stmt> stmt_open stmt_fetch stmt_close stmt_null
136136

137137
%type <exceptions> exception_sect proc_exceptions
138138
%type <exception> proc_exception
@@ -592,29 +592,31 @@ proc_sect :
592592

593593
proc_stmts : proc_stmts proc_stmt
594594
{
595+
if ($2 != NULL)
596+
{
595597
if ($1->stmts_used == $1->stmts_alloc)
596598
{
597599
$1->stmts_alloc *= 2;
598600
$1->stmts = realloc($1->stmts, sizeof(PLpgSQL_stmt *) * $1->stmts_alloc);
599601
}
600602
$1->stmts[$1->stmts_used++] = $2;
601-
602-
$$ = $1;
603+
}
604+
$$ = $1;
603605
}
604606
| proc_stmt
605607
{
606-
PLpgSQL_stmts *new;
608+
PLpgSQL_stmts *new;
607609

608-
new = malloc(sizeof(PLpgSQL_stmts));
609-
memset(new, 0, sizeof(PLpgSQL_stmts));
610+
new = malloc(sizeof(PLpgSQL_stmts));
611+
memset(new, 0, sizeof(PLpgSQL_stmts));
610612

611-
new->stmts_alloc = 64;
612-
new->stmts_used = 1;
613-
new->stmts = malloc(sizeof(PLpgSQL_stmt *) * new->stmts_alloc);
614-
new->stmts[0] = $1;
613+
new->stmts_alloc = 32;
614+
new->stmts = malloc(sizeof(PLpgSQL_stmt *) * new->stmts_alloc);
615615

616-
$$ = new;
616+
if ($1 != NULL)
617+
new->stmts[new->stmts_used++] = $1;
617618

619+
$$ = new;
618620
}
619621
;
620622

@@ -654,6 +656,8 @@ proc_stmt : pl_block ';'
654656
{ $$ = $1; }
655657
| stmt_close
656658
{ $$ = $1; }
659+
| stmt_null
660+
{ $$ = $1; }
657661
;
658662

659663
stmt_perform : K_PERFORM lno expr_until_semi
@@ -1493,6 +1497,13 @@ stmt_close : K_CLOSE lno cursor_variable ';'
14931497
}
14941498
;
14951499

1500+
stmt_null : K_NULL ';'
1501+
{
1502+
/* We do not bother building a node for NULL */
1503+
$$ = NULL;
1504+
}
1505+
;
1506+
14961507
cursor_varptr : T_SCALAR
14971508
{
14981509
if (yylval.scalar->dtype != PLPGSQL_DTYPE_VAR)

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy