/postgresql-9.1.4/postgresql-9.0.8/src/pl/plpgsql/src/gram.y
Happy | 3373 lines | 2990 code | 383 blank | 0 comment | 0 complexity | 2a981823912cf555170fcdd980a1e755 MD5 | raw file
Possible License(s): AGPL-3.0, LGPL-2.0
Large files files are truncated, but you can click here to view the full file
- %{
- /*-------------------------------------------------------------------------
- *
- * gram.y - Parser for the PL/pgSQL procedural language
- *
- * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
- * Portions Copyright (c) 1994, Regents of the University of California
- *
- *
- * IDENTIFICATION
- * $PostgreSQL: pgsql/src/pl/plpgsql/src/gram.y,v 1.143.2.1 2010/08/19 18:58:04 tgl Exp $
- *
- *-------------------------------------------------------------------------
- */
- #include "plpgsql.h"
- #include "catalog/namespace.h"
- #include "catalog/pg_type.h"
- #include "parser/parser.h"
- #include "parser/parse_type.h"
- #include "parser/scanner.h"
- #include "parser/scansup.h"
- /* Location tracking support --- simpler than bison's default */
- #define YYLLOC_DEFAULT(Current, Rhs, N) \
- do { \
- if (N) \
- (Current) = (Rhs)[1]; \
- else \
- (Current) = (Rhs)[0]; \
- } while (0)
- /*
- * Bison doesn't allocate anything that needs to live across parser calls,
- * so we can easily have it use palloc instead of malloc. This prevents
- * memory leaks if we error out during parsing. Note this only works with
- * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
- * if possible, so there's not really much problem anyhow, at least if
- * you're building with gcc.
- */
- #define YYMALLOC palloc
- #define YYFREE pfree
- typedef struct
- {
- int location;
- int leaderlen;
- } sql_error_callback_arg;
- #define parser_errposition(pos) plpgsql_scanner_errposition(pos)
- union YYSTYPE; /* need forward reference for tok_is_keyword */
- static bool tok_is_keyword(int token, union YYSTYPE *lval,
- int kw_token, const char *kw_str);
- static void word_is_not_variable(PLword *word, int location);
- static void cword_is_not_variable(PLcword *cword, int location);
- static void current_token_is_not_variable(int tok);
- static PLpgSQL_expr *read_sql_construct(int until,
- int until2,
- int until3,
- const char *expected,
- const char *sqlstart,
- bool isexpression,
- bool valid_sql,
- int *startloc,
- int *endtoken);
- static PLpgSQL_expr *read_sql_expression(int until,
- const char *expected);
- static PLpgSQL_expr *read_sql_expression2(int until, int until2,
- const char *expected,
- int *endtoken);
- static PLpgSQL_expr *read_sql_stmt(const char *sqlstart);
- static PLpgSQL_type *read_datatype(int tok);
- static PLpgSQL_stmt *make_execsql_stmt(int firsttoken, int location);
- static PLpgSQL_stmt_fetch *read_fetch_direction(void);
- static void complete_direction(PLpgSQL_stmt_fetch *fetch,
- bool *check_FROM);
- static PLpgSQL_stmt *make_return_stmt(int location);
- static PLpgSQL_stmt *make_return_next_stmt(int location);
- static PLpgSQL_stmt *make_return_query_stmt(int location);
- static PLpgSQL_stmt *make_case(int location, PLpgSQL_expr *t_expr,
- List *case_when_list, List *else_stmts);
- static char *NameOfDatum(PLwdatum *wdatum);
- static void check_assignable(PLpgSQL_datum *datum, int location);
- static void read_into_target(PLpgSQL_rec **rec, PLpgSQL_row **row,
- bool *strict);
- static PLpgSQL_row *read_into_scalar_list(char *initial_name,
- PLpgSQL_datum *initial_datum,
- int initial_location);
- static PLpgSQL_row *make_scalar_list1(char *initial_name,
- PLpgSQL_datum *initial_datum,
- int lineno, int location);
- static void check_sql_expr(const char *stmt, int location,
- int leaderlen);
- static void plpgsql_sql_error_callback(void *arg);
- static PLpgSQL_type *parse_datatype(const char *string, int location);
- static void check_labels(const char *start_label,
- const char *end_label,
- int end_location);
- static PLpgSQL_expr *read_cursor_args(PLpgSQL_var *cursor,
- int until, const char *expected);
- static List *read_raise_options(void);
- %}
- %expect 0
- %name-prefix="plpgsql_yy"
- %locations
- %union {
- core_YYSTYPE core_yystype;
- /* these fields must match core_YYSTYPE: */
- int ival;
- char *str;
- const char *keyword;
- PLword word;
- PLcword cword;
- PLwdatum wdatum;
- bool boolean;
- struct
- {
- char *name;
- int lineno;
- } varname;
- struct
- {
- char *name;
- int lineno;
- PLpgSQL_datum *scalar;
- PLpgSQL_rec *rec;
- PLpgSQL_row *row;
- } forvariable;
- struct
- {
- char *label;
- int n_initvars;
- int *initvarnos;
- } declhdr;
- struct
- {
- List *stmts;
- char *end_label;
- int end_label_location;
- } loop_body;
- List *list;
- PLpgSQL_type *dtype;
- PLpgSQL_datum *datum;
- PLpgSQL_var *var;
- PLpgSQL_expr *expr;
- PLpgSQL_stmt *stmt;
- PLpgSQL_condition *condition;
- PLpgSQL_exception *exception;
- PLpgSQL_exception_block *exception_block;
- PLpgSQL_nsitem *nsitem;
- PLpgSQL_diag_item *diagitem;
- PLpgSQL_stmt_fetch *fetch;
- PLpgSQL_case_when *casewhen;
- }
- %type <declhdr> decl_sect
- %type <varname> decl_varname
- %type <boolean> decl_const decl_notnull exit_type
- %type <expr> decl_defval decl_cursor_query
- %type <dtype> decl_datatype
- %type <datum> decl_cursor_args
- %type <list> decl_cursor_arglist
- %type <nsitem> decl_aliasitem
- %type <expr> expr_until_semi expr_until_rightbracket
- %type <expr> expr_until_then expr_until_loop opt_expr_until_when
- %type <expr> opt_exitcond
- %type <ival> assign_var
- %type <var> cursor_variable
- %type <datum> decl_cursor_arg
- %type <forvariable> for_variable
- %type <stmt> for_control
- %type <str> any_identifier opt_block_label opt_label
- %type <list> proc_sect proc_stmts stmt_else
- %type <loop_body> loop_body
- %type <stmt> proc_stmt pl_block
- %type <stmt> stmt_assign stmt_if stmt_loop stmt_while stmt_exit
- %type <stmt> stmt_return stmt_raise stmt_execsql
- %type <stmt> stmt_dynexecute stmt_for stmt_perform stmt_getdiag
- %type <stmt> stmt_open stmt_fetch stmt_move stmt_close stmt_null
- %type <stmt> stmt_case
- %type <list> proc_exceptions
- %type <exception_block> exception_sect
- %type <exception> proc_exception
- %type <condition> proc_conditions proc_condition
- %type <casewhen> case_when
- %type <list> case_when_list opt_case_else
- %type <list> getdiag_list
- %type <diagitem> getdiag_list_item
- %type <ival> getdiag_item getdiag_target
- %type <ival> opt_scrollable
- %type <fetch> opt_fetch_direction
- %type <keyword> unreserved_keyword
- /*
- * Basic non-keyword token types. These are hard-wired into the core lexer.
- * They must be listed first so that their numeric codes do not depend on
- * the set of keywords. Keep this list in sync with backend/parser/gram.y!
- *
- * Some of these are not directly referenced in this file, but they must be
- * here anyway.
- */
- %token <str> IDENT FCONST SCONST BCONST XCONST Op
- %token <ival> ICONST PARAM
- %token TYPECAST DOT_DOT COLON_EQUALS
- /*
- * Other tokens recognized by plpgsql's lexer interface layer (pl_scanner.c).
- */
- %token <word> T_WORD /* unrecognized simple identifier */
- %token <cword> T_CWORD /* unrecognized composite identifier */
- %token <wdatum> T_DATUM /* a VAR, ROW, REC, or RECFIELD variable */
- %token LESS_LESS
- %token GREATER_GREATER
- /*
- * Keyword tokens. Some of these are reserved and some are not;
- * see pl_scanner.c for info. Be sure unreserved keywords are listed
- * in the "unreserved_keyword" production below.
- */
- %token <keyword> K_ABSOLUTE
- %token <keyword> K_ALIAS
- %token <keyword> K_ALL
- %token <keyword> K_BACKWARD
- %token <keyword> K_BEGIN
- %token <keyword> K_BY
- %token <keyword> K_CASE
- %token <keyword> K_CLOSE
- %token <keyword> K_CONSTANT
- %token <keyword> K_CONTINUE
- %token <keyword> K_CURSOR
- %token <keyword> K_DEBUG
- %token <keyword> K_DECLARE
- %token <keyword> K_DEFAULT
- %token <keyword> K_DETAIL
- %token <keyword> K_DIAGNOSTICS
- %token <keyword> K_DUMP
- %token <keyword> K_ELSE
- %token <keyword> K_ELSIF
- %token <keyword> K_END
- %token <keyword> K_ERRCODE
- %token <keyword> K_ERROR
- %token <keyword> K_EXCEPTION
- %token <keyword> K_EXECUTE
- %token <keyword> K_EXIT
- %token <keyword> K_FETCH
- %token <keyword> K_FIRST
- %token <keyword> K_FOR
- %token <keyword> K_FORWARD
- %token <keyword> K_FROM
- %token <keyword> K_GET
- %token <keyword> K_HINT
- %token <keyword> K_IF
- %token <keyword> K_IN
- %token <keyword> K_INFO
- %token <keyword> K_INSERT
- %token <keyword> K_INTO
- %token <keyword> K_IS
- %token <keyword> K_LAST
- %token <keyword> K_LOG
- %token <keyword> K_LOOP
- %token <keyword> K_MESSAGE
- %token <keyword> K_MOVE
- %token <keyword> K_NEXT
- %token <keyword> K_NO
- %token <keyword> K_NOT
- %token <keyword> K_NOTICE
- %token <keyword> K_NULL
- %token <keyword> K_OPEN
- %token <keyword> K_OPTION
- %token <keyword> K_OR
- %token <keyword> K_PERFORM
- %token <keyword> K_PRIOR
- %token <keyword> K_QUERY
- %token <keyword> K_RAISE
- %token <keyword> K_RELATIVE
- %token <keyword> K_RESULT_OID
- %token <keyword> K_RETURN
- %token <keyword> K_REVERSE
- %token <keyword> K_ROWTYPE
- %token <keyword> K_ROW_COUNT
- %token <keyword> K_SCROLL
- %token <keyword> K_SQLSTATE
- %token <keyword> K_STRICT
- %token <keyword> K_THEN
- %token <keyword> K_TO
- %token <keyword> K_TYPE
- %token <keyword> K_USE_COLUMN
- %token <keyword> K_USE_VARIABLE
- %token <keyword> K_USING
- %token <keyword> K_VARIABLE_CONFLICT
- %token <keyword> K_WARNING
- %token <keyword> K_WHEN
- %token <keyword> K_WHILE
- %%
- pl_function : comp_options pl_block opt_semi
- {
- plpgsql_parse_result = (PLpgSQL_stmt_block *) $2;
- }
- ;
- comp_options :
- | comp_options comp_option
- ;
- comp_option : '#' K_OPTION K_DUMP
- {
- plpgsql_DumpExecTree = true;
- }
- | '#' K_VARIABLE_CONFLICT K_ERROR
- {
- plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_ERROR;
- }
- | '#' K_VARIABLE_CONFLICT K_USE_VARIABLE
- {
- plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_VARIABLE;
- }
- | '#' K_VARIABLE_CONFLICT K_USE_COLUMN
- {
- plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_COLUMN;
- }
- ;
- opt_semi :
- | ';'
- ;
- pl_block : decl_sect K_BEGIN proc_sect exception_sect K_END opt_label
- {
- PLpgSQL_stmt_block *new;
- new = palloc0(sizeof(PLpgSQL_stmt_block));
- new->cmd_type = PLPGSQL_STMT_BLOCK;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1.label;
- new->n_initvars = $1.n_initvars;
- new->initvarnos = $1.initvarnos;
- new->body = $3;
- new->exceptions = $4;
- check_labels($1.label, $6, @6);
- plpgsql_ns_pop();
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- decl_sect : opt_block_label
- {
- /* done with decls, so resume identifier lookup */
- plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
- $$.label = $1;
- $$.n_initvars = 0;
- $$.initvarnos = NULL;
- }
- | opt_block_label decl_start
- {
- plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
- $$.label = $1;
- $$.n_initvars = 0;
- $$.initvarnos = NULL;
- }
- | opt_block_label decl_start decl_stmts
- {
- plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
- $$.label = $1;
- /* Remember variables declared in decl_stmts */
- $$.n_initvars = plpgsql_add_initdatums(&($$.initvarnos));
- }
- ;
- decl_start : K_DECLARE
- {
- /* Forget any variables created before block */
- plpgsql_add_initdatums(NULL);
- /*
- * Disable scanner lookup of identifiers while
- * we process the decl_stmts
- */
- plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
- }
- ;
- decl_stmts : decl_stmts decl_stmt
- | decl_stmt
- ;
- decl_stmt : decl_statement
- | K_DECLARE
- {
- /* We allow useless extra DECLAREs */
- }
- | LESS_LESS any_identifier GREATER_GREATER
- {
- /*
- * Throw a helpful error if user tries to put block
- * label just before BEGIN, instead of before DECLARE.
- */
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("block label must be placed before DECLARE, not after"),
- parser_errposition(@1)));
- }
- ;
- decl_statement : decl_varname decl_const decl_datatype decl_notnull decl_defval
- {
- PLpgSQL_variable *var;
- var = plpgsql_build_variable($1.name, $1.lineno,
- $3, true);
- if ($2)
- {
- if (var->dtype == PLPGSQL_DTYPE_VAR)
- ((PLpgSQL_var *) var)->isconst = $2;
- else
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("row or record variable cannot be CONSTANT"),
- parser_errposition(@2)));
- }
- if ($4)
- {
- if (var->dtype == PLPGSQL_DTYPE_VAR)
- ((PLpgSQL_var *) var)->notnull = $4;
- else
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("row or record variable cannot be NOT NULL"),
- parser_errposition(@4)));
- }
- if ($5 != NULL)
- {
- if (var->dtype == PLPGSQL_DTYPE_VAR)
- ((PLpgSQL_var *) var)->default_val = $5;
- else
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("default value for row or record variable is not supported"),
- parser_errposition(@5)));
- }
- }
- | decl_varname K_ALIAS K_FOR decl_aliasitem ';'
- {
- plpgsql_ns_additem($4->itemtype,
- $4->itemno, $1.name);
- }
- | decl_varname opt_scrollable K_CURSOR
- { plpgsql_ns_push($1.name); }
- decl_cursor_args decl_is_for decl_cursor_query
- {
- PLpgSQL_var *new;
- PLpgSQL_expr *curname_def;
- char buf[1024];
- char *cp1;
- char *cp2;
- /* pop local namespace for cursor args */
- plpgsql_ns_pop();
- new = (PLpgSQL_var *)
- plpgsql_build_variable($1.name, $1.lineno,
- plpgsql_build_datatype(REFCURSOROID,
- -1),
- true);
- curname_def = palloc0(sizeof(PLpgSQL_expr));
- curname_def->dtype = PLPGSQL_DTYPE_EXPR;
- strcpy(buf, "SELECT ");
- cp1 = new->refname;
- cp2 = buf + strlen(buf);
- /*
- * Don't trust standard_conforming_strings here;
- * it might change before we use the string.
- */
- if (strchr(cp1, '\\') != NULL)
- *cp2++ = ESCAPE_STRING_SYNTAX;
- *cp2++ = '\'';
- while (*cp1)
- {
- if (SQL_STR_DOUBLE(*cp1, true))
- *cp2++ = *cp1;
- *cp2++ = *cp1++;
- }
- strcpy(cp2, "'::pg_catalog.refcursor");
- curname_def->query = pstrdup(buf);
- new->default_val = curname_def;
- new->cursor_explicit_expr = $7;
- if ($5 == NULL)
- new->cursor_explicit_argrow = -1;
- else
- new->cursor_explicit_argrow = $5->dno;
- new->cursor_options = CURSOR_OPT_FAST_PLAN | $2;
- }
- ;
- opt_scrollable :
- {
- $$ = 0;
- }
- | K_NO K_SCROLL
- {
- $$ = CURSOR_OPT_NO_SCROLL;
- }
- | K_SCROLL
- {
- $$ = CURSOR_OPT_SCROLL;
- }
- ;
- decl_cursor_query :
- {
- $$ = read_sql_stmt("");
- }
- ;
- decl_cursor_args :
- {
- $$ = NULL;
- }
- | '(' decl_cursor_arglist ')'
- {
- PLpgSQL_row *new;
- int i;
- ListCell *l;
- new = palloc0(sizeof(PLpgSQL_row));
- new->dtype = PLPGSQL_DTYPE_ROW;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->rowtupdesc = NULL;
- new->nfields = list_length($2);
- new->fieldnames = palloc(new->nfields * sizeof(char *));
- new->varnos = palloc(new->nfields * sizeof(int));
- i = 0;
- foreach (l, $2)
- {
- PLpgSQL_variable *arg = (PLpgSQL_variable *) lfirst(l);
- new->fieldnames[i] = arg->refname;
- new->varnos[i] = arg->dno;
- i++;
- }
- list_free($2);
- plpgsql_adddatum((PLpgSQL_datum *) new);
- $$ = (PLpgSQL_datum *) new;
- }
- ;
- decl_cursor_arglist : decl_cursor_arg
- {
- $$ = list_make1($1);
- }
- | decl_cursor_arglist ',' decl_cursor_arg
- {
- $$ = lappend($1, $3);
- }
- ;
- decl_cursor_arg : decl_varname decl_datatype
- {
- $$ = (PLpgSQL_datum *)
- plpgsql_build_variable($1.name, $1.lineno,
- $2, true);
- }
- ;
- decl_is_for : K_IS | /* Oracle */
- K_FOR; /* SQL standard */
- decl_aliasitem : T_WORD
- {
- PLpgSQL_nsitem *nsi;
- nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
- $1.ident, NULL, NULL,
- NULL);
- if (nsi == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_UNDEFINED_OBJECT),
- errmsg("variable \"%s\" does not exist",
- $1.ident),
- parser_errposition(@1)));
- $$ = nsi;
- }
- | T_CWORD
- {
- PLpgSQL_nsitem *nsi;
- if (list_length($1.idents) == 2)
- nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
- strVal(linitial($1.idents)),
- strVal(lsecond($1.idents)),
- NULL,
- NULL);
- else if (list_length($1.idents) == 3)
- nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
- strVal(linitial($1.idents)),
- strVal(lsecond($1.idents)),
- strVal(lthird($1.idents)),
- NULL);
- else
- nsi = NULL;
- if (nsi == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_UNDEFINED_OBJECT),
- errmsg("variable \"%s\" does not exist",
- NameListToString($1.idents)),
- parser_errposition(@1)));
- $$ = nsi;
- }
- ;
- decl_varname : T_WORD
- {
- $$.name = $1.ident;
- $$.lineno = plpgsql_location_to_lineno(@1);
- /*
- * Check to make sure name isn't already declared
- * in the current block.
- */
- if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
- $1.ident, NULL, NULL,
- NULL) != NULL)
- yyerror("duplicate declaration");
- }
- | unreserved_keyword
- {
- $$.name = pstrdup($1);
- $$.lineno = plpgsql_location_to_lineno(@1);
- /*
- * Check to make sure name isn't already declared
- * in the current block.
- */
- if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
- $1, NULL, NULL,
- NULL) != NULL)
- yyerror("duplicate declaration");
- }
- ;
- decl_const :
- { $$ = false; }
- | K_CONSTANT
- { $$ = true; }
- ;
- decl_datatype :
- {
- /*
- * If there's a lookahead token, read_datatype
- * should consume it.
- */
- $$ = read_datatype(yychar);
- yyclearin;
- }
- ;
- decl_notnull :
- { $$ = false; }
- | K_NOT K_NULL
- { $$ = true; }
- ;
- decl_defval : ';'
- { $$ = NULL; }
- | decl_defkey
- {
- $$ = read_sql_expression(';', ";");
- }
- ;
- decl_defkey : assign_operator
- | K_DEFAULT
- ;
- assign_operator : '='
- | COLON_EQUALS
- ;
- proc_sect :
- { $$ = NIL; }
- | proc_stmts
- { $$ = $1; }
- ;
- proc_stmts : proc_stmts proc_stmt
- {
- if ($2 == NULL)
- $$ = $1;
- else
- $$ = lappend($1, $2);
- }
- | proc_stmt
- {
- if ($1 == NULL)
- $$ = NIL;
- else
- $$ = list_make1($1);
- }
- ;
- proc_stmt : pl_block ';'
- { $$ = $1; }
- | stmt_assign
- { $$ = $1; }
- | stmt_if
- { $$ = $1; }
- | stmt_case
- { $$ = $1; }
- | stmt_loop
- { $$ = $1; }
- | stmt_while
- { $$ = $1; }
- | stmt_for
- { $$ = $1; }
- | stmt_exit
- { $$ = $1; }
- | stmt_return
- { $$ = $1; }
- | stmt_raise
- { $$ = $1; }
- | stmt_execsql
- { $$ = $1; }
- | stmt_dynexecute
- { $$ = $1; }
- | stmt_perform
- { $$ = $1; }
- | stmt_getdiag
- { $$ = $1; }
- | stmt_open
- { $$ = $1; }
- | stmt_fetch
- { $$ = $1; }
- | stmt_move
- { $$ = $1; }
- | stmt_close
- { $$ = $1; }
- | stmt_null
- { $$ = $1; }
- ;
- stmt_perform : K_PERFORM expr_until_semi
- {
- PLpgSQL_stmt_perform *new;
- new = palloc0(sizeof(PLpgSQL_stmt_perform));
- new->cmd_type = PLPGSQL_STMT_PERFORM;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->expr = $2;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_assign : assign_var assign_operator expr_until_semi
- {
- PLpgSQL_stmt_assign *new;
- new = palloc0(sizeof(PLpgSQL_stmt_assign));
- new->cmd_type = PLPGSQL_STMT_ASSIGN;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->varno = $1;
- new->expr = $3;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_getdiag : K_GET K_DIAGNOSTICS getdiag_list ';'
- {
- PLpgSQL_stmt_getdiag *new;
- new = palloc0(sizeof(PLpgSQL_stmt_getdiag));
- new->cmd_type = PLPGSQL_STMT_GETDIAG;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->diag_items = $3;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- getdiag_list : getdiag_list ',' getdiag_list_item
- {
- $$ = lappend($1, $3);
- }
- | getdiag_list_item
- {
- $$ = list_make1($1);
- }
- ;
- getdiag_list_item : getdiag_target assign_operator getdiag_item
- {
- PLpgSQL_diag_item *new;
- new = palloc(sizeof(PLpgSQL_diag_item));
- new->target = $1;
- new->kind = $3;
- $$ = new;
- }
- ;
- getdiag_item :
- {
- int tok = yylex();
- if (tok_is_keyword(tok, &yylval,
- K_ROW_COUNT, "row_count"))
- $$ = PLPGSQL_GETDIAG_ROW_COUNT;
- else if (tok_is_keyword(tok, &yylval,
- K_RESULT_OID, "result_oid"))
- $$ = PLPGSQL_GETDIAG_RESULT_OID;
- else
- yyerror("unrecognized GET DIAGNOSTICS item");
- }
- ;
- getdiag_target : T_DATUM
- {
- check_assignable($1.datum, @1);
- if ($1.datum->dtype == PLPGSQL_DTYPE_ROW ||
- $1.datum->dtype == PLPGSQL_DTYPE_REC)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("\"%s\" is not a scalar variable",
- NameOfDatum(&($1))),
- parser_errposition(@1)));
- $$ = $1.datum->dno;
- }
- | T_WORD
- {
- /* just to give a better message than "syntax error" */
- word_is_not_variable(&($1), @1);
- }
- | T_CWORD
- {
- /* just to give a better message than "syntax error" */
- cword_is_not_variable(&($1), @1);
- }
- ;
- assign_var : T_DATUM
- {
- check_assignable($1.datum, @1);
- $$ = $1.datum->dno;
- }
- | assign_var '[' expr_until_rightbracket
- {
- PLpgSQL_arrayelem *new;
- new = palloc0(sizeof(PLpgSQL_arrayelem));
- new->dtype = PLPGSQL_DTYPE_ARRAYELEM;
- new->subscript = $3;
- new->arrayparentno = $1;
- plpgsql_adddatum((PLpgSQL_datum *) new);
- $$ = new->dno;
- }
- ;
- stmt_if : K_IF expr_until_then proc_sect stmt_else K_END K_IF ';'
- {
- PLpgSQL_stmt_if *new;
- new = palloc0(sizeof(PLpgSQL_stmt_if));
- new->cmd_type = PLPGSQL_STMT_IF;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->cond = $2;
- new->true_body = $3;
- new->false_body = $4;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_else :
- {
- $$ = NIL;
- }
- | K_ELSIF expr_until_then proc_sect stmt_else
- {
- /*----------
- * Translate the structure: into:
- *
- * IF c1 THEN IF c1 THEN
- * ... ...
- * ELSIF c2 THEN ELSE
- * IF c2 THEN
- * ... ...
- * ELSE ELSE
- * ... ...
- * END IF END IF
- * END IF
- *----------
- */
- PLpgSQL_stmt_if *new_if;
- /* first create a new if-statement */
- new_if = palloc0(sizeof(PLpgSQL_stmt_if));
- new_if->cmd_type = PLPGSQL_STMT_IF;
- new_if->lineno = plpgsql_location_to_lineno(@1);
- new_if->cond = $2;
- new_if->true_body = $3;
- new_if->false_body = $4;
- /* wrap the if-statement in a "container" list */
- $$ = list_make1(new_if);
- }
- | K_ELSE proc_sect
- {
- $$ = $2;
- }
- ;
- stmt_case : K_CASE opt_expr_until_when case_when_list opt_case_else K_END K_CASE ';'
- {
- $$ = make_case(@1, $2, $3, $4);
- }
- ;
- opt_expr_until_when :
- {
- PLpgSQL_expr *expr = NULL;
- int tok = yylex();
- if (tok != K_WHEN)
- {
- plpgsql_push_back_token(tok);
- expr = read_sql_expression(K_WHEN, "WHEN");
- }
- plpgsql_push_back_token(K_WHEN);
- $$ = expr;
- }
- ;
- case_when_list : case_when_list case_when
- {
- $$ = lappend($1, $2);
- }
- | case_when
- {
- $$ = list_make1($1);
- }
- ;
- case_when : K_WHEN expr_until_then proc_sect
- {
- PLpgSQL_case_when *new = palloc(sizeof(PLpgSQL_case_when));
- new->lineno = plpgsql_location_to_lineno(@1);
- new->expr = $2;
- new->stmts = $3;
- $$ = new;
- }
- ;
- opt_case_else :
- {
- $$ = NIL;
- }
- | K_ELSE proc_sect
- {
- /*
- * proc_sect could return an empty list, but we
- * must distinguish that from not having ELSE at all.
- * Simplest fix is to return a list with one NULL
- * pointer, which make_case() must take care of.
- */
- if ($2 != NIL)
- $$ = $2;
- else
- $$ = list_make1(NULL);
- }
- ;
- stmt_loop : opt_block_label K_LOOP loop_body
- {
- PLpgSQL_stmt_loop *new;
- new = palloc0(sizeof(PLpgSQL_stmt_loop));
- new->cmd_type = PLPGSQL_STMT_LOOP;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1;
- new->body = $3.stmts;
- check_labels($1, $3.end_label, $3.end_label_location);
- plpgsql_ns_pop();
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_while : opt_block_label K_WHILE expr_until_loop loop_body
- {
- PLpgSQL_stmt_while *new;
- new = palloc0(sizeof(PLpgSQL_stmt_while));
- new->cmd_type = PLPGSQL_STMT_WHILE;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1;
- new->cond = $3;
- new->body = $4.stmts;
- check_labels($1, $4.end_label, $4.end_label_location);
- plpgsql_ns_pop();
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_for : opt_block_label K_FOR for_control loop_body
- {
- /* This runs after we've scanned the loop body */
- if ($3->cmd_type == PLPGSQL_STMT_FORI)
- {
- PLpgSQL_stmt_fori *new;
- new = (PLpgSQL_stmt_fori *) $3;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1;
- new->body = $4.stmts;
- $$ = (PLpgSQL_stmt *) new;
- }
- else
- {
- PLpgSQL_stmt_forq *new;
- Assert($3->cmd_type == PLPGSQL_STMT_FORS ||
- $3->cmd_type == PLPGSQL_STMT_FORC ||
- $3->cmd_type == PLPGSQL_STMT_DYNFORS);
- /* forq is the common supertype of all three */
- new = (PLpgSQL_stmt_forq *) $3;
- new->lineno = plpgsql_location_to_lineno(@2);
- new->label = $1;
- new->body = $4.stmts;
- $$ = (PLpgSQL_stmt *) new;
- }
- check_labels($1, $4.end_label, $4.end_label_location);
- /* close namespace started in opt_block_label */
- plpgsql_ns_pop();
- }
- ;
- for_control : for_variable K_IN
- {
- int tok = yylex();
- int tokloc = yylloc;
- if (tok == K_EXECUTE)
- {
- /* EXECUTE means it's a dynamic FOR loop */
- PLpgSQL_stmt_dynfors *new;
- PLpgSQL_expr *expr;
- int term;
- expr = read_sql_expression2(K_LOOP, K_USING,
- "LOOP or USING",
- &term);
- new = palloc0(sizeof(PLpgSQL_stmt_dynfors));
- new->cmd_type = PLPGSQL_STMT_DYNFORS;
- if ($1.rec)
- {
- new->rec = $1.rec;
- check_assignable((PLpgSQL_datum *) new->rec, @1);
- }
- else if ($1.row)
- {
- new->row = $1.row;
- check_assignable((PLpgSQL_datum *) new->row, @1);
- }
- else if ($1.scalar)
- {
- /* convert single scalar to list */
- new->row = make_scalar_list1($1.name, $1.scalar,
- $1.lineno, @1);
- /* no need for check_assignable */
- }
- else
- {
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("loop variable of loop over rows must be a record or row variable or list of scalar variables"),
- parser_errposition(@1)));
- }
- new->query = expr;
- if (term == K_USING)
- {
- do
- {
- expr = read_sql_expression2(',', K_LOOP,
- ", or LOOP",
- &term);
- new->params = lappend(new->params, expr);
- } while (term == ',');
- }
- $$ = (PLpgSQL_stmt *) new;
- }
- else if (tok == T_DATUM &&
- yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_VAR &&
- ((PLpgSQL_var *) yylval.wdatum.datum)->datatype->typoid == REFCURSOROID)
- {
- /* It's FOR var IN cursor */
- PLpgSQL_stmt_forc *new;
- PLpgSQL_var *cursor = (PLpgSQL_var *) yylval.wdatum.datum;
- new = (PLpgSQL_stmt_forc *) palloc0(sizeof(PLpgSQL_stmt_forc));
- new->cmd_type = PLPGSQL_STMT_FORC;
- new->curvar = cursor->dno;
- /* Should have had a single variable name */
- if ($1.scalar && $1.row)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("cursor FOR loop must have only one target variable"),
- parser_errposition(@1)));
- /* can't use an unbound cursor this way */
- if (cursor->cursor_explicit_expr == NULL)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("cursor FOR loop must use a bound cursor variable"),
- parser_errposition(tokloc)));
- /* collect cursor's parameters if any */
- new->argquery = read_cursor_args(cursor,
- K_LOOP,
- "LOOP");
- /* create loop's private RECORD variable */
- new->rec = plpgsql_build_record($1.name,
- $1.lineno,
- true);
- $$ = (PLpgSQL_stmt *) new;
- }
- else
- {
- PLpgSQL_expr *expr1;
- int expr1loc;
- bool reverse = false;
- /*
- * We have to distinguish between two
- * alternatives: FOR var IN a .. b and FOR
- * var IN query. Unfortunately this is
- * tricky, since the query in the second
- * form needn't start with a SELECT
- * keyword. We use the ugly hack of
- * looking for two periods after the first
- * token. We also check for the REVERSE
- * keyword, which means it must be an
- * integer loop.
- */
- if (tok_is_keyword(tok, &yylval,
- K_REVERSE, "reverse"))
- reverse = true;
- else
- plpgsql_push_back_token(tok);
- /*
- * Read tokens until we see either a ".."
- * or a LOOP. The text we read may not
- * necessarily be a well-formed SQL
- * statement, so we need to invoke
- * read_sql_construct directly.
- */
- expr1 = read_sql_construct(DOT_DOT,
- K_LOOP,
- 0,
- "LOOP",
- "SELECT ",
- true,
- false,
- &expr1loc,
- &tok);
- if (tok == DOT_DOT)
- {
- /* Saw "..", so it must be an integer loop */
- PLpgSQL_expr *expr2;
- PLpgSQL_expr *expr_by;
- PLpgSQL_var *fvar;
- PLpgSQL_stmt_fori *new;
- /* Check first expression is well-formed */
- check_sql_expr(expr1->query, expr1loc, 7);
- /* Read and check the second one */
- expr2 = read_sql_expression2(K_LOOP, K_BY,
- "LOOP",
- &tok);
- /* Get the BY clause if any */
- if (tok == K_BY)
- expr_by = read_sql_expression(K_LOOP,
- "LOOP");
- else
- expr_by = NULL;
- /* Should have had a single variable name */
- if ($1.scalar && $1.row)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("integer FOR loop must have only one target variable"),
- parser_errposition(@1)));
- /* create loop's private variable */
- fvar = (PLpgSQL_var *)
- plpgsql_build_variable($1.name,
- $1.lineno,
- plpgsql_build_datatype(INT4OID,
- -1),
- true);
- new = palloc0(sizeof(PLpgSQL_stmt_fori));
- new->cmd_type = PLPGSQL_STMT_FORI;
- new->var = fvar;
- new->reverse = reverse;
- new->lower = expr1;
- new->upper = expr2;
- new->step = expr_by;
- $$ = (PLpgSQL_stmt *) new;
- }
- else
- {
- /*
- * No "..", so it must be a query loop. We've
- * prefixed an extra SELECT to the query text,
- * so we need to remove that before performing
- * syntax checking.
- */
- char *tmp_query;
- PLpgSQL_stmt_fors *new;
- if (reverse)
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("cannot specify REVERSE in query FOR loop"),
- parser_errposition(tokloc)));
- Assert(strncmp(expr1->query, "SELECT ", 7) == 0);
- tmp_query = pstrdup(expr1->query + 7);
- pfree(expr1->query);
- expr1->query = tmp_query;
- check_sql_expr(expr1->query, expr1loc, 0);
- new = palloc0(sizeof(PLpgSQL_stmt_fors));
- new->cmd_type = PLPGSQL_STMT_FORS;
- if ($1.rec)
- {
- new->rec = $1.rec;
- check_assignable((PLpgSQL_datum *) new->rec, @1);
- }
- else if ($1.row)
- {
- new->row = $1.row;
- check_assignable((PLpgSQL_datum *) new->row, @1);
- }
- else if ($1.scalar)
- {
- /* convert single scalar to list */
- new->row = make_scalar_list1($1.name, $1.scalar,
- $1.lineno, @1);
- /* no need for check_assignable */
- }
- else
- {
- ereport(ERROR,
- (errcode(ERRCODE_SYNTAX_ERROR),
- errmsg("loop variable of loop over rows must be a record or row variable or list of scalar variables"),
- parser_errposition(@1)));
- }
- new->query = expr1;
- $$ = (PLpgSQL_stmt *) new;
- }
- }
- }
- ;
- /*
- * Processing the for_variable is tricky because we don't yet know if the
- * FOR is an integer FOR loop or a loop over query results. In the former
- * case, the variable is just a name that we must instantiate as a loop
- * local variable, regardless of any other definition it might have.
- * Therefore, we always save the actual identifier into $$.name where it
- * can be used for that case. We also save the outer-variable definition,
- * if any, because that's what we need for the loop-over-query case. Note
- * that we must NOT apply check_assignable() or any other semantic check
- * until we know what's what.
- *
- * However, if we see a comma-separated list of names, we know that it
- * can't be an integer FOR loop and so it's OK to check the variables
- * immediately. In particular, for T_WORD followed by comma, we should
- * complain that the name is not known rather than say it's a syntax error.
- * Note that the non-error result of this case sets *both* $$.scalar and
- * $$.row; see the for_control production.
- */
- for_variable : T_DATUM
- {
- $$.name = NameOfDatum(&($1));
- $$.lineno = plpgsql_location_to_lineno(@1);
- if ($1.datum->dtype == PLPGSQL_DTYPE_ROW)
- {
- $$.scalar = NULL;
- $$.rec = NULL;
- $$.row = (PLpgSQL_row *) $1.datum;
- }
- else if ($1.datum->dtype == PLPGSQL_DTYPE_REC)
- {
- $$.scalar = NULL;
- $$.rec = (PLpgSQL_rec *) $1.datum;
- $$.row = NULL;
- }
- else
- {
- int tok;
- $$.scalar = $1.datum;
- $$.rec = NULL;
- $$.row = NULL;
- /* check for comma-separated list */
- tok = yylex();
- plpgsql_push_back_token(tok);
- if (tok == ',')
- $$.row = read_into_scalar_list($$.name,
- $$.scalar,
- @1);
- }
- }
- | T_WORD
- {
- int tok;
- $$.name = $1.ident;
- $$.lineno = plpgsql_location_to_lineno(@1);
- $$.scalar = NULL;
- $$.rec = NULL;
- $$.row = NULL;
- /* check for comma-separated list */
- tok = yylex();
- plpgsql_push_back_token(tok);
- if (tok == ',')
- word_is_not_variable(&($1), @1);
- }
- | T_CWORD
- {
- /* just to give a better message than "syntax error" */
- cword_is_not_variable(&($1), @1);
- }
- ;
- stmt_exit : exit_type opt_label opt_exitcond
- {
- PLpgSQL_stmt_exit *new;
- new = palloc0(sizeof(PLpgSQL_stmt_exit));
- new->cmd_type = PLPGSQL_STMT_EXIT;
- new->is_exit = $1;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->label = $2;
- new->cond = $3;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- exit_type : K_EXIT
- {
- $$ = true;
- }
- | K_CONTINUE
- {
- $$ = false;
- }
- ;
- stmt_return : K_RETURN
- {
- int tok;
- tok = yylex();
- if (tok == 0)
- yyerror("unexpected end of function definition");
- if (tok_is_keyword(tok, &yylval,
- K_NEXT, "next"))
- {
- $$ = make_return_next_stmt(@1);
- }
- else if (tok_is_keyword(tok, &yylval,
- K_QUERY, "query"))
- {
- $$ = make_return_query_stmt(@1);
- }
- else
- {
- plpgsql_push_back_token(tok);
- $$ = make_return_stmt(@1);
- }
- }
- ;
- stmt_raise : K_RAISE
- {
- PLpgSQL_stmt_raise *new;
- int tok;
- new = palloc(sizeof(PLpgSQL_stmt_raise));
- new->cmd_type = PLPGSQL_STMT_RAISE;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->elog_level = ERROR; /* default */
- new->condname = NULL;
- new->message = NULL;
- new->params = NIL;
- new->options = NIL;
- tok = yylex();
- if (tok == 0)
- yyerror("unexpected end of function definition");
- /*
- * We could have just RAISE, meaning to re-throw
- * the current error.
- */
- if (tok != ';')
- {
- /*
- * First is an optional elog severity level.
- */
- if (tok_is_keyword(tok, &yylval,
- K_EXCEPTION, "exception"))
- {
- new->elog_level = ERROR;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_WARNING, "warning"))
- {
- new->elog_level = WARNING;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_NOTICE, "notice"))
- {
- new->elog_level = NOTICE;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_INFO, "info"))
- {
- new->elog_level = INFO;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_LOG, "log"))
- {
- new->elog_level = LOG;
- tok = yylex();
- }
- else if (tok_is_keyword(tok, &yylval,
- K_DEBUG, "debug"))
- {
- new->elog_level = DEBUG1;
- tok = yylex();
- }
- if (tok == 0)
- yyerror("unexpected end of function definition");
- /*
- * Next we can have a condition name, or
- * equivalently SQLSTATE 'xxxxx', or a string
- * literal that is the old-style message format,
- * or USING to start the option list immediately.
- */
- if (tok == SCONST)
- {
- /* old style message and parameters */
- new->message = yylval.str;
- /*
- * We expect either a semi-colon, which
- * indicates no parameters, or a comma that
- * begins the list of parameter expressions,
- * or USING to begin the options list.
- */
- tok = yylex();
- if (tok != ',' && tok != ';' && tok != K_USING)
- yyerror("syntax error");
- while (tok == ',')
- {
- PLpgSQL_expr *expr;
- expr = read_sql_construct(',', ';', K_USING,
- ", or ; or USING",
- "SELECT ",
- true, true,
- NULL, &tok);
- new->params = lappend(new->params, expr);
- }
- }
- else if (tok != K_USING)
- {
- /* must be condition name or SQLSTATE */
- if (tok_is_keyword(tok, &yylval,
- K_SQLSTATE, "sqlstate"))
- {
- /* next token should be a string literal */
- char *sqlstatestr;
- if (yylex() != SCONST)
- yyerror("syntax error");
- sqlstatestr = yylval.str;
- if (strlen(sqlstatestr) != 5)
- yyerror("invalid SQLSTATE code");
- if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
- yyerror("invalid SQLSTATE code");
- new->condname = sqlstatestr;
- }
- else
- {
- if (tok != T_WORD)
- yyerror("syntax error");
- new->condname = yylval.word.ident;
- plpgsql_recognize_err_condition(new->condname,
- false);
- }
- tok = yylex();
- if (tok != ';' && tok != K_USING)
- yyerror("syntax error");
- }
- if (tok == K_USING)
- new->options = read_raise_options();
- }
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- loop_body : proc_sect K_END K_LOOP opt_label ';'
- {
- $$.stmts = $1;
- $$.end_label = $4;
- $$.end_label_location = @4;
- }
- ;
- /*
- * T_WORD+T_CWORD match any initial identifier that is not a known plpgsql
- * variable. (The composite case is probably a syntax error, but we'll let
- * the core parser decide that.) Normally, we should assume that such a
- * word is a SQL statement keyword that isn't also a plpgsql keyword.
- * However, if the next token is assignment or '[', it can't be a valid
- * SQL statement, and what we're probably looking at is an intended variable
- * assignment. Give an appropriate complaint for that, instead of letting
- * the core parser throw an unhelpful "syntax error".
- */
- stmt_execsql : K_INSERT
- {
- $$ = make_execsql_stmt(K_INSERT, @1);
- }
- | T_WORD
- {
- int tok;
- tok = yylex();
- plpgsql_push_back_token(tok);
- if (tok == '=' || tok == COLON_EQUALS || tok == '[')
- word_is_not_variable(&($1), @1);
- $$ = make_execsql_stmt(T_WORD, @1);
- }
- | T_CWORD
- {
- int tok;
- tok = yylex();
- plpgsql_push_back_token(tok);
- if (tok == '=' || tok == COLON_EQUALS || tok == '[')
- cword_is_not_variable(&($1), @1);
- $$ = make_execsql_stmt(T_CWORD, @1);
- }
- ;
- stmt_dynexecute : K_EXECUTE
- {
- PLpgSQL_stmt_dynexecute *new;
- PLpgSQL_expr *expr;
- int endtoken;
- expr = read_sql_construct(K_INTO, K_USING, ';',
- "INTO or USING or ;",
- "SELECT ",
- true, true,
- NULL, &endtoken);
- new = palloc(sizeof(PLpgSQL_stmt_dynexecute));
- new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->query = expr;
- new->into = false;
- new->strict = false;
- new->rec = NULL;
- new->row = NULL;
- new->params = NIL;
- /*
- * We loop to allow the INTO and USING clauses to
- * appear in either order, since people easily get
- * that wrong. This coding also prevents "INTO foo"
- * from getting absorbed into a USING expression,
- * which is *really* confusing.
- */
- for (;;)
- {
- if (endtoken == K_INTO)
- {
- if (new->into) /* multiple INTO */
- yyerror("syntax error");
- new->into = true;
- read_into_target(&new->rec, &new->row, &new->strict);
- endtoken = yylex();
- }
- else if (endtoken == K_USING)
- {
- if (new->params) /* multiple USING */
- yyerror("syntax error");
- do
- {
- expr = read_sql_construct(',', ';', K_INTO,
- ", or ; or INTO",
- "SELECT ",
- true, true,
- NULL, &endtoken);
- new->params = lappend(new->params, expr);
- } while (endtoken == ',');
- }
- else if (endtoken == ';')
- break;
- else
- yyerror("syntax error");
- }
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_open : K_OPEN cursor_variable
- {
- PLpgSQL_stmt_open *new;
- int tok;
- new = palloc0(sizeof(PLpgSQL_stmt_open));
- new->cmd_type = PLPGSQL_STMT_OPEN;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->curvar = $2->dno;
- new->cursor_options = CURSOR_OPT_FAST_PLAN;
- if ($2->cursor_explicit_expr == NULL)
- {
- /* be nice if we could use opt_scrollable here */
- tok = yylex();
- if (tok_is_keyword(tok, &yylval,
- K_NO, "no"))
- {
- tok = yylex();
- if (tok_is_keyword(tok, &yylval,
- K_SCROLL, "scroll"))
- {
- new->cursor_options |= CURSOR_OPT_NO_SCROLL;
- tok = yylex();
- }
- }
- else if (tok_is_keyword(tok, &yylval,
- K_SCROLL, "scroll"))
- {
- new->cursor_options |= CURSOR_OPT_SCROLL;
- tok = yylex();
- }
- if (tok != K_FOR)
- yyerror("syntax error, expected \"FOR\"");
- tok = yylex();
- if (tok == K_EXECUTE)
- {
- int endtoken;
- new->dynquery =
- read_sql_expression2(K_USING, ';',
- "USING or ;",
- &endtoken);
- /* If we found "USING", collect argument(s) */
- if (endtoken == K_USING)
- {
- PLpgSQL_expr *expr;
-
- do
- {
- expr = read_sql_expression2(',', ';',
- ", or ;",
- &endtoken);
- new->params = lappend(new->params,
- expr);
- } while (endtoken == ',');
- }
- }
- else
- {
- plpgsql_push_back_token(tok);
- new->query = read_sql_stmt("");
- }
- }
- else
- {
- /* predefined cursor query, so read args */
- new->argquery = read_cursor_args($2, ';', ";");
- }
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_fetch : K_FETCH opt_fetch_direction cursor_variable K_INTO
- {
- PLpgSQL_stmt_fetch *fetch = $2;
- PLpgSQL_rec *rec;
- PLpgSQL_row *row;
- /* We have already parsed everything through the INTO keyword */
- read_into_target(&rec, &row, NULL);
- if (yylex() != ';')
- yyerror("syntax error");
- /*
- * We don't allow multiple rows in PL/pgSQL's FETCH
- * statement, only in MOVE.
- */
- if (fetch->returns_multiple_rows)
- ereport(ERROR,
- (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
- errmsg("FETCH statement cannot return multiple rows"),
- parser_errposition(@1)));
- fetch->lineno = plpgsql_location_to_lineno(@1);
- fetch->rec = rec;
- fetch->row = row;
- fetch->curvar = $3->dno;
- fetch->is_move = false;
- $$ = (PLpgSQL_stmt *)fetch;
- }
- ;
- stmt_move : K_MOVE opt_fetch_direction cursor_variable ';'
- {
- PLpgSQL_stmt_fetch *fetch = $2;
- fetch->lineno = plpgsql_location_to_lineno(@1);
- fetch->curvar = $3->dno;
- fetch->is_move = true;
- $$ = (PLpgSQL_stmt *)fetch;
- }
- ;
- opt_fetch_direction :
- {
- $$ = read_fetch_direction();
- }
- ;
- stmt_close : K_CLOSE cursor_variable ';'
- {
- PLpgSQL_stmt_close *new;
- new = palloc(sizeof(PLpgSQL_stmt_close));
- new->cmd_type = PLPGSQL_STMT_CLOSE;
- new->lineno = plpgsql_location_to_lineno(@1);
- new->curvar = $2->dno;
- $$ = (PLpgSQL_stmt *)new;
- }
- ;
- stmt_null : K_NULL ';'
- {
- /* We do not bother building a node for NULL */
- $$ = NULL;
- }
- ;
- cursor_variable : T_DATUM
- {
- if ($1.datum->dtype != PLPGSQL_DTYPE_VAR)
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("cursor variable must be a simple variable"),
- parser_errposition(@1)));
- if (((PLpgSQL_var *) $1.datum)->datatype->typoid != REFCURSOROID)
- ereport(ERROR,
- (errcode(ERRCODE_DATATYPE_MISMATCH),
- errmsg("variable \"%s\" must be of type cursor or refcursor",
- ((PLpgSQL_var *) $1.datum)->refname),
- parser_errposition(@1)));
- $$ = (PLpgSQL_var *) $1.datum;
- }
- | T_WORD
- {
- /* just to give a better message than "syntax error" */
- word_is_not_variable(&($1), @1);
- }
- | T_CWORD
- {
- /* just to give a better message than "syntax error" */
- cword_is_not_variable(&($1), @1);
- }
- ;
- exception_sect :
- { $$ = NULL; }
- | K_EXCEPTION
- {
- /*
- * We use a mid-rule action to add these
- * special variables to the namespace before
- * parsing the WHEN clauses themselves. The
- * scope of the names extends to the end of the
- * current block.
- */
- int lineno = plpgsql_location_to_lineno(@1);
- PLpgSQL_exception_block *new = palloc(sizeof(PLpgSQL_exception_block));
- PLpgSQL_variable *var;
- var = plpgsql_build_variable("sqlstate", lineno,
- plpgsql_build_datatype(TEXTOID, -1),
- true);
- ((PLpgSQL_var *) var)->isconst = true;
- new->sqlstate_varno = var->dno;
- var = plpgsql_build_variable("sqlerrm", lineno,
- plpgsql_build_datatype(TEXTOID, -1),
- true);
- ((PLpgSQL_var *) var)->isconst = true;
- new->sqlerrm_varno = var->dno;
- $<exception_block>$ = new;
- }
- proc_exceptions
- {
- PLpgSQL_exception_block *new = $<exception_block>2;
- new->exc_list = $3;
- $$ = new;
- }
- ;
- proc_exceptions : proc_exceptions proc_exception
- {
- $$ = lappend($1, $2);
- }
- | proc_exception
- {
- $$ = list_make1($1);
- }
- ;
- proc_exception : K_WHEN proc_conditions K_THEN proc_sect
- {
- PLpgSQL_exception *new;
- new = palloc0(sizeof(PLpgSQL_exception));
- new->lineno = plpgsql_location_to_lineno(@1);
- new->conditions = $2;
- new->action = $4;
- $$ = new;
- }
- ;
- proc_conditions : proc_conditions K_OR proc_condition
- {
- PLpgSQL_condition *old;
- for (old = $1; old->next !=…
Large files files are truncated, but you can click here to view the full file