]> granicus.if.org Git - postgresql/blob - src/pl/plpgsql/src/plpgsql.h
c81b0a3b1bc99d7b538f1f25b2883f07bec65807
[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.26 2002/08/30 00:28:41 tgl 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 };
105
106
107 /* ----------
108  * Execution node return codes
109  * ----------
110  */
111 enum
112 {
113         PLPGSQL_RC_OK,
114         PLPGSQL_RC_EXIT,
115         PLPGSQL_RC_RETURN
116 };
117
118 /* ----------
119  * GET DIAGNOSTICS system attrs
120  * ----------
121  */
122 enum
123 {
124         PLPGSQL_GETDIAG_ROW_COUNT,
125         PLPGSQL_GETDIAG_RESULT_OID
126 };
127
128
129 /**********************************************************************
130  * Node and structure definitions
131  **********************************************************************/
132
133
134 typedef struct
135 {                                                               /* Dynamic string control structure */
136         int                     alloc;
137         int                     used;
138         char       *value;
139 }       PLpgSQL_dstring;
140
141
142 typedef struct
143 {                                                               /* Postgres base data type              */
144         char       *typname;
145         Oid                     typoid;
146         FmgrInfo        typinput;
147         Oid                     typelem;
148         int16           typlen;
149         bool            typbyval;
150         int32           atttypmod;
151 }       PLpgSQL_type;
152
153
154 typedef struct
155 {                                                               /* Generic datum array item             */
156         int                     dtype;
157         int                     dno;
158 }       PLpgSQL_datum;
159
160
161 typedef struct
162 {                                                               /* SQL Query to plan and execute        */
163         int                     dtype;
164         int                     exprno;
165         char       *query;
166         void       *plan;
167         Node       *plan_simple_expr;
168         Oid                     plan_simple_type;
169         Oid                *plan_argtypes;
170         int                     nparams;
171         int                     params[1];
172 }       PLpgSQL_expr;
173
174
175 typedef struct
176 {                                                               /* Local variable                       */
177         int                     dtype;
178         int                     varno;
179         char       *refname;
180         int                     lineno;
181
182         PLpgSQL_type *datatype;
183         int                     isconst;
184         int                     notnull;
185         PLpgSQL_expr *default_val;
186         PLpgSQL_expr *cursor_explicit_expr;
187         int                     cursor_explicit_argrow;
188
189         Datum           value;
190         bool            isnull;
191         bool            freeval;
192 }       PLpgSQL_var;
193
194
195 typedef struct
196 {                                                               /* Rowtype                              */
197         int                     dtype;
198         int                     rowno;
199         char       *refname;
200         int                     lineno;
201         Oid                     rowtypeclass;
202
203         int                     nfields;
204         char      **fieldnames;
205         int                *varnos;
206 }       PLpgSQL_row;
207
208
209 typedef struct
210 {                                                               /* Record of undefined structure        */
211         int                     dtype;
212         int                     recno;
213         char       *refname;
214         int                     lineno;
215
216         HeapTuple       tup;
217         TupleDesc       tupdesc;
218         bool            freetup;
219         bool            freetupdesc;
220 }       PLpgSQL_rec;
221
222
223 typedef struct
224 {                                                               /* Field in record                      */
225         int                     dtype;
226         int                     rfno;
227         char       *fieldname;
228         int                     recno;
229 }       PLpgSQL_recfield;
230
231
232 typedef struct
233 {                                                               /* Positional argument to trigger       */
234         int                     dtype;
235         int                     dno;
236         PLpgSQL_expr *argnum;
237 }       PLpgSQL_trigarg;
238
239
240 typedef struct
241 {                                                               /* Item in the compilers namestack      */
242         int                     itemtype;
243         int                     itemno;
244         char            name[1];
245 }       PLpgSQL_nsitem;
246
247
248 typedef struct PLpgSQL_ns
249 {                                                               /* Compiler namestack level             */
250         int                     items_alloc;
251         int                     items_used;
252         PLpgSQL_nsitem **items;
253         struct PLpgSQL_ns *upper;
254 }       PLpgSQL_ns;
255
256
257 typedef struct
258 {                                                               /* List of execution nodes              */
259         int                     stmts_alloc;
260         int                     stmts_used;
261         struct PLpgSQL_stmt **stmts;
262 }       PLpgSQL_stmts;
263
264
265 typedef struct
266 {                                                               /* Generic execution node               */
267         int                     cmd_type;
268         int                     lineno;
269 }       PLpgSQL_stmt;
270
271
272 typedef struct
273 {                                                               /* Block of statements                  */
274         int                     cmd_type;
275         int                     lineno;
276         char       *label;
277         PLpgSQL_stmts *body;
278         int                     n_initvars;
279         int                *initvarnos;
280 }       PLpgSQL_stmt_block;
281
282
283 typedef struct
284 {                                                               /* Assign statement                     */
285         int                     cmd_type;
286         int                     lineno;
287         int                     varno;
288         PLpgSQL_expr *expr;
289 }       PLpgSQL_stmt_assign;
290
291
292 typedef struct
293 {                                                               /* Get Diagnostics item         */
294         int                     item;                   /* id for diagnostic value desired */
295         int                     target;                 /* where to assign it */
296 }       PLpgSQL_diag_item;
297
298 typedef struct
299 {                                                               /* Get Diagnostics statement            */
300         int                     cmd_type;
301         int                     lineno;
302         int                     ndtitems;
303         PLpgSQL_diag_item *dtitems;
304 }       PLpgSQL_stmt_getdiag;
305
306
307 typedef struct
308 {                                                               /* IF statement                         */
309         int                     cmd_type;
310         int                     lineno;
311         PLpgSQL_expr *cond;
312         PLpgSQL_stmts *true_body;
313         PLpgSQL_stmts *false_body;
314 }       PLpgSQL_stmt_if;
315
316
317 typedef struct
318 {                                                               /* Unconditional LOOP statement         */
319         int                     cmd_type;
320         int                     lineno;
321         char       *label;
322         PLpgSQL_stmts *body;
323 }       PLpgSQL_stmt_loop;
324
325
326 typedef struct
327 {                                                               /* WHILE cond LOOP statement            */
328         int                     cmd_type;
329         int                     lineno;
330         char       *label;
331         PLpgSQL_expr *cond;
332         PLpgSQL_stmts *body;
333 }       PLpgSQL_stmt_while;
334
335
336 typedef struct
337 {                                                               /* FOR statement with integer loopvar   */
338         int                     cmd_type;
339         int                     lineno;
340         char       *label;
341         PLpgSQL_var *var;
342         PLpgSQL_expr *lower;
343         PLpgSQL_expr *upper;
344         int                     reverse;
345         PLpgSQL_stmts *body;
346 }       PLpgSQL_stmt_fori;
347
348
349 typedef struct
350 {                                                               /* FOR statement running over SELECT    */
351         int                     cmd_type;
352         int                     lineno;
353         char       *label;
354         PLpgSQL_rec *rec;
355         PLpgSQL_row *row;
356         PLpgSQL_expr *query;
357         PLpgSQL_stmts *body;
358 }       PLpgSQL_stmt_fors;
359
360
361 typedef struct
362 {                                                               /* FOR statement running over EXECUTE   */
363         int                     cmd_type;
364         int                     lineno;
365         char       *label;
366         PLpgSQL_rec *rec;
367         PLpgSQL_row *row;
368         PLpgSQL_expr *query;
369         PLpgSQL_stmts *body;
370 }       PLpgSQL_stmt_dynfors;
371
372
373 typedef struct
374 {                                                               /* SELECT ... INTO statement            */
375         int                     cmd_type;
376         int                     lineno;
377         PLpgSQL_rec *rec;
378         PLpgSQL_row *row;
379         PLpgSQL_expr *query;
380 }       PLpgSQL_stmt_select;
381
382
383 typedef struct
384 {                                                               /* OPEN a curvar                                        */
385         int                     cmd_type;
386         int                     lineno;
387         int                     curvar;
388         PLpgSQL_row *returntype;
389         PLpgSQL_expr *argquery;
390         PLpgSQL_expr *query;
391         PLpgSQL_expr *dynquery;
392 }       PLpgSQL_stmt_open;
393
394
395 typedef struct
396 {                                                               /* FETCH curvar INTO statement          */
397         int                     cmd_type;
398         int                     lineno;
399         PLpgSQL_rec *rec;
400         PLpgSQL_row *row;
401         int                     curvar;
402 }       PLpgSQL_stmt_fetch;
403
404
405 typedef struct
406 {                                                               /* CLOSE curvar                                         */
407         int                     cmd_type;
408         int                     lineno;
409         int                     curvar;
410 }       PLpgSQL_stmt_close;
411
412
413 typedef struct
414 {                                                               /* EXIT statement                       */
415         int                     cmd_type;
416         int                     lineno;
417         char       *label;
418         PLpgSQL_expr *cond;
419 }       PLpgSQL_stmt_exit;
420
421
422 typedef struct
423 {                                                               /* RETURN statement                     */
424         int                     cmd_type;
425         int                     lineno;
426         PLpgSQL_expr *expr;
427         int                     retrecno;
428 }       PLpgSQL_stmt_return;
429
430 typedef struct
431 {                                                               /* RETURN NEXT statement */
432         int                     cmd_type;
433         int                     lineno;
434         PLpgSQL_rec *rec;
435         PLpgSQL_row *row;
436         PLpgSQL_expr *expr;
437 }       PLpgSQL_stmt_return_next;
438
439 typedef struct
440 {                                                               /* RAISE statement                      */
441         int                     cmd_type;
442         int                     lineno;
443         int                     elog_level;
444         char       *message;
445         int                     nparams;
446         int                *params;
447 }       PLpgSQL_stmt_raise;
448
449
450 typedef struct
451 {                                                               /* Generic SQL statement to execute */
452         int                     cmd_type;
453         int                     lineno;
454         PLpgSQL_expr *sqlstmt;
455 }       PLpgSQL_stmt_execsql;
456
457
458 typedef struct
459 {                                                               /* Dynamic SQL string to execute */
460         int                     cmd_type;
461         int                     lineno;
462         PLpgSQL_expr *query;
463 }       PLpgSQL_stmt_dynexecute;
464
465
466 typedef struct PLpgSQL_function
467 {                                                               /* Complete compiled function     */
468         char       *fn_name;
469         Oid                     fn_oid;
470         TransactionId fn_xmin;
471         CommandId       fn_cmin;
472         int                     fn_functype;
473
474         Oid                     fn_rettype;
475         int                     fn_rettyplen;
476         bool            fn_retbyval;
477         FmgrInfo        fn_retinput;
478         Oid                     fn_rettypelem;
479         bool            fn_retistuple;
480         bool            fn_retset;
481
482         int                     fn_nargs;
483         int                     fn_argvarnos[FUNC_MAX_ARGS];
484         int                     found_varno;
485         int                     new_varno;
486         int                     old_varno;
487         int                     tg_name_varno;
488         int                     tg_when_varno;
489         int                     tg_level_varno;
490         int                     tg_op_varno;
491         int                     tg_relid_varno;
492         int                     tg_relname_varno;
493         int                     tg_nargs_varno;
494
495         int                     ndatums;
496         PLpgSQL_datum **datums;
497         PLpgSQL_stmt_block *action;
498
499         struct PLpgSQL_function *next;          /* for chaining list of functions */
500 }       PLpgSQL_function;
501
502
503 typedef struct
504 {                                                               /* Runtime execution data       */
505         Datum           retval;
506         bool            retisnull;
507         Oid                     rettype;                /* type of current retval */
508
509         Oid                     fn_rettype;             /* info about declared function rettype */
510         bool            retistuple;
511         bool            retisset;
512
513         TupleDesc       rettupdesc;
514         char       *exitlabel;
515
516         Tuplestorestate *tuple_store;           /* SRFs accumulate results here */
517         MemoryContext   tuple_store_cxt;
518         ReturnSetInfo *rsi;
519
520         int                     trig_nargs;
521         Datum      *trig_argv;
522
523         int                     found_varno;
524         int                     ndatums;
525         PLpgSQL_datum **datums;
526
527         /* temporary state for results from evaluation of query or expr */
528         SPITupleTable *eval_tuptable;
529         uint32          eval_processed;
530         Oid                     eval_lastoid;
531         ExprContext *eval_econtext;
532 }       PLpgSQL_execstate;
533
534
535 /**********************************************************************
536  * Global variable declarations
537  **********************************************************************/
538
539 extern int      plpgsql_DumpExecTree;
540 extern int      plpgsql_SpaceScanned;
541 extern int      plpgsql_nDatums;
542 extern PLpgSQL_datum **plpgsql_Datums;
543
544 extern int      plpgsql_error_lineno;
545 extern char *plpgsql_error_funcname;
546
547 /* linkage to the real yytext and yylineno variables */
548 extern char *plpgsql_base_yytext;
549
550 #define plpgsql_yytext plpgsql_base_yytext
551 extern int      plpgsql_base_yylineno;
552
553 #define plpgsql_yylineno plpgsql_base_yylineno
554
555 extern PLpgSQL_function *plpgsql_curr_compile;
556
557 /**********************************************************************
558  * Function declarations
559  **********************************************************************/
560
561 /* ----------
562  * Functions in pl_comp.c
563  * ----------
564  */
565 extern PLpgSQL_function *plpgsql_compile(Oid fn_oid, int functype);
566 extern int      plpgsql_parse_word(char *word);
567 extern int      plpgsql_parse_dblword(char *word);
568 extern int      plpgsql_parse_tripword(char *word);
569 extern int      plpgsql_parse_wordtype(char *word);
570 extern int      plpgsql_parse_dblwordtype(char *word);
571 extern int      plpgsql_parse_wordrowtype(char *word);
572 extern PLpgSQL_type *plpgsql_parse_datatype(char *string);
573 extern void plpgsql_adddatum(PLpgSQL_datum * new);
574 extern int      plpgsql_add_initdatums(int **varnos);
575 extern void plpgsql_yyerror(const char *s);
576
577 /* ----------
578  * Functions in pl_handler.c
579  * ----------
580  */
581 extern Datum plpgsql_call_handler(PG_FUNCTION_ARGS);
582
583 /* ----------
584  * Functions in pl_exec.c
585  * ----------
586  */
587 extern Datum plpgsql_exec_function(PLpgSQL_function * func,
588                                           FunctionCallInfo fcinfo);
589 extern HeapTuple plpgsql_exec_trigger(PLpgSQL_function * func,
590                                          TriggerData *trigdata);
591
592
593 /* ----------
594  * Functions for the dynamic string handling in pl_funcs.c
595  * ----------
596  */
597 extern void plpgsql_dstring_init(PLpgSQL_dstring * ds);
598 extern void plpgsql_dstring_free(PLpgSQL_dstring * ds);
599 extern void plpgsql_dstring_append(PLpgSQL_dstring * ds, char *str);
600 extern char *plpgsql_dstring_get(PLpgSQL_dstring * ds);
601
602 /* ----------
603  * Functions for the namestack handling in pl_funcs.c
604  * ----------
605  */
606 extern void plpgsql_ns_init(void);
607 extern bool plpgsql_ns_setlocal(bool flag);
608 extern void plpgsql_ns_push(char *label);
609 extern void plpgsql_ns_pop(void);
610 extern void plpgsql_ns_additem(int itemtype, int itemno, char *name);
611 extern PLpgSQL_nsitem *plpgsql_ns_lookup(char *name, char *nsname);
612 extern void plpgsql_ns_rename(char *oldname, char *newname);
613
614 /* ----------
615  * Other functions in pl_funcs.c
616  * ----------
617  */
618 extern void plpgsql_convert_ident(const char *s, char **output, int numidents);
619 extern const char *plpgsql_stmt_typename(PLpgSQL_stmt * stmt);
620 extern void plpgsql_dumptree(PLpgSQL_function * func);
621
622 /* ----------
623  * Externs in gram.y and scan.l
624  * ----------
625  */
626 extern PLpgSQL_expr *plpgsql_read_expression(int until, const char *expected);
627 extern int      plpgsql_yyparse(void);
628 extern int      plpgsql_base_yylex(void);
629 extern int      plpgsql_yylex(void);
630 extern void plpgsql_push_back_token(int token);
631 extern void plpgsql_setinput(char *s, int functype);
632
633 #endif   /* PLPGSQL_H */