Search in sources :

Example 26 with Table

use of org.apache.torque.engine.database.model.Table 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 27 with Table

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

Example 28 with Table

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

the class DfAdditionalPrimaryKeyInitializer method initializeAdditionalPrimaryKey.

// ===================================================================================
// AdditionalPrimaryKey
// ====================
public void initializeAdditionalPrimaryKey() {
    _log.info("/=======================================");
    _log.info("...Initializing additional primary keys.");
    final Map<String, Map<String, String>> additionalPrimaryKeyMap = getAdditionalPrimaryKeyMap();
    final Set<String> primaryNameKeySet = additionalPrimaryKeyMap.keySet();
    for (String primaryKeyName : primaryNameKeySet) {
        final String tableName = getTableName(primaryKeyName);
        assertTable(primaryKeyName, tableName);
        final List<String> columnNameList = getLocalColumnNameList(primaryKeyName);
        assertColumnList(primaryKeyName, tableName, columnNameList);
        final Table table = getTable(tableName);
        if (table.hasPrimaryKey()) {
            String pkDisp = table.getPrimaryKeyDispValueString();
            String msg = "The primary key of the table has already set up:";
            msg = msg + " tableName=" + tableName + " existing primaryKey=" + pkDisp;
            msg = msg + " your specified primaryKey=" + columnNameList;
            _log.info(msg);
            continue;
        }
        for (String columnName : columnNameList) {
            final Column column = table.getColumn(columnName);
            column.setPrimaryKey(true);
            column.setAdditionalPrimaryKey(true);
        }
        showResult(primaryKeyName, table, columnNameList);
    }
    _log.info("==========/");
}
Also used : Table(org.apache.torque.engine.database.model.Table) Column(org.apache.torque.engine.database.model.Column) Map(java.util.Map)

Example 29 with Table

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

the class DfIncludeQueryInitializer method checkQueryMap.

protected void checkQueryMap(Map<String, Map<String, Map<String, List<String>>>> map) {
    final String allMark = DfIncludeQueryProperties.ALL_MARK;
    final String commonColumnMark = DfIncludeQueryProperties.COMMON_COLUMN_MARK;
    final String versionNoMark = DfIncludeQueryProperties.VERSION_NO_MARK;
    final String typeMark = DfIncludeQueryProperties.TYPE_MARK;
    final String[] hintMarks = DfNameHintUtil.getMarkList().toArray(new String[] {});
    for (Entry<String, Map<String, Map<String, List<String>>>> entry : map.entrySet()) {
        final String propType = entry.getKey();
        final Map<String, Map<String, List<String>>> ckeyMap = entry.getValue();
        _log.info(propType);
        for (Entry<String, Map<String, List<String>>> ckeyEntry : ckeyMap.entrySet()) {
            final String ckey = ckeyEntry.getKey();
            final Map<String, List<String>> tableColumnMap = ckeyEntry.getValue();
            final Set<String> tableElementKeySet = tableColumnMap.keySet();
            _log.info("  " + ckey + " -> " + tableElementKeySet);
            for (String tableName : tableElementKeySet) {
                final boolean allTable = tableName.equalsIgnoreCase(allMark);
                final boolean markTable = Srl.containsAnyIgnoreCase(tableName, hintMarks);
                final boolean pureTable = !allTable && !markTable;
                Table targetTable = null;
                if (pureTable) {
                    // check existence
                    targetTable = _tableFinder.findTable(tableName);
                    if (targetTable == null) {
                        throwIncludeQueryTableNotFoundException(ckey, tableName, map);
                    }
                }
                List<String> columnNameList = null;
                try {
                    columnNameList = tableColumnMap.get(tableName);
                } catch (ClassCastException e) {
                    // also type check
                    throwIncludeQueryNotListColumnSpecificationException(ckey, tableName, map, e);
                }
                if (pureTable) {
                    for (String columnName : columnNameList) {
                        final boolean commonColumn = columnName.equalsIgnoreCase(commonColumnMark);
                        final boolean versionNo = columnName.equalsIgnoreCase(versionNoMark);
                        final boolean columnType = Srl.containsAnyIgnoreCase(columnName, typeMark);
                        final boolean columnHint = Srl.containsAnyIgnoreCase(columnName, hintMarks);
                        if (!commonColumn && !versionNo && !columnType && !columnHint) {
                            // check existence
                            final Column targetColumn = targetTable.getColumn(columnName);
                            if (targetColumn == null) {
                                throwIncludeQueryColumnNotFoundException(ckey, tableName, columnName, map);
                            }
                        }
                    }
                }
            }
        }
    }
}
Also used : Table(org.apache.torque.engine.database.model.Table) Column(org.apache.torque.engine.database.model.Column) List(java.util.List) Map(java.util.Map)

