PageRenderTime 71ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 1ms

/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

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

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