1
- <!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.123 2008/01 /23 02:04:47 tgl Exp $ -->
1
+ <!-- $PostgreSQL: pgsql/doc/src/sgml/plpgsql.sgml,v 1.124 2008/03 /23 00:24:19 tgl Exp $ -->
2
2
3
3
<chapter id="plpgsql">
4
4
<title><application>PL/pgSQL</application> - <acronym>SQL</acronym> Procedural Language</title>
@@ -1066,6 +1066,24 @@ EXECUTE <replaceable class="command">command-string</replaceable> <optional> INT
1066
1066
</para>
1067
1067
</note>
1068
1068
1069
+ <example id="plpgsql-quote-literal-example">
1070
+ <title>Quoting values in dynamic queries</title>
1071
+
1072
+ <indexterm>
1073
+ <primary>quote_ident</primary>
1074
+ <secondary>use in PL/PgSQL</secondary>
1075
+ </indexterm>
1076
+
1077
+ <indexterm>
1078
+ <primary>quote_literal</primary>
1079
+ <secondary>use in PL/PgSQL</secondary>
1080
+ </indexterm>
1081
+
1082
+ <indexterm>
1083
+ <primary>quote_nullable</primary>
1084
+ <secondary>use in PL/PgSQL</secondary>
1085
+ </indexterm>
1086
+
1069
1087
<para>
1070
1088
When working with dynamic commands you will often have to handle escaping
1071
1089
of single quotes. The recommended method for quoting fixed text in your
@@ -1091,32 +1109,64 @@ EXECUTE 'UPDATE tbl SET '
1091
1109
</programlisting>
1092
1110
</para>
1093
1111
1094
- <indexterm>
1095
- <primary>quote_ident</primary>
1096
- <secondary>use in PL/PgSQL</secondary>
1097
- </indexterm>
1098
-
1099
- <indexterm>
1100
- <primary>quote_literal</primary>
1101
- <secondary>use in PL/PgSQL</secondary>
1102
- </indexterm>
1103
-
1104
1112
<para>
1105
1113
This example demonstrates the use of the
1106
1114
<function>quote_ident</function> and
1107
- <function>quote_literal</function> functions. For safety,
1108
- expressions containing column and table identifiers should be
1109
- passed to <function>quote_ident</function>. Expressions containing
1110
- values that should be literal strings in the constructed command
1111
- should be passed to <function>quote_literal</function>. Both
1112
- take the appropriate steps to return the input text enclosed in
1113
- double or single quotes respectively, with any embedded special
1114
- characters properly escaped.
1115
+ <function>quote_literal</function> functions (see <xref
1116
+ linkend="functions-string">). For safety, expressions containing column
1117
+ or table identifiers should be passed through
1118
+ <function>quote_ident</function> before insertion in a dynamic query.
1119
+ Expressions containing values that should be literal strings in the
1120
+ constructed command should be passed through <function>quote_literal</>.
1121
+ These functions take the appropriate steps to return the input text
1122
+ enclosed in double or single quotes respectively, with any embedded
1123
+ special characters properly escaped.
1124
+ </para>
1125
+
1126
+ <para>
1127
+ Because <function>quote_literal</function> is labelled
1128
+ <literal>STRICT</literal>, it will always return null when called with a
1129
+ null argument. In the above example, if <literal>newvalue</> or
1130
+ <literal>keyvalue</> were null, the entire dynamic query string would
1131
+ become null, leading to an error from <command>EXECUTE</command>.
1132
+ You can avoid this problem by using the <function>quote_nullable</>
1133
+ function, which works the same as <function>quote_literal</> except that
1134
+ when called with a null argument it returns the string <literal>NULL</>.
1135
+ For example,
1136
+ <programlisting>
1137
+ EXECUTE 'UPDATE tbl SET '
1138
+ || quote_ident(colname)
1139
+ || ' = '
1140
+ || quote_nullable(newvalue)
1141
+ || ' WHERE key = '
1142
+ || quote_nullable(keyvalue);
1143
+ </programlisting>
1144
+ If you are dealing with values that might be null, you should usually
1145
+ use <function>quote_nullable</> in place of <function>quote_literal</>.
1146
+ </para>
1147
+
1148
+ <para>
1149
+ As always, care must be taken to ensure that null values in a query do
1150
+ not deliver unintended results. For example the <literal>WHERE</> clause
1151
+ <programlisting>
1152
+ 'WHERE key = ' || quote_nullable(keyvalue)
1153
+ </programlisting>
1154
+ will never succeed if <literal>keyvalue</> is null, because the
1155
+ result of using the equality operator <literal>=</> with a null operand
1156
+ is always null. If you wish null to work like an ordinary key value,
1157
+ you would need to rewrite the above as
1158
+ <programlisting>
1159
+ 'WHERE key IS NOT DISTINCT FROM ' || quote_nullable(keyvalue)
1160
+ </programlisting>
1161
+ (At present, <literal>IS NOT DISTINCT FROM</> is handled much less
1162
+ efficiently than <literal>=</>, so don't do this unless you must.
1163
+ See <xref linkend="functions-comparison"> for
1164
+ more information on nulls and <literal>IS DISTINCT</>.)
1115
1165
</para>
1116
1166
1117
1167
<para>
1118
1168
Note that dollar quoting is only useful for quoting fixed text.
1119
- It would be a very bad idea to try to do the above example as:
1169
+ It would be a very bad idea to try to write this example as:
1120
1170
<programlisting>
1121
1171
EXECUTE 'UPDATE tbl SET '
1122
1172
|| quote_ident(colname)
@@ -1129,8 +1179,10 @@ EXECUTE 'UPDATE tbl SET '
1129
1179
happened to contain <literal>$$</>. The same objection would
1130
1180
apply to any other dollar-quoting delimiter you might pick.
1131
1181
So, to safely quote text that is not known in advance, you
1132
- <emphasis>must</> use <function>quote_literal</function>.
1182
+ <emphasis>must</> use <function>quote_literal</>,
1183
+ <function>quote_nullable</>, or <function>quote_ident</>, as appropriate.
1133
1184
</para>
1185
+ </example>
1134
1186
1135
1187
<para>
1136
1188
A much larger example of a dynamic command and
0 commit comments