Search in sources :

Example 31 with DBMeta

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

the class HpFixedConditionQueryResolver method resolveOverRelation.

// ===================================================================================
// Resolve Over Relation
// =====================
protected String resolveOverRelation(String fixedCondition, boolean fixedInline) {
    // analyze:
    // - "$$over($localTable.memberSecurity)$$.REMINDER_QUESTION"
    // - "$$over($foreignTable.memberStatus, DISPLAY_ORDER)$$.ORDER_NO"
    // - "$$over(PURCHASE.product.productStatus)$$.PRODUCT_STATUS_NAME"
    final String relationBeginMark = getRelationBeginMark();
    final String relationEndMark = getRelationEndMark();
    String resolvedClause = fixedCondition;
    String remainder = resolvedClause;
    while (true) {
        // "|$$over(|$localTable.memberSecurity)$$.REMINDER_QUESTION"
        final IndexOfInfo relationBeginIndex = Srl.indexOfFirst(remainder, relationBeginMark);
        if (relationBeginIndex == null) {
            break;
        }
        remainder = relationBeginIndex.substringRear();
        // "$localTable.memberSecurity|)$$|.REMINDER_QUESTION"
        final IndexOfInfo relationEndIndex = Srl.indexOfFirst(remainder, relationEndMark);
        if (relationEndIndex == null) {
            break;
        }
        // remainder is e.g. "$localTable.memberSecurity)$$" now
        // e.g. "$localTable.memberSecurity" or "$foreignTable.memberStatus, DISPLAY_ORDER"
        final String relationExp = relationEndIndex.substringFront();
        // e.g. "$$over($localTable.memberSecurity)$$" or "$$over($foreignTable.memberStatus, DISPLAY_ORDER)$$"
        final String relationVariable = relationBeginMark + relationExp + relationEndMark;
        // e.g. "$localTable" or "$foreignTable" or "PURCHASE"
        final String pointTable;
        // e.g. "memberSecurity" or "product.productStatus" or null (means base only)
        final String targetRelation;
        // e.g. DISPLAY_ORDER or null (means no argument)
        final String secondArg;
        {
            final IndexOfInfo separatorIndex = Srl.indexOfFirst(relationExp, ".");
            if (separatorIndex != null) {
                // normally here
                // e.g. $localTable
                pointTable = separatorIndex.substringFrontTrimmed();
                final String separatorRear = separatorIndex.substringRearTrimmed();
                final IndexOfInfo argIndex = Srl.indexOfFirst(separatorRear, ",");
                targetRelation = argIndex != null ? argIndex.substringFrontTrimmed() : separatorRear;
                secondArg = argIndex != null ? argIndex.substringRearTrimmed() : null;
            } else {
                // e.g. "$$over(PURCHASE)$$"
                final IndexOfInfo argIndex = Srl.indexOfFirst(relationExp, ",");
                pointTable = argIndex != null ? argIndex.substringFrontTrimmed() : Srl.trim(relationExp);
                targetRelation = null;
                secondArg = argIndex != null ? argIndex.substringRearTrimmed() : null;
            }
        }
        final ConditionQuery relationPointCQ;
        final ConditionQuery columnTargetCQ;
        if (Srl.equalsPlain(pointTable, getLocalTableMark())) {
            // local table
            relationPointCQ = _localCQ;
            if (targetRelation != null) {
                columnTargetCQ = invokeColumnTargetCQ(relationPointCQ, targetRelation);
            } else {
                String notice = "The relation on fixed condition is required if the table is not referrer.";
                throwIllegalFixedConditionOverRelationException(notice, pointTable, null, fixedCondition);
                // unreachable
                return null;
            }
        } else if (Srl.equalsPlain(pointTable, getForeignTableMark())) {
            // foreign table
            relationPointCQ = _foreignCQ;
            columnTargetCQ = relationPointCQ;
            if (targetRelation == null) {
                String notice = "The relation on fixed condition is required if the table is not referrer.";
                throwIllegalFixedConditionOverRelationException(notice, pointTable, null, fixedCondition);
                // unreachable
                return null;
            }
            // prepare fixed InlineView
            if (_inlineViewResourceMap == null) {
                _inlineViewResourceMap = new LinkedHashMap<String, InlineViewResource>();
            }
            final InlineViewResource resource;
            if (_inlineViewResourceMap.containsKey(targetRelation)) {
                resource = _inlineViewResourceMap.get(targetRelation);
            } else {
                resource = new InlineViewResource();
                _inlineViewResourceMap.put(targetRelation, resource);
            }
            final String columnName;
            {
                // e.g. "$$over($localTable.memberSecurity)$$|.|REMINDER_QUESTION = ..."
                final IndexOfInfo rearIndex = Srl.indexOfFirst(relationEndIndex.substringRearTrimmed(), ".");
                if (rearIndex == null || rearIndex.getIndex() > 0) {
                    String notice = "The OverRelation variable should continue to column after the variable.";
                    throwIllegalFixedConditionOverRelationException(notice, pointTable, targetRelation, fixedCondition);
                    // unreachable
                    return null;
                }
                // e.g. REMINDER_QUESTION = ...
                final String columnStart = rearIndex.substringRear();
                final IndexOfInfo indexInfo = Srl.indexOfFirst(columnStart, " ", ",", ")", "\n", "\t");
                // REMINDER_QUESTION
                columnName = indexInfo != null ? indexInfo.substringFront() : columnStart;
            }
            // the secondArg should be a column DB name, and then rear column is alias name
            final String resolvedColumn = secondArg != null ? secondArg + " as " + columnName : columnName;
            // selected in in-line view
            resource.addAdditionalColumn(resolvedColumn);
            if (!resource.hasJoinInfo()) {
                // first analyze
                final List<String> traceList = Srl.splitList(targetRelation, ".");
                DBMeta currentDBMeta = _dbmetaProvider.provideDBMeta(_foreignCQ.asTableDbName());
                for (String trace : traceList) {
                    final ForeignInfo foreignInfo = currentDBMeta.findForeignInfo(trace);
                    resource.addJoinInfo(foreignInfo);
                    currentDBMeta = foreignInfo.getForeignDBMeta();
                }
            }
            final List<ForeignInfo> joinInfoList = resource.getJoinInfoList();
            if (!joinInfoList.isEmpty()) {
                // basically true (but just in case)
                final ForeignInfo latestForeignInfo = joinInfoList.get(joinInfoList.size() - 1);
                resource.addOptimizedVariable(relationVariable, latestForeignInfo);
            }
        } else {
            // referrer table
            final DBMeta pointDBMeta;
            try {
                pointDBMeta = _dbmetaProvider.provideDBMeta(pointTable);
            } catch (DBMetaNotFoundException e) {
                String notice = "The table for relation on fixed condition does not exist.";
                throwIllegalFixedConditionOverRelationException(notice, pointTable, targetRelation, fixedCondition, e);
                // unreachable
                return null;
            }
            ConditionQuery referrerQuery = _localCQ.xgetReferrerQuery();
            while (true) {
                if (referrerQuery == null) {
                    // means not found
                    break;
                }
                if (Srl.equalsPlain(pointDBMeta.getTableDbName(), referrerQuery.asTableDbName())) {
                    break;
                }
                referrerQuery = referrerQuery.xgetReferrerQuery();
            }
            relationPointCQ = referrerQuery;
            if (relationPointCQ == null) {
                String notice = "The table for relation on fixed condition was not found in the scope.";
                throwIllegalFixedConditionOverRelationException(notice, pointTable, targetRelation, fixedCondition);
                // unreachable
                return null;
            }
            if (targetRelation != null) {
                columnTargetCQ = invokeColumnTargetCQ(relationPointCQ, targetRelation);
            } else {
                columnTargetCQ = relationPointCQ;
            }
        }
        // resolve over-relation variables in clause
        // e.g. "dfrel_4"
        final String relationAlias = columnTargetCQ.xgetAliasName();
        resolvedClause = replaceString(resolvedClause, relationVariable, relationAlias);
        // after case for loop
        remainder = relationEndIndex.substringRear();
    // no replace even if same relation because of additional column
    // // to prevent from processing same one
    // remainder = replaceString(remainder, relationVariable, relationAlias);
    }
    resolvedClause = adjustOptimizedLine(resolvedClause);
    return resolvedClause;
}
Also used : ForeignInfo(org.dbflute.dbmeta.info.ForeignInfo) DBMeta(org.dbflute.dbmeta.DBMeta) IndexOfInfo(org.dbflute.util.Srl.IndexOfInfo) ConditionQuery(org.dbflute.cbean.ConditionQuery) ArrayList(java.util.ArrayList) List(java.util.List) DBMetaNotFoundException(org.dbflute.exception.DBMetaNotFoundException) LinkedHashMap(java.util.LinkedHashMap)

