Search in sources :

Example 46 with DBMeta

use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.

the class TnRowCreatorExtension method createRowCreator.

/**
 * @param beanClass The class of target bean to find DB-meta. (NullAllowed)
 * @return The instance of internal row creator. (NotNull)
 */
public static TnRowCreatorExtension createRowCreator(Class<?> beanClass) {
    final TnRowCreatorExtension rowCreator = new TnRowCreatorExtension();
    if (beanClass != null) {
        final DBMeta dbmeta = findDBMetaByClass(beanClass);
        if (dbmeta != null) {
            rowCreator.setFixedDBMeta(dbmeta);
            rowCreator.setCreatableByDBMeta(isCreatableByDBMeta(beanClass, dbmeta.getEntityType()));
        }
    }
    return rowCreator;
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta)

Example 47 with DBMeta

use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.

the class TnRowCreatorExtension method createRow.

// ===================================================================================
// Main
// ====
/**
 * {@inheritDoc}
 */
public Object createRow(ResultSet rs, Map<String, Map<String, Integer>> selectIndexMap, Map<String, TnPropertyMapping> propertyCache, Class<?> beanClass, ConditionBean cb) throws SQLException {
    if (propertyCache.isEmpty()) {
        String msg = "The propertyCache should not be empty: bean=" + beanClass.getName();
        throw new IllegalStateException(msg);
    }
    // temporary variable, for exception message, debug message
    String columnName = null;
    TnPropertyMapping mapping = null;
    String propertyName = null;
    Object selectedValue = null;
    ColumnInfo columnInfo = null;
    final Object row;
    final DBMeta dbmeta;
    if (_fixedDBMeta != null) {
        if (_creatableByDBMeta) {
            // mainly here
            final Entity entity = _fixedDBMeta.newEntity();
            reflectConditionBeanOptionToEntity(cb, entity);
            row = entity;
        } else {
            // e.g. manual-extended entity
            row = newBean(beanClass);
        }
        dbmeta = _fixedDBMeta;
    } else {
        // e.g. manual-created bean of outsideSql
        row = newBean(beanClass);
        // find just in case
        dbmeta = findCachedDBMeta(row);
    }
    try {
        if (dbmeta != null) {
            // mainly here
            // almost always true
            final boolean isEntity = row instanceof Entity;
            final Entity entityRow = isEntity ? (Entity) row : null;
            for (Entry<String, TnPropertyMapping> entry : propertyCache.entrySet()) {
                columnName = entry.getKey();
                mapping = entry.getValue();
                propertyName = mapping.getPropertyName();
                selectedValue = getValue(rs, columnName, mapping.getValueType(), selectIndexMap);
                columnInfo = mapping.getEntityColumnInfo();
                if (columnInfo != null && isEntity) {
                    columnInfo.write(entityRow, selectedValue);
                } else {
                    mapping.getPropertyAccessor().setValue(row, selectedValue);
                }
            }
            if (canHandleDerivedMap(row)) {
                processDerivedMap(rs, selectIndexMap, propertyCache, row);
            }
        } else {
            // not DBFlute entity
            for (Entry<String, TnPropertyMapping> entry : propertyCache.entrySet()) {
                columnName = entry.getKey();
                mapping = entry.getValue();
                propertyName = mapping.getPropertyName();
                selectedValue = getValue(rs, columnName, mapping.getValueType(), selectIndexMap);
                mapping.getPropertyAccessor().setValue(row, selectedValue);
            }
        }
        return row;
    } catch (ClassCastException e) {
        throwMappingClassCastException(row, dbmeta, mapping, selectedValue, e);
        // unreachable
        return null;
    } catch (SQLException e) {
        if (_log.isDebugEnabled()) {
            String msg = "Failed to get selected values while resultSet handling:";
            msg = msg + " target=" + DfTypeUtil.toClassTitle(beanClass) + "." + propertyName;
            _log.debug(msg);
        }
        throw e;
    }
}
Also used : Entity(org.dbflute.Entity) DBMeta(org.dbflute.dbmeta.DBMeta) TnPropertyMapping(org.dbflute.s2dao.metadata.TnPropertyMapping) SQLException(java.sql.SQLException) MappingClassCastException(org.dbflute.exception.MappingClassCastException) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

Example 48 with DBMeta

use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.

the class OutsideSqlContext method buildBehaviorSqlPackageName.

protected String buildBehaviorSqlPackageName() {
    final DBMeta dbmeta = _dbmetaProvider.provideDBMetaChecked(_tableDbName);
    final String behaviorType = dbmeta.getBehaviorTypeName();
    final String outsideSqlPackage = _outsideSqlPackage;
    if (outsideSqlPackage != null && outsideSqlPackage.trim().length() > 0) {
        return mergeBehaviorSqlPackage(behaviorType, outsideSqlPackage);
    } else {
        return behaviorType;
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta)

Example 49 with DBMeta

use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.

the class AbstractSqlClause method buildJoinTableClause.

protected void buildJoinTableClause(StringBuilder sb, LeftOuterJoinInfo joinInfo, String joinExp, boolean canBeInnerJoin) {
    final String foreignTableDbName = joinInfo.getForeignTableDbName();
    // basically for in-line view indent
    final int tablePos = 3 + joinExp.length();
    final DBMeta foreignDBMeta = findDBMeta(foreignTableDbName);
    final TableSqlName foreignTableSqlName = foreignDBMeta.getTableSqlName();
    final List<QueryClause> inlineWhereClauseList = joinInfo.getInlineWhereClauseList();
    final String tableExp;
    if (inlineWhereClauseList.isEmpty()) {
        tableExp = foreignTableSqlName.toString();
    } else {
        tableExp = getInlineViewClause(foreignTableSqlName, inlineWhereClauseList, tablePos);
    }
    if (joinInfo.hasFixedCondition()) {
        sb.append(joinInfo.resolveFixedInlineView(tableExp, canBeInnerJoin));
    } else {
        sb.append(tableExp);
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) QueryClause(org.dbflute.cbean.sqlclause.query.QueryClause) OrScopeQueryAndPartQueryClause(org.dbflute.cbean.sqlclause.query.OrScopeQueryAndPartQueryClause) StringQueryClause(org.dbflute.cbean.sqlclause.query.StringQueryClause) TableSqlName(org.dbflute.dbmeta.name.TableSqlName)

Example 50 with DBMeta

use of org.dbflute.dbmeta.DBMeta in project dbflute-core by dbflute.

the class AbstractSqlClause method getClauseQueryUpdate.

// -----------------------------------------------------
// Query Update
// ------------
public String getClauseQueryUpdate(Map<String, Object> columnParameterMap) {
    if (columnParameterMap == null) {
        String msg = "The argument 'columnParameterMap' should not be null.";
        throw new IllegalArgumentException(msg);
    }
    if (columnParameterMap.isEmpty()) {
        return null;
    }
    final DBMeta dbmeta = getDBMeta();
    final StringBuilder sb = new StringBuilder();
    sb.append("update ").append(dbmeta.getTableSqlName());
    if (isUseQueryUpdateDirect(dbmeta)) {
        // direct (in-scope unsupported or compound primary keys)
        final String whereClause = processSubQueryIndent(getWhereClause());
        buildQueryUpdateDirectClause(columnParameterMap, whereClause, dbmeta, sb);
    } else {
        // basically here
        buildQueryUpdateInScopeClause(columnParameterMap, dbmeta, sb);
    }
    return sb.toString();
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta)

Aggregations

DBMeta (org.dbflute.dbmeta.DBMeta)64 ColumnInfo (org.dbflute.dbmeta.info.ColumnInfo)32 ArrayList (java.util.ArrayList)13 ForeignInfo (org.dbflute.dbmeta.info.ForeignInfo)13 List (java.util.List)12 LinkedHashMap (java.util.LinkedHashMap)11 ColumnSqlName (org.dbflute.dbmeta.name.ColumnSqlName)10 Entity (org.dbflute.Entity)9 PrimaryInfo (org.dbflute.dbmeta.info.PrimaryInfo)9 ReferrerInfo (org.dbflute.dbmeta.info.ReferrerInfo)9 Method (java.lang.reflect.Method)8 ReflectionFailureException (org.dbflute.util.DfReflectionUtil.ReflectionFailureException)8 Map (java.util.Map)7 ColumnFunctionCipher (org.dbflute.cbean.cipher.ColumnFunctionCipher)7 RelationInfo (org.dbflute.dbmeta.info.RelationInfo)7 LocalDate (java.time.LocalDate)6 LocalDateTime (java.time.LocalDateTime)6 Collection (java.util.Collection)6 Date (java.util.Date)6 HashMap (java.util.HashMap)6