Search in sources :

Example 26 with DBMeta

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

the class InsertOption method xacceptInsertColumnModifiedPropertiesIfNeeds.

// -----------------------------------------------------
// Modified Properties
// -------------------
public void xacceptInsertColumnModifiedPropertiesIfNeeds(List<? extends Entity> entityList) {
    // internal
    if (entityList == null) {
        throw new IllegalArgumentException("The argument 'entityList' should not be null.");
    }
    if (_insertColumnSpecification != null) {
        // already specified
        return;
    }
    if (entityList.isEmpty()) {
        // do nothing
        return;
    }
    if (xisCompatibleBatchInsertDefaultEveryColumn()) {
        // every column for compatible
        return;
    }
    final Entity firstEntity = entityList.get(0);
    if (firstEntity.createdBySelect()) {
        // all columns e.g. copy insert
        specify(new SpecifyQuery<CB>() {

            public void specify(CB cb) {
                final List<ColumnInfo> infoList = firstEntity.asDBMeta().getColumnInfoList();
                for (ColumnInfo info : infoList) {
                    if (!info.isPrimary()) {
                        // except PK
                        cb.localSp().xspecifyColumn(info.getColumnDbName());
                    }
                }
            }
        });
    } else {
        // least common multiple or same-set columns
        final Set<String> targetProps = xgatherInsertColumnModifiedProperties(entityList, firstEntity);
        final DBMeta dbmeta = firstEntity.asDBMeta();
        specify(new SpecifyQuery<CB>() {

            public void specify(CB cb) {
                // you don't need to specify primary key because primary key has special handling
                for (String prop : targetProps) {
                    final ColumnInfo info = dbmeta.findColumnInfo(prop);
                    if (!info.isPrimary()) {
                        // except PK
                        cb.localSp().xspecifyColumn(info.getColumnDbName());
                    }
                }
            }
        });
    }
}
Also used : Entity(org.dbflute.Entity) DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) List(java.util.List)

Example 27 with DBMeta

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

the class InsertOption method xcheckSpecifiedInsertColumnPrimaryKey.

// -----------------------------------------------------
// Insert Process
// --------------
public void xcheckSpecifiedInsertColumnPrimaryKey() {
    // checked later by process if it needs
    if (_insertColumnSpecification == null) {
        return;
    }
    assertInsertColumnSpecifiedCB();
    final CB cb = _insertColumnSpecifiedCB;
    final String basePointAliasName = cb.getSqlClause().getBasePointAliasName();
    final DBMeta dbmeta = cb.asDBMeta();
    if (dbmeta.hasPrimaryKey()) {
        final PrimaryInfo pkInfo = dbmeta.getPrimaryInfo();
        final List<ColumnInfo> pkList = pkInfo.getPrimaryColumnList();
        for (ColumnInfo pk : pkList) {
            final String columnDbName = pk.getColumnDbName();
            if (cb.getSqlClause().hasSpecifiedSelectColumn(basePointAliasName, columnDbName)) {
                String msg = "PK columns should not be allowed to specify as update columns: " + columnDbName;
                throw new SpecifyUpdateColumnInvalidException(msg);
            }
        }
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) SpecifyUpdateColumnInvalidException(org.dbflute.exception.SpecifyUpdateColumnInvalidException) PrimaryInfo(org.dbflute.dbmeta.info.PrimaryInfo)

Example 28 with DBMeta

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

the class GreatWallOfOracleType method mappingOracleStructToEntity.

protected Entity mappingOracleStructToEntity(Object oracleStruct, Object entityType) throws SQLException {
    if (oracleStruct == null) {
        return null;
    }
    final Entity prototype;
    if (entityType instanceof Entity) {
        prototype = (Entity) entityType;
    } else if (entityType instanceof Class<?>) {
        prototype = (Entity) DfReflectionUtil.newInstance((Class<?>) entityType);
    } else {
        String msg = "The entityType should be entity instance or entity type: " + entityType;
        throw new IllegalArgumentException(msg);
    }
    final DBMeta dbmeta = prototype.asDBMeta();
    final Object[] attrs = toStandardStructAttributes(oracleStruct);
    return doMappingOracleStructToEntity(dbmeta, attrs);
}
Also used : Entity(org.dbflute.Entity) DBMeta(org.dbflute.dbmeta.DBMeta)

Example 29 with DBMeta

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

the class GreatWallOfOracleType method mappingEntityToOracleStruct.

protected Object mappingEntityToOracleStruct(Connection conn, Object paramExp, Entity entity) throws SQLException {
    final DBMeta dbmeta = entity.asDBMeta();
    final List<ColumnInfo> columnInfoList = dbmeta.getColumnInfoList();
    final List<Object> attrList = new ArrayList<Object>();
    for (ColumnInfo columnInfo : columnInfoList) {
        final Object propertyValue = columnInfo.read(entity);
        final Object mappedValue;
        if (propertyValue instanceof List<?>) {
            // array in struct
            // it works only when the element type is scalar
            // (but property type is Object because Sql2Entity does not support this)
            final List<?> nested = ((List<?>) propertyValue);
            final String arrayTypeName = columnInfo.getColumnDbType();
            final Class<?> nativeType = columnInfo.getObjectNativeType();
            Class<?> elementType = nativeType;
            if (List.class.isAssignableFrom(nativeType)) {
                elementType = columnInfo.getGenericType();
            } else if (Object.class.equals(nativeType) && DfCollectionUtil.hasValidElement(nested)) {
                final Class<?> firstElementType = DfCollectionUtil.findFirstElementType(nested);
                if (firstElementType != null) {
                    elementType = nested.iterator().next().getClass();
                }
            }
            mappedValue = mappingListToOracleArray(conn, paramExp, nested, arrayTypeName, elementType);
        } else if (propertyValue instanceof Entity) {
            // struct in struct
            // it works only when the entity structure matches with the struct type
            // (but property type is Object because Sql2Entity does not support this)
            mappedValue = mappingEntityToOracleStruct(conn, paramExp, (Entity) propertyValue);
        } else {
            mappedValue = mappingScalarToSqlValue(conn, paramExp, propertyValue, columnInfo);
        }
        attrList.add(mappedValue);
    }
    final String structTypeName = dbmeta.getTableSqlName().toString();
    return toOracleStruct(getOracleConnection(conn), structTypeName, attrList.toArray());
}
Also used : Entity(org.dbflute.Entity) DBMeta(org.dbflute.dbmeta.DBMeta) ArrayList(java.util.ArrayList) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) ArrayList(java.util.ArrayList) List(java.util.List)