Example 32 with DBMeta

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

the class HpSDRFunction method doAssertConflictAliasName.

protected void doAssertConflictAliasName(String aliasName) {
    if (isPurposeNullAlias()) {
        return;
    }
    final String mappingAliasPrefix = DerivedMappable.MAPPING_ALIAS_PREFIX;
    final String realName;
    if (aliasName.startsWith(mappingAliasPrefix)) {
        realName = Srl.substringFirstRear(aliasName, mappingAliasPrefix);
    } else {
        realName = aliasName;
    }
    final String tableDbName = _baseCB.asTableDbName();
    final DBMeta dbmeta = _dbmetaProvider.provideDBMetaChecked(tableDbName);
    if (dbmeta.hasColumn(realName)) {
        throwSpecifyDerivedReferrerConflictAliasNameException(aliasName, dbmeta.findColumnInfo(realName));
    }
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta)

Example 33 with DBMeta

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

the class TransactionSavedRecentResult method prepareEntityResultMap.

// ===================================================================================
// Entity Result
// =============
protected Map<String, Object> prepareEntityResultMap(Entity entity) {
    final Map<String, Object> resultMap;
    final DBMeta dbmeta = entity.asDBMeta();
    if (dbmeta.hasPrimaryKey() && entity.hasPrimaryKeyValue()) {
        // mainly here
        resultMap = dbmeta.extractPrimaryKeyMap(entity);
    } else {
        // no PK table
        resultMap = prepareHashResultMap(entity.instanceHash());
    }
    return resultMap;
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta)

