📄 createconstraintconstantaction.java
字号:
// the two cases of Source creation and Target replication. At the source // database, we allocate a new UUID. At the Target, we just use the UUID that // the Source sent along. UUID constraintId = uuidFactory.createUUID(); /* Now, lets create the constraint descriptor */ DataDescriptorGenerator ddg = dd.getDataDescriptorGenerator(); switch (constraintType) { case DataDictionary.PRIMARYKEY_CONSTRAINT: conDesc = ddg.newPrimaryKeyConstraintDescriptor( td, constraintName, false, //deferable, false, //initiallyDeferred, genColumnPositions(td, false), //int[], constraintId, indexId, sd, enabled, 0 // referenceCount ); dd.addConstraintDescriptor(conDesc, tc); break; case DataDictionary.UNIQUE_CONSTRAINT: conDesc = ddg.newUniqueConstraintDescriptor( td, constraintName, false, //deferable, false, //initiallyDeferred, genColumnPositions(td, false), //int[], constraintId, indexId, sd, enabled, 0 // referenceCount ); dd.addConstraintDescriptor(conDesc, tc); break; case DataDictionary.CHECK_CONSTRAINT: conDesc = ddg.newCheckConstraintDescriptor( td, constraintName, false, //deferable, false, //initiallyDeferred, constraintId, constraintText, new ReferencedColumnsDescriptorImpl(genColumnPositions(td, false)), //int[], sd, enabled ); dd.addConstraintDescriptor(conDesc, tc); break; case DataDictionary.FOREIGNKEY_CONSTRAINT: ReferencedKeyConstraintDescriptor referencedConstraint = DDUtils.locateReferencedConstraint ( dd, td, constraintName, columnNames, otherConstraintInfo ); DDUtils.validateReferentialActions(dd, td, constraintName, otherConstraintInfo,columnNames); conDesc = ddg.newForeignKeyConstraintDescriptor( td, constraintName, false, //deferable, false, //initiallyDeferred, genColumnPositions(td, false), //int[], constraintId, indexId, sd, referencedConstraint, enabled, otherConstraintInfo.getReferentialActionDeleteRule(), otherConstraintInfo.getReferentialActionUpdateRule() ); // try to create the constraint first, because it // is expensive to do the bulk check, find obvious // errors first dd.addConstraintDescriptor(conDesc, tc); /* No need to do check if we're creating a * table. */ if ( (! forCreateTable) && dd.activeConstraint( conDesc ) ) { validateFKConstraint(tc, dd, (ForeignKeyConstraintDescriptor)conDesc, referencedConstraint, ((CreateIndexConstantAction)indexAction).getIndexTemplateRow()); } /* Create stored dependency on the referenced constraint */ dm.addDependency(conDesc, referencedConstraint, lcc.getContextManager()); break; default: if (SanityManager.DEBUG) { SanityManager.THROWASSERT("contraintType (" + constraintType + ") has unexpected value"); } break; } /* Create stored dependencies for each provider */ if (providerInfo != null) { for (int ix = 0; ix < providerInfo.length; ix++) { Provider provider = null; /* We should always be able to find the Provider */ try { provider = (Provider) providerInfo[ix]. getDependableFinder(). getDependable( providerInfo[ix].getObjectId()); } catch(java.sql.SQLException te) { if (SanityManager.DEBUG) { SanityManager.THROWASSERT("unexpected java.sql.SQLException - " + te); } } dm.addDependency(conDesc, provider, lcc.getContextManager()); } } /* Finally, invalidate off of the table descriptor(s) * to ensure that any dependent statements get * re-compiled. */ if (! forCreateTable) { dm.invalidateFor(td, DependencyManager.CREATE_CONSTRAINT, lcc); } if (constraintType == DataDictionary.FOREIGNKEY_CONSTRAINT) { if (SanityManager.DEBUG) { SanityManager.ASSERT(conDesc != null, "conDesc expected to be non-null"); if (! (conDesc instanceof ForeignKeyConstraintDescriptor)) { SanityManager.THROWASSERT( "conDesc expected to be instance of ForeignKeyConstraintDescriptor, not " + conDesc.getClass().getName()); } } dm.invalidateFor( ((ForeignKeyConstraintDescriptor)conDesc). getReferencedConstraint(). getTableDescriptor(), DependencyManager.CREATE_CONSTRAINT, lcc); } } /** * Is the constant action for a foreign key * * @return true/false */ public boolean isForeignKeyConstraint() { return (constraintType == DataDictionary.FOREIGNKEY_CONSTRAINT); } /** * Generate an array of column positions for the column list in * the constraint. * * @param td The TableDescriptor for the table in question * @param columnsMustBeOrderable true for primaryKey and unique constraints * * @return int[] The column positions. */ private int[] genColumnPositions(TableDescriptor td, boolean columnsMustBeOrderable) throws StandardException { int[] baseColumnPositions; // Translate the base column names to column positions baseColumnPositions = new int[columnNames.length]; for (int i = 0; i < columnNames.length; i++) { ColumnDescriptor columnDescriptor; // Look up the column in the data dictionary columnDescriptor = td.getColumnDescriptor(columnNames[i]); if (columnDescriptor == null) { throw StandardException.newException(SQLState.LANG_COLUMN_NOT_FOUND_IN_TABLE, columnNames[i], tableName); } // Don't allow a column to be created on a non-orderable type // (for primaryKey and unique constraints) if ( columnsMustBeOrderable && ( ! columnDescriptor.getType().getTypeId().orderable( cf)) ) { throw StandardException.newException(SQLState.LANG_COLUMN_NOT_ORDERABLE_DURING_EXECUTION, columnDescriptor.getType().getTypeId().getSQLTypeName()); } // Remember the position in the base table of each column baseColumnPositions[i] = columnDescriptor.getPosition(); } return baseColumnPositions; } /////////////////////////////////////////////////////////////////////// // // ACCESSORS // /////////////////////////////////////////////////////////////////////// /** * Get the names of the columns touched by this constraint. * * @return the array of touched column names. */ public String[] getColumnNames() { return columnNames; } /** * Get the text defining this constraint. * * @return constraint text */ public String getConstraintText() { return constraintText; } public String toString() { // Do not put this under SanityManager.DEBUG - it is needed for // error reporting. StringBuffer strbuf = new StringBuffer(); strbuf.append( "CREATE CONSTRAINT " + constraintName ); strbuf.append("\n=========================\n"); if (columnNames == null) { strbuf.append("columnNames == null\n"); } else { for (int ix=0; ix < columnNames.length; ix++) { strbuf.append("\n\tcol["+ix+"]"+columnNames[ix].toString()); } } strbuf.append("\n"); strbuf.append(constraintText); strbuf.append("\n"); if (otherConstraintInfo != null) { strbuf.append(otherConstraintInfo.toString()); } strbuf.append("\n"); return strbuf.toString(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -