|
8 | 8 | * |
9 | 9 | * |
10 | 10 | * IDENTIFICATION |
11 | | - * $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.15 2002/09/21 18:39:25 tgl Exp $ |
| 11 | + * $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.15.2.1 2007/06/20 18:16:30 tgl Exp $ |
12 | 12 | * |
13 | 13 | * DESCRIPTION |
14 | 14 | * The "DefineFoo" routines take the parse tree and pick out the |
@@ -397,6 +397,7 @@ DefineDomain(CreateDomainStmt *stmt) |
397 | 397 | Node *defaultExpr = NULL; |
398 | 398 | char *defaultValue = NULL; |
399 | 399 | char *defaultValueBin = NULL; |
| 400 | + bool saw_default = false; |
400 | 401 | bool typNotNull = false; |
401 | 402 | bool nullDefined = false; |
402 | 403 | Oid basetypelem; |
@@ -493,42 +494,52 @@ DefineDomain(CreateDomainStmt *stmt) |
493 | 494 | foreach(listptr, schema) |
494 | 495 | { |
495 | 496 | Constraint *colDef = lfirst(listptr); |
496 | | - ParseState *pstate; |
497 | 497 |
|
498 | 498 | switch (colDef->contype) |
499 | 499 | { |
| 500 | + case CONSTR_DEFAULT: |
500 | 501 | /* |
501 | 502 | * The inherited default value may be overridden by the |
502 | | - * user with the DEFAULT <expr> statement. |
503 | | - * |
504 | | - * We have to search the entire constraint tree returned as |
505 | | - * we don't want to cook or fiddle too much. |
| 503 | + * user with the DEFAULT <expr> clause ... but only once. |
506 | 504 | */ |
507 | | - case CONSTR_DEFAULT: |
508 | | - if (defaultExpr) |
| 505 | + if (saw_default) |
509 | 506 | elog(ERROR, "CREATE DOMAIN has multiple DEFAULT expressions"); |
510 | | - /* Create a dummy ParseState for transformExpr */ |
511 | | - pstate = make_parsestate(NULL); |
512 | | - |
513 | | - /* |
514 | | - * Cook the colDef->raw_expr into an expression. Note: |
515 | | - * Name is strictly for error message |
516 | | - */ |
517 | | - defaultExpr = cookDefault(pstate, colDef->raw_expr, |
518 | | - basetypeoid, |
519 | | - stmt->typename->typmod, |
520 | | - domainName); |
521 | | - |
522 | | - /* |
523 | | - * Expression must be stored as a nodeToString result, but |
524 | | - * we also require a valid textual representation (mainly |
525 | | - * to make life easier for pg_dump). |
526 | | - */ |
527 | | - defaultValue = deparse_expression(defaultExpr, |
528 | | - deparse_context_for(domainName, |
529 | | - InvalidOid), |
530 | | - false, false); |
531 | | - defaultValueBin = nodeToString(defaultExpr); |
| 507 | + saw_default = true; |
| 508 | + |
| 509 | + if (colDef->raw_expr) |
| 510 | + { |
| 511 | + ParseState *pstate; |
| 512 | + |
| 513 | + /* Create a dummy ParseState for transformExpr */ |
| 514 | + pstate = make_parsestate(NULL); |
| 515 | + |
| 516 | + /* |
| 517 | + * Cook the colDef->raw_expr into an expression. |
| 518 | + * Note: name is strictly for error message |
| 519 | + */ |
| 520 | + defaultExpr = cookDefault(pstate, colDef->raw_expr, |
| 521 | + basetypeoid, |
| 522 | + stmt->typename->typmod, |
| 523 | + domainName); |
| 524 | + |
| 525 | + /* |
| 526 | + * Expression must be stored as a nodeToString result, but |
| 527 | + * we also require a valid textual representation (mainly |
| 528 | + * to make life easier for pg_dump). |
| 529 | + */ |
| 530 | + defaultValue = |
| 531 | + deparse_expression(defaultExpr, |
| 532 | + deparse_context_for(domainName, |
| 533 | + InvalidOid), |
| 534 | + false, false); |
| 535 | + defaultValueBin = nodeToString(defaultExpr); |
| 536 | + } |
| 537 | + else |
| 538 | + { |
| 539 | + /* DEFAULT NULL is same as not having a default */ |
| 540 | + defaultValue = NULL; |
| 541 | + defaultValueBin = NULL; |
| 542 | + } |
532 | 543 | break; |
533 | 544 |
|
534 | 545 | /* |
|
0 commit comments