PageRenderTime 55ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/src/pl/plpgsql/src/gram.y

http://github.com/postgres/postgres
Happy | 2562 lines | 2293 code | 269 blank | 0 comment | 0 complexity | 74c6e45ac35110d58124d44e5cb2af0e MD5 | raw file
Possible License(s): AGPL-3.0

Large files files are truncated, but you can click here to view the full file

  1. %{
  2. /*-------------------------------------------------------------------------
  3. *
  4. * gram.y - Parser for the PL/pgSQL procedural language
  5. *
  6. * Portions Copyright (c) 1996-2012, PostgreSQL Global Development Group
  7. * Portions Copyright (c) 1994, Regents of the University of California
  8. *
  9. *
  10. * IDENTIFICATION
  11. * src/pl/plpgsql/src/gram.y
  12. *
  13. *-------------------------------------------------------------------------
  14. */
  15. #include "plpgsql.h"
  16. #include "catalog/namespace.h"
  17. #include "catalog/pg_type.h"
  18. #include "parser/parser.h"
  19. #include "parser/parse_type.h"
  20. #include "parser/scanner.h"
  21. #include "parser/scansup.h"
  22. #include "utils/builtins.h"
  23. /* Location tracking support --- simpler than bison's default */
  24. #define YYLLOC_DEFAULT(Current, Rhs, N) \
  25. do { \
  26. if (N) \
  27. (Current) = (Rhs)[1]; \
  28. else \
  29. (Current) = (Rhs)[0]; \
  30. } while (0)
  31. /*
  32. * Bison doesn't allocate anything that needs to live across parser calls,
  33. * so we can easily have it use palloc instead of malloc. This prevents
  34. * memory leaks if we error out during parsing. Note this only works with
  35. * bison >= 2.0. However, in bison 1.875 the default is to use alloca()
  36. * if possible, so there's not really much problem anyhow, at least if
  37. * you're building with gcc.
  38. */
  39. #define YYMALLOC palloc
  40. #define YYFREE pfree
  41. typedef struct
  42. {
  43. int location;
  44. int leaderlen;
  45. } sql_error_callback_arg;
  46. #define parser_errposition(pos) plpgsql_scanner_errposition(pos)
  47. union YYSTYPE; /* need forward reference for tok_is_keyword */
  48. static bool tok_is_keyword(int token, union YYSTYPE *lval,
  49. int kw_token, const char *kw_str);
  50. static void word_is_not_variable(PLword *word, int location);
  51. static void cword_is_not_variable(PLcword *cword, int location);
  52. static void current_token_is_not_variable(int tok);
  53. static PLpgSQL_expr *read_sql_construct(int until,
  54. int until2,
  55. int until3,
  56. const char *expected,
  57. const char *sqlstart,
  58. bool isexpression,
  59. bool valid_sql,
  60. bool trim,
  61. int *startloc,
  62. int *endtoken);
  63. static PLpgSQL_expr *read_sql_expression(int until,
  64. const char *expected);
  65. static PLpgSQL_expr *read_sql_expression2(int until, int until2,
  66. const char *expected,
  67. int *endtoken);
  68. static PLpgSQL_expr *read_sql_stmt(const char *sqlstart);
  69. static PLpgSQL_type *read_datatype(int tok);
  70. static PLpgSQL_stmt *make_execsql_stmt(int firsttoken, int location);
  71. static PLpgSQL_stmt_fetch *read_fetch_direction(void);
  72. static void complete_direction(PLpgSQL_stmt_fetch *fetch,
  73. bool *check_FROM);
  74. static PLpgSQL_stmt *make_return_stmt(int location);
  75. static PLpgSQL_stmt *make_return_next_stmt(int location);
  76. static PLpgSQL_stmt *make_return_query_stmt(int location);
  77. static PLpgSQL_stmt *make_case(int location, PLpgSQL_expr *t_expr,
  78. List *case_when_list, List *else_stmts);
  79. static char *NameOfDatum(PLwdatum *wdatum);
  80. static void check_assignable(PLpgSQL_datum *datum, int location);
  81. static void read_into_target(PLpgSQL_rec **rec, PLpgSQL_row **row,
  82. bool *strict);
  83. static PLpgSQL_row *read_into_scalar_list(char *initial_name,
  84. PLpgSQL_datum *initial_datum,
  85. int initial_location);
  86. static PLpgSQL_row *make_scalar_list1(char *initial_name,
  87. PLpgSQL_datum *initial_datum,
  88. int lineno, int location);
  89. static void check_sql_expr(const char *stmt, int location,
  90. int leaderlen);
  91. static void plpgsql_sql_error_callback(void *arg);
  92. static PLpgSQL_type *parse_datatype(const char *string, int location);
  93. static void check_labels(const char *start_label,
  94. const char *end_label,
  95. int end_location);
  96. static PLpgSQL_expr *read_cursor_args(PLpgSQL_var *cursor,
  97. int until, const char *expected);
  98. static List *read_raise_options(void);
  99. %}
  100. %expect 0
  101. %name-prefix="plpgsql_yy"
  102. %locations
  103. %union {
  104. core_YYSTYPE core_yystype;
  105. /* these fields must match core_YYSTYPE: */
  106. int ival;
  107. char *str;
  108. const char *keyword;
  109. PLword word;
  110. PLcword cword;
  111. PLwdatum wdatum;
  112. bool boolean;
  113. Oid oid;
  114. struct
  115. {
  116. char *name;
  117. int lineno;
  118. } varname;
  119. struct
  120. {
  121. char *name;
  122. int lineno;
  123. PLpgSQL_datum *scalar;
  124. PLpgSQL_rec *rec;
  125. PLpgSQL_row *row;
  126. } forvariable;
  127. struct
  128. {
  129. char *label;
  130. int n_initvars;
  131. int *initvarnos;
  132. } declhdr;
  133. struct
  134. {
  135. List *stmts;
  136. char *end_label;
  137. int end_label_location;
  138. } loop_body;
  139. List *list;
  140. PLpgSQL_type *dtype;
  141. PLpgSQL_datum *datum;
  142. PLpgSQL_var *var;
  143. PLpgSQL_expr *expr;
  144. PLpgSQL_stmt *stmt;
  145. PLpgSQL_condition *condition;
  146. PLpgSQL_exception *exception;
  147. PLpgSQL_exception_block *exception_block;
  148. PLpgSQL_nsitem *nsitem;
  149. PLpgSQL_diag_item *diagitem;
  150. PLpgSQL_stmt_fetch *fetch;
  151. PLpgSQL_case_when *casewhen;
  152. }
  153. %type <declhdr> decl_sect
  154. %type <varname> decl_varname
  155. %type <boolean> decl_const decl_notnull exit_type
  156. %type <expr> decl_defval decl_cursor_query
  157. %type <dtype> decl_datatype
  158. %type <oid> decl_collate
  159. %type <datum> decl_cursor_args
  160. %type <list> decl_cursor_arglist
  161. %type <nsitem> decl_aliasitem
  162. %type <expr> expr_until_semi expr_until_rightbracket
  163. %type <expr> expr_until_then expr_until_loop opt_expr_until_when
  164. %type <expr> opt_exitcond
  165. %type <ival> assign_var foreach_slice
  166. %type <var> cursor_variable
  167. %type <datum> decl_cursor_arg
  168. %type <forvariable> for_variable
  169. %type <stmt> for_control
  170. %type <str> any_identifier opt_block_label opt_label
  171. %type <list> proc_sect proc_stmts stmt_elsifs stmt_else
  172. %type <loop_body> loop_body
  173. %type <stmt> proc_stmt pl_block
  174. %type <stmt> stmt_assign stmt_if stmt_loop stmt_while stmt_exit
  175. %type <stmt> stmt_return stmt_raise stmt_execsql
  176. %type <stmt> stmt_dynexecute stmt_for stmt_perform stmt_getdiag
  177. %type <stmt> stmt_open stmt_fetch stmt_move stmt_close stmt_null
  178. %type <stmt> stmt_case stmt_foreach_a
  179. %type <list> proc_exceptions
  180. %type <exception_block> exception_sect
  181. %type <exception> proc_exception
  182. %type <condition> proc_conditions proc_condition
  183. %type <casewhen> case_when
  184. %type <list> case_when_list opt_case_else
  185. %type <boolean> getdiag_area_opt
  186. %type <list> getdiag_list
  187. %type <diagitem> getdiag_list_item
  188. %type <ival> getdiag_item getdiag_target
  189. %type <ival> opt_scrollable
  190. %type <fetch> opt_fetch_direction
  191. %type <keyword> unreserved_keyword
  192. /*
  193. * Basic non-keyword token types. These are hard-wired into the core lexer.
  194. * They must be listed first so that their numeric codes do not depend on
  195. * the set of keywords. Keep this list in sync with backend/parser/gram.y!
  196. *
  197. * Some of these are not directly referenced in this file, but they must be
  198. * here anyway.
  199. */
  200. %token <str> IDENT FCONST SCONST BCONST XCONST Op
  201. %token <ival> ICONST PARAM
  202. %token TYPECAST DOT_DOT COLON_EQUALS
  203. /*
  204. * Other tokens recognized by plpgsql's lexer interface layer (pl_scanner.c).
  205. */
  206. %token <word> T_WORD /* unrecognized simple identifier */
  207. %token <cword> T_CWORD /* unrecognized composite identifier */
  208. %token <wdatum> T_DATUM /* a VAR, ROW, REC, or RECFIELD variable */
  209. %token LESS_LESS
  210. %token GREATER_GREATER
  211. /*
  212. * Keyword tokens. Some of these are reserved and some are not;
  213. * see pl_scanner.c for info. Be sure unreserved keywords are listed
  214. * in the "unreserved_keyword" production below.
  215. */
  216. %token <keyword> K_ABSOLUTE
  217. %token <keyword> K_ALIAS
  218. %token <keyword> K_ALL
  219. %token <keyword> K_ARRAY
  220. %token <keyword> K_BACKWARD
  221. %token <keyword> K_BEGIN
  222. %token <keyword> K_BY
  223. %token <keyword> K_CASE
  224. %token <keyword> K_CLOSE
  225. %token <keyword> K_COLLATE
  226. %token <keyword> K_CONSTANT
  227. %token <keyword> K_CONTINUE
  228. %token <keyword> K_CURRENT
  229. %token <keyword> K_CURSOR
  230. %token <keyword> K_DEBUG
  231. %token <keyword> K_DECLARE
  232. %token <keyword> K_DEFAULT
  233. %token <keyword> K_DETAIL
  234. %token <keyword> K_DIAGNOSTICS
  235. %token <keyword> K_DUMP
  236. %token <keyword> K_ELSE
  237. %token <keyword> K_ELSIF
  238. %token <keyword> K_END
  239. %token <keyword> K_ERRCODE
  240. %token <keyword> K_ERROR
  241. %token <keyword> K_EXCEPTION
  242. %token <keyword> K_EXECUTE
  243. %token <keyword> K_EXIT
  244. %token <keyword> K_FETCH
  245. %token <keyword> K_FIRST
  246. %token <keyword> K_FOR
  247. %token <keyword> K_FOREACH
  248. %token <keyword> K_FORWARD
  249. %token <keyword> K_FROM
  250. %token <keyword> K_GET
  251. %token <keyword> K_HINT
  252. %token <keyword> K_IF
  253. %token <keyword> K_IN
  254. %token <keyword> K_INFO
  255. %token <keyword> K_INSERT
  256. %token <keyword> K_INTO
  257. %token <keyword> K_IS
  258. %token <keyword> K_LAST
  259. %token <keyword> K_LOG
  260. %token <keyword> K_LOOP
  261. %token <keyword> K_MESSAGE
  262. %token <keyword> K_MESSAGE_TEXT
  263. %token <keyword> K_MOVE
  264. %token <keyword> K_NEXT
  265. %token <keyword> K_NO
  266. %token <keyword> K_NOT
  267. %token <keyword> K_NOTICE
  268. %token <keyword> K_NULL
  269. %token <keyword> K_OPEN
  270. %token <keyword> K_OPTION
  271. %token <keyword> K_OR
  272. %token <keyword> K_PERFORM
  273. %token <keyword> K_PG_EXCEPTION_CONTEXT
  274. %token <keyword> K_PG_EXCEPTION_DETAIL
  275. %token <keyword> K_PG_EXCEPTION_HINT
  276. %token <keyword> K_PRIOR
  277. %token <keyword> K_QUERY
  278. %token <keyword> K_RAISE
  279. %token <keyword> K_RELATIVE
  280. %token <keyword> K_RESULT_OID
  281. %token <keyword> K_RETURN
  282. %token <keyword> K_RETURNED_SQLSTATE
  283. %token <keyword> K_REVERSE
  284. %token <keyword> K_ROWTYPE
  285. %token <keyword> K_ROW_COUNT
  286. %token <keyword> K_SCROLL
  287. %token <keyword> K_SLICE
  288. %token <keyword> K_SQLSTATE
  289. %token <keyword> K_STACKED
  290. %token <keyword> K_STRICT
  291. %token <keyword> K_THEN
  292. %token <keyword> K_TO
  293. %token <keyword> K_TYPE
  294. %token <keyword> K_USE_COLUMN
  295. %token <keyword> K_USE_VARIABLE
  296. %token <keyword> K_USING
  297. %token <keyword> K_VARIABLE_CONFLICT
  298. %token <keyword> K_WARNING
  299. %token <keyword> K_WHEN
  300. %token <keyword> K_WHILE
  301. %%
  302. pl_function : comp_options pl_block opt_semi
  303. {
  304. plpgsql_parse_result = (PLpgSQL_stmt_block *) $2;
  305. }
  306. ;
  307. comp_options :
  308. | comp_options comp_option
  309. ;
  310. comp_option : '#' K_OPTION K_DUMP
  311. {
  312. plpgsql_DumpExecTree = true;
  313. }
  314. | '#' K_VARIABLE_CONFLICT K_ERROR
  315. {
  316. plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_ERROR;
  317. }
  318. | '#' K_VARIABLE_CONFLICT K_USE_VARIABLE
  319. {
  320. plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_VARIABLE;
  321. }
  322. | '#' K_VARIABLE_CONFLICT K_USE_COLUMN
  323. {
  324. plpgsql_curr_compile->resolve_option = PLPGSQL_RESOLVE_COLUMN;
  325. }
  326. ;
  327. opt_semi :
  328. | ';'
  329. ;
  330. pl_block : decl_sect K_BEGIN proc_sect exception_sect K_END opt_label
  331. {
  332. PLpgSQL_stmt_block *new;
  333. new = palloc0(sizeof(PLpgSQL_stmt_block));
  334. new->cmd_type = PLPGSQL_STMT_BLOCK;
  335. new->lineno = plpgsql_location_to_lineno(@2);
  336. new->label = $1.label;
  337. new->n_initvars = $1.n_initvars;
  338. new->initvarnos = $1.initvarnos;
  339. new->body = $3;
  340. new->exceptions = $4;
  341. check_labels($1.label, $6, @6);
  342. plpgsql_ns_pop();
  343. $$ = (PLpgSQL_stmt *)new;
  344. }
  345. ;
  346. decl_sect : opt_block_label
  347. {
  348. /* done with decls, so resume identifier lookup */
  349. plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
  350. $$.label = $1;
  351. $$.n_initvars = 0;
  352. $$.initvarnos = NULL;
  353. }
  354. | opt_block_label decl_start
  355. {
  356. plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
  357. $$.label = $1;
  358. $$.n_initvars = 0;
  359. $$.initvarnos = NULL;
  360. }
  361. | opt_block_label decl_start decl_stmts
  362. {
  363. plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_NORMAL;
  364. $$.label = $1;
  365. /* Remember variables declared in decl_stmts */
  366. $$.n_initvars = plpgsql_add_initdatums(&($$.initvarnos));
  367. }
  368. ;
  369. decl_start : K_DECLARE
  370. {
  371. /* Forget any variables created before block */
  372. plpgsql_add_initdatums(NULL);
  373. /*
  374. * Disable scanner lookup of identifiers while
  375. * we process the decl_stmts
  376. */
  377. plpgsql_IdentifierLookup = IDENTIFIER_LOOKUP_DECLARE;
  378. }
  379. ;
  380. decl_stmts : decl_stmts decl_stmt
  381. | decl_stmt
  382. ;
  383. decl_stmt : decl_statement
  384. | K_DECLARE
  385. {
  386. /* We allow useless extra DECLAREs */
  387. }
  388. | LESS_LESS any_identifier GREATER_GREATER
  389. {
  390. /*
  391. * Throw a helpful error if user tries to put block
  392. * label just before BEGIN, instead of before DECLARE.
  393. */
  394. ereport(ERROR,
  395. (errcode(ERRCODE_SYNTAX_ERROR),
  396. errmsg("block label must be placed before DECLARE, not after"),
  397. parser_errposition(@1)));
  398. }
  399. ;
  400. decl_statement : decl_varname decl_const decl_datatype decl_collate decl_notnull decl_defval
  401. {
  402. PLpgSQL_variable *var;
  403. /*
  404. * If a collation is supplied, insert it into the
  405. * datatype. We assume decl_datatype always returns
  406. * a freshly built struct not shared with other
  407. * variables.
  408. */
  409. if (OidIsValid($4))
  410. {
  411. if (!OidIsValid($3->collation))
  412. ereport(ERROR,
  413. (errcode(ERRCODE_DATATYPE_MISMATCH),
  414. errmsg("collations are not supported by type %s",
  415. format_type_be($3->typoid)),
  416. parser_errposition(@4)));
  417. $3->collation = $4;
  418. }
  419. var = plpgsql_build_variable($1.name, $1.lineno,
  420. $3, true);
  421. if ($2)
  422. {
  423. if (var->dtype == PLPGSQL_DTYPE_VAR)
  424. ((PLpgSQL_var *) var)->isconst = $2;
  425. else
  426. ereport(ERROR,
  427. (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
  428. errmsg("row or record variable cannot be CONSTANT"),
  429. parser_errposition(@2)));
  430. }
  431. if ($5)
  432. {
  433. if (var->dtype == PLPGSQL_DTYPE_VAR)
  434. ((PLpgSQL_var *) var)->notnull = $5;
  435. else
  436. ereport(ERROR,
  437. (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
  438. errmsg("row or record variable cannot be NOT NULL"),
  439. parser_errposition(@4)));
  440. }
  441. if ($6 != NULL)
  442. {
  443. if (var->dtype == PLPGSQL_DTYPE_VAR)
  444. ((PLpgSQL_var *) var)->default_val = $6;
  445. else
  446. ereport(ERROR,
  447. (errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
  448. errmsg("default value for row or record variable is not supported"),
  449. parser_errposition(@5)));
  450. }
  451. }
  452. | decl_varname K_ALIAS K_FOR decl_aliasitem ';'
  453. {
  454. plpgsql_ns_additem($4->itemtype,
  455. $4->itemno, $1.name);
  456. }
  457. | decl_varname opt_scrollable K_CURSOR
  458. { plpgsql_ns_push($1.name); }
  459. decl_cursor_args decl_is_for decl_cursor_query
  460. {
  461. PLpgSQL_var *new;
  462. PLpgSQL_expr *curname_def;
  463. char buf[1024];
  464. char *cp1;
  465. char *cp2;
  466. /* pop local namespace for cursor args */
  467. plpgsql_ns_pop();
  468. new = (PLpgSQL_var *)
  469. plpgsql_build_variable($1.name, $1.lineno,
  470. plpgsql_build_datatype(REFCURSOROID,
  471. -1,
  472. InvalidOid),
  473. true);
  474. curname_def = palloc0(sizeof(PLpgSQL_expr));
  475. curname_def->dtype = PLPGSQL_DTYPE_EXPR;
  476. strcpy(buf, "SELECT ");
  477. cp1 = new->refname;
  478. cp2 = buf + strlen(buf);
  479. /*
  480. * Don't trust standard_conforming_strings here;
  481. * it might change before we use the string.
  482. */
  483. if (strchr(cp1, '\\') != NULL)
  484. *cp2++ = ESCAPE_STRING_SYNTAX;
  485. *cp2++ = '\'';
  486. while (*cp1)
  487. {
  488. if (SQL_STR_DOUBLE(*cp1, true))
  489. *cp2++ = *cp1;
  490. *cp2++ = *cp1++;
  491. }
  492. strcpy(cp2, "'::pg_catalog.refcursor");
  493. curname_def->query = pstrdup(buf);
  494. new->default_val = curname_def;
  495. new->cursor_explicit_expr = $7;
  496. if ($5 == NULL)
  497. new->cursor_explicit_argrow = -1;
  498. else
  499. new->cursor_explicit_argrow = $5->dno;
  500. new->cursor_options = CURSOR_OPT_FAST_PLAN | $2;
  501. }
  502. ;
  503. opt_scrollable :
  504. {
  505. $$ = 0;
  506. }
  507. | K_NO K_SCROLL
  508. {
  509. $$ = CURSOR_OPT_NO_SCROLL;
  510. }
  511. | K_SCROLL
  512. {
  513. $$ = CURSOR_OPT_SCROLL;
  514. }
  515. ;
  516. decl_cursor_query :
  517. {
  518. $$ = read_sql_stmt("");
  519. }
  520. ;
  521. decl_cursor_args :
  522. {
  523. $$ = NULL;
  524. }
  525. | '(' decl_cursor_arglist ')'
  526. {
  527. PLpgSQL_row *new;
  528. int i;
  529. ListCell *l;
  530. new = palloc0(sizeof(PLpgSQL_row));
  531. new->dtype = PLPGSQL_DTYPE_ROW;
  532. new->lineno = plpgsql_location_to_lineno(@1);
  533. new->rowtupdesc = NULL;
  534. new->nfields = list_length($2);
  535. new->fieldnames = palloc(new->nfields * sizeof(char *));
  536. new->varnos = palloc(new->nfields * sizeof(int));
  537. i = 0;
  538. foreach (l, $2)
  539. {
  540. PLpgSQL_variable *arg = (PLpgSQL_variable *) lfirst(l);
  541. new->fieldnames[i] = arg->refname;
  542. new->varnos[i] = arg->dno;
  543. i++;
  544. }
  545. list_free($2);
  546. plpgsql_adddatum((PLpgSQL_datum *) new);
  547. $$ = (PLpgSQL_datum *) new;
  548. }
  549. ;
  550. decl_cursor_arglist : decl_cursor_arg
  551. {
  552. $$ = list_make1($1);
  553. }
  554. | decl_cursor_arglist ',' decl_cursor_arg
  555. {
  556. $$ = lappend($1, $3);
  557. }
  558. ;
  559. decl_cursor_arg : decl_varname decl_datatype
  560. {
  561. $$ = (PLpgSQL_datum *)
  562. plpgsql_build_variable($1.name, $1.lineno,
  563. $2, true);
  564. }
  565. ;
  566. decl_is_for : K_IS | /* Oracle */
  567. K_FOR; /* SQL standard */
  568. decl_aliasitem : T_WORD
  569. {
  570. PLpgSQL_nsitem *nsi;
  571. nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
  572. $1.ident, NULL, NULL,
  573. NULL);
  574. if (nsi == NULL)
  575. ereport(ERROR,
  576. (errcode(ERRCODE_UNDEFINED_OBJECT),
  577. errmsg("variable \"%s\" does not exist",
  578. $1.ident),
  579. parser_errposition(@1)));
  580. $$ = nsi;
  581. }
  582. | T_CWORD
  583. {
  584. PLpgSQL_nsitem *nsi;
  585. if (list_length($1.idents) == 2)
  586. nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
  587. strVal(linitial($1.idents)),
  588. strVal(lsecond($1.idents)),
  589. NULL,
  590. NULL);
  591. else if (list_length($1.idents) == 3)
  592. nsi = plpgsql_ns_lookup(plpgsql_ns_top(), false,
  593. strVal(linitial($1.idents)),
  594. strVal(lsecond($1.idents)),
  595. strVal(lthird($1.idents)),
  596. NULL);
  597. else
  598. nsi = NULL;
  599. if (nsi == NULL)
  600. ereport(ERROR,
  601. (errcode(ERRCODE_UNDEFINED_OBJECT),
  602. errmsg("variable \"%s\" does not exist",
  603. NameListToString($1.idents)),
  604. parser_errposition(@1)));
  605. $$ = nsi;
  606. }
  607. ;
  608. decl_varname : T_WORD
  609. {
  610. $$.name = $1.ident;
  611. $$.lineno = plpgsql_location_to_lineno(@1);
  612. /*
  613. * Check to make sure name isn't already declared
  614. * in the current block.
  615. */
  616. if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
  617. $1.ident, NULL, NULL,
  618. NULL) != NULL)
  619. yyerror("duplicate declaration");
  620. }
  621. | unreserved_keyword
  622. {
  623. $$.name = pstrdup($1);
  624. $$.lineno = plpgsql_location_to_lineno(@1);
  625. /*
  626. * Check to make sure name isn't already declared
  627. * in the current block.
  628. */
  629. if (plpgsql_ns_lookup(plpgsql_ns_top(), true,
  630. $1, NULL, NULL,
  631. NULL) != NULL)
  632. yyerror("duplicate declaration");
  633. }
  634. ;
  635. decl_const :
  636. { $$ = false; }
  637. | K_CONSTANT
  638. { $$ = true; }
  639. ;
  640. decl_datatype :
  641. {
  642. /*
  643. * If there's a lookahead token, read_datatype
  644. * should consume it.
  645. */
  646. $$ = read_datatype(yychar);
  647. yyclearin;
  648. }
  649. ;
  650. decl_collate :
  651. { $$ = InvalidOid; }
  652. | K_COLLATE T_WORD
  653. {
  654. $$ = get_collation_oid(list_make1(makeString($2.ident)),
  655. false);
  656. }
  657. | K_COLLATE T_CWORD
  658. {
  659. $$ = get_collation_oid($2.idents, false);
  660. }
  661. ;
  662. decl_notnull :
  663. { $$ = false; }
  664. | K_NOT K_NULL
  665. { $$ = true; }
  666. ;
  667. decl_defval : ';'
  668. { $$ = NULL; }
  669. | decl_defkey
  670. {
  671. $$ = read_sql_expression(';', ";");
  672. }
  673. ;
  674. decl_defkey : assign_operator
  675. | K_DEFAULT
  676. ;
  677. assign_operator : '='
  678. | COLON_EQUALS
  679. ;
  680. proc_sect :
  681. { $$ = NIL; }
  682. | proc_stmts
  683. { $$ = $1; }
  684. ;
  685. proc_stmts : proc_stmts proc_stmt
  686. {
  687. if ($2 == NULL)
  688. $$ = $1;
  689. else
  690. $$ = lappend($1, $2);
  691. }
  692. | proc_stmt
  693. {
  694. if ($1 == NULL)
  695. $$ = NIL;
  696. else
  697. $$ = list_make1($1);
  698. }
  699. ;
  700. proc_stmt : pl_block ';'
  701. { $$ = $1; }
  702. | stmt_assign
  703. { $$ = $1; }
  704. | stmt_if
  705. { $$ = $1; }
  706. | stmt_case
  707. { $$ = $1; }
  708. | stmt_loop
  709. { $$ = $1; }
  710. | stmt_while
  711. { $$ = $1; }
  712. | stmt_for
  713. { $$ = $1; }
  714. | stmt_foreach_a
  715. { $$ = $1; }
  716. | stmt_exit
  717. { $$ = $1; }
  718. | stmt_return
  719. { $$ = $1; }
  720. | stmt_raise
  721. { $$ = $1; }
  722. | stmt_execsql
  723. { $$ = $1; }
  724. | stmt_dynexecute
  725. { $$ = $1; }
  726. | stmt_perform
  727. { $$ = $1; }
  728. | stmt_getdiag
  729. { $$ = $1; }
  730. | stmt_open
  731. { $$ = $1; }
  732. | stmt_fetch
  733. { $$ = $1; }
  734. | stmt_move
  735. { $$ = $1; }
  736. | stmt_close
  737. { $$ = $1; }
  738. | stmt_null
  739. { $$ = $1; }
  740. ;
  741. stmt_perform : K_PERFORM expr_until_semi
  742. {
  743. PLpgSQL_stmt_perform *new;
  744. new = palloc0(sizeof(PLpgSQL_stmt_perform));
  745. new->cmd_type = PLPGSQL_STMT_PERFORM;
  746. new->lineno = plpgsql_location_to_lineno(@1);
  747. new->expr = $2;
  748. $$ = (PLpgSQL_stmt *)new;
  749. }
  750. ;
  751. stmt_assign : assign_var assign_operator expr_until_semi
  752. {
  753. PLpgSQL_stmt_assign *new;
  754. new = palloc0(sizeof(PLpgSQL_stmt_assign));
  755. new->cmd_type = PLPGSQL_STMT_ASSIGN;
  756. new->lineno = plpgsql_location_to_lineno(@1);
  757. new->varno = $1;
  758. new->expr = $3;
  759. $$ = (PLpgSQL_stmt *)new;
  760. }
  761. ;
  762. stmt_getdiag : K_GET getdiag_area_opt K_DIAGNOSTICS getdiag_list ';'
  763. {
  764. PLpgSQL_stmt_getdiag *new;
  765. ListCell *lc;
  766. new = palloc0(sizeof(PLpgSQL_stmt_getdiag));
  767. new->cmd_type = PLPGSQL_STMT_GETDIAG;
  768. new->lineno = plpgsql_location_to_lineno(@1);
  769. new->is_stacked = $2;
  770. new->diag_items = $4;
  771. /*
  772. * Check information items are valid for area option.
  773. */
  774. foreach(lc, new->diag_items)
  775. {
  776. PLpgSQL_diag_item *ditem = (PLpgSQL_diag_item *) lfirst(lc);
  777. switch (ditem->kind)
  778. {
  779. /* these fields are disallowed in stacked case */
  780. case PLPGSQL_GETDIAG_ROW_COUNT:
  781. case PLPGSQL_GETDIAG_RESULT_OID:
  782. if (new->is_stacked)
  783. ereport(ERROR,
  784. (errcode(ERRCODE_SYNTAX_ERROR),
  785. errmsg("diagnostics item %s is not allowed in GET STACKED DIAGNOSTICS",
  786. plpgsql_getdiag_kindname(ditem->kind)),
  787. parser_errposition(@1)));
  788. break;
  789. /* these fields are disallowed in current case */
  790. case PLPGSQL_GETDIAG_ERROR_CONTEXT:
  791. case PLPGSQL_GETDIAG_ERROR_DETAIL:
  792. case PLPGSQL_GETDIAG_ERROR_HINT:
  793. case PLPGSQL_GETDIAG_RETURNED_SQLSTATE:
  794. case PLPGSQL_GETDIAG_MESSAGE_TEXT:
  795. if (!new->is_stacked)
  796. ereport(ERROR,
  797. (errcode(ERRCODE_SYNTAX_ERROR),
  798. errmsg("diagnostics item %s is not allowed in GET CURRENT DIAGNOSTICS",
  799. plpgsql_getdiag_kindname(ditem->kind)),
  800. parser_errposition(@1)));
  801. break;
  802. default:
  803. elog(ERROR, "unrecognized diagnostic item kind: %d",
  804. ditem->kind);
  805. break;
  806. }
  807. }
  808. $$ = (PLpgSQL_stmt *)new;
  809. }
  810. ;
  811. getdiag_area_opt :
  812. {
  813. $$ = false;
  814. }
  815. | K_CURRENT
  816. {
  817. $$ = false;
  818. }
  819. | K_STACKED
  820. {
  821. $$ = true;
  822. }
  823. ;
  824. getdiag_list : getdiag_list ',' getdiag_list_item
  825. {
  826. $$ = lappend($1, $3);
  827. }
  828. | getdiag_list_item
  829. {
  830. $$ = list_make1($1);
  831. }
  832. ;
  833. getdiag_list_item : getdiag_target assign_operator getdiag_item
  834. {
  835. PLpgSQL_diag_item *new;
  836. new = palloc(sizeof(PLpgSQL_diag_item));
  837. new->target = $1;
  838. new->kind = $3;
  839. $$ = new;
  840. }
  841. ;
  842. getdiag_item :
  843. {
  844. int tok = yylex();
  845. if (tok_is_keyword(tok, &yylval,
  846. K_ROW_COUNT, "row_count"))
  847. $$ = PLPGSQL_GETDIAG_ROW_COUNT;
  848. else if (tok_is_keyword(tok, &yylval,
  849. K_RESULT_OID, "result_oid"))
  850. $$ = PLPGSQL_GETDIAG_RESULT_OID;
  851. else if (tok_is_keyword(tok, &yylval,
  852. K_PG_EXCEPTION_DETAIL, "pg_exception_detail"))
  853. $$ = PLPGSQL_GETDIAG_ERROR_DETAIL;
  854. else if (tok_is_keyword(tok, &yylval,
  855. K_PG_EXCEPTION_HINT, "pg_exception_hint"))
  856. $$ = PLPGSQL_GETDIAG_ERROR_HINT;
  857. else if (tok_is_keyword(tok, &yylval,
  858. K_PG_EXCEPTION_CONTEXT, "pg_exception_context"))
  859. $$ = PLPGSQL_GETDIAG_ERROR_CONTEXT;
  860. else if (tok_is_keyword(tok, &yylval,
  861. K_MESSAGE_TEXT, "message_text"))
  862. $$ = PLPGSQL_GETDIAG_MESSAGE_TEXT;
  863. else if (tok_is_keyword(tok, &yylval,
  864. K_RETURNED_SQLSTATE, "returned_sqlstate"))
  865. $$ = PLPGSQL_GETDIAG_RETURNED_SQLSTATE;
  866. else
  867. yyerror("unrecognized GET DIAGNOSTICS item");
  868. }
  869. ;
  870. getdiag_target : T_DATUM
  871. {
  872. check_assignable($1.datum, @1);
  873. if ($1.datum->dtype == PLPGSQL_DTYPE_ROW ||
  874. $1.datum->dtype == PLPGSQL_DTYPE_REC)
  875. ereport(ERROR,
  876. (errcode(ERRCODE_SYNTAX_ERROR),
  877. errmsg("\"%s\" is not a scalar variable",
  878. NameOfDatum(&($1))),
  879. parser_errposition(@1)));
  880. $$ = $1.datum->dno;
  881. }
  882. | T_WORD
  883. {
  884. /* just to give a better message than "syntax error" */
  885. word_is_not_variable(&($1), @1);
  886. }
  887. | T_CWORD
  888. {
  889. /* just to give a better message than "syntax error" */
  890. cword_is_not_variable(&($1), @1);
  891. }
  892. ;
  893. assign_var : T_DATUM
  894. {
  895. check_assignable($1.datum, @1);
  896. $$ = $1.datum->dno;
  897. }
  898. | assign_var '[' expr_until_rightbracket
  899. {
  900. PLpgSQL_arrayelem *new;
  901. new = palloc0(sizeof(PLpgSQL_arrayelem));
  902. new->dtype = PLPGSQL_DTYPE_ARRAYELEM;
  903. new->subscript = $3;
  904. new->arrayparentno = $1;
  905. /* initialize cached type data to "not valid" */
  906. new->parenttypoid = InvalidOid;
  907. plpgsql_adddatum((PLpgSQL_datum *) new);
  908. $$ = new->dno;
  909. }
  910. ;
  911. stmt_if : K_IF expr_until_then proc_sect stmt_elsifs stmt_else K_END K_IF ';'
  912. {
  913. PLpgSQL_stmt_if *new;
  914. new = palloc0(sizeof(PLpgSQL_stmt_if));
  915. new->cmd_type = PLPGSQL_STMT_IF;
  916. new->lineno = plpgsql_location_to_lineno(@1);
  917. new->cond = $2;
  918. new->then_body = $3;
  919. new->elsif_list = $4;
  920. new->else_body = $5;
  921. $$ = (PLpgSQL_stmt *)new;
  922. }
  923. ;
  924. stmt_elsifs :
  925. {
  926. $$ = NIL;
  927. }
  928. | stmt_elsifs K_ELSIF expr_until_then proc_sect
  929. {
  930. PLpgSQL_if_elsif *new;
  931. new = palloc0(sizeof(PLpgSQL_if_elsif));
  932. new->lineno = plpgsql_location_to_lineno(@2);
  933. new->cond = $3;
  934. new->stmts = $4;
  935. $$ = lappend($1, new);
  936. }
  937. ;
  938. stmt_else :
  939. {
  940. $$ = NIL;
  941. }
  942. | K_ELSE proc_sect
  943. {
  944. $$ = $2;
  945. }
  946. ;
  947. stmt_case : K_CASE opt_expr_until_when case_when_list opt_case_else K_END K_CASE ';'
  948. {
  949. $$ = make_case(@1, $2, $3, $4);
  950. }
  951. ;
  952. opt_expr_until_when :
  953. {
  954. PLpgSQL_expr *expr = NULL;
  955. int tok = yylex();
  956. if (tok != K_WHEN)
  957. {
  958. plpgsql_push_back_token(tok);
  959. expr = read_sql_expression(K_WHEN, "WHEN");
  960. }
  961. plpgsql_push_back_token(K_WHEN);
  962. $$ = expr;
  963. }
  964. ;
  965. case_when_list : case_when_list case_when
  966. {
  967. $$ = lappend($1, $2);
  968. }
  969. | case_when
  970. {
  971. $$ = list_make1($1);
  972. }
  973. ;
  974. case_when : K_WHEN expr_until_then proc_sect
  975. {
  976. PLpgSQL_case_when *new = palloc(sizeof(PLpgSQL_case_when));
  977. new->lineno = plpgsql_location_to_lineno(@1);
  978. new->expr = $2;
  979. new->stmts = $3;
  980. $$ = new;
  981. }
  982. ;
  983. opt_case_else :
  984. {
  985. $$ = NIL;
  986. }
  987. | K_ELSE proc_sect
  988. {
  989. /*
  990. * proc_sect could return an empty list, but we
  991. * must distinguish that from not having ELSE at all.
  992. * Simplest fix is to return a list with one NULL
  993. * pointer, which make_case() must take care of.
  994. */
  995. if ($2 != NIL)
  996. $$ = $2;
  997. else
  998. $$ = list_make1(NULL);
  999. }
  1000. ;
  1001. stmt_loop : opt_block_label K_LOOP loop_body
  1002. {
  1003. PLpgSQL_stmt_loop *new;
  1004. new = palloc0(sizeof(PLpgSQL_stmt_loop));
  1005. new->cmd_type = PLPGSQL_STMT_LOOP;
  1006. new->lineno = plpgsql_location_to_lineno(@2);
  1007. new->label = $1;
  1008. new->body = $3.stmts;
  1009. check_labels($1, $3.end_label, $3.end_label_location);
  1010. plpgsql_ns_pop();
  1011. $$ = (PLpgSQL_stmt *)new;
  1012. }
  1013. ;
  1014. stmt_while : opt_block_label K_WHILE expr_until_loop loop_body
  1015. {
  1016. PLpgSQL_stmt_while *new;
  1017. new = palloc0(sizeof(PLpgSQL_stmt_while));
  1018. new->cmd_type = PLPGSQL_STMT_WHILE;
  1019. new->lineno = plpgsql_location_to_lineno(@2);
  1020. new->label = $1;
  1021. new->cond = $3;
  1022. new->body = $4.stmts;
  1023. check_labels($1, $4.end_label, $4.end_label_location);
  1024. plpgsql_ns_pop();
  1025. $$ = (PLpgSQL_stmt *)new;
  1026. }
  1027. ;
  1028. stmt_for : opt_block_label K_FOR for_control loop_body
  1029. {
  1030. /* This runs after we've scanned the loop body */
  1031. if ($3->cmd_type == PLPGSQL_STMT_FORI)
  1032. {
  1033. PLpgSQL_stmt_fori *new;
  1034. new = (PLpgSQL_stmt_fori *) $3;
  1035. new->lineno = plpgsql_location_to_lineno(@2);
  1036. new->label = $1;
  1037. new->body = $4.stmts;
  1038. $$ = (PLpgSQL_stmt *) new;
  1039. }
  1040. else
  1041. {
  1042. PLpgSQL_stmt_forq *new;
  1043. Assert($3->cmd_type == PLPGSQL_STMT_FORS ||
  1044. $3->cmd_type == PLPGSQL_STMT_FORC ||
  1045. $3->cmd_type == PLPGSQL_STMT_DYNFORS);
  1046. /* forq is the common supertype of all three */
  1047. new = (PLpgSQL_stmt_forq *) $3;
  1048. new->lineno = plpgsql_location_to_lineno(@2);
  1049. new->label = $1;
  1050. new->body = $4.stmts;
  1051. $$ = (PLpgSQL_stmt *) new;
  1052. }
  1053. check_labels($1, $4.end_label, $4.end_label_location);
  1054. /* close namespace started in opt_block_label */
  1055. plpgsql_ns_pop();
  1056. }
  1057. ;
  1058. for_control : for_variable K_IN
  1059. {
  1060. int tok = yylex();
  1061. int tokloc = yylloc;
  1062. if (tok == K_EXECUTE)
  1063. {
  1064. /* EXECUTE means it's a dynamic FOR loop */
  1065. PLpgSQL_stmt_dynfors *new;
  1066. PLpgSQL_expr *expr;
  1067. int term;
  1068. expr = read_sql_expression2(K_LOOP, K_USING,
  1069. "LOOP or USING",
  1070. &term);
  1071. new = palloc0(sizeof(PLpgSQL_stmt_dynfors));
  1072. new->cmd_type = PLPGSQL_STMT_DYNFORS;
  1073. if ($1.rec)
  1074. {
  1075. new->rec = $1.rec;
  1076. check_assignable((PLpgSQL_datum *) new->rec, @1);
  1077. }
  1078. else if ($1.row)
  1079. {
  1080. new->row = $1.row;
  1081. check_assignable((PLpgSQL_datum *) new->row, @1);
  1082. }
  1083. else if ($1.scalar)
  1084. {
  1085. /* convert single scalar to list */
  1086. new->row = make_scalar_list1($1.name, $1.scalar,
  1087. $1.lineno, @1);
  1088. /* no need for check_assignable */
  1089. }
  1090. else
  1091. {
  1092. ereport(ERROR,
  1093. (errcode(ERRCODE_DATATYPE_MISMATCH),
  1094. errmsg("loop variable of loop over rows must be a record or row variable or list of scalar variables"),
  1095. parser_errposition(@1)));
  1096. }
  1097. new->query = expr;
  1098. if (term == K_USING)
  1099. {
  1100. do
  1101. {
  1102. expr = read_sql_expression2(',', K_LOOP,
  1103. ", or LOOP",
  1104. &term);
  1105. new->params = lappend(new->params, expr);
  1106. } while (term == ',');
  1107. }
  1108. $$ = (PLpgSQL_stmt *) new;
  1109. }
  1110. else if (tok == T_DATUM &&
  1111. yylval.wdatum.datum->dtype == PLPGSQL_DTYPE_VAR &&
  1112. ((PLpgSQL_var *) yylval.wdatum.datum)->datatype->typoid == REFCURSOROID)
  1113. {
  1114. /* It's FOR var IN cursor */
  1115. PLpgSQL_stmt_forc *new;
  1116. PLpgSQL_var *cursor = (PLpgSQL_var *) yylval.wdatum.datum;
  1117. new = (PLpgSQL_stmt_forc *) palloc0(sizeof(PLpgSQL_stmt_forc));
  1118. new->cmd_type = PLPGSQL_STMT_FORC;
  1119. new->curvar = cursor->dno;
  1120. /* Should have had a single variable name */
  1121. if ($1.scalar && $1.row)
  1122. ereport(ERROR,
  1123. (errcode(ERRCODE_SYNTAX_ERROR),
  1124. errmsg("cursor FOR loop must have only one target variable"),
  1125. parser_errposition(@1)));
  1126. /* can't use an unbound cursor this way */
  1127. if (cursor->cursor_explicit_expr == NULL)
  1128. ereport(ERROR,
  1129. (errcode(ERRCODE_SYNTAX_ERROR),
  1130. errmsg("cursor FOR loop must use a bound cursor variable"),
  1131. parser_errposition(tokloc)));
  1132. /* collect cursor's parameters if any */
  1133. new->argquery = read_cursor_args(cursor,
  1134. K_LOOP,
  1135. "LOOP");
  1136. /* create loop's private RECORD variable */
  1137. new->rec = plpgsql_build_record($1.name,
  1138. $1.lineno,
  1139. true);
  1140. $$ = (PLpgSQL_stmt *) new;
  1141. }
  1142. else
  1143. {
  1144. PLpgSQL_expr *expr1;
  1145. int expr1loc;
  1146. bool reverse = false;
  1147. /*
  1148. * We have to distinguish between two
  1149. * alternatives: FOR var IN a .. b and FOR
  1150. * var IN query. Unfortunately this is
  1151. * tricky, since the query in the second
  1152. * form needn't start with a SELECT
  1153. * keyword. We use the ugly hack of
  1154. * looking for two periods after the first
  1155. * token. We also check for the REVERSE
  1156. * keyword, which means it must be an
  1157. * integer loop.
  1158. */
  1159. if (tok_is_keyword(tok, &yylval,
  1160. K_REVERSE, "reverse"))
  1161. reverse = true;
  1162. else
  1163. plpgsql_push_back_token(tok);
  1164. /*
  1165. * Read tokens until we see either a ".."
  1166. * or a LOOP. The text we read may not
  1167. * necessarily be a well-formed SQL
  1168. * statement, so we need to invoke
  1169. * read_sql_construct directly.
  1170. */
  1171. expr1 = read_sql_construct(DOT_DOT,
  1172. K_LOOP,
  1173. 0,
  1174. "LOOP",
  1175. "SELECT ",
  1176. true,
  1177. false,
  1178. true,
  1179. &expr1loc,
  1180. &tok);
  1181. if (tok == DOT_DOT)
  1182. {
  1183. /* Saw "..", so it must be an integer loop */
  1184. PLpgSQL_expr *expr2;
  1185. PLpgSQL_expr *expr_by;
  1186. PLpgSQL_var *fvar;
  1187. PLpgSQL_stmt_fori *new;
  1188. /* Check first expression is well-formed */
  1189. check_sql_expr(expr1->query, expr1loc, 7);
  1190. /* Read and check the second one */
  1191. expr2 = read_sql_expression2(K_LOOP, K_BY,
  1192. "LOOP",
  1193. &tok);
  1194. /* Get the BY clause if any */
  1195. if (tok == K_BY)
  1196. expr_by = read_sql_expression(K_LOOP,
  1197. "LOOP");
  1198. else
  1199. expr_by = NULL;
  1200. /* Should have had a single variable name */
  1201. if ($1.scalar && $1.row)
  1202. ereport(ERROR,
  1203. (errcode(ERRCODE_SYNTAX_ERROR),
  1204. errmsg("integer FOR loop must have only one target variable"),
  1205. parser_errposition(@1)));
  1206. /* create loop's private variable */
  1207. fvar = (PLpgSQL_var *)
  1208. plpgsql_build_variable($1.name,
  1209. $1.lineno,
  1210. plpgsql_build_datatype(INT4OID,
  1211. -1,
  1212. InvalidOid),
  1213. true);
  1214. new = palloc0(sizeof(PLpgSQL_stmt_fori));
  1215. new->cmd_type = PLPGSQL_STMT_FORI;
  1216. new->var = fvar;
  1217. new->reverse = reverse;
  1218. new->lower = expr1;
  1219. new->upper = expr2;
  1220. new->step = expr_by;
  1221. $$ = (PLpgSQL_stmt *) new;
  1222. }
  1223. else
  1224. {
  1225. /*
  1226. * No "..", so it must be a query loop. We've
  1227. * prefixed an extra SELECT to the query text,
  1228. * so we need to remove that before performing
  1229. * syntax checking.
  1230. */
  1231. char *tmp_query;
  1232. PLpgSQL_stmt_fors *new;
  1233. if (reverse)
  1234. ereport(ERROR,
  1235. (errcode(ERRCODE_SYNTAX_ERROR),
  1236. errmsg("cannot specify REVERSE in query FOR loop"),
  1237. parser_errposition(tokloc)));
  1238. Assert(strncmp(expr1->query, "SELECT ", 7) == 0);
  1239. tmp_query = pstrdup(expr1->query + 7);
  1240. pfree(expr1->query);
  1241. expr1->query = tmp_query;
  1242. check_sql_expr(expr1->query, expr1loc, 0);
  1243. new = palloc0(sizeof(PLpgSQL_stmt_fors));
  1244. new->cmd_type = PLPGSQL_STMT_FORS;
  1245. if ($1.rec)
  1246. {
  1247. new->rec = $1.rec;
  1248. check_assignable((PLpgSQL_datum *) new->rec, @1);
  1249. }
  1250. else if ($1.row)
  1251. {
  1252. new->row = $1.row;
  1253. check_assignable((PLpgSQL_datum *) new->row, @1);
  1254. }
  1255. else if ($1.scalar)
  1256. {
  1257. /* convert single scalar to list */
  1258. new->row = make_scalar_list1($1.name, $1.scalar,
  1259. $1.lineno, @1);
  1260. /* no need for check_assignable */
  1261. }
  1262. else
  1263. {
  1264. ereport(ERROR,
  1265. (errcode(ERRCODE_SYNTAX_ERROR),
  1266. errmsg("loop variable of loop over rows must be a record or row variable or list of scalar variables"),
  1267. parser_errposition(@1)));
  1268. }
  1269. new->query = expr1;
  1270. $$ = (PLpgSQL_stmt *) new;
  1271. }
  1272. }
  1273. }
  1274. ;
  1275. /*
  1276. * Processing the for_variable is tricky because we don't yet know if the
  1277. * FOR is an integer FOR loop or a loop over query results. In the former
  1278. * case, the variable is just a name that we must instantiate as a loop
  1279. * local variable, regardless of any other definition it might have.
  1280. * Therefore, we always save the actual identifier into $$.name where it
  1281. * can be used for that case. We also save the outer-variable definition,
  1282. * if any, because that's what we need for the loop-over-query case. Note
  1283. * that we must NOT apply check_assignable() or any other semantic check
  1284. * until we know what's what.
  1285. *
  1286. * However, if we see a comma-separated list of names, we know that it
  1287. * can't be an integer FOR loop and so it's OK to check the variables
  1288. * immediately. In particular, for T_WORD followed by comma, we should
  1289. * complain that the name is not known rather than say it's a syntax error.
  1290. * Note that the non-error result of this case sets *both* $$.scalar and
  1291. * $$.row; see the for_control production.
  1292. */
  1293. for_variable : T_DATUM
  1294. {
  1295. $$.name = NameOfDatum(&($1));
  1296. $$.lineno = plpgsql_location_to_lineno(@1);
  1297. if ($1.datum->dtype == PLPGSQL_DTYPE_ROW)
  1298. {
  1299. $$.scalar = NULL;
  1300. $$.rec = NULL;
  1301. $$.row = (PLpgSQL_row *) $1.datum;
  1302. }
  1303. else if ($1.datum->dtype == PLPGSQL_DTYPE_REC)
  1304. {
  1305. $$.scalar = NULL;
  1306. $$.rec = (PLpgSQL_rec *) $1.datum;
  1307. $$.row = NULL;
  1308. }
  1309. else
  1310. {
  1311. int tok;
  1312. $$.scalar = $1.datum;
  1313. $$.rec = NULL;
  1314. $$.row = NULL;
  1315. /* check for comma-separated list */
  1316. tok = yylex();
  1317. plpgsql_push_back_token(tok);
  1318. if (tok == ',')
  1319. $$.row = read_into_scalar_list($$.name,
  1320. $$.scalar,
  1321. @1);
  1322. }
  1323. }
  1324. | T_WORD
  1325. {
  1326. int tok;
  1327. $$.name = $1.ident;
  1328. $$.lineno = plpgsql_location_to_lineno(@1);
  1329. $$.scalar = NULL;
  1330. $$.rec = NULL;
  1331. $$.row = NULL;
  1332. /* check for comma-separated list */
  1333. tok = yylex();
  1334. plpgsql_push_back_token(tok);
  1335. if (tok == ',')
  1336. word_is_not_variable(&($1), @1);
  1337. }
  1338. | T_CWORD
  1339. {
  1340. /* just to give a better message than "syntax error" */
  1341. cword_is_not_variable(&($1), @1);
  1342. }
  1343. ;
  1344. stmt_foreach_a : opt_block_label K_FOREACH for_variable foreach_slice K_IN K_ARRAY expr_until_loop loop_body
  1345. {
  1346. PLpgSQL_stmt_foreach_a *new;
  1347. new = palloc0(sizeof(PLpgSQL_stmt_foreach_a));
  1348. new->cmd_type = PLPGSQL_STMT_FOREACH_A;
  1349. new->lineno = plpgsql_location_to_lineno(@2);
  1350. new->label = $1;
  1351. new->slice = $4;
  1352. new->expr = $7;
  1353. new->body = $8.stmts;
  1354. if ($3.rec)
  1355. {
  1356. new->varno = $3.rec->dno;
  1357. check_assignable((PLpgSQL_datum *) $3.rec, @3);
  1358. }
  1359. else if ($3.row)
  1360. {
  1361. new->varno = $3.row->dno;
  1362. check_assignable((PLpgSQL_datum *) $3.row, @3);
  1363. }
  1364. else if ($3.scalar)
  1365. {
  1366. new->varno = $3.scalar->dno;
  1367. check_assignable($3.scalar, @3);
  1368. }
  1369. else
  1370. {
  1371. ereport(ERROR,
  1372. (errcode(ERRCODE_SYNTAX_ERROR),
  1373. errmsg("loop variable of FOREACH must be a known variable or list of variables"),
  1374. parser_errposition(@3)));
  1375. }
  1376. check_labels($1, $8.end_label, $8.end_label_location);
  1377. plpgsql_ns_pop();
  1378. $$ = (PLpgSQL_stmt *) new;
  1379. }
  1380. ;
  1381. foreach_slice :
  1382. {
  1383. $$ = 0;
  1384. }
  1385. | K_SLICE ICONST
  1386. {
  1387. $$ = $2;
  1388. }
  1389. ;
  1390. stmt_exit : exit_type opt_label opt_exitcond
  1391. {
  1392. PLpgSQL_stmt_exit *new;
  1393. new = palloc0(sizeof(PLpgSQL_stmt_exit));
  1394. new->cmd_type = PLPGSQL_STMT_EXIT;
  1395. new->is_exit = $1;
  1396. new->lineno = plpgsql_location_to_lineno(@1);
  1397. new->label = $2;
  1398. new->cond = $3;
  1399. $$ = (PLpgSQL_stmt *)new;
  1400. }
  1401. ;
  1402. exit_type : K_EXIT
  1403. {
  1404. $$ = true;
  1405. }
  1406. | K_CONTINUE
  1407. {
  1408. $$ = false;
  1409. }
  1410. ;
  1411. stmt_return : K_RETURN
  1412. {
  1413. int tok;
  1414. tok = yylex();
  1415. if (tok == 0)
  1416. yyerror("unexpected end of function definition");
  1417. if (tok_is_keyword(tok, &yylval,
  1418. K_NEXT, "next"))
  1419. {
  1420. $$ = make_return_next_stmt(@1);
  1421. }
  1422. else if (tok_is_keyword(tok, &yylval,
  1423. K_QUERY, "query"))
  1424. {
  1425. $$ = make_return_query_stmt(@1);
  1426. }
  1427. else
  1428. {
  1429. plpgsql_push_back_token(tok);
  1430. $$ = make_return_stmt(@1);
  1431. }
  1432. }
  1433. ;
  1434. stmt_raise : K_RAISE
  1435. {
  1436. PLpgSQL_stmt_raise *new;
  1437. int tok;
  1438. new = palloc(sizeof(PLpgSQL_stmt_raise));
  1439. new->cmd_type = PLPGSQL_STMT_RAISE;
  1440. new->lineno = plpgsql_location_to_lineno(@1);
  1441. new->elog_level = ERROR; /* default */
  1442. new->condname = NULL;
  1443. new->message = NULL;
  1444. new->params = NIL;
  1445. new->options = NIL;
  1446. tok = yylex();
  1447. if (tok == 0)
  1448. yyerror("unexpected end of function definition");
  1449. /*
  1450. * We could have just RAISE, meaning to re-throw
  1451. * the current error.
  1452. */
  1453. if (tok != ';')
  1454. {
  1455. /*
  1456. * First is an optional elog severity level.
  1457. */
  1458. if (tok_is_keyword(tok, &yylval,
  1459. K_EXCEPTION, "exception"))
  1460. {
  1461. new->elog_level = ERROR;
  1462. tok = yylex();
  1463. }
  1464. else if (tok_is_keyword(tok, &yylval,
  1465. K_WARNING, "warning"))
  1466. {
  1467. new->elog_level = WARNING;
  1468. tok = yylex();
  1469. }
  1470. else if (tok_is_keyword(tok, &yylval,
  1471. K_NOTICE, "notice"))
  1472. {
  1473. new->elog_level = NOTICE;
  1474. tok = yylex();
  1475. }
  1476. else if (tok_is_keyword(tok, &yylval,
  1477. K_INFO, "info"))
  1478. {
  1479. new->elog_level = INFO;
  1480. tok = yylex();
  1481. }
  1482. else if (tok_is_keyword(tok, &yylval,
  1483. K_LOG, "log"))
  1484. {
  1485. new->elog_level = LOG;
  1486. tok = yylex();
  1487. }
  1488. else if (tok_is_keyword(tok, &yylval,
  1489. K_DEBUG, "debug"))
  1490. {
  1491. new->elog_level = DEBUG1;
  1492. tok = yylex();
  1493. }
  1494. if (tok == 0)
  1495. yyerror("unexpected end of function definition");
  1496. /*
  1497. * Next we can have a condition name, or
  1498. * equivalently SQLSTATE 'xxxxx', or a string
  1499. * literal that is the old-style message format,
  1500. * or USING to start the option list immediately.
  1501. */
  1502. if (tok == SCONST)
  1503. {
  1504. /* old style message and parameters */
  1505. new->message = yylval.str;
  1506. /*
  1507. * We expect either a semi-colon, which
  1508. * indicates no parameters, or a comma that
  1509. * begins the list of parameter expressions,
  1510. * or USING to begin the options list.
  1511. */
  1512. tok = yylex();
  1513. if (tok != ',' && tok != ';' && tok != K_USING)
  1514. yyerror("syntax error");
  1515. while (tok == ',')
  1516. {
  1517. PLpgSQL_expr *expr;
  1518. expr = read_sql_construct(',', ';', K_USING,
  1519. ", or ; or USING",
  1520. "SELECT ",
  1521. true, true, true,
  1522. NULL, &tok);
  1523. new->params = lappend(new->params, expr);
  1524. }
  1525. }
  1526. else if (tok != K_USING)
  1527. {
  1528. /* must be condition name or SQLSTATE */
  1529. if (tok_is_keyword(tok, &yylval,
  1530. K_SQLSTATE, "sqlstate"))
  1531. {
  1532. /* next token should be a string literal */
  1533. char *sqlstatestr;
  1534. if (yylex() != SCONST)
  1535. yyerror("syntax error");
  1536. sqlstatestr = yylval.str;
  1537. if (strlen(sqlstatestr) != 5)
  1538. yyerror("invalid SQLSTATE code");
  1539. if (strspn(sqlstatestr, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ") != 5)
  1540. yyerror("invalid SQLSTATE code");
  1541. new->condname = sqlstatestr;
  1542. }
  1543. else
  1544. {
  1545. if (tok != T_WORD)
  1546. yyerror("syntax error");
  1547. new->condname = yylval.word.ident;
  1548. plpgsql_recognize_err_condition(new->condname,
  1549. false);
  1550. }
  1551. tok = yylex();
  1552. if (tok != ';' && tok != K_USING)
  1553. yyerror("syntax error");
  1554. }
  1555. if (tok == K_USING)
  1556. new->options = read_raise_options();
  1557. }
  1558. $$ = (PLpgSQL_stmt *)new;
  1559. }
  1560. ;
  1561. loop_body : proc_sect K_END K_LOOP opt_label ';'
  1562. {
  1563. $$.stmts = $1;
  1564. $$.end_label = $4;
  1565. $$.end_label_location = @4;
  1566. }
  1567. ;
  1568. /*
  1569. * T_WORD+T_CWORD match any initial identifier that is not a known plpgsql
  1570. * variable. (The composite case is probably a syntax error, but we'll let
  1571. * the core parser decide that.) Normally, we should assume that such a
  1572. * word is a SQL statement keyword that isn't also a plpgsql keyword.
  1573. * However, if the next token is assignment or '[', it can't be a valid
  1574. * SQL statement, and what we're probably looking at is an intended variable
  1575. * assignment. Give an appropriate complaint for that, instead of letting
  1576. * the core parser throw an unhelpful "syntax error".
  1577. */
  1578. stmt_execsql : K_INSERT
  1579. {
  1580. $$ = make_execsql_stmt(K_INSERT, @1);
  1581. }
  1582. | T_WORD
  1583. {
  1584. int tok;
  1585. tok = yylex();
  1586. plpgsql_push_back_token(tok);
  1587. if (tok == '=' || tok == COLON_EQUALS || tok == '[')
  1588. word_is_not_variable(&($1), @1);
  1589. $$ = make_execsql_stmt(T_WORD, @1);
  1590. }
  1591. | T_CWORD
  1592. {
  1593. int tok;
  1594. tok = yylex();
  1595. plpgsql_push_back_token(tok);
  1596. if (tok == '=' || tok == COLON_EQUALS || tok == '[')
  1597. cword_is_not_variable(&($1), @1);
  1598. $$ = make_execsql_stmt(T_CWORD, @1);
  1599. }
  1600. ;
  1601. stmt_dynexecute : K_EXECUTE
  1602. {
  1603. PLpgSQL_stmt_dynexecute *new;
  1604. PLpgSQL_expr *expr;
  1605. int endtoken;
  1606. expr = read_sql_construct(K_INTO, K_USING, ';',
  1607. "INTO or USING or ;",
  1608. "SELECT ",
  1609. true, true, true,
  1610. NULL, &endtoken);
  1611. new = palloc(sizeof(PLpgSQL_stmt_dynexecute));
  1612. new->cmd_type = PLPGSQL_STMT_DYNEXECUTE;
  1613. new->lineno = plpgsql_location_to_lineno(@1);
  1614. new->query = expr;
  1615. new->into = false;
  1616. new->strict = false;
  1617. new->rec = NULL;
  1618. new->row = NULL;
  1619. new->params = NIL;
  1620. /*
  1621. * We loop to allow the INTO and USING clauses to
  1622. * appear in either order, since people easily get
  1623. * that wrong. This coding also prevents "INTO foo"
  1624. * from getting absorbed into a USING expression,
  1625. * which is *really* confusing.
  1626. */
  1627. for (;;)
  1628. {
  1629. if (endtoken == K_INTO)
  1630. {
  1631. if (new->into) /* multiple INTO */
  1632. yyerror("syntax error");
  1633. new->into = true;
  1634. read_into_target(&new->rec, &new->row, &new->strict);
  1635. endtoken = yylex();
  1636. }
  1637. else if (endtoken == K_USING)
  1638. {
  1639. if (new->params) /* multiple USING */
  1640. yyerror("syntax error");
  1641. do
  1642. {
  1643. expr = read_sql_construct(',', ';', K_INTO,
  1644. ", or ; or INTO",
  1645. "SELECT ",
  1646. true, true, true,
  1647. NULL, &endtoken);
  1648. new->params = lappend(new->params, expr);
  1649. } while (endtoken == ',');
  1650. }
  1651. else if (endtoken == ';')
  1652. break;
  1653. else
  1654. yyerror("syntax error");
  1655. }
  1656. $$ = (PLpgSQL_stmt *)new;
  1657. }
  1658. ;
  1659. stmt_open : K_OPEN cursor_variable
  1660. {
  1661. PLpgSQL_stmt_open *new;
  1662. int tok;
  1663. new = palloc0(sizeof(PLpgSQL_stmt_open));
  1664. new->cmd_type = PLPGSQL_STMT_OPEN;
  1665. new->lineno = plpgsql_location_to_lineno(@1);
  1666. new->curvar = $2->dno;
  1667. new->cursor_options = CURSOR_OPT_FAST_PLAN;
  1668. if ($2->cursor_explicit_expr == NULL)
  1669. {
  1670. /* be nice if we could use opt_scrollable here */
  1671. tok = yylex();
  1672. if (tok_is_keyword(tok, &yylval,
  1673. K_NO, "no"))
  1674. {
  1675. tok = yylex();
  1676. if (tok_is_keyword(tok, &yylval,
  1677. K_SCROLL, "scroll"))
  1678. {
  1679. new->cursor_options |= CURSOR_OPT_NO_SCROLL;
  1680. tok = yylex();
  1681. }
  1682. }
  1683. else if (tok_is_keyword(tok, &yylval,
  1684. K_SCROLL, "scroll"))
  1685. {
  1686. new->cursor_options |= CURSOR_OPT_SCROLL;
  1687. tok = yylex();
  1688. }
  1689. if (tok != K_FOR)
  1690. yyerror("syntax error, expected \"FOR\"");
  1691. tok = yylex();
  1692. if (tok == K_EXECUTE)
  1693. {
  1694. int endtoken;
  1695. new->dynquery =
  1696. read_sql_expression2(K_USING, ';',
  1697. "USING or ;",
  1698. &endtoken);
  1699. /* If we found "USING", collect argument(s) */
  1700. if (endtoken == K_USING)
  1701. {
  1702. PLpgSQL_expr *expr;
  1703. do
  1704. {
  1705. expr = read_sql_expression2(',', ';',
  1706. ", or ;",
  1707. &endtoken);
  1708. new->params = lappend(new->params,

Large files files are truncated, but you can click here to view the full file