Example 34 with DBMeta

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

the class AbstractBehaviorWritable method injectSequenceToPrimaryKeyIfNeeds.

// -----------------------------------------------------
// Common
// ------
protected void injectSequenceToPrimaryKeyIfNeeds(Entity entity) {
    final DBMeta dbmeta = entity.asDBMeta();
    if (!dbmeta.hasSequence() || dbmeta.hasCompoundPrimaryKey() || entity.hasPrimaryKeyValue()) {
        return;
    }
    // basically property(column) type is same as next value type
    // so there is NOT type conversion cost when writing to the entity
    dbmeta.getPrimaryInfo().getFirstColumn().write(entity, readNextVal());
}
Also used : DBMeta(org.dbflute.dbmeta.DBMeta)

Example 35 with DBMeta

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

the class AbstractBehaviorWritable method helpReloadPrimaryKeyIfUniqueByIfNeeds.

protected <RESULT extends ENTITY> void helpReloadPrimaryKeyIfUniqueByIfNeeds(RESULT entity, UpdateOption<CB> option) {
    if (option == null || !option.isReloadPrimaryKeyIfUniqueBy()) {
        return;
    }
    final Set<String> uniqueProp = entity.myuniqueDrivenProperties();
    if (uniqueProp.isEmpty()) {
        // updated by PK normally
        return;
    }
    final DBMeta dbmeta = entity.asDBMeta();
    if (!dbmeta.hasPrimaryKey()) {
        // no PK table but has unique key
        return;
    }
    final CB cb = newConditionBean();
    final List<ColumnInfo> pkList = dbmeta.getPrimaryInfo().getPrimaryColumnList();
    for (ColumnInfo pk : pkList) {
        cb.invokeSpecifyColumn(pk.getPropertyName());
    }
    for (String uq : uniqueProp) {
        cb.localCQ().invokeQueryEqual(uq, dbmeta.findColumnInfo(uq).read(entity));
    }
    final Entity read = readEntityWithDeletedCheck(cb);
    dbmeta.acceptPrimaryKeyMap(entity, dbmeta.extractPrimaryKeyMap(read));
}
Also used : Entity(org.dbflute.Entity) DBMeta(org.dbflute.dbmeta.DBMeta) ColumnInfo(org.dbflute.dbmeta.info.ColumnInfo)

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