use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfSPolicyTableStatementChecker method doEvaluateColumnThenItemValue.
protected String doEvaluateColumnThenItemValue(DfSPolicyStatement statement, DfSPolicyThenPart thenPart, Table table, Function<String, String> violationCall) {
final String thenItem = thenPart.getThenItem();
final String thenValue = thenPart.getThenValue();
final boolean notThenValue = thenPart.isNotThenValue();
if (thenItem.equalsIgnoreCase("tableName")) {
// e.g. tableName is prefix:CLS_
final String tableName = toComparingTableName(table);
if (!isHitExp(statement, tableName, toTableNameComparingThenValue(table, thenValue)) == !notThenValue) {
return violationCall.apply(tableName);
}
} else if (thenItem.equalsIgnoreCase("alias")) {
// e.g. alias is suffix:History
final String alias = table.getAlias();
if (!isHitExp(statement, alias, toAliasComparingThenValue(table, thenValue)) == !notThenValue) {
return violationCall.apply(alias);
}
} else if (thenItem.equalsIgnoreCase("comment")) {
// e.g. comment is contain:SEA
final String comment = table.getComment();
if (!isHitExp(statement, comment, thenValue) == !notThenValue) {
return violationCall.apply(comment);
}
} else if (thenItem.equalsIgnoreCase("pkName")) {
// e.g. pkName is prefix:PK_
if (table.hasPrimaryKey()) {
final List<Column> columnList = table.getPrimaryKey();
// same name if compound
final Column pk = columnList.get(0);
final String pkName = pk.getPrimaryKeyName();
final String comparingThenValue = toConstraintNameComparingThenValue(table, thenValue, columnList);
if (!isHitExp(statement, pkName, comparingThenValue) == !notThenValue) {
final String disp = pkName + (pk.isAdditionalPrimaryKey() ? ADDITIONAL_SUFFIX : "");
return violationCall.apply(disp);
}
}
} else if (thenItem.equalsIgnoreCase("fkName")) {
// e.g. fkName is prefix:FK_
for (ForeignKey fk : table.getForeignKeyList()) {
final String fkName = fk.getName();
final String comparingThenValue = toConstraintNameComparingThenValue(table, thenValue, fk.getLocalColumnList());
if (!isHitExp(statement, fkName, comparingThenValue) == !notThenValue) {
final String disp = fkName + (fk.isAdditionalForeignKey() ? ADDITIONAL_SUFFIX : "");
return violationCall.apply(disp);
}
}
} else if (thenItem.equalsIgnoreCase("uniqueName")) {
// e.g. uniqueName is prefix:UQ_
for (Unique uq : table.getUniqueList()) {
final String uqName = uq.getName();
final String comparingThenValue = toConstraintNameComparingThenValue(table, thenValue, uq.getColumnList());
if (!isHitExp(statement, uqName, comparingThenValue)) {
final String disp = uqName + (uq.isAdditional() ? ADDITIONAL_SUFFIX : "");
return violationCall.apply(disp);
}
}
} else if (thenItem.equalsIgnoreCase("indexName")) {
// e.g. indexName is prefix:IX_
for (Index ix : table.getIndexList()) {
final String ixName = ix.getName();
final String comparingThenValue = toConstraintNameComparingThenValue(table, thenValue, ix.getColumnList());
if (!isHitExp(statement, ixName, comparingThenValue) == !notThenValue) {
return violationCall.apply(ixName);
}
}
} else if (thenItem.equalsIgnoreCase("pk_columnName")) {
return determinePkSomethingThenValue(statement, table, violationCall, thenValue, notThenValue, pk -> toComparingColumnName(pk));
} else if (thenItem.equalsIgnoreCase("pk_dbType") || thenItem.equalsIgnoreCase("pkDbType")) {
// for compatible
return determinePkSomethingThenValue(statement, table, violationCall, thenValue, notThenValue, pk -> pk.getDbType());
} else if (thenItem.equalsIgnoreCase("pk_size")) {
return determinePkSomethingThenValue(statement, table, violationCall, thenValue, notThenValue, pk -> pk.getColumnSize());
} else if (thenItem.equalsIgnoreCase("pk_dbType_with_size")) {
// e.g. char(3)
return determinePkSomethingThenValue(statement, table, violationCall, thenValue, notThenValue, pk -> toComparingDbTypeWithSize(pk));
} else {
throwSchemaPolicyCheckIllegalIfThenStatementException(statement, "Unknown then-item: " + thenItem);
}
// no violation
return null;
}
use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfAdditionalForeignKeyInitializer method setupForeignKeyToTable.
protected void setupForeignKeyToTable(String foreignKeyName, String foreignTableName, List<String> foreignColumnNameList, Table table, List<String> localColumnNameList, DfAdditionalForeignKeyOption option) {
// set up foreign key instance
final ForeignKey fk = createAdditionalForeignKey(foreignKeyName, foreignTableName, localColumnNameList, foreignColumnNameList, option);
reflectOptionToExistingFKIfNeeds(foreignKeyName, option, fk);
table.addForeignKey(fk);
// set up referrer instance
final Table foreignTable = getTable(foreignTableName);
final boolean canBeReferrer = foreignTable.addReferrer(fk);
if (canBeReferrer) {
for (String foreignColumnName : foreignColumnNameList) {
final Column foreignColumn = foreignTable.getColumn(foreignColumnName);
foreignColumn.addReferrer(fk);
}
} else {
_log.info(" *Referrer setting was not allowed in this case");
}
// set up implicit reverse foreign key if fixed condition is valid
// (and if a same-structured FK does not exist)
// because biz-one-to-one needs reverse foreign key for ConditionBean's Specify
// ...
// ...
// Sorry, I forgot the detail of the reason...
// ...
// ...
// (2014/09/18)
// actually, no problem for generation if suppressed
// so suppressed (deprecated) as default since 1.1
final DfLittleAdjustmentProperties prop = getLittleAdjustmentProperties();
if (prop.isCompatibleBizOneToOneImplicitReverseFkAllowed()) {
// basically false since 1.1
if (fk.hasFixedCondition() && !isSuppressImplicitReverseFK(foreignKeyName)) {
// but compatible just in case
if (!fk.isFixedReferrer()) {
processImplicitReverseForeignKey(fk, table, foreignTable, localColumnNameList, foreignColumnNameList, option);
}
}
}
}
use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfAdditionalForeignKeyInitializer method processImplicitReverseForeignKey.
protected void processImplicitReverseForeignKey(ForeignKey correspondingFk, Table table, Table foreignTable, List<String> localColumnNameList, List<String> foreignColumnNameList, DfAdditionalForeignKeyOption option) {
// called only when a fixed condition exists
// name is "FK_ + foreign + local" because it's reversed
final String localTableName = table.getTableDbName();
final String foreignTableName = foreignTable.getTableDbName();
final String reverseName = buildReverseFKName(localTableName, foreignTableName);
final String comment = "Implicit Reverse FK to " + correspondingFk.getName();
final List<Column> primaryKey = table.getPrimaryKey();
if (localColumnNameList.size() != primaryKey.size()) {
// may be biz-many-to-one (not biz-one-to-one)
return;
}
for (String localColumnName : localColumnNameList) {
final Column localColumn = table.getColumn(localColumnName);
if (!localColumn.isPrimaryKey()) {
// basically no way because a fixed condition exists
return;
// and FK to unique key is unsupported
}
}
// here all local columns are elements of primary key
if (foreignTable.existsForeignKey(localTableName, foreignColumnNameList, localColumnNameList)) {
// same-structured FK already exists
return;
}
String fixedSuffix = null;
if (foreignTable.hasForeignTableContainsOne(table)) {
final StringBuilder sb = new StringBuilder();
sb.append("By");
for (String foreignColumnName : foreignColumnNameList) {
sb.append(foreignTable.getColumn(foreignColumnName).getJavaName());
}
fixedSuffix = sb.toString();
}
DfAdditionalForeignKeyOption implicitRes = new DfAdditionalForeignKeyOption();
implicitRes.setFixedSuffix(fixedSuffix);
implicitRes.setComment(comment);
final ForeignKey fk = createAdditionalForeignKey(reverseName, localTableName, foreignColumnNameList, localColumnNameList, implicitRes);
fk.setImplicitReverseForeignKey(true);
foreignTable.addForeignKey(fk);
final boolean canBeReferrer = table.addReferrer(fk);
if (canBeReferrer) {
// basically true because foreign columns are PK and no fixed condition
for (String localColumnName : localColumnNameList) {
final Column localColumn = table.getColumn(localColumnName);
localColumn.addReferrer(fk);
}
_log.info(" *Reversed FK was also made implicitly");
}
}
use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfAdditionalForeignKeyInitializer method processOneTableFK.
protected void processOneTableFK(String foreignKeyName, String localTableName, String foreignTableName, List<String> foreignColumnNameList, DfAdditionalForeignKeyOption option) {
assertLocalTable(foreignKeyName, localTableName);
final Table table = getTable(localTableName);
final boolean searchFromExistingFK = false;
final boolean errorIfNotFound = true;
final List<String> localColumnNameList = getLocalColumnNameList(table, foreignKeyName, foreignTableName, foreignColumnNameList, localTableName, option, searchFromExistingFK, errorIfNotFound);
assertLocalTableColumn(foreignKeyName, localTableName, localColumnNameList, option);
// check same foreign key existence
final String fixedSuffix = getFixedSuffix(foreignKeyName);
final ForeignKey existingFK = table.findExistingForeignKey(foreignTableName, localColumnNameList, foreignColumnNameList, fixedSuffix);
if (existingFK != null) {
_log.info("The foreign key has already set up: " + foreignKeyName + "(" + fixedSuffix + ")");
reflectOptionToExistingFKIfNeeds(foreignKeyName, option, existingFK);
return;
}
setupForeignKeyToTable(foreignKeyName, foreignTableName, foreignColumnNameList, table, localColumnNameList, option);
showResult(foreignTableName, foreignColumnNameList, table, localColumnNameList, option);
}
use of org.apache.torque.engine.database.model.ForeignKey in project dbflute-core by dbflute.
the class DfClsDeploymentInitializer method initializeClassificationDeployment.
// ===================================================================================
// Initialize
// ==========
public Map<String, Map<String, String>> initializeClassificationDeployment(Database database) {
// this should be called immediately after creating schema data
// it should be called after additional-foreign-key initialization
// so no modification for now
_log.info("...Initializing ClassificationDeployment: project={}", database.getProjectName());
// only overridden if called with same database
if (_allColumnClassificationMap != null) {
final List<Table> tableList = database.getTableList();
for (Table table : tableList) {
final Map<String, String> columnClsMap = getColumnClsMap(_existingDeploymentMap, table.getTableDbName());
for (Entry<String, String> entry : _allColumnClassificationMap.entrySet()) {
columnClsMap.put(entry.getKey(), entry.getValue());
}
}
}
_classificationDefinitionInitializer.process();
for (Entry<String, DfClassificationElement> entry : _tableClassificationMap.entrySet()) {
final DfClassificationElement element = entry.getValue();
if (element.getClassificationTop().isSuppressAutoDeploy()) {
continue;
}
final Map<String, String> columnClsMap = getColumnClsMap(_existingDeploymentMap, element.getTable());
final String classificationName = element.getClassificationName();
registerColumnClsIfNeeds(columnClsMap, element.getCode(), classificationName);
final Table table = database.getTable(element.getTable());
if (table == null || table.hasCompoundPrimaryKey()) {
continue;
}
final Column column = table.getColumn(element.getCode());
if (column == null || !column.isPrimaryKey()) {
continue;
}
final List<ForeignKey> referrers = column.getReferrers();
for (ForeignKey referrer : referrers) {
if (!referrer.isSimpleKeyFK()) {
continue;
}
final Table referrerTable = referrer.getTable();
final String referrerTableDbName = referrerTable.getTableDbName();
final Map<String, String> referrerClsMap = getColumnClsMap(_existingDeploymentMap, referrerTableDbName);
final Column localColumnAsOne = referrer.getLocalColumnAsOne();
registerColumnClsIfNeeds(referrerClsMap, localColumnAsOne.getName(), classificationName);
}
}
return _existingDeploymentMap;
}
Aggregations