Search in sources :

Example 1 with DBMeta

use of org.dbflute.dbmeta.DBMeta in project lastaflute by lastaflute.

the class TransactionSavedRecentResult method prepareEntityUpdateKeyMap.

// ===================================================================================
// Entity Update
// =============
protected Map<String, Object> prepareEntityUpdateKeyMap(BehaviorCommandMeta meta) {
    // always can get if entity update
    final Entity entity = extractArgumentEntity(meta);
    if (entity == null) {
        // no way, just in case
        return Collections.emptyMap();
    }
    final DBMeta dbmeta = entity.asDBMeta();
    final Map<String, Object> keyMap;
    final Set<String> uniqueProps = entity.myuniqueDrivenProperties();
    if (!uniqueProps.isEmpty()) {
        final Map<String, Object> uniqueMap = uniqueProps.stream().map(prop -> {
            return dbmeta.findColumnInfo(prop);
        }).collect(Collectors.toMap(col -> col.getColumnDbName(), col -> col.read(entity)));
        keyMap = uniqueMap;
    } else if (dbmeta.hasPrimaryKey() && entity.hasPrimaryKeyValue()) {
        keyMap = dbmeta.extractPrimaryKeyMap(entity);
    } else {
        // no way if entity update, just in case
        keyMap = Collections.emptyMap();
    }
    return keyMap;
}
Also used : BehaviorCommandMeta(org.dbflute.bhv.core.BehaviorCommandMeta) DfCollectionUtil(org.dbflute.util.DfCollectionUtil) BehaviorCommand(org.dbflute.bhv.core.BehaviorCommand) Logger(org.slf4j.Logger) Entity(org.dbflute.Entity) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) DBMeta(org.dbflute.dbmeta.DBMeta) Collectors(java.util.stream.Collectors) LinkedHashMap(java.util.LinkedHashMap) List(java.util.List) Map(java.util.Map) Collections(java.util.Collections) Entity(org.dbflute.Entity) DBMeta(org.dbflute.dbmeta.DBMeta)

Example 2 with DBMeta

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

the class AbstractSqlClause method createSelectedSelectColumnInfo.

protected Map<String, SelectedRelationColumn> createSelectedSelectColumnInfo(String foreignTableAliasName, String localTableDbName, String foreignPropertyName, String localRelationPath) {
    final DBMeta dbmeta = findDBMeta(localTableDbName);
    final ForeignInfo foreignInfo = dbmeta.findForeignInfo(foreignPropertyName);
    final int relationNo = foreignInfo.getRelationNo();
    String nextRelationPath = RELATION_PATH_DELIMITER + relationNo;
    if (localRelationPath != null) {
        nextRelationPath = localRelationPath + nextRelationPath;
    }
    final Map<String, SelectedRelationColumn> resultMap = new LinkedHashMap<String, SelectedRelationColumn>();
    final DBMeta foreignDBMeta = foreignInfo.getForeignDBMeta();
    final List<ColumnInfo> columnInfoList = foreignDBMeta.getColumnInfoList();
    for (ColumnInfo columnInfo : columnInfoList) {
        final String columnDbName = columnInfo.getColumnDbName();
        final SelectedRelationColumn selectColumnInfo = new SelectedRelationColumn();
        selectColumnInfo.setTableAliasName(foreignTableAliasName);
        selectColumnInfo.setColumnInfo(columnInfo);
        selectColumnInfo.setRelationNoSuffix(nextRelationPath);
        resultMap.put(columnDbName, selectColumnInfo);
    }
    return resultMap;
}
Also used : ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) DBMeta(org.dbflute.dbmeta.DBMeta) SelectedRelationColumn(org.dbflute.cbean.sqlclause.select.SelectedRelationColumn) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) LinkedHashMap(java.util.LinkedHashMap)

Example 3 with DBMeta

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

the class AbstractSqlClause method findDBMeta.

protected DBMeta findDBMeta(String tableDbName) {
    DBMeta dbmeta = getCachedDBMetaMap().get(tableDbName);
    if (dbmeta != null) {
        return dbmeta;
    }
    if (_dbmetaProvider == null) {
        String msg = "The DB meta provider should not be null when using findDBMeta(): " + tableDbName;
        throw new IllegalStateException(msg);
    }
    dbmeta = _dbmetaProvider.provideDBMetaChecked(tableDbName);
    getCachedDBMetaMap().put(tableDbName, dbmeta);
    return dbmeta;
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta)

Example 4 with DBMeta

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

the class AbstractSqlClause method getClauseQueryDelete.

// -----------------------------------------------------
// Query Delete
// ------------
public String getClauseQueryDelete() {
    final DBMeta dbmeta = getDBMeta();
    final StringBuilder sb = new StringBuilder();
    sb.append("delete");
    final boolean useQueryUpdateDirect = isUseQueryUpdateDirect(dbmeta);
    String whereClause = null;
    if (useQueryUpdateDirect) {
        // prepare for direct case
        whereClause = processSubQueryIndent(getWhereClause());
        if (needsDeleteTableAliasHint(whereClause)) {
            sb.append(" ").append(getBasePointAliasName());
        }
    }
    sb.append(" from ").append(dbmeta.getTableSqlName());
    if (useQueryUpdateDirect) {
        // direct (in-scope unsupported or compound primary keys)
        buildQueryUpdateDirectClause(null, whereClause, dbmeta, sb);
    } else {
        // basically here
        buildQueryUpdateInScopeClause(null, dbmeta, sb);
    }
    return sb.toString();
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta)

