1 21 22 package org.apache.derby.impl.sql.compile; 23 24 import org.apache.derby.iapi.types.TypeId; 25 import org.apache.derby.iapi.sql.dictionary.DataDictionary; 26 import org.apache.derby.iapi.sql.dictionary.SchemaDescriptor; 27 import org.apache.derby.iapi.sql.dictionary.TableDescriptor; 28 import org.apache.derby.iapi.sql.dictionary.ColumnDescriptor; 29 import org.apache.derby.iapi.sql.conn.Authorizer; 30 import org.apache.derby.iapi.error.StandardException; 31 import org.apache.derby.iapi.services.sanity.SanityManager; 32 33 import org.apache.derby.iapi.error.StandardException; 34 import org.apache.derby.iapi.reference.SQLState; 35 36 import org.apache.derby.impl.sql.compile.ActivationClassBuilder; 37 import org.apache.derby.impl.sql.execute.ConstraintInfo; 38 39 import org.apache.derby.iapi.util.JBitSet; 40 import org.apache.derby.iapi.util.ReuseFactory; 41 import org.apache.derby.iapi.sql.dictionary.DDUtils; 42 43 48 49 public final class FKConstraintDefinitionNode extends ConstraintDefinitionNode 50 { 51 TableName refTableName; 52 ResultColumnList refRcl; 53 SchemaDescriptor refTableSd; 54 int refActionDeleteRule; int refActionUpdateRule; public void init( 57 Object constraintName, 58 Object refTableName, 59 Object fkRcl, 60 Object refRcl, 61 Object refActions) 62 { 63 super.init( 64 constraintName, 65 ReuseFactory.getInteger(DataDictionary.FOREIGNKEY_CONSTRAINT), 66 fkRcl, 67 null, 68 null, 69 null); 70 this.refRcl = (ResultColumnList) refRcl; 71 this.refTableName = (TableName) refTableName; 72 73 this.refActionDeleteRule = ((int[]) refActions)[0]; 74 this.refActionUpdateRule = ((int[]) refActions)[1]; 75 } 76 77 85 protected void bind(DDLStatementNode ddlNode, DataDictionary dd) throws StandardException 86 { 87 super.bind(ddlNode, dd); 88 89 refTableSd = getSchemaDescriptor(refTableName.getSchemaName()); 90 91 if (refTableSd.isSystemSchema()) 92 { 93 throw StandardException.newException(SQLState.LANG_NO_FK_ON_SYSTEM_SCHEMA); 94 } 95 96 if (refTableName.equals(ddlNode.getObjectName())) 98 return; 99 100 TableDescriptor td = getTableDescriptor(refTableName.getTableName(), refTableSd); 102 if (td == null) 103 throw StandardException.newException(SQLState.LANG_INVALID_FK_NO_REF_TAB, 104 getConstraintMoniker(), 105 refTableName.getTableName()); 106 107 getCompilerContext().pushCurrentPrivType(getPrivType()); 109 110 getCompilerContext().createDependency(td); 113 114 if (refRcl.size()==0 && (td.getPrimaryKey() != null)) 116 { 117 int[] refCols = td.getPrimaryKey().getReferencedColumns(); 119 for (int i=0; i<refCols.length; i++) 120 { 121 ColumnDescriptor cd = td.getColumnDescriptor(refCols[i]); 122 cd.setTableDescriptor(td); 125 if (isPrivilegeCollectionRequired()) 126 getCompilerContext().addRequiredColumnPriv(cd); 127 } 128 129 } 130 else 131 { 132 for (int i=0; i<refRcl.size(); i++) 133 { 134 ResultColumn rc = (ResultColumn) refRcl.elementAt(i); 135 ColumnDescriptor cd = td.getColumnDescriptor(rc.getName()); 136 if (cd != null) 137 { 138 cd.setTableDescriptor(td); 141 if (isPrivilegeCollectionRequired()) 142 getCompilerContext().addRequiredColumnPriv(cd); 143 } 144 } 145 } 146 getCompilerContext().popCurrentPrivType(); 147 } 148 149 public ConstraintInfo getReferencedConstraintInfo() 150 { 151 if (SanityManager.DEBUG) 152 { 153 SanityManager.ASSERT(refTableSd != null, 154 "You must call bind() before calling getConstraintInfo"); 155 } 156 return new ConstraintInfo(refTableName.getTableName(), refTableSd, 157 refRcl.getColumnNames(), refActionDeleteRule, 158 refActionUpdateRule); 159 } 160 161 public TableName getRefTableName() { return refTableName; } 162 163 int getPrivType() 164 { 165 return Authorizer.REFERENCES_PRIV; 166 } 167 } 168 | Popular Tags |