Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit bc0afb7

Browse files
committed
EXECUTE documentation, from "Robert B. Easter" <reaster@comptechnews.com>.
I threw in spell check run over the whole file.
1 parent 3942ee3 commit bc0afb7

File tree

1 file changed

+80
-36
lines changed

1 file changed

+80
-36
lines changed

doc/src/sgml/plsql.sgml

Lines changed: 80 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.11 2000/12/22 18:57:50 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.12 2001/01/06 12:26:08 petere Exp $
33
-->
44

55
<chapter id="plsql">
@@ -55,8 +55,8 @@ $Header: /cvsroot/pgsql/doc/src/sgml/Attic/plsql.sgml,v 2.11 2000/12/22 18:57:50
5555
</para>
5656
<para>
5757
The PL/pgSQL call handler parses the functions source text and
58-
produces an internal binary instruction tree on the first time, the
59-
function is called by a backend. The produced bytecode is identified
58+
produces an internal binary instruction tree on the first time the
59+
function is called. The produced bytecode is identified
6060
in the call handler by the object ID of the function. This ensures,
6161
that changing a function by a DROP/CREATE sequence will take effect
6262
without establishing a new database connection.
@@ -109,8 +109,8 @@ END;
109109
</para>
110110

111111
<para>
112-
There can be any number of subblocks in the statement section
113-
of a block. Subblocks can be used to hide variables from outside a
112+
There can be any number of sub-blocks in the statement section
113+
of a block. Sub-blocks can be used to hide variables from outside a
114114
block of statements. The variables
115115
declared in the declarations section preceding a block are
116116
initialized to their default values every time the block is entered,
@@ -132,7 +132,7 @@ END;
132132
<para>
133133
There are two types of comments in PL/pgSQL. A double dash '--'
134134
starts a comment that extends to the end of the line. A '/*'
135-
starts a block comment that extends to the next occurence of '*/'.
135+
starts a block comment that extends to the next occurrence of '*/'.
136136
Block comments cannot be nested, but double dash comments can be
137137
enclosed into a block comment and a double dash can hide
138138
the block comment delimiters '/*' and '*/'.
@@ -145,8 +145,8 @@ END;
145145
<title>Declarations</title>
146146

147147
<para>
148-
All variables, rows and records used in a block or it's
149-
subblocks must be declared in the declarations section of a block
148+
All variables, rows and records used in a block or its
149+
sub-blocks must be declared in the declarations section of a block
150150
except for the loop variable of a FOR loop iterating over a range
151151
of integer values. Parameters given to a PL/pgSQL function are
152152
automatically declared with the usual identifiers $n.
@@ -174,7 +174,7 @@ END;
174174
assigning '<replaceable>now</replaceable>' to a variable of type
175175
<replaceable>datetime</replaceable> causes the variable to have the
176176
time of the actual function call, not when the function was
177-
precompiled into it's bytecode.
177+
precompiled into its bytecode.
178178
</para>
179179
</listitem>
180180
</varlistentry>
@@ -186,7 +186,7 @@ END;
186186
<listitem>
187187
<para>
188188
Declares a row with the structure of the given class. Class must be
189-
an existing table- or viewname of the database. The fields of the row
189+
an existing table- or view name of the database. The fields of the row
190190
are accessed in the dot notation. Parameters to a function can
191191
be composite types (complete table rows). In that case, the
192192
corresponding identifier $n will be a rowtype, but it
@@ -196,7 +196,7 @@ END;
196196
don't have useful system attributes).
197197
</para>
198198
<para>
199-
The fields of the rowtype inherit the tables fieldsizes
199+
The fields of the rowtype inherit the table's field sizes
200200
or precision for char() etc. data types.
201201
</para>
202202
</listitem>
@@ -263,7 +263,7 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
263263
<title>Data Types</title>
264264

265265
<para>
266-
The type of a varible can be any of the existing basetypes of
266+
The type of a variable can be any of the existing base types of
267267
the database. <replaceable>type</replaceable> in the declarations
268268
section above is defined as:
269269
</para>
@@ -298,14 +298,14 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
298298
</para>
299299
<para>
300300
Using the <replaceable>class.field</replaceable>%TYPE
301-
causes PL/pgSQL to lookup the attributes definitions at the
301+
causes PL/pgSQL to look up the attributes definitions at the
302302
first call to the function during the lifetime of a backend.
303303
Have a table with a char(20) attribute and some PL/pgSQL functions
304-
that deal with it's content in local variables. Now someone
304+
that deal with its content in local variables. Now someone
305305
decides that char(20) isn't enough, dumps the table, drops it,
306306
recreates it now with the attribute in question defined as
307307
char(40) and restores the data. Ha - he forgot about the
308-
funcitons. The computations inside them will truncate the values
308+
functions. The computations inside them will truncate the values
309309
to 20 characters. But if they are defined using the
310310
<replaceable>class.field</replaceable>%TYPE
311311
declarations, they will automagically handle the size change or
@@ -320,7 +320,7 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
320320