Example 30 with DBMeta

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

the class HpFixedConditionQueryResolver method resolveFixedInlineView.

// ===================================================================================
// Resolve Fixed InlineView
// ========================
public String resolveFixedInlineView(String foreignTableSqlName, boolean treatedAsInnerJoin) {
    // so it can uses bridge variables here
    if (_inlineViewResourceMap == null || _inlineViewResourceMap.isEmpty()) {
        // not uses InlineView
        return foreignTableSqlName;
    }
    // alias is required because foreignTableSqlName may be (normal) InlineView
    final String baseAlias = "dffixedbase";
    final String baseIndent;
    if (treatedAsInnerJoin) {
        // ----------"    inner join "
        baseIndent = "               ";
    } else {
        // ----------"    left outer join "
        baseIndent = "                    ";
    }
    final StringBuilder joinSb = new StringBuilder();
    final Map<ForeignInfo, String> relationMap = new HashMap<ForeignInfo, String>();
    final List<String> additionalRealColumnList = new ArrayList<String>();
    // basically not null
    final String resolvedFixedCondition = _resolvedFixedCondition;
    String optimizedCondition = _inlineViewOptimizedCondition;
    int groupIndex = 0;
    for (InlineViewResource resource : _inlineViewResourceMap.values()) {
        final List<ForeignInfo> joinInfoList = resource.getJoinInfoList();
        final String aliasBase = "dffixedjoin";
        String preForeignAlias = null;
        String foreignAlias = null;
        int joinIndex = 0;
        final Map<ForeignInfo, String> foreignAliasMap = new HashMap<ForeignInfo, String>(joinInfoList.size());
        for (ForeignInfo joinInfo : joinInfoList) {
            if (relationMap.containsKey(joinInfo)) {
                // already joined
                // update previous alias
                preForeignAlias = relationMap.get(joinInfo);
                continue;
            }
            final TableSqlName foreignTable;
            final String localAlias;
            {
                final DBMeta foreignDBMeta = joinInfo.getForeignDBMeta();
                foreignTable = foreignDBMeta.getTableSqlName();
                localAlias = (preForeignAlias != null ? preForeignAlias : baseAlias);
                foreignAlias = aliasBase + "_" + groupIndex + "_" + joinIndex;
                preForeignAlias = foreignAlias;
            }
            joinSb.append(ln()).append(baseIndent);
            joinSb.append("     left outer join ").append(foreignTable).append(" ").append(foreignAlias);
            joinSb.append(" on ");
            final Map<ColumnInfo, ColumnInfo> columnInfoMap = joinInfo.getLocalForeignColumnInfoMap();
            int columnIndex = 0;
            for (Entry<ColumnInfo, ColumnInfo> localForeignEntry : columnInfoMap.entrySet()) {
                final ColumnInfo localColumnInfo = localForeignEntry.getKey();
                final ColumnInfo foreignColumninfo = localForeignEntry.getValue();
                if (columnIndex > 0) {
                    joinSb.append(" and ");
                }
                joinSb.append(localAlias).append(".").append(localColumnInfo.getColumnSqlName());
                joinSb.append(" = ").append(foreignAlias).append(".").append(foreignColumninfo.getColumnSqlName());
                ++columnIndex;
            }
            foreignAliasMap.put(joinInfo, foreignAlias);
            relationMap.put(joinInfo, foreignAlias);
            ++joinIndex;
        }
        if (optimizedCondition != null) {
            optimizedCondition = resolvedOptimizedCondition(optimizedCondition, resource, foreignAliasMap);
        }
        collectAdditionalRealColumnList(additionalRealColumnList, resolvedFixedCondition, resource, foreignAlias);
        ++groupIndex;
    }
    if (optimizedCondition != null) {
        // foreign alias for in-line view is resolved here
        optimizedCondition = replaceString(optimizedCondition, getForeignAliasMark(), baseAlias);
        optimizedCondition = filterSubQueryIndentMark(optimizedCondition, false, true);
    }
    final StringBuilder sqlSb = new StringBuilder();
    sqlSb.append("(select ").append(baseAlias).append(".*");
    for (String columnName : additionalRealColumnList) {
        sqlSb.append(", ").append(columnName);
    }
    sqlSb.append(ln()).append(baseIndent);
    sqlSb.append("   from ").append(foreignTableSqlName).append(" ").append(baseAlias);
    sqlSb.append(joinSb);
    if (optimizedCondition != null) {
        buildOptimizedInlineWhereClause(optimizedCondition, baseIndent, sqlSb);
    }
    sqlSb.append(ln()).append(baseIndent);
    sqlSb.append(")");
    return sqlSb.toString();
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ArrayList(java.util.ArrayList) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) DBMeta(org.dbflute.dbmeta.DBMeta) TableSqlName(org.dbflute.dbmeta.name.TableSqlName)

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