21
21
#include " parser/parse_type.h"
22
22
#include " parser/scanner.h"
23
23
#include " parser/scansup.h"
24
+ #include " utils/builtins.h"
24
25
25
26
26
27
/* Location tracking support --- simpler than bison's default */
@@ -122,6 +123,7 @@ static List *read_raise_options(void);
122
123
PLcword cword;
123
124
PLwdatum wdatum;
124
125
bool boolean;
126
+ Oid oid;
125
127
struct
126
128
{
127
129
char *name;
@@ -167,6 +169,7 @@ static List *read_raise_options(void);
167
169
%type <boolean> decl_const decl_notnull exit_type
168
170
%type <expr> decl_defval decl_cursor_query
169
171
%type <dtype> decl_datatype
172
+ %type <oid> decl_collate
170
173
%type <datum> decl_cursor_args
171
174
%type <list> decl_cursor_arglist
172
175
%type <nsitem> decl_aliasitem
@@ -245,6 +248,7 @@ static List *read_raise_options(void);
245
248
%token <keyword> K_BY
246
249
%token <keyword> K_CASE
247
250
%token <keyword> K_CLOSE
251
+ %token <keyword> K_COLLATE
248
252
%token <keyword> K_CONSTANT
249
253
%token <keyword> K_CONTINUE
250
254
%token <keyword> K_CURSOR
@@ -428,10 +432,27 @@ decl_stmt : decl_statement
428
432
}
429
433
;
430
434
431
- decl_statement : decl_varname decl_const decl_datatype decl_notnull decl_defval
435
+ decl_statement : decl_varname decl_const decl_datatype decl_collate decl_notnull decl_defval
432
436
{
433
437
PLpgSQL_variable *var;
434
438
439
+ /*
440
+ * If a collation is supplied, insert it into the
441
+ * datatype. We assume decl_datatype always returns
442
+ * a freshly built struct not shared with other
443
+ * variables.
444
+ */
445
+ if (OidIsValid($4 ))
446
+ {
447
+ if (!OidIsValid($3 ->collation))
448
+ ereport (ERROR,
449
+ (errcode(ERRCODE_DATATYPE_MISMATCH),
450
+ errmsg(" collations are not supported by type %s" ,
451
+ format_type_be ($3 ->typoid)),
452
+ parser_errposition(@4 )));
453
+ $3 ->collation = $4 ;
454
+ }
455
+
435
456
var = plpgsql_build_variable($1 .name, $1 .lineno,
436
457
$3 , true );
437
458
if ($2 )
@@ -444,21 +465,21 @@ decl_statement : decl_varname decl_const decl_datatype decl_notnull decl_defval
444
465
errmsg(" row or record variable cannot be CONSTANT" ),
445
466
parser_errposition(@2 )));
446
467
}
447
- if ($4 )
468
+ if ($5 )
448
469
{
449
470
if (var->dtype == PLPGSQL_DTYPE_VAR)
450
- ((PLpgSQL_var *) var)->notnull = $4 ;
471
+ ((PLpgSQL_var *) var)->notnull = $5 ;
451
472
else
452
473
ereport (ERROR,
453
474
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
454
475
errmsg(" row or record variable cannot be NOT NULL" ),
455
476
parser_errposition(@4 )));
456
477
457
478
}
458
- if ($5 != NULL )
479
+ if ($6 != NULL )
459
480
{
460
481
if (var->dtype == PLPGSQL_DTYPE_VAR)
461
- ((PLpgSQL_var *) var)->default_val = $5 ;
482
+ ((PLpgSQL_var *) var)->default_val = $6 ;
462
483
else
463
484
ereport (ERROR,
464
485
(errcode(ERRCODE_FEATURE_NOT_SUPPORTED),
@@ -685,6 +706,19 @@ decl_datatype :
685
706
}
686
707
;
687
708
709
+ decl_collate :
710
+ { $$ = InvalidOid; }
711
+ | K_COLLATE T_WORD
712
+ {
713
+ $$ = get_collation_oid (list_make1 (makeString ($2 .ident )),
714
+ false );
715
+ }
716
+ | K_COLLATE T_CWORD
717
+ {
718
+ $$ = get_collation_oid ($2 .idents , false );
719
+ }
720
+ ;
721
+
688
722
decl_notnull :
689
723
{ $$ = false ; }
690
724
| K_NOT K_NULL
@@ -2432,7 +2466,8 @@ read_datatype(int tok)
2432
2466
yyerror("incomplete data type declaration");
2433
2467
}
2434
2468
/* Possible followers for datatype in a declaration */
2435
- if (tok == K_NOT || tok == '=' || tok == COLON_EQUALS || tok == K_DEFAULT)
2469
+ if (tok == K_COLLATE || tok == K_NOT ||
2470
+ tok == '=' || tok == COLON_EQUALS || tok == K_DEFAULT)
2436
2471
break;
2437
2472
/* Possible followers for datatype in a cursor_arg list */
2438
2473
if ((tok == ',' || tok == ')') && parenlevel == 0)
0 commit comments