use of org.dbflute.cbean.ConditionQuery in project fess by codelibs.
the class EsAbstractConditionQuery method xgetLocationBase.
@Override
public String xgetLocationBase() {
final StringBuilder sb = new StringBuilder();
ConditionQuery query = this;
while (true) {
if (query.isBaseQuery()) {
sb.insert(0, CQ_PROPERTY + ".");
break;
} else {
final String foreignPropertyName = query.xgetForeignPropertyName();
if (foreignPropertyName == null) {
String msg = "The foreignPropertyName of the query should not be null:";
msg = msg + " query=" + query;
throw new IllegalStateException(msg);
}
sb.insert(0, CQ_PROPERTY + Srl.initCap(foreignPropertyName) + ".");
}
query = query.xgetReferrerQuery();
}
return sb.toString();
}
use of org.dbflute.cbean.ConditionQuery 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;
}
use of org.dbflute.cbean.ConditionQuery in project fess by codelibs.
the class EsAbstractConditionQuery method xgetLocationBase.
@Override
public String xgetLocationBase() {
final StringBuilder sb = new StringBuilder();
ConditionQuery query = this;
while (true) {
if (query.isBaseQuery()) {
sb.insert(0, CQ_PROPERTY + ".");
break;
} else {
final String foreignPropertyName = query.xgetForeignPropertyName();
if (foreignPropertyName == null) {
String msg = "The foreignPropertyName of the query should not be null:";
msg = msg + " query=" + query;
throw new IllegalStateException(msg);
}
sb.insert(0, CQ_PROPERTY + Srl.initCap(foreignPropertyName) + ".");
}
query = query.xgetReferrerQuery();
}
return sb.toString();
}
use of org.dbflute.cbean.ConditionQuery in project fess by codelibs.
the class EsAbstractConditionQuery method xgetLocationBase.
@Override
public String xgetLocationBase() {
final StringBuilder sb = new StringBuilder();
ConditionQuery query = this;
while (true) {
if (query.isBaseQuery()) {
sb.insert(0, CQ_PROPERTY + ".");
break;
} else {
final String foreignPropertyName = query.xgetForeignPropertyName();
if (foreignPropertyName == null) {
String msg = "The foreignPropertyName of the query should not be null:";
msg = msg + " query=" + query;
throw new IllegalStateException(msg);
}
sb.insert(0, CQ_PROPERTY + Srl.initCap(foreignPropertyName) + ".");
}
query = query.xgetReferrerQuery();
}
return sb.toString();
}
Aggregations