321321
<para>
322322
All expressions used in PL/pgSQL statements are processed using
323-
the backends executor. Expressions that appear to contain
323+
the backend's executor. Expressions that appear to contain
324324
constants may in fact require run-time evaluation (e.g. 'now' for the
325325
datetime type) so
326326
it is impossible for the PL/pgSQL parser
@@ -329,11 +329,12 @@ RENAME <replaceable>oldname</replaceable> TO <replaceable>newname</replaceable>;
329329
<programlisting>
330330
SELECT <replaceable>expression</replaceable>
331331
</programlisting>
332-
using the SPI manager. In the expression, occurences of variable
332+
using the SPI manager. In the expression, occurrences of variable
333333
identifiers are substituted by parameters and the actual values from
334334
the variables are passed to the executor in the parameter array. All
335335
expressions used in a PL/pgSQL function are only prepared and
336-
saved once.
336+
saved once. The only exception to this rule is an EXECUTE statement
337+
if parsing of a query is needed each time it is encountered.
337338
</para>
338339
<para>
339340
The type checking done by the <productname>Postgres</productname>
@@ -379,7 +380,7 @@ CREATE FUNCTION logfunc2 (text) RETURNS datetime AS '
379380
<para>
380381
In the case of logfunc2(), the <productname>Postgres</productname>
381382
main parser does not know
382-
what type 'now' should become and therefor it returns a datatype of
383+
what type 'now' should become and therefore it returns a data type of
383384
text containing the string 'now'. During the assignment
384385
to the local variable curtime, the PL/pgSQL interpreter casts this
385386
string to the datetime type by calling the text_out() and datetime_in()
@@ -467,7 +468,7 @@ END IF;
467468
<term>Calling another function</term>
468469
<listitem>
469470
<para>
470-
All functions defined in a <productname>Prostgres</productname>
471+
All functions defined in a <productname>Postgres</productname>
471472
database return a value. Thus, the normal way to call a function
472473
is to execute a SELECT query or doing an assignment (resulting
473474
in a PL/pgSQL internal SELECT). But there are cases where someone
@@ -482,6 +483,49 @@ PERFORM <replaceable>query</replaceable>
482483
</listitem>
483484
</varlistentry>
484485

486+
<varlistentry>
487+
<term>Executing dynamic queries</term>
488+
<listitem>
489+
<cmdsynopsis>
490+
<command>EXECUTE</command>
491+
<arg choice="req"><replaceable class="command">query</replaceable></arg>
492+
</cmdsynopsis>
493+
494+
<para>
495+
Unlike all other queries in PL/pgSQL, a
496+
<replaceable>query</replaceable> run by an EXECUTE statement
497+
is not prepared and saved just once during the life of the
498+
server. Instead, the <replaceable>query</replaceable> is
499+
prepared each time the statement is run. This allows the
500+
<replaceable>query</replaceable> to be dynamically created
501+
within the procedure to perform actions on variable tables and
502+
fields.
503+
</para>
504+
505+
<para>
506+
An example:
507+
<programlisting>
508+
EXECUTE ''UPDATE tbl SET ''
509+
|| quote_ident(fieldname)
510+
|| '' = ''
511+
|| quote_literal(newvalue)
512+
|| '' WHERE ...'';
513+
</programlisting>
514+
This example shows use of the functions
515+
<function>quote_ident</function>(<type>TEXT</type>) and
516+
<function>quote_literal</function>(<type>TEXT</type>).
517+
Variables containing field and table identifiers should be
518+
passed to function <function>quote_ident()</function>.
519+
Variables containing literal elements of the dynamic query
520+
string should be passed to
521+
<function>quote_literal()</function>. Both take the
522+
appropriate steps to return the input text enclosed in single
523+
or double quotes and with any embedded special characters
524+
intact.
525+
</para>
526+
</listitem>
527+
</varlistentry>
528+
485529
<varlistentry>
486530
<term>Returning from the function</term>
487531
<listitem>
@@ -491,7 +535,7 @@ RETURN <replaceable>expression</replaceable>
491535
</programlisting>
492536
The function terminates and the value of <replaceable>expression</replaceable>
493537
will be returned to the upper executor. The return value of a function
494-
cannot be undefined. If control reaches the end of the toplevel block
538+
cannot be undefined. If control reaches the end of the top-level block
495539
of the function without hitting a RETURN statement, a runtime error
496540
will occur.
497541
</para>
@@ -619,15 +663,15 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
619663
</para>
620664
<para>
621665
First they have some special variables created automatically in the
622-
toplevel blocks declaration section. They are
666+
top-level blocks declaration section. They are
623667
</para>
624668