Example 30 with Table

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

the class DfSql2EntityTask method setupColumnComment.

protected void setupColumnComment(Map<String, DfColumnMeta> metaMap, String columnName, Column column) {
    final DfColumnMeta columnMeta = metaMap.get(columnName);
    final String sql2EntityRelatedTableName = columnMeta.getSql2EntityRelatedTableName();
    final Table relatedTable = getRelatedTable(sql2EntityRelatedTableName);
    String relatedComment = null;
    if (relatedTable != null) {
        final String relatedColumnName = columnMeta.getSql2EntityRelatedColumnName();
        final Column relatedColumn = relatedTable.getColumn(relatedColumnName);
        if (relatedColumn != null) {
            relatedComment = relatedColumn.getPlainComment();
        }
    }
    // the meta has its select column comment
    final String selectColumnComment = columnMeta.getColumnComment();
    final String commentMark = "// ";
    final String delimiter = getAliasDelimiterInDbComment();
    final StringBuilder sb = new StringBuilder();
    if (Srl.is_NotNull_and_NotTrimmedEmpty(relatedComment)) {
        sb.append(relatedComment);
        if (Srl.is_NotNull_and_NotTrimmedEmpty(selectColumnComment)) {
            // both exist
            if (Srl.is_NotNull_and_NotTrimmedEmpty(delimiter)) {
                // use alias option
                if (relatedComment.contains(delimiter)) {
                    // resolved in related comment
                    sb.append(ln()).append(commentMark).append(selectColumnComment);
                } else {
                    // unresolved yet
                    if (isDbCommentOnAliasBasis()) {
                        // related comment is alias
                        sb.append(delimiter);
                    } else {
                        // related comment is description
                        sb.append(ln());
                    }
                    sb.append(commentMark).append(selectColumnComment);
                }
            } else {
                // no alias option
                sb.append(ln()).append(commentMark).append(selectColumnComment);
            }
        }
    } else {
        // not found related comment
        if (Srl.is_NotNull_and_NotTrimmedEmpty(selectColumnComment)) {
            if (Srl.is_NotNull_and_NotTrimmedEmpty(delimiter)) {
                // use alias option
                if (isDbCommentOnAliasBasis()) {
                    // select column comment is treated as description
                    sb.append(delimiter);
                }
            }
            sb.append(commentMark).append(selectColumnComment);
        }
    }
    column.setPlainComment(sb.toString());
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) Table(org.apache.torque.engine.database.model.Table) Column(org.apache.torque.engine.database.model.Column)

Aggregations

Table (org.apache.torque.engine.database.model.Table)58 Column (org.apache.torque.engine.database.model.Column)16 ArrayList (java.util.ArrayList)15 List (java.util.List)14 File (java.io.File)11 LinkedHashMap (java.util.LinkedHashMap)10 Map (java.util.Map)10 ForeignKey (org.apache.torque.engine.database.model.ForeignKey)9 HashSet (java.util.HashSet)8 DfDataTable (org.dbflute.helper.dataset.DfDataTable)8 Database (org.apache.torque.engine.database.model.Database)6 IOException (java.io.IOException)4 Entry (java.util.Map.Entry)4 DfBuildProperties (org.dbflute.DfBuildProperties)4 DfDataSet (org.dbflute.helper.dataset.DfDataSet)4 HashMap (java.util.HashMap)3 Function (java.util.function.Function)3 DfLReverseOutputResource (org.dbflute.logic.doc.lreverse.DfLReverseOutputResource)3 BufferedWriter (java.io.BufferedWriter)2 FileOutputStream (java.io.FileOutputStream)2