88 *
99 *
1010 * 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 $
1212 *
1313 * NOTES
1414 * The PerformAddAttribute() code, like most of the relation
5555
5656
5757static bool needs_toast_table (Relation rel );
58+ static bool is_view (char * relname , char * command );
59+
60+
5861
5962
6063/* --------------------------------
@@ -1087,9 +1090,6 @@ void
10871090AlterTableAddConstraint (char * relationName ,
10881091 bool inh , Node * newConstraint )
10891092{
1090- char rulequery [41 + NAMEDATALEN ];
1091- void * qplan ;
1092- char nulls [1 ]= "" ;
10931093
10941094 if (newConstraint == NULL )
10951095 elog (ERROR , "ALTER TABLE / ADD CONSTRAINT passed invalid constraint." );
@@ -1100,19 +1100,8 @@ AlterTableAddConstraint(char *relationName,
11001100#endif
11011101
11021102 /* 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." );
11161105
11171106 switch (nodeTag (newConstraint ))
11181107 {
@@ -1261,19 +1250,8 @@ AlterTableAddConstraint(char *relationName,
12611250 }
12621251
12631252 /* 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." );
12771255
12781256 /*
12791257 * Grab an exclusive lock on the pk table, so that someone
@@ -1720,6 +1698,9 @@ LockTableCommand(LockStmt *lockstmt)
17201698 Relation rel ;
17211699 int aclresult ;
17221700
1701+ if (is_view (lockstmt -> relname , "LOCK TABLE" ))
1702+ elog (ERROR , "LOCK TABLE: cannot lock a view" );
1703+
17231704 rel = heap_openr (lockstmt -> relname , NoLock );
17241705
17251706 if (lockstmt -> mode == AccessShareLock )
@@ -1735,3 +1716,29 @@ LockTableCommand(LockStmt *lockstmt)
17351716 heap_close (rel , NoLock ); /* close rel, keep lock */
17361717}
17371718
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