Search in sources :

Example 21 with Column

use of org.apache.torque.engine.database.model.Column in project dbflute-core by dbflute.

the class DfSPolicyChecker method doCheckTableColumn.

protected void doCheckTableColumn(DfSPolicyParsedPolicy policy, DfSPolicyResult result, Table table) {
    final DfSPolicyParsedPolicyPart tablePolicyPart = policy.getTablePolicyPart();
    _tableThemeChecker.checkTableTheme(tablePolicyPart.getThemeList(), result, table);
    _tableStatementChecker.checkTableStatement(tablePolicyPart.getStatementList(), result, table);
    final List<Column> columnList = table.getColumnList();
    final DfSPolicyParsedPolicyPart columnPolicyPart = policy.getColumnPolicyPart();
    for (Column column : columnList) {
        if (!isTargetColumn(column)) {
            continue;
        }
        _columnThemeChecker.checkColumnTheme(columnPolicyPart.getThemeList(), result, column);
        _columnStatementChecker.checkColumnStatement(columnPolicyPart.getStatementList(), result, column);
    }
}
Also used : Column(org.apache.torque.engine.database.model.Column) DfSPolicyParsedPolicyPart(org.dbflute.logic.doc.spolicy.parsed.DfSPolicyParsedPolicy.DfSPolicyParsedPolicyPart)

Example 22 with Column

use of org.apache.torque.engine.database.model.Column in project dbflute-core by dbflute.

the class DfSPolicyCrossSecretary method determineSameWhatIfSameColumnName.

protected String determineSameWhatIfSameColumnName(Column myColumn, Predicate<Column> yourTargeting, Function<Column, Object> valueProvider, boolean ignoreEmpty) {
    final Map<String, List<Column>> columnListMap = getSameNameColumnListMap(myColumn.getTable().getDatabase());
    final List<Column> columnList = columnListMap.getOrDefault(myColumn.getName(), DfCollectionUtil.emptyList());
    for (Column yourColumn : columnList) {
        if (myColumn.equals(yourColumn)) {
            // myself
            continue;
        }
        if (!yourTargeting.test(yourColumn)) {
            // non-target (e.g. by statement)
            continue;
        }
        final Object myValue = valueProvider.apply(myColumn);
        final Object yourValue = valueProvider.apply(yourColumn);
        if (ignoreEmpty && eitherEmpty(myValue, yourValue)) {
            continue;
        }
        if (!isEqual(myValue, yourValue)) {
            // different in spite of same column name
            return toColumnExp(myColumn) + "=" + myValue + ", " + toColumnExp(yourColumn) + "=" + yourValue;
        }
    }
    // no problem
    return null;
// memorable code before performance tuning
// for (Table yourTable : myTable.getDatabase().getTableList()) {
// if (!isTargetTable(yourTable)) { // non-target
// continue;
// }
// if (myTable.equals(yourTable)) { // myself
// continue;
// }
// final String myColumnName = myColumn.getName();
// final Column yourColumn = yourTable.getColumn(myColumnName);
// if (yourColumn != null) {
// if (!isTargetColumn(yourColumn)) { // non-target
// continue;
// }
// if (!yourTargeting.test(yourColumn)) { // non-target (e.g. by statement)
// continue;
// }
// final Object myValue = valueProvider.apply(myColumn);
// final Object yourValue = valueProvider.apply(yourColumn);
// if (ignoreEmpty && eitherEmpty(myValue, yourValue)) {
// continue;
// }
// if (!isEqual(myValue, yourValue)) { // different in spite of same column name
// return toColumnExp(myColumn) + "=" + myValue + ", " + toColumnExp(yourColumn) + "=" + yourValue;
// }
// }
// }
// return null;
}
Also used : Column(org.apache.torque.engine.database.model.Column) ArrayList(java.util.ArrayList) List(java.util.List)

Example 23 with Column

use of org.apache.torque.engine.database.model.Column in project dbflute-core by dbflute.

the class DfAdditionalForeignKeyInitializer method getForeignColumnNameList.

protected List<String> getForeignColumnNameList(String foreignKeyName, final String foreignTableName) {
    List<String> foreignColumnNameList = getForeignColumnNameList(foreignKeyName);
    if (foreignColumnNameList != null && !foreignColumnNameList.isEmpty()) {
        return foreignColumnNameList;
    }
    foreignColumnNameList = DfCollectionUtil.newArrayList();
    final List<Column> foreignPrimaryKeyList = getTable(foreignTableName).getPrimaryKey();
    if (foreignPrimaryKeyList.isEmpty()) {
        final ExceptionMessageBuilder br = new ExceptionMessageBuilder();
        br.addNotice("Not found primary key on the foreign table of additionalForeignKey.");
        br.addItem("Advice");
        br.addElement("Foreign table should have primary keys.");
        br.addItem("Additional FK");
        br.addElement(foreignKeyName);
        br.addItem("Foreign Table");
        br.addElement(foreignTableName);
        final String msg = br.buildExceptionMessage();
        throw new DfIllegalPropertySettingException(msg);
    }
    for (Column column : foreignPrimaryKeyList) {
        foreignColumnNameList.add(column.getName());
    }
    return foreignColumnNameList;
}
Also used : Column(org.apache.torque.engine.database.model.Column) ExceptionMessageBuilder(org.dbflute.helper.message.ExceptionMessageBuilder) DfIllegalPropertySettingException(org.dbflute.exception.DfIllegalPropertySettingException)

Example 24 with Column

use of org.apache.torque.engine.database.model.Column 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);
            }
        }
    }
}
Also used : Table(org.apache.torque.engine.database.model.Table) Column(org.apache.torque.engine.database.model.Column) DfLittleAdjustmentProperties(org.dbflute.properties.DfLittleAdjustmentProperties) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Example 25 with Column

use of org.apache.torque.engine.database.model.Column 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");
    }
}
Also used : Column(org.apache.torque.engine.database.model.Column) ForeignKey(org.apache.torque.engine.database.model.ForeignKey)

Aggregations

Column (org.apache.torque.engine.database.model.Column)50 Table (org.apache.torque.engine.database.model.Table)16 List (java.util.List)10 Map (java.util.Map)8 ForeignKey (org.apache.torque.engine.database.model.ForeignKey)8 ArrayList (java.util.ArrayList)6 LinkedHashMap (java.util.LinkedHashMap)6 Database (org.apache.torque.engine.database.model.Database)5 DfBuildProperties (org.dbflute.DfBuildProperties)4 DfDataRow (org.dbflute.helper.dataset.DfDataRow)4 DfDataTable (org.dbflute.helper.dataset.DfDataTable)4 ExceptionMessageBuilder (org.dbflute.helper.message.ExceptionMessageBuilder)4 DfLanguageDependency (org.dbflute.logic.generate.language.DfLanguageDependency)4 Function (java.util.function.Function)3 File (java.io.File)2 IOException (java.io.IOException)2 SQLException (java.sql.SQLException)2 Entry (java.util.Map.Entry)2 Set (java.util.Set)2 Predicate (java.util.function.Predicate)2