625669
<variablelist>
626670
<varlistentry>
627671
<term>NEW</term>
628672
<listitem>
629673
<para>
630-
Datatype RECORD; variable holding the new database row on INSERT/UPDATE
674+
Data type RECORD; variable holding the new database row on INSERT/UPDATE
631675
operations on ROW level triggers.
632676
</para>
633677
</listitem>
@@ -637,7 +681,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
637681
<term>OLD</term>
638682
<listitem>
639683
<para>
640-
Datatype RECORD; variable holding the old database row on UPDATE/DELETE
684+
Data type RECORD; variable holding the old database row on UPDATE/DELETE
641685
operations on ROW level triggers.
642686
</para>
643687
</listitem>
@@ -647,7 +691,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
647691
<term>TG_NAME</term>
648692
<listitem>
649693
<para>
650-
Datatype name; variable that contains the name of the trigger actually
694+
Data type name; variable that contains the name of the trigger actually
651695
fired.
652696
</para>
653697
</listitem>
@@ -657,7 +701,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
657701
<term>TG_WHEN</term>
658702
<listitem>
659703
<para>
660-
Datatype text; a string of either 'BEFORE' or 'AFTER' depending on the
704+
Data type text; a string of either 'BEFORE' or 'AFTER' depending on the
661705
triggers definition.
662706
</para>
663707
</listitem>
@@ -667,7 +711,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
667711
<term>TG_LEVEL</term>
668712
<listitem>
669713
<para>
670-
Datatype text; a string of either 'ROW' or 'STATEMENT' depending on the
714+
Data type text; a string of either 'ROW' or 'STATEMENT' depending on the
671715
triggers definition.
672716
</para>
673717
</listitem>
@@ -677,7 +721,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
677721
<term>TG_OP</term>
678722
<listitem>
679723
<para>
680-
Datatype text; a string of 'INSERT', 'UPDATE' or 'DELETE' telling
724+
Data type text; a string of 'INSERT', 'UPDATE' or 'DELETE' telling
681725
for which operation the trigger is actually fired.
682726
</para>
683727
</listitem>
@@ -687,7 +731,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
687731
<term>TG_RELID</term>
688732
<listitem>
689733
<para>
690-
Datatype oid; the object ID of the table that caused the
734+
Data type oid; the object ID of the table that caused the
691735
trigger invocation.
692736
</para>
693737
</listitem>
@@ -697,7 +741,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
697741
<term>TG_RELNAME</term>
698742
<listitem>
699743
<para>
700-
Datatype name; the name of the table that caused the trigger
744+
Data type name; the name of the table that caused the trigger
701745
invocation.
702746
</para>
703747
</listitem>
@@ -707,7 +751,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
707751
<term>TG_NARGS</term>
708752
<listitem>
709753
<para>
710-
Datatype integer; the number of arguments given to the trigger
754+
Data type integer; the number of arguments given to the trigger
711755
procedure in the CREATE TRIGGER statement.
712756
</para>
713757
</listitem>
@@ -717,7 +761,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
717761
<term>TG_ARGV[]</term>
718762
<listitem>
719763
<para>
720-
Datatype array of text; the arguments from the CREATE TRIGGER statement.
764+
Data type array of text; the arguments from the CREATE TRIGGER statement.
721765
The index counts from 0 and can be given as an expression. Invalid
722766
indices (&lt; 0 or &gt;= tg_nargs) result in a NULL value.
723767
</para>
@@ -748,11 +792,11 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
748792
exception handling model. Whenever the parser, planner/optimizer
749793
or executor decide that a statement cannot be processed any longer,
750794
the whole transaction gets aborted and the system jumps back
751-
into the mainloop to get the next query from the client application.
795+
into the main loop to get the next query from the client application.
752796
</para>
753797
<para>
754798
It is possible to hook into the error mechanism to notice that this
755-
happens. But currently it's impossible to tell what really
799+
happens. But currently it is impossible to tell what really
756800
caused the abort (input/output conversion error, floating point
757801
error, parse error). And it is possible that the database backend
758802
is in an inconsistent state at this point so returning to the upper
@@ -787,7 +831,7 @@ EXIT [ <replaceable>label</replaceable> ] [ WHEN <replaceable>expression</replac
787831
of single quotes. The functions source text on CREATE FUNCTION must
788832
be a literal string. Single quotes inside of literal strings must be
789833
either doubled or quoted with a backslash. We are still looking for
790-
an elegant alternative. In the meantime, doubling the single qoutes
834+
an elegant alternative. In the meantime, doubling the single quotes
791835
as in the examples below should be used. Any solution for this
792836
in future versions of <productname>Postgres</productname> will be
793837
upward compatible.
@@ -848,7 +892,7 @@ CREATE FUNCTION c_overpaid (EMP, int4) RETURNS bool AS '
848892

849893
<para>
850894
This trigger ensures, that any time a row is inserted or updated
851-
in the table, the current username and time are stamped into the
895+
in the table, the current user name and time are stamped into the
852896
row. And it ensures that an employees name is given and that the
853897
salary is a positive value.
854898

0 commit comments

Comments
 (0)