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