@@ -94,11 +94,11 @@ $$ LANGUAGE pltcl;
94
94
95
95
<para>
96
96
The body of the function is simply a piece of Tcl script.
97
- When the function is called, the argument values are passed as
98
- variables <literal>$ 1</literal> ... <literal>$<replaceable>n</replaceable></literal> to the
99
- Tcl script . The result is returned
100
- from the Tcl code in the usual way, with a <literal>return</literal>
101
- statement.
97
+ When the function is called, the argument values are passed to the
98
+ Tcl script as variables named <literal>1</literal>
99
+ ... <literal><replaceable>n</replaceable></literal> . The result is
100
+ returned from the Tcl code in the usual way, with
101
+ a <literal>return</literal> statement.
102
102
</para>
103
103
104
104
<para>
@@ -173,17 +173,57 @@ $$ LANGUAGE pltcl;
173
173
</para>
174
174
175
175
<para>
176
- There is currently no support for returning a composite-type
177
- result value, nor for returning sets.
176
+ PL/Tcl functions can return composite-type results, too. To do this,
177
+ the Tcl code must return a list of column name/value pairs matching
178
+ the expected result type. Any column names omitted from the list
179
+ are returned as nulls, and an error is raised if there are unexpected
180
+ column names. Here is an example:
181
+
182
+ <programlisting>
183
+ CREATE FUNCTION square_cube(in int, out squared int, out cubed int) AS $$
184
+ return [list squared [expr {$1 * $1}] cubed [expr {$1 * $1 * $1}]]
185
+ $$ LANGUAGE pltcl;
186
+ </programlisting>
178
187
</para>
179
188
189
+ <tip>
190
+ <para>
191
+ The result list can be made from an array representation of the
192
+ desired tuple with the <literal>array get</> Tcl command. For example:
193
+
194
+ <programlisting>
195
+ CREATE FUNCTION raise_pay(employee, delta int) RETURNS employee AS $$
196
+ set 1(salary) [expr {$1(salary) + $2}]
197
+ return [array get 1]
198
+ $$ LANGUAGE pltcl;
199
+ </programlisting>
200
+ </para>
201
+ </tip>
202
+
180
203
<para>
181
- <application>PL/Tcl</> does not currently have full support for
182
- domain types: it treats a domain the same as the underlying scalar
183
- type. This means that constraints associated with the domain will
184
- not be enforced. This is not an issue for function arguments, but
185
- it is a hazard if you declare a <application>PL/Tcl</> function
186
- as returning a domain type.
204
+ PL/Tcl functions can return sets. To do this, the Tcl code should
205
+ call <function>return_next</function> once per row to be returned,
206
+ passing either the appropriate value when returning a scalar type,
207
+ or a list of column name/value pairs when returning a composite type.
208
+ Here is an example returning a scalar type:
209
+
210
+ <programlisting>
211
+ CREATE FUNCTION sequence(int, int) RETURNS SETOF int AS $$
212
+ for {set i $1} {$i < $2} {incr i} {
213
+ return_next $i
214
+ }
215
+ $$ LANGUAGE pltcl;
216
+ </programlisting>
217
+
218
+ and here is one returning a composite type:
219
+
220
+ <programlisting>
221
+ CREATE FUNCTION table_of_squares(int, int) RETURNS TABLE (x int, x2 int) AS $$
222
+ for {set i $1} {$i < $2} {incr i} {
223
+ return_next [list x $i x2 [expr {$i * $i}]]
224
+ }
225
+ $$ LANGUAGE pltcl;
226
+ </programlisting>
187
227
</para>
188
228
189
229
</sect1>
@@ -195,10 +235,9 @@ $$ LANGUAGE pltcl;
195
235
The argument values supplied to a PL/Tcl function's code are simply
196
236
the input arguments converted to text form (just as if they had been
197
237
displayed by a <command>SELECT</> statement). Conversely, the
198
- <literal>return</>
199
- command will accept any string that is acceptable input format for
200
- the function's declared return type. So, within the PL/Tcl function,
201
- all values are just text strings.
238
+ <literal>return</> and <literal>return_next</> commands will accept
239
+ any string that is acceptable input format for the function's declared
240
+ result type, or for the specified column of a composite result type.
202
241
</para>
203
242
204
243
</sect1>
0 commit comments