8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.20 2002/12/06 05:00:11 momjian Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/commands/typecmds.c,v 1.21 2002/12/09 20:31:05 tgl Exp $
12
12
*
13
13
* DESCRIPTION
14
14
* The "DefineFoo" routines take the parse tree and pick out the
63
63
static Oid findTypeIOFunction (List * procname , Oid typeOid , bool isOutput );
64
64
static List * get_rels_with_domain (Oid domainOid );
65
65
static void domainPermissionCheck (HeapTuple tup , TypeName * typename );
66
- static void domainCheckForUnsupportedConstraints (Node * newConstraint );
67
66
static char * domainAddConstraint (Oid domainOid , Oid domainNamespace ,
68
67
Oid baseTypeOid ,
69
68
int typMod , Constraint * constr ,
@@ -519,24 +518,23 @@ DefineDomain(CreateDomainStmt *stmt)
519
518
Constraint * colDef ;
520
519
ParseState * pstate ;
521
520
522
- /*
523
- * Check for constraint types which are not supported by
524
- * domains. Throws an error if it finds one.
525
- */
526
- domainCheckForUnsupportedConstraints (newConstraint );
521
+ /* Check for unsupported constraint types */
522
+ if (IsA (newConstraint , FkConstraint ))
523
+ elog (ERROR , "CREATE DOMAIN / FOREIGN KEY constraints not supported" );
524
+
525
+ /* this case should not happen */
526
+ if (!IsA (newConstraint , Constraint ))
527
+ elog (ERROR , "DefineDomain: unexpected constraint node type" );
527
528
528
- /* Assume its a CHECK, DEFAULT, NULL or NOT NULL constraint */
529
529
colDef = (Constraint * ) newConstraint ;
530
+
530
531
switch (colDef -> contype )
531
532
{
533
+ case CONSTR_DEFAULT :
532
534
/*
533
535
* The inherited default value may be overridden by the
534
536
* user with the DEFAULT <expr> statement.
535
- *
536
- * We have to search the entire constraint tree returned as
537
- * we don't want to cook or fiddle too much.
538
537
*/
539
- case CONSTR_DEFAULT :
540
538
if (defaultExpr )
541
539
elog (ERROR , "CREATE DOMAIN has multiple DEFAULT expressions" );
542
540
/* Create a dummy ParseState for transformExpr */
@@ -563,9 +561,6 @@ DefineDomain(CreateDomainStmt *stmt)
563
561
defaultValueBin = nodeToString (defaultExpr );
564
562
break ;
565
563
566
- /*
567
- * Find the NULL constraint.
568
- */
569
564
case CONSTR_NOTNULL :
570
565
if (nullDefined )
571
566
elog (ERROR , "CREATE DOMAIN has conflicting NULL / NOT NULL constraint" );
@@ -580,19 +575,34 @@ DefineDomain(CreateDomainStmt *stmt)
580
575
nullDefined = true;
581
576
break ;
582
577
583
- /*
584
- * Check constraints are handled after domain creation, as they require
585
- * the Oid of the domain
586
- */
587
578
case CONSTR_CHECK :
579
+ /*
580
+ * Check constraints are handled after domain creation, as they
581
+ * require the Oid of the domain
582
+ */
583
+ break ;
584
+
585
+ /*
586
+ * All else are error cases
587
+ */
588
+ case CONSTR_UNIQUE :
589
+ elog (ERROR , "CREATE DOMAIN / UNIQUE not supported" );
590
+ break ;
591
+
592
+ case CONSTR_PRIMARY :
593
+ elog (ERROR , "CREATE DOMAIN / PRIMARY KEY not supported" );
594
+ break ;
595
+
596
+ case CONSTR_ATTR_DEFERRABLE :
597
+ case CONSTR_ATTR_NOT_DEFERRABLE :
598
+ case CONSTR_ATTR_DEFERRED :
599
+ case CONSTR_ATTR_IMMEDIATE :
600
+ elog (ERROR , "CREATE DOMAIN: DEFERRABLE, NON DEFERRABLE, DEFERRED"
601
+ " and IMMEDIATE not supported" );
588
602
break ;
589
603
590
- /*
591
- * If we reach this, then domainCheckForUnsupportedConstraints()
592
- * doesn't have a complete list of unsupported domain constraints
593
- */
594
604
default :
595
- elog (ERROR , "DefineDomain: unrecognized constraint node type " );
605
+ elog (ERROR , "DefineDomain: unrecognized constraint subtype " );
596
606
break ;
597
607
}
598
608
}
@@ -629,6 +639,8 @@ DefineDomain(CreateDomainStmt *stmt)
629
639
{
630
640
Constraint * constr = lfirst (listptr );
631
641
642
+ /* it must be a Constraint, per check above */
643
+
632
644
switch (constr -> contype )
633
645
{
634
646
case CONSTR_CHECK :
@@ -642,7 +654,8 @@ DefineDomain(CreateDomainStmt *stmt)
642
654
}
643
655
break ;
644
656
645
- /* Errors for other constraints are taken care of prior to domain creation */
657
+ /* Other constraint types were fully processed above */
658
+
646
659
default :
647
660
break ;
648
661
}
@@ -1262,21 +1275,21 @@ AlterDomainAddConstraint(List *names, Node *newConstraint)
1262
1275
elog (ERROR , "AlterDomain: type \"%s\" does not exist" ,
1263
1276
TypeNameToString (typename ));
1264
1277
1278
+ typTup = (Form_pg_type ) GETSTRUCT (tup );
1265
1279
1266
1280
/* Doesn't return if user isn't allowed to alter the domain */
1267
1281
domainPermissionCheck (tup , typename );
1268
1282
1269
- typTup = (Form_pg_type ) GETSTRUCT (tup );
1270
-
1283
+ /* Check for unsupported constraint types */
1284
+ if (IsA (newConstraint , FkConstraint ))
1285
+ elog (ERROR , "ALTER DOMAIN / FOREIGN KEY constraints not supported" );
1271
1286
1272
- /*
1273
- * Check for constraint types which are not supported by
1274
- * domains. Throws an error if it finds one.
1275
- */
1276
- domainCheckForUnsupportedConstraints (newConstraint );
1287
+ /* this case should not happen */
1288
+ if (!IsA (newConstraint , Constraint ))
1289
+ elog (ERROR , "AlterDomainAddConstraint: unexpected constraint node type" );
1277
1290
1278
- /* Assume its a CHECK, DEFAULT, NULL or NOT NULL constraint */
1279
1291
constr = (Constraint * ) newConstraint ;
1292
+
1280
1293
switch (constr -> contype )
1281
1294
{
1282
1295
case CONSTR_DEFAULT :
@@ -1288,32 +1301,42 @@ AlterDomainAddConstraint(List *names, Node *newConstraint)
1288
1301
elog (ERROR , "Use ALTER DOMAIN .. [ SET | DROP ] NOT NULL instead" );
1289
1302
break ;
1290
1303
1291
- /*
1292
- * Check constraints are handled after domain creation, as they require
1293
- * the Oid of the domain
1294
- */
1295
1304
case CONSTR_CHECK :
1296
- {
1297
- /* Returns the cooked constraint which is not needed during creation */
1298
- ccbin = domainAddConstraint (HeapTupleGetOid (tup ), typTup -> typnamespace ,
1299
- typTup -> typbasetype , typTup -> typtypmod ,
1300
- constr , & counter , NameStr (typTup -> typname ));
1301
- }
1305
+ /* processed below */
1302
1306
break ;
1303
1307
1304
- /*
1305
- * If we reach this, then domainCheckForUnsupportedConstraints()
1306
- * doesn't have a complete list of unsupported domain constraints
1307
- */
1308
+ case CONSTR_UNIQUE :
1309
+ elog (ERROR , "ALTER DOMAIN / UNIQUE indexes not supported" );
1310
+ break ;
1311
+
1312
+ case CONSTR_PRIMARY :
1313
+ elog (ERROR , "ALTER DOMAIN / PRIMARY KEY indexes not supported" );
1314
+ break ;
1315
+
1316
+ case CONSTR_ATTR_DEFERRABLE :
1317
+ case CONSTR_ATTR_NOT_DEFERRABLE :
1318
+ case CONSTR_ATTR_DEFERRED :
1319
+ case CONSTR_ATTR_IMMEDIATE :
1320
+ elog (ERROR , "ALTER DOMAIN: DEFERRABLE, NON DEFERRABLE, DEFERRED"
1321
+ " and IMMEDIATE not supported" );
1322
+ break ;
1323
+
1308
1324
default :
1309
- elog (ERROR , "DefineDomain : unrecognized constraint node type" );
1325
+ elog (ERROR , "AlterDomainAddConstraint : unrecognized constraint node type" );
1310
1326
break ;
1311
1327
}
1312
1328
1313
1329
/*
1314
1330
* Since all other constraint types throw errors, this must be
1315
- * a check constraint, and ccbin must be set.
1316
- *
1331
+ * a check constraint.
1332
+ */
1333
+
1334
+ /* Returns the cooked constraint which is not needed during creation */
1335
+ ccbin = domainAddConstraint (HeapTupleGetOid (tup ), typTup -> typnamespace ,
1336
+ typTup -> typbasetype , typTup -> typtypmod ,
1337
+ constr , & counter , NameStr (typTup -> typname ));
1338
+
1339
+ /*
1317
1340
* Test all values stored in the attributes based on the domain
1318
1341
* the constraint is being added to.
1319
1342
*/
@@ -1424,7 +1447,7 @@ get_rels_with_domain(Oid domainOid)
1424
1447
/* Scan through pg_class for tables */
1425
1448
while ((classTup = heap_getnext (classScan , ForwardScanDirection )) != NULL )
1426
1449
{
1427
- bool addToList = true ;
1450
+ relToCheck * rtc = NULL ;
1428
1451
int nkeys = 0 ;
1429
1452
HeapTuple attTup ;
1430
1453
HeapScanDesc attScan ;
@@ -1447,13 +1470,9 @@ get_rels_with_domain(Oid domainOid)
1447
1470
/* Scan through pg_attribute for attributes based on the domain */
1448
1471
while ((attTup = heap_getnext (attScan , ForwardScanDirection )) != NULL )
1449
1472
{
1450
- relToCheck * rtc ;
1451
-
1452
- /* Make the list entries for the relation */
1453
- if (addToList )
1473
+ if (rtc == NULL )
1454
1474
{
1455
- addToList = false;
1456
-
1475
+ /* First one found for this rel */
1457
1476
rtc = (relToCheck * )palloc (sizeof (relToCheck ));
1458
1477
rtc -> atts = (int * )palloc (sizeof (int ) * pg_class -> relnatts );
1459
1478
rtc -> relOid = HeapTupleGetOid (classTup );
@@ -1625,42 +1644,3 @@ domainAddConstraint(Oid domainOid, Oid domainNamespace, Oid baseTypeOid,
1625
1644
*/
1626
1645
return ccbin ;
1627
1646
}
1628
-
1629
- /*
1630
- * domainCheckForUnsupportedConstraints
1631
- *
1632
- * Throws an error on constraints that are unsupported by the
1633
- * domains.
1634
- */
1635
- void
1636
- domainCheckForUnsupportedConstraints (Node * newConstraint )
1637
- {
1638
- Constraint * colDef ;
1639
-
1640
- if (nodeTag (newConstraint ) == T_FkConstraint )
1641
- elog (ERROR , "CREATE DOMAIN / FOREIGN KEY constraints not supported" );
1642
-
1643
- colDef = (Constraint * ) newConstraint ;
1644
-
1645
- switch (colDef -> contype )
1646
- {
1647
- case CONSTR_UNIQUE :
1648
- elog (ERROR , "CREATE DOMAIN / UNIQUE indexes not supported" );
1649
- break ;
1650
-
1651
- case CONSTR_PRIMARY :
1652
- elog (ERROR , "CREATE DOMAIN / PRIMARY KEY indexes not supported" );
1653
- break ;
1654
-
1655
- case CONSTR_ATTR_DEFERRABLE :
1656
- case CONSTR_ATTR_NOT_DEFERRABLE :
1657
- case CONSTR_ATTR_DEFERRED :
1658
- case CONSTR_ATTR_IMMEDIATE :
1659
- elog (ERROR , "DefineDomain: DEFERRABLE, NON DEFERRABLE, DEFERRED"
1660
- " and IMMEDIATE not supported" );
1661
- break ;
1662
-
1663
- default :
1664
- break ;
1665
- }
1666
- }
0 commit comments