88 *
99 *
1010 * IDENTIFICATION
11- * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.99 2000/09/12 04:30:08 momjian Exp $
11+ * $Header: /cvsroot/pgsql/src/backend/commands/Attic/command.c,v 1.100 2000/09/12 04:33:18 momjian Exp $
1212 *
1313 * NOTES
1414 * The PerformAddAttribute() code, like most of the relation
2626#include "catalog/indexing.h"
2727#include "catalog/pg_attrdef.h"
2828#include "catalog/pg_opclass.h"
29+ #include "catalog/pg_rewrite.h"
2930#include "commands/command.h"
3031#include "executor/spi.h"
3132#include "catalog/heap.h"
5556
5657
5758static bool needs_toast_table (Relation rel );
58- static bool is_view (char * relname , char * command );
59+ static bool is_viewr (char * relname );
60+ static bool is_view (Relation rel );
5961
6062
6163
@@ -1100,7 +1102,7 @@ AlterTableAddConstraint(char *relationName,
11001102#endif
11011103
11021104 /* check to see if the table to be constrained is a view. */
1103- if (is_view (relationName , "ALTER TABLE" ))
1105+ if (is_viewr (relationName ))
11041106 elog (ERROR , "ALTER TABLE: Cannot add constraints to views." );
11051107
11061108 switch (nodeTag (newConstraint ))
@@ -1250,7 +1252,7 @@ AlterTableAddConstraint(char *relationName,
12501252 }
12511253
12521254 /* check to see if the referenced table is a view. */
1253- if (is_view (fkconstraint -> pktable_name , "ALTER TABLE" ))
1255+ if (is_viewr (fkconstraint -> pktable_name ))
12541256 elog (ERROR , "ALTER TABLE: Cannot add constraints to views." );
12551257
12561258 /*
@@ -1698,11 +1700,11 @@ LockTableCommand(LockStmt *lockstmt)
16981700 Relation rel ;
16991701 int aclresult ;
17001702
1701- if (is_view (lockstmt -> relname , "LOCK TABLE" ))
1702- elog (ERROR , "LOCK TABLE: cannot lock a view" );
1703-
17041703 rel = heap_openr (lockstmt -> relname , NoLock );
17051704
1705+ if (is_view (rel ))
1706+ elog (ERROR , "LOCK TABLE: cannot lock a view" );
1707+
17061708 if (lockstmt -> mode == AccessShareLock )
17071709 aclresult = pg_aclcheck (lockstmt -> relname , GetUserId (), ACL_RD );
17081710 else
@@ -1719,26 +1721,63 @@ LockTableCommand(LockStmt *lockstmt)
17191721
17201722static
17211723bool
1722- is_view (char * relname , char * command )
1724+ is_viewr (char * name )
17231725{
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 );
1726+ Relation rel = heap_openr (name , NoLock );
1727+
1728+ bool retval = is_view (rel );
1729+
1730+ heap_close (rel , NoLock );
1731+
1732+ return retval ;
1733+ }
1734+
1735+ static
1736+ bool
1737+ is_view (Relation rel )
1738+ {
1739+ Relation RewriteRelation ;
1740+ HeapScanDesc scanDesc ;
1741+ ScanKeyData scanKeyData ;
1742+ HeapTuple tuple ;
1743+ Form_pg_rewrite data ;
1744+
1745+
1746+ bool retval = 0 ;
1747+
1748+ /*
1749+ * Open the pg_rewrite relation.
1750+ */
1751+ RewriteRelation = heap_openr (RewriteRelationName , RowExclusiveLock );
1752+
1753+ /*
1754+ * Scan the RuleRelation ('pg_rewrite') for all the tuples that has
1755+ * the same ev_class as the relation being checked.
1756+ */
1757+ ScanKeyEntryInitialize (& scanKeyData ,
1758+ 0 ,
1759+ Anum_pg_rewrite_ev_class ,
1760+ F_OIDEQ ,
1761+ ObjectIdGetDatum (rel -> rd_id ));
1762+ scanDesc = heap_beginscan (RewriteRelation ,
1763+ 0 , SnapshotNow , 1 , & scanKeyData );
1764+
1765+ while (HeapTupleIsValid (tuple = heap_getnext (scanDesc , 0 )))
1766+ {
1767+ if (tuple -> t_data != NULL )
1768+ {
1769+ data = (Form_pg_rewrite ) GETSTRUCT (tuple );
1770+ if (data -> ev_type == '1' )
1771+ {
1772+ retval = 1 ;
1773+ break ;
1774+ }
1775+ }
1776+
1777+ }
17421778
1779+ heap_endscan (scanDesc );
1780+ heap_close (RewriteRelation , RowExclusiveLock );
1781+
17431782 return retval ;
17441783}
0 commit comments