]> granicus.if.org Git - postgresql/blob - src/pl/plpgsql/src/plpgsql.h
This patch makes a minor cleanup to the implementation of PERFORM in
[postgresql] / src / pl / plpgsql / src / plpgsql.h
1 /**********************************************************************
2  * plpgsql.h            - Definitions for the PL/pgSQL
3  *                        procedural language
4  *
5  * IDENTIFICATION
6  *        $Header: /cvsroot/pgsql/src/pl/plpgsql/src/plpgsql.h,v 1.29 2002/11/10 00:35:58 momjian Exp $
7  *
8  *        This software is copyrighted by Jan Wieck - Hamburg.
9  *
10  *        The author hereby grants permission  to  use,  copy,  modify,
11  *        distribute,  and      license this software and its documentation
12  *        for any purpose, provided that existing copyright notices are
13  *        retained      in      all  copies  and  that  this notice is included
14  *        verbatim in any distributions. No written agreement, license,
15  *        or  royalty  fee      is required for any of the authorized uses.
16  *        Modifications to this software may be  copyrighted  by  their
17  *        author  and  need  not  follow  the licensing terms described
18  *        here, provided that the new terms are  clearly  indicated  on
19  *        the first page of each file where they apply.
20  *
21  *        IN NO EVENT SHALL THE AUTHOR OR DISTRIBUTORS BE LIABLE TO ANY
22  *        PARTY  FOR  DIRECT,   INDIRECT,       SPECIAL,   INCIDENTAL,   OR
23  *        CONSEQUENTIAL   DAMAGES  ARISING      OUT  OF  THE  USE  OF  THIS
24  *        SOFTWARE, ITS DOCUMENTATION, OR ANY DERIVATIVES THEREOF, EVEN
25  *        IF  THE  AUTHOR  HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH
26  *        DAMAGE.
27  *
28  *        THE  AUTHOR  AND      DISTRIBUTORS  SPECIFICALLY       DISCLAIM       ANY
29  *        WARRANTIES,  INCLUDING,  BUT  NOT  LIMITED  TO,  THE  IMPLIED
30  *        WARRANTIES  OF  MERCHANTABILITY,      FITNESS  FOR  A  PARTICULAR
31  *        PURPOSE,      AND NON-INFRINGEMENT.  THIS SOFTWARE IS PROVIDED ON
32  *        AN "AS IS" BASIS, AND THE AUTHOR      AND  DISTRIBUTORS  HAVE  NO
33  *        OBLIGATION   TO       PROVIDE   MAINTENANCE,   SUPPORT,  UPDATES,
34  *        ENHANCEMENTS, OR MODIFICATIONS.
35  *
36  **********************************************************************/
37 #ifndef PLPGSQL_H
38 #define PLPGSQL_H
39
40 #include "postgres.h"
41
42 #include "fmgr.h"
43 #include "miscadmin.h"
44 #include "executor/spi.h"
45 #include "commands/trigger.h"
46 #include "utils/tuplestore.h"
47
48 /**********************************************************************
49  * Definitions
50  **********************************************************************/
51
52 /* ----------
53  * Compilers namestack item types
54  * ----------
55  */
56 enum
57 {
58         PLPGSQL_NSTYPE_LABEL,
59         PLPGSQL_NSTYPE_VAR,
60         PLPGSQL_NSTYPE_ROW,
61         PLPGSQL_NSTYPE_REC,
62         PLPGSQL_NSTYPE_RECFIELD
63 };
64
65 /* ----------
66  * Datum array node types
67  * ----------
68  */
69 enum
70 {
71         PLPGSQL_DTYPE_VAR,
72         PLPGSQL_DTYPE_ROW,
73         PLPGSQL_DTYPE_REC,
74         PLPGSQL_DTYPE_RECFIELD,
75         PLPGSQL_DTYPE_EXPR,
76         PLPGSQL_DTYPE_TRIGARG
77 };
78
79 /* ----------
80  * Execution tree node types
81  * ----------
82  */
83 enum
84 {
85         PLPGSQL_STMT_BLOCK,
86         PLPGSQL_STMT_ASSIGN,
87         PLPGSQL_STMT_IF,
88         PLPGSQL_STMT_LOOP,
89         PLPGSQL_STMT_WHILE,
90         PLPGSQL_STMT_FORI,
91         PLPGSQL_STMT_FORS,
92         PLPGSQL_STMT_SELECT,
93         PLPGSQL_STMT_EXIT,
94         PLPGSQL_STMT_RETURN,
95         PLPGSQL_STMT_RETURN_NEXT,
96         PLPGSQL_STMT_RAISE,
97         PLPGSQL_STMT_EXECSQL,
98         PLPGSQL_STMT_DYNEXECUTE,
99         PLPGSQL_STMT_DYNFORS,
100         PLPGSQL_STMT_GETDIAG,
101         PLPGSQL_STMT_OPEN,
102         PLPGSQL_STMT_FETCH,
103         PLPGSQL_STMT_CLOSE,
104         PLPGSQL_STMT_PERFORM
105 };
106
107
108 /* ----------
109  * Execution node return codes
110  * ----------
111  */
112 enum
113 {
114         PLPGSQL_RC_OK,
115         PLPGSQL_RC_EXIT,
116         PLPGSQL_RC_RETURN
117 };
118
119 /* ----------
120  * GET DIAGNOSTICS system attrs
121  * ----------
122  */
123 enum
124 {
125         PLPGSQL_GETDIAG_ROW_COUNT,
126         PLPGSQL_GETDIAG_RESULT_OID
127 };
128
129
130 /**********************************************************************
131  * Node and structure definitions
132  **********************************************************************/
133
134
135 typedef struct
136 {                                                               /* Dynamic string control structure */
137         int                     alloc;
138         int                     used;
139         char       *value;
140 }       PLpgSQL_dstring;
141
142
143 typedef struct
144 {                                                               /* Postgres base data type              */
145         char       *typname;
146         Oid                     typoid;
147         FmgrInfo        typinput;
148         Oid                     typelem;
149         int16           typlen;
150         bool            typbyval;
151         int32           atttypmod;
152 }       PLpgSQL_type;
153
154
155 typedef struct
156 {                                                               /* Generic datum array item             */
157         int                     dtype;
158         int                     dno;
159 }       PLpgSQL_datum;
160
161
162 typedef struct
163 {                                                               /* SQL Query to plan and execute        */
164         int                     dtype;
165         int                     exprno;
166         char       *query;
167         void       *plan;
168         Node       *plan_simple_expr;
169         Oid                     plan_simple_type;
170         Oid                *plan_argtypes;
171         int                     nparams;
172         int                     params[1];
173 }       PLpgSQL_expr;
174
175
176 typedef struct
177 {                                                               /* Local variable                       */
178         int                     dtype;
179         int                     varno;
180         char       *refname;
181         int                     lineno;
182
183         PLpgSQL_type *datatype;
184         int                     isconst;
185         int                     notnull;
186         PLpgSQL_expr *default_val;
187         PLpgSQL_expr *cursor_explicit_expr;
188         int                     cursor_explicit_argrow;
189
190         Datum           value;
191         bool            isnull;
192         bool            freeval;
193 }       PLpgSQL_var;
194
195
196 typedef struct
197 {                                                               /* Rowtype                              */
198         int                     dtype;
199         int                     rowno;
200         char       *refname;
201         int                     lineno;
202         Oid                     rowtypeclass;
203
204         int                     nfields;
205         char      **fieldnames;
206         int                *varnos;
207 }       PLpgSQL_row;
208
209
210 typedef struct
211 {                                                               /* Record of undefined structure        */
212         int                     dtype;
213         int                     recno;
214         char       *refname;
215         int                     lineno;
216
217         HeapTuple       tup;
218         TupleDesc       tupdesc;
219         bool            freetup;
220         bool            freetupdesc;
221 }       PLpgSQL_rec;
222
223
224 typedef struct
225 {                                                               /* Field in record                      */
226         int                     dtype;
227         int                     rfno;
228         char       *fieldname;
229         int                     recno;
230 }       PLpgSQL_recfield;
231
232
233 typedef struct
234 {                                                               /* Positional argument to trigger       */
235         int                     dtype;
236         int                     dno;
237         PLpgSQL_expr *argnum;
238 }       PLpgSQL_trigarg;
239
240
241 typedef struct
242 {                                                               /* Item in the compilers namestack      */
243         int                     itemtype;
244         int                     itemno;
245         char            name[1];
246 }       PLpgSQL_nsitem;
247
248
249 typedef struct PLpgSQL_ns
250 {                                                               /* Compiler namestack level             */
251         int                     items_alloc;
252         int                     items_used;
253         PLpgSQL_nsitem **items;
254         struct PLpgSQL_ns *upper;
255 }       PLpgSQL_ns;
256
257
258 typedef struct
259 {                                                               /* List of execution nodes              */
260         int                     stmts_alloc;
261         int                     stmts_used;
262         struct PLpgSQL_stmt **stmts;
263 }       PLpgSQL_stmts;
264
265
266 typedef struct
267 {                                                               /* Generic execution node               */
268         int                     cmd_type;
269         int                     lineno;
270 }       PLpgSQL_stmt;
271
272
273 typedef struct
274 {                                                               /* Block of statements                  */
275         int                     cmd_type;
276         int                     lineno;
277         char       *label;
278         PLpgSQL_stmts *body;
279         int                     n_initvars;
280         int                *initvarnos;
281 }       PLpgSQL_stmt_block;
282
283
284 typedef struct
285 {                                                               /* Assign statement                     */
286         int                     cmd_type;
287         int                     lineno;
288         int                     varno;
289         PLpgSQL_expr *expr;
290 }       PLpgSQL_stmt_assign;
291
292 typedef struct
293 {                                                               /* PERFORM statement            */
294         int                     cmd_type;
295         int                     lineno;
296         PLpgSQL_expr *expr;
297 }   PLpgSQL_stmt_perform;
298
299 typedef struct
300 {                                                               /* Get Diagnostics item         */
301         int                     item;                   /* id for diagnostic value desired */
302         int                     target;                 /* where to assign it */
303 }       PLpgSQL_diag_item;
304
305 typedef struct
306 {                                                               /* Get Diagnostics statement            */
307         int                     cmd_type;
308         int                     lineno;
309         int                     ndtitems;
310         PLpgSQL_diag_item *dtitems;
311 }       PLpgSQL_stmt_getdiag;
312
313
314 typedef struct
315 {                                                               /* IF statement                         */
316         int                     cmd_type;
317         int                     lineno;
318         PLpgSQL_expr *cond;
319         PLpgSQL_stmts *true_body;
320         PLpgSQL_stmts *false_body;
321 }       PLpgSQL_stmt_if;
322
323
324 typedef struct
325 {                                                               /* Unconditional LOOP statement         */
326         int                     cmd_type;
327         int                     lineno;
328         char       *label;
329         PLpgSQL_stmts *body;
330 }       PLpgSQL_stmt_loop;
331
332
333 typedef struct
334 {                                                               /* WHILE cond LOOP statement            */
335         int                     cmd_type;
336         int                     lineno;
337         char       *label;
338         PLpgSQL_expr *cond;
339         PLpgSQL_stmts *body;
340 }       PLpgSQL_stmt_while;
341
342
343 typedef struct
344 {                                                               /* FOR statement with integer loopvar   */
345         int                     cmd_type;
346         int                     lineno;
347         char       *label;
348         PLpgSQL_var *var;
349         PLpgSQL_expr *lower;
350         PLpgSQL_expr *upper;
351         int                     reverse;
352         PLpgSQL_stmts *body;
353 }       PLpgSQL_stmt_fori;
354
355
356 typedef struct
357 {                                                               /* FOR statement running over SELECT    */
358         int                     cmd_type;
359         int                     lineno;
360         char       *label;
361         PLpgSQL_rec *rec;
362         PLpgSQL_row *row;
363         PLpgSQL_expr *query;
364         PLpgSQL_stmts *body;
365 }       PLpgSQL_stmt_fors;
366
367
368 typedef struct
369 {                                                               /* FOR statement running over EXECUTE   */
370         int                     cmd_type;
371         int                     lineno;
372         char       *label;
373         PLpgSQL_rec *rec;
374         PLpgSQL_row *row;
375         PLpgSQL_expr *query;
376         PLpgSQL_stmts *body;
377 }       PLpgSQL_stmt_dynfors;
378
379
380 typedef struct
381 {                                                               /* SELECT ... INTO statement            */
382         int                     cmd_type;
383         int                     lineno;
384         PLpgSQL_rec *rec;
385         PLpgSQL_row *row;
386         PLpgSQL_expr *query;
387 }       PLpgSQL_stmt_select;
388
389
390 typedef struct
391 {                                                               /* OPEN a curvar                                        */
392         int                     cmd_type;
393         int                     lineno;
394         int                     curvar;
395         PLpgSQL_row *returntype;
396         PLpgSQL_expr *argquery;
397         PLpgSQL_expr *query;
398         PLpgSQL_expr *dynquery;
399 }       PLpgSQL_stmt_open;
400
401
402 typedef struct
403 {                                                               /* FETCH curvar INTO statement          */
404         int                     cmd_type;
405         int                     lineno;
406         PLpgSQL_rec *rec;
407         PLpgSQL_row *row;
408         int                     curvar;
409 }       PLpgSQL_stmt_fetch;
410
411
412 typedef struct
413 {                                                               /* CLOSE curvar                                         */
414         int                     cmd_type;
415         int                     lineno;
416         int                     curvar;
417 }       PLpgSQL_stmt_close;
418
419
420 typedef struct
421 {                                                               /* EXIT statement                       */
422         int                     cmd_type;
423         int                     lineno;
424         char       *label;
425         PLpgSQL_expr *cond;
426 }       PLpgSQL_stmt_exit;
427
428
429 typedef struct
430 {                                                               /* RETURN statement                     */
431         int                     cmd_type;
432         int                     lineno;
433         PLpgSQL_expr *expr;
434         int                     retrecno;
435 }       PLpgSQL_stmt_return;
436
437 typedef struct
438 {                                                               /* RETURN NEXT statement */
439         int                     cmd_type;
440         int                     lineno;
441         PLpgSQL_rec *rec;
442         PLpgSQL_row *row;
443         PLpgSQL_expr *expr;
444 }       PLpgSQL_stmt_return_next;
445
446 typedef struct
447 {                                                               /* RAISE statement                      */
448         int                     cmd_type;
449         int                     lineno;
450         int                     elog_level;
451         char       *message;
452         int                     nparams;
453         int                *params;
454 }       PLpgSQL_stmt_raise;
455
456
457 typedef struct
458 {                                                               /* Generic SQL statement to execute */
459         int                     cmd_type;
460         int                     lineno;
461         PLpgSQL_expr *sqlstmt;
462 }       PLpgSQL_stmt_execsql;
463
464
465 typedef struct
466 {                                                               /* Dynamic SQL string to execute */
467         int                     cmd_type;
468         int                     lineno;
469         PLpgSQL_expr *query;
470 }       PLpgSQL_stmt_dynexecute;
471
472
473 typedef struct PLpgSQL_function
474 {                                                               /* Complete compiled function     */
475         char       *fn_name;
476         Oid                     fn_oid;
477         TransactionId fn_xmin;
478         CommandId       fn_cmin;
479         int                     fn_functype;
480
481         Oid                     fn_rettype;
482         int                     fn_rettyplen;
483         bool            fn_retbyval;
484         FmgrInfo        fn_retinput;
485         Oid                     fn_rettypelem;
486         bool            fn_retistuple;
487         bool            fn_retset;
488
489         int                     fn_nargs;
490         int                     fn_argvarnos[FUNC_MAX_ARGS];
491         int                     found_varno;
492         int                     new_varno;
493         int                     old_varno;
494         int                     tg_name_varno;
495         int                     tg_when_varno;
496         int                     tg_level_varno;
497         int                     tg_op_varno;
498         int                     tg_relid_varno;
499         int                     tg_relname_varno;
500         int                     tg_nargs_varno;
501
502         int                     ndatums;
503         PLpgSQL_datum **datums;
504         PLpgSQL_stmt_block *action;
505
506         struct PLpgSQL_function *next;          /* for chaining list of functions */
507 }       PLpgSQL_function;
508
509
510 typedef struct
511 {                                                               /* Runtime execution data       */
512         Datum           retval;
513         bool            retisnull;
514         Oid                     rettype;                /* type of current retval */
515
516         Oid                     fn_rettype;             /* info about declared function rettype */
517         bool            retistuple;
518         bool            retisset;
519
520         TupleDesc       rettupdesc;
521         char       *exitlabel;
522
523         Tuplestorestate *tuple_store;           /* SRFs accumulate results here */
524         MemoryContext tuple_store_cxt;
525         ReturnSetInfo *rsi;
526
527         int                     trig_nargs;
528         Datum      *trig_argv;
529
530         int                     found_varno;
531         int                     ndatums;
532         PLpgSQL_datum **datums;
533
534         /* temporary state for results from evaluation of query or expr */
535         SPITupleTable *eval_tuptable;
536         uint32          eval_processed;
537         Oid                     eval_lastoid;
538         ExprContext *eval_econtext;
539 }       PLpgSQL_execstate;
540
541
542 /**********************************************************************
543  * Global variable declarations
544  **********************************************************************/
545
546 extern int      plpgsql_DumpExecTree;
547 extern int      plpgsql_SpaceScanned;
548 extern int      plpgsql_nDatums;
549 extern PLpgSQL_datum **plpgsql_Datums;
550
551 extern int      plpgsql_error_lineno;
552 extern char *plpgsql_error_funcname;
553
554 /* linkage to the real yytext and yylineno variables */
555 extern char *plpgsql_base_yytext;
556
557 #define plpgsql_yytext plpgsql_base_yytext
558 extern int      plpgsql_base_yylineno;
559
560 #define plpgsql_yylineno plpgsql_base_yylineno
561
562 extern PLpgSQL_function *plpgsql_curr_compile;
563
564 /**********************************************************************
565  * Function declarations
566  **********************************************************************/
567
568 /* ----------
569  * Functions in pl_comp.c
570  * ----------
571  */
572 extern PLpgSQL_function *plpgsql_compile(Oid fn_oid, int functype);
573 extern int      plpgsql_parse_word(char *word);
574 extern int      plpgsql_parse_dblword(char *word);
575 extern int      plpgsql_parse_tripword(char *word);
576 extern int      plpgsql_parse_wordtype(char *word);
577 extern int      plpgsql_parse_dblwordtype(char *word);
578 extern int      plpgsql_parse_tripwordtype(char *word);
579 extern int      plpgsql_parse_wordrowtype(char *word);
580 extern int      plpgsql_parse_dblwordrowtype(char *word);
581 extern PLpgSQL_type *plpgsql_parse_datatype(char *string);
582 extern void plpgsql_adddatum(PLpgSQL_datum * new);
583 extern int      plpgsql_add_initdatums(int **varnos);
584 extern void plpgsql_yyerror(const char *s);
585
586 /* ----------
587  * Functions in pl_handler.c
588  * ----------
589  */
590 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
591
592 /* ----------
593  * Functions in pl_exec.c
594  * ----------
595  */
596 extern Datum plpgsql_exec_function(PLpgSQL_function * func,
597                                           FunctionCallInfo fcinfo);
598 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function * func,
599                                          TriggerData *trigdata);
600
601
602 /* ----------
603  * Functions for the dynamic string handling in pl_funcs.c
604  * ----------
605  */
606 extern void plpgsql_dstring_init(PLpgSQL_dstring * ds);
607 extern void plpgsql_dstring_free(PLpgSQL_dstring * ds);
608 extern void plpgsql_dstring_append(PLpgSQL_dstring * ds, char *str);
609 extern char *plpgsql_dstring_get(PLpgSQL_dstring * ds);
610
611 /* ----------
612  * Functions for the namestack handling in pl_funcs.c
613  * ----------
614  */
615 extern void plpgsql_ns_init(void);
616 extern bool plpgsql_ns_setlocal(bool flag);
617 extern void plpgsql_ns_push(char *label);
618 extern void plpgsql_ns_pop(void);
619 extern void plpgsql_ns_additem(int itemtype, int itemno, char *name);
620 extern PLpgSQL_nsitem *plpgsql_ns_lookup(char *name, char *nsname);
621 extern void plpgsql_ns_rename(char *oldname, char *newname);
622
623 /* ----------
624  * Other functions in pl_funcs.c
625  * ----------
626  */
627 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
628 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt * stmt);
629 extern void plpgsql_dumptree(PLpgSQL_function * func);
630
631 /* ----------
632  * Externs in gram.y and scan.l
633  * ----------
634  */
635 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
636 extern int      plpgsql_yyparse(void);
637 extern int      plpgsql_base_yylex(void);
638 extern int      plpgsql_yylex(void);
639 extern void plpgsql_push_back_token(int token);
640 extern void plpgsql_setinput(char *s, int functype);
641
642 #endif   /* PLPGSQL_H */