Example 5 with DBMeta

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

the class AbstractSqlClause method processSelectClauseLocal.

protected int processSelectClauseLocal(StringBuilder sb) {
    final String basePointAliasName = getBasePointAliasName();
    final DBMeta dbmeta = getDBMeta();
    final Map<String, SpecifiedColumn> localSpecifiedMap;
    if (_specifiedSelectColumnMap != null) {
        localSpecifiedMap = _specifiedSelectColumnMap.get(basePointAliasName);
    } else {
        localSpecifiedMap = null;
    }
    final List<ColumnInfo> columnInfoList;
    final boolean validSpecifiedLocal;
    final boolean selectClauseTypeUniqueScalar = isSelectClauseTypeUniqueScalar();
    // unique scalar, has PK, specified scalar, specified column
    ColumnInfo specifiedUniqueScalarColumnInfo = null;
    if (selectClauseTypeUniqueScalar) {
        // it always has union-query because it's handled before this process
        if (dbmeta.hasPrimaryKey()) {
            columnInfoList = new ArrayList<ColumnInfo>();
            columnInfoList.addAll(dbmeta.getPrimaryInfo().getPrimaryColumnList());
            if (isSelectClauseTypeSpecifiedScalar()) {
                final ColumnInfo specifiedColumn = getSpecifiedColumnInfoAsOne();
                if (specifiedColumn != null && !specifiedColumn.isPrimary()) {
                    specifiedUniqueScalarColumnInfo = specifiedColumn;
                    columnInfoList.add(specifiedColumn);
                }
            // derivingSubQuery is handled after this process
            }
        } else {
            // all columns are target if no-PK and unique-scalar and union-query
            columnInfoList = dbmeta.getColumnInfoList();
        }
        // because specified columns are fixed here
        validSpecifiedLocal = false;
    } else {
        columnInfoList = dbmeta.getColumnInfoList();
        validSpecifiedLocal = localSpecifiedMap != null && !localSpecifiedMap.isEmpty();
    }
    // because 1 origin in JDBC
    int selectIndex = 0;
    boolean needsDelimiter = false;
    for (ColumnInfo columnInfo : columnInfoList) {
        final String columnDbName = columnInfo.getColumnDbName();
        final ColumnSqlName columnSqlName = columnInfo.getColumnSqlName();
        if (_pkOnlySelectForcedlyEnabled && !columnInfo.isPrimary()) {
            continue;
        }
        if (validSpecifiedLocal && !localSpecifiedMap.containsKey(columnDbName)) {
            // a case for scalar-select has been already resolved here
            continue;
        }
        if (canBeNullObjectSpecifiedColumn(columnInfo)) {
            registerColumnNullObject(basePointAliasName, columnInfo);
            continue;
        }
        if (needsDelimiter) {
            sb.append(", ");
        } else {
            sb.append("select");
            appendSelectHint(sb);
            sb.append(" ");
            needsDelimiter = true;
        }
        final String realAliasName;
        if (selectClauseTypeUniqueScalar) {
            if (specifiedUniqueScalarColumnInfo != null && columnInfo.equals(specifiedUniqueScalarColumnInfo)) {
                // has PK, specified column in unique scalar
                // might be relation column
                realAliasName = getSpecifiedColumnTableAliasNameAsOne();
            } else {
                // has no PK
                realAliasName = basePointAliasName;
            }
        } else {
            realAliasName = basePointAliasName;
        }
        final String realColumnName = realAliasName + "." + columnSqlName;
        final String onQueryName;
        ++selectIndex;
        if (_useSelectIndex) {
            final String entityNo = BASE_POINT_HANDLING_ENTITY_NO;
            onQueryName = buildSelectIndexAlias(columnSqlName, null, selectIndex, entityNo);
            registerSelectIndex(entityNo, columnDbName, onQueryName, selectIndex);
        } else {
            onQueryName = columnSqlName.toString();
        }
        sb.append(decryptSelectColumnIfNeeds(columnInfo, realColumnName)).append(" as ").append(onQueryName);
        getSelectClauseRealColumnAliasMap().put(realColumnName, onQueryName);
        if (validSpecifiedLocal && localSpecifiedMap.containsKey(columnDbName)) {
            final SpecifiedColumn specifiedColumn = localSpecifiedMap.get(columnDbName);
            // basically for queryInsert()
            specifiedColumn.setOnQueryName(onQueryName);
        }
    }
    return selectIndex;
}
Also used : ColumnSqlName(org.dbflute.dbmeta.name.ColumnSqlName) DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo) SpecifiedColumn(org.dbflute.cbean.dream.SpecifiedColumn)

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