Search in sources :

Example 66 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfStructExtractorOracle method extractStructInfoMap.

// ===================================================================================
// Extract
// =======
/**
 * Extract the map of struct info. <br>
 * The info is so simple, for example, no nested info.
 * And this sets type name and attributes only.
 * @param unifiedSchema The unified schema. (NotNull)
 * @return The map of struct info. {key = schema.struct-type-name} (NotNull)
 */
public StringKeyMap<DfTypeStructInfo> extractStructInfoMap(UnifiedSchema unifiedSchema) {
    final List<Map<String, String>> resultList = selectStructAttribute(unifiedSchema);
    final StringKeyMap<DfTypeStructInfo> structInfoMap = StringKeyMap.createAsFlexibleOrdered();
    for (Map<String, String> map : resultList) {
        final String typeName = DfTypeStructInfo.generateTypeName(unifiedSchema, map.get("TYPE_NAME"));
        DfTypeStructInfo info = structInfoMap.get(typeName);
        if (info == null) {
            info = new DfTypeStructInfo(unifiedSchema, typeName);
            structInfoMap.put(typeName, info);
        }
        final DfColumnMeta attributeInfo = new DfColumnMeta();
        final String attrName = map.get("ATTR_NAME");
        if (Srl.is_Null_or_TrimmedEmpty(attrName)) {
            continue;
        }
        attributeInfo.setColumnName(attrName);
        final String dbTypeName;
        {
            // ARRAY and STRUCT only
            final String attrTypeOwner = map.get("ATTR_TYPE_OWNER");
            final String attrTypeName = map.get("ATTR_TYPE_NAME");
            dbTypeName = Srl.connectPrefix(attrTypeName, attrTypeOwner, ".");
        }
        attributeInfo.setDbTypeName(dbTypeName);
        final String length = map.get("LENGTH");
        if (Srl.is_NotNull_and_NotTrimmedEmpty(length)) {
            // for example, varchar2
            attributeInfo.setColumnSize(Integer.valueOf(length));
        } else {
            final String precision = map.get("PRECISION");
            if (Srl.is_NotNull_and_NotTrimmedEmpty(precision)) {
                // for example, number
                attributeInfo.setColumnSize(Integer.valueOf(precision));
            }
        }
        final String scale = map.get("SCALE");
        if (Srl.is_NotNull_and_NotTrimmedEmpty(scale)) {
            attributeInfo.setDecimalDigits(Integer.valueOf(scale));
        }
        info.putAttributeInfo(attributeInfo);
    }
    return structInfoMap;
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) DfTypeStructInfo(org.dbflute.logic.jdbc.metadata.info.DfTypeStructInfo) Map(java.util.Map) StringKeyMap(org.dbflute.helper.StringKeyMap)

Example 67 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfSql2EntityTask method setupSql2EntityRelatedTable.

protected Table setupSql2EntityRelatedTable(String entityName, DfCustomizeEntityInfo entityInfo, Map<String, DfColumnMeta> metaMap, String columnName, Column column, String pkRelatedTableName) {
    final DfColumnMeta columnMeta = metaMap.get(columnName);
    final String sql2EntityRelatedTableName = columnMeta.getSql2EntityRelatedTableName();
    // first attack
    Table relatedTable = getRelatedTable(sql2EntityRelatedTableName);
    if (relatedTable == null) {
        if (pkRelatedTableName != null) {
            // second attack using PK-related
            relatedTable = getRelatedTable(pkRelatedTableName);
            if (relatedTable == null) {
                throwTableRelatedPrimaryKeyNotFoundException(entityName, entityInfo, pkRelatedTableName, columnName);
            }
        } else {
            return null;
        }
    } else {
        if (pkRelatedTableName != null) {
            if (!Srl.equalsFlexible(sql2EntityRelatedTableName, pkRelatedTableName)) {
                throwTableRelatedPrimaryKeyDifferentException(entityName, entityInfo, sql2EntityRelatedTableName, pkRelatedTableName, columnName);
            }
        }
    }
    column.setSql2EntityRelatedTable(relatedTable);
    return relatedTable;
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) Table(org.apache.torque.engine.database.model.Table)

Example 68 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfSql2EntityTask method setupSql2EntityRelatedColumn.

protected Column setupSql2EntityRelatedColumn(String entityName, DfCustomizeEntityInfo entityInfo, Table relatedTable, Map<String, DfColumnMeta> metaMap, String columnName, Column column) {
    if (relatedTable == null) {
        return null;
    }
    final DfColumnMeta metaInfo = metaMap.get(columnName);
    final String sql2EntityRelatedColumnName = metaInfo.getSql2EntityRelatedColumnName();
    final Column relatedColumn = relatedTable.getColumn(sql2EntityRelatedColumnName);
    if (relatedColumn == null) {
        return null;
    }
    column.setSql2EntityRelatedColumn(relatedColumn);
    return column;
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) Column(org.apache.torque.engine.database.model.Column)

Example 69 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfSql2EntityTask method initControlContext.

// ===================================================================================
// Prepare Generation
// ==================
@Override
public Context initControlContext() throws Exception {
    _log.info("");
    _log.info("...Preparing generation of customize-entities and parameter-beans");
    _log.info("* * * * * * * * * *");
    _log.info("* CustomizeEntity *");
    _log.info("* * * * * * * * * *");
    final StringBuilder logSb = new StringBuilder();
    final Database database = _database;
    database.setSql2EntitySchemaData(_schemaData);
    database.setPmbMetaDataMap(_sql2entityMeta.getPmbMetaDataMap());
    database.setSkipDeleteOldClass(isSkipDeleteOldClass());
    final Map<String, DfCustomizeEntityInfo> entityInfoMap = _sql2entityMeta.getEntityInfoMap();
    final Set<String> entityNameSet = entityInfoMap.keySet();
    for (String entityName : entityNameSet) {
        final DfCustomizeEntityInfo entityInfo = entityInfoMap.get(entityName);
        final Map<String, DfColumnMeta> metaMap = entityInfo.getColumnMap();
        final DfOutsideSqlFile outsideSqlFile = entityInfo.getOutsideSqlFile();
        final Table tbl = new Table();
        tbl.setSql2EntityCustomize(true);
        if (outsideSqlFile != null) {
            // basically true but checked just in case
            tbl.setSql2EntitySqlFile(outsideSqlFile);
        }
        tbl.setName(entityInfo.getTableDbName());
        if (!entityInfo.needsJavaNameConvert()) {
            // basically here (except STRUCT type)
            tbl.suppressJavaNameConvert();
        }
        if (entityInfo.hasNestedCustomizeEntity()) {
            // basically when STRUCT type
            tbl.setSql2EntityCustomizeHasNested(true);
        }
        if (entityInfo.isAdditionalSchema()) {
            // basically when STRUCT type
            tbl.setUnifiedSchema(entityInfo.getAdditionalSchema());
        }
        tbl.setSql2EntityTypeSafeCursor(entityInfo.isCursorHandling());
        buildCustomizeEntityTitle(logSb, entityName, entityInfo);
        final StringKeyMap<String> pkMap = getPrimaryKeyMap(entityInfo);
        final boolean allCommonColumn = hasAllCommonColumn(metaMap);
        final Set<String> columnNameSet = metaMap.keySet();
        for (String columnName : columnNameSet) {
            final Column column = new Column();
            setupColumnName(columnName, column);
            // an element removed from pkMap if true
            // and a table name related to primary key is returned
            final String pkRelatedTableName = setupPrimaryKey(pkMap, entityName, columnName, column);
            setupTorqueType(metaMap, columnName, column, allCommonColumn);
            setupDbType(metaMap, columnName, column);
            setupNotNull(metaMap, columnName, column);
            setupColumnSizeContainsDigit(metaMap, columnName, column);
            setupColumnComment(metaMap, columnName, column);
            setupSql2EntityElement(entityName, entityInfo, metaMap, columnName, column, pkRelatedTableName, logSb);
            tbl.addColumn(column);
        }
        if (!pkMap.isEmpty()) {
            // if not-removed columns exist
            throwPrimaryKeyNotFoundException(entityName, pkMap, columnNameSet);
        }
        if (entityInfo.isScalarHandling()) {
            // it does not generate an only-one-column entity
            // one-way love for utility (just in case)
            tbl.setDatabase(database);
            processScalarHandling(entityInfo, tbl);
        } else if (entityInfo.isDomainHandling()) {
            // it does not generate an customize-entity
            // one-way love for utility (just in case)
            tbl.setDatabase(database);
            processDomainHandling(entityInfo, tbl);
        } else {
            // initialize a class name of the entity for typed parameter-bean
            // should be before getting names
            database.addTable(tbl);
            entityInfo.setEntityClassName(tbl.getExtendedEntityClassName());
            entityInfo.setImmutableClassName(tbl.getImmutableExtendedEntityClassName());
        }
        logSb.append(ln());
    }
    final String databaseType = getDatabaseTypeFacadeProp().getTargetDatabase();
    final AppData appData = new AppData(databaseType);
    appData.addDatabase(database);
    showCustomizeEntity(logSb);
    showParameterBean();
    final VelocityContext context = createVelocityContext(appData);
    return context;
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta) Table(org.apache.torque.engine.database.model.Table) VelocityContext(org.apache.velocity.VelocityContext) DfCustomizeEntityInfo(org.dbflute.logic.sql2entity.cmentity.DfCustomizeEntityInfo) DfOutsideSqlFile(org.dbflute.logic.sql2entity.analyzer.DfOutsideSqlFile) Column(org.apache.torque.engine.database.model.Column) AppData(org.apache.torque.engine.database.model.AppData) Database(org.apache.torque.engine.database.model.Database)

Example 70 with DfColumnMeta

use of org.dbflute.logic.jdbc.metadata.info.DfColumnMeta in project dbflute-core by dbflute.

the class DfSql2EntityTask method setupNotNull.

protected void setupNotNull(Map<String, DfColumnMeta> metaMap, String columnName, Column column) {
    // basically not meta data so false, true might be from select column specification
    final DfColumnMeta columnMeta = metaMap.get(columnName);
    column.setNotNull(columnMeta.isRequired());
}
Also used : DfColumnMeta(org.dbflute.logic.jdbc.metadata.info.DfColumnMeta)

Aggregations

DfColumnMeta (org.dbflute.logic.jdbc.metadata.info.DfColumnMeta)71 SQLException (java.sql.SQLException)16 Connection (java.sql.Connection)10 LinkedHashMap (java.util.LinkedHashMap)9 Map (java.util.Map)8 File (java.io.File)7 StringKeyMap (org.dbflute.helper.StringKeyMap)7 ResultSet (java.sql.ResultSet)5 Statement (java.sql.Statement)5 ArrayList (java.util.ArrayList)5 DfJDBCException (org.dbflute.exception.DfJDBCException)5 FileInputStream (java.io.FileInputStream)4 BigDecimal (java.math.BigDecimal)4 DatabaseMetaData (java.sql.DatabaseMetaData)4 PreparedStatement (java.sql.PreparedStatement)4 Timestamp (java.sql.Timestamp)4 HashMap (java.util.HashMap)4 StringSet (org.dbflute.helper.StringSet)4 DfDataRow (org.dbflute.helper.dataset.DfDataRow)4 DfDataTable (org.dbflute.helper.dataset.DfDataTable)4