8
8
*
9
9
*
10
10
* IDENTIFICATION
11
- * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.98 2000/09/06 14:15:16 petere Exp $
11
+ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.99 2000/09/12 04:30:08 momjian Exp $
12
12
*
13
13
* NOTES
14
14
* The PerformAddAttribute() code, like most of the relation
55
55
56
56
57
57
static bool needs_toast_table (Relation rel );
58
+ static bool is_view (char * relname , char * command );
59
+
60
+
58
61
59
62
60
63
/* --------------------------------
@@ -1087,9 +1090,6 @@ void
1087
1090
AlterTableAddConstraint (char * relationName ,
1088
1091
bool inh , Node * newConstraint )
1089
1092
{
1090
- char rulequery [41 + NAMEDATALEN ];
1091
- void * qplan ;
1092
- char nulls [1 ]= "" ;
1093
1093
1094
1094
if (newConstraint == NULL )
1095
1095
elog (ERROR , "ALTER TABLE / ADD CONSTRAINT passed invalid constraint." );
@@ -1100,19 +1100,8 @@ AlterTableAddConstraint(char *relationName,
1100
1100
#endif
1101
1101
1102
1102
/* check to see if the table to be constrained is a view. */
1103
- sprintf (rulequery , "select * from pg_views where viewname='%s'" , relationName );
1104
- if (SPI_connect ()!= SPI_OK_CONNECT )
1105
- elog (ERROR , "ALTER TABLE: Unable to determine if %s is a view - SPI_connect failure.." , relationName );
1106
- qplan = SPI_prepare (rulequery , 0 , NULL );
1107
- if (!qplan )
1108
- elog (ERROR , "ALTER TABLE: Unable to determine if %s is a view - SPI_prepare failure." , relationName );
1109
- qplan = SPI_saveplan (qplan );
1110
- if (SPI_execp (qplan , NULL , nulls , 1 )!= SPI_OK_SELECT )
1111
- elog (ERROR , "ALTER TABLE: Unable to determine if %s is a view - SPI_execp failure." , relationName );
1112
- if (SPI_processed != 0 )
1113
- elog (ERROR , "ALTER TABLE: Cannot add constraints to views." );
1114
- if (SPI_finish () != SPI_OK_FINISH )
1115
- elog (NOTICE , "SPI_finish() failed in ALTER TABLE" );
1103
+ if (is_view (relationName , "ALTER TABLE" ))
1104
+ elog (ERROR , "ALTER TABLE: Cannot add constraints to views." );
1116
1105
1117
1106
switch (nodeTag (newConstraint ))
1118
1107
{
@@ -1261,19 +1250,8 @@ AlterTableAddConstraint(char *relationName,
1261
1250
}
1262
1251
1263
1252
/* check to see if the referenced table is a view. */
1264
- sprintf (rulequery , "select * from pg_views where viewname='%s'" , fkconstraint -> pktable_name );
1265
- if (SPI_connect ()!= SPI_OK_CONNECT )
1266
- elog (ERROR , "ALTER TABLE: Unable to determine if %s is a view." , relationName );
1267
- qplan = SPI_prepare (rulequery , 0 , NULL );
1268
- if (!qplan )
1269
- elog (ERROR , "ALTER TABLE: Unable to determine if %s is a view." , relationName );
1270
- qplan = SPI_saveplan (qplan );
1271
- if (SPI_execp (qplan , NULL , nulls , 1 )!= SPI_OK_SELECT )
1272
- elog (ERROR , "ALTER TABLE: Unable to determine if %s is a view." , relationName );
1273
- if (SPI_processed != 0 )
1274
- elog (ERROR , "ALTER TABLE: Cannot add constraints to views." );
1275
- if (SPI_finish () != SPI_OK_FINISH )
1276
- elog (NOTICE , "SPI_finish() failed in RI_FKey_check()" );
1253
+ if (is_view (fkconstraint -> pktable_name , "ALTER TABLE" ))
1254
+ elog (ERROR , "ALTER TABLE: Cannot add constraints to views." );
1277
1255
1278
1256
/*
1279
1257
* Grab an exclusive lock on the pk table, so that someone
@@ -1720,6 +1698,9 @@ LockTableCommand(LockStmt *lockstmt)
1720
1698
Relation rel ;
1721
1699
int aclresult ;
1722
1700
1701
+ if (is_view (lockstmt -> relname , "LOCK TABLE" ))
1702
+ elog (ERROR , "LOCK TABLE: cannot lock a view" );
1703
+
1723
1704
rel = heap_openr (lockstmt -> relname , NoLock );
1724
1705
1725
1706
if (lockstmt -> mode == AccessShareLock )
@@ -1735,3 +1716,29 @@ LockTableCommand(LockStmt *lockstmt)
1735
1716
heap_close (rel , NoLock ); /* close rel, keep lock */
1736
1717
}
1737
1718
1719
+
1720
+ static
1721
+ bool
1722
+ is_view (char * relname , char * command )
1723
+ {
1724
+ bool retval ;
1725
+ char rulequery [41 + NAMEDATALEN ];
1726
+ void * qplan ;
1727
+ char nulls [1 ]= "" ;
1728
+
1729
+ sprintf (rulequery , "select * from pg_views where viewname='%s'" , relname );
1730
+ if (SPI_connect ()!= SPI_OK_CONNECT )
1731
+ elog (ERROR , "%s: Unable to determine if %s is a view - SPI_connect failure.." , command , relname );
1732
+ qplan = SPI_prepare (rulequery , 0 , NULL );
1733
+ if (!qplan )
1734
+ elog (ERROR , "%s: Unable to determine if %s is a view - SPI_prepare failure." , command , relname );
1735
+ qplan = SPI_saveplan (qplan );
1736
+ if (SPI_execp (qplan , NULL , nulls , 1 )!= SPI_OK_SELECT )
1737
+ elog (ERROR , "%s: Unable to determine if %s is a view - SPI_execp failure." , command , relname );
1738
+
1739
+ retval = (SPI_processed != 0 );
1740
+ if (SPI_finish () != SPI_OK_FINISH )
1741
+ elog (NOTICE , "SPI_finish() failed in %s" , command );
1742
+
1743
+ return retval ;
1744
+ }
0 commit comments