8
8
*
9
9
*
10
10
* 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 $
12
12
*
13
13
* NOTES
14
14
* The PerformAddAttribute() code, like most of the relation
26
26
#include "catalog/indexing.h"
27
27
#include "catalog/pg_attrdef.h"
28
28
#include "catalog/pg_opclass.h"
29
+ #include "catalog/pg_rewrite.h"
29
30
#include "commands/command.h"
30
31
#include "executor/spi.h"
31
32
#include "catalog/heap.h"
55
56
56
57
57
58
static 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 );
59
61
60
62
61
63
@@ -1100,7 +1102,7 @@ AlterTableAddConstraint(char *relationName,
1100
1102
#endif
1101
1103
1102
1104
/* check to see if the table to be constrained is a view. */
1103
- if (is_view (relationName , "ALTER TABLE" ))
1105
+ if (is_viewr (relationName ))
1104
1106
elog (ERROR , "ALTER TABLE: Cannot add constraints to views." );
1105
1107
1106
1108
switch (nodeTag (newConstraint ))
@@ -1250,7 +1252,7 @@ AlterTableAddConstraint(char *relationName,
1250
1252
}
1251
1253
1252
1254
/* 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 ))
1254
1256
elog (ERROR , "ALTER TABLE: Cannot add constraints to views." );
1255
1257
1256
1258
/*
@@ -1698,11 +1700,11 @@ LockTableCommand(LockStmt *lockstmt)
1698
1700
Relation rel ;
1699
1701
int aclresult ;
1700
1702
1701
- if (is_view (lockstmt -> relname , "LOCK TABLE" ))
1702
- elog (ERROR , "LOCK TABLE: cannot lock a view" );
1703
-
1704
1703
rel = heap_openr (lockstmt -> relname , NoLock );
1705
1704
1705
+ if (is_view (rel ))
1706
+ elog (ERROR , "LOCK TABLE: cannot lock a view" );
1707
+
1706
1708
if (lockstmt -> mode == AccessShareLock )
1707
1709
aclresult = pg_aclcheck (lockstmt -> relname , GetUserId (), ACL_RD );
1708
1710
else
@@ -1719,26 +1721,63 @@ LockTableCommand(LockStmt *lockstmt)
1719
1721
1720
1722
static
1721
1723
bool
1722
- is_view (char * relname , char * command )
1724
+ is_viewr (char * name )
1723
1725
{
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
+ }
1742
1778
1779
+ heap_endscan (scanDesc );
1780
+ heap_close (RewriteRelation , RowExclusiveLock );
1781
+
1743
1782
return retval ;
1744
1783
}
0 commit comments