Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,20 @@ static EntityWhereString makeConditionWhere(String sqlString) {
String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
Datasource datasourceInfo);

/**
* Dumps the corresponding SQL string, using {@code delegator} (when available) to validate
* field/value type compatibility instead of assuming the {@code "default"} delegator.
* @param modelEntity the model of the entity
* @param entityConditionParams the effective parameters used to substitute '?' parameters
* @param datasourceInfo the model of the data source interpreting the SQL
* @param delegator the delegator actually being used to run this query, or {@code null} if unavailable
* @return the corresponding SQL string
*/
default String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
Datasource datasourceInfo, Delegator delegator) {
return makeWhereString(modelEntity, entityConditionParams, datasourceInfo);
}

/**
* Verifies that this condition expression is valid.
* @param modelEntity the model of the entity
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ public boolean isEmpty() {

@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, Datasource datasourceInfo) {
return condition.makeWhereString(modelEntity, entityConditionParams, datasourceInfo);
return makeWhereString(modelEntity, entityConditionParams, datasourceInfo, null);
}

@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
Datasource datasourceInfo, Delegator delegator) {
return condition.makeWhereString(modelEntity, entityConditionParams, datasourceInfo, delegator);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,14 @@ public boolean isEmpty() {

@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, Datasource datasourceInfo) {
return makeWhereString(modelEntity, entityConditionParams, datasourceInfo, null);
}

@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
Datasource datasourceInfo, Delegator delegator) {
StringBuilder sql = new StringBuilder();
operator.addSqlValue(sql, modelEntity, entityConditionParams, conditions, datasourceInfo);
operator.addSqlValue(sql, modelEntity, entityConditionParams, conditions, datasourceInfo, delegator);
return sql.toString();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
import org.apache.ofbiz.base.util.ObjectType;
import org.apache.ofbiz.base.util.UtilGenerics;
import org.apache.ofbiz.entity.Delegator;
import org.apache.ofbiz.entity.DelegatorFactory;
import org.apache.ofbiz.entity.GenericEntity;
import org.apache.ofbiz.entity.GenericEntityException;
import org.apache.ofbiz.entity.GenericModelException;
Expand Down Expand Up @@ -136,7 +135,13 @@ public boolean isEmpty() {
@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
Datasource datasourceInfo) {
checkRhsType(modelEntity, null);
return makeWhereString(modelEntity, entityConditionParams, datasourceInfo, null);
}

@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
Datasource datasourceInfo, Delegator delegator) {
checkRhsType(modelEntity, delegator);
StringBuilder sql = new StringBuilder();
operator.addSqlValue(sql, modelEntity, entityConditionParams, true, lhs, rhs, datasourceInfo);
return sql.toString();
Expand Down Expand Up @@ -174,7 +179,7 @@ public void accept(EntityConditionVisitor visitor) {
* @param delegator the delegator used to check the condition expression
*/
public void checkRhsType(ModelEntity modelEntity, Delegator delegator) {
if (EntityExpr.isNullField(rhs) || modelEntity == null) {
if (EntityExpr.isNullField(rhs) || modelEntity == null || delegator == null) {
return;
}

Expand All @@ -194,9 +199,7 @@ public void checkRhsType(ModelEntity modelEntity, Delegator delegator) {
}
}

// This will be the common case for now as the delegator isn't available where we want to do this
// we'll cheat a little here and assume the default delegator.
Delegator deleg = (delegator == null) ? DelegatorFactory.getDelegator("default") : delegator;
Delegator deleg = delegator;

String fieldName = null;
ModelField curField;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,20 @@ public void addSqlValue(StringBuilder sql, ModelEntity modelEntity, List<EntityC
*/
public void addSqlValue(StringBuilder sql, ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
List<? extends EntityCondition> conditionList, Datasource datasourceInfo) {
addSqlValue(sql, modelEntity, entityConditionParams, conditionList, datasourceInfo, null);
}

/**
* Add sql value.
* @param sql the sql
* @param modelEntity the model entity
* @param entityConditionParams the entity condition params
* @param conditionList the condition list
* @param datasourceInfo the datasource info
* @param delegator the delegator actually being used to run this query, or {@code null} if unavailable
*/
public void addSqlValue(StringBuilder sql, ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
List<? extends EntityCondition> conditionList, Datasource datasourceInfo, Delegator delegator) {
if (UtilValidate.isNotEmpty(conditionList)) {
boolean hadSomething = false;
Iterator<? extends EntityCondition> conditionIter = conditionList.iterator();
Expand All @@ -81,7 +95,7 @@ public void addSqlValue(StringBuilder sql, ModelEntity modelEntity, List<EntityC
hadSomething = true;
sql.append('(');
}
sql.append(condition.makeWhereString(modelEntity, entityConditionParams, datasourceInfo));
sql.append(condition.makeWhereString(modelEntity, entityConditionParams, datasourceInfo, delegator));
}
if (hadSomething) {
sql.append(')');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,15 @@ public boolean isEmpty() {

@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams, Datasource datasourceInfo) {
return makeWhereString(modelEntity, entityConditionParams, datasourceInfo, null);
}

@Override
public String makeWhereString(ModelEntity modelEntity, List<EntityConditionParam> entityConditionParams,
Datasource datasourceInfo, Delegator delegator) {
return new StringBuilder()
.append("NOT(")
.append(condition.makeWhereString(modelEntity, entityConditionParams, datasourceInfo))
.append(condition.makeWhereString(modelEntity, entityConditionParams, datasourceInfo, delegator))
.append(')')
.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ public int updateByCondition(ModelEntity modelEntity, Map<String, ? extends Obje
params.add(new EntityConditionParam(field, entry.getValue()));
}
}
sql.append(" WHERE ").append(condition.makeWhereString(modelEntity, params, this.datasource));
sql.append(" WHERE ").append(condition.makeWhereString(modelEntity, params, this.datasource, sqlP.getDelegator()));

sqlP.prepareStatement(sql.toString());
for (EntityConditionParam param : params) {
Expand Down Expand Up @@ -773,7 +773,7 @@ public EntityListIterator selectListIteratorByCondition(Delegator delegator, Mod

// WHERE clause
List<EntityConditionParam> whereEntityConditionParams = new LinkedList<>();
makeConditionWhereString(sqlBuffer, " WHERE ", modelEntity, whereEntityCondition, viewWhereConditions, whereEntityConditionParams);
makeConditionWhereString(sqlBuffer, " WHERE ", modelEntity, whereEntityCondition, viewWhereConditions, whereEntityConditionParams, delegator);

// GROUP BY clause for view-entity
if (modelViewEntity != null) {
Expand All @@ -782,7 +782,8 @@ public EntityListIterator selectListIteratorByCondition(Delegator delegator, Mod

// HAVING clause
List<EntityConditionParam> havingEntityConditionParams = new LinkedList<>();
makeConditionHavingString(sqlBuffer, " HAVING ", modelEntity, havingEntityCondition, viewHavingConditions, havingEntityConditionParams);
makeConditionHavingString(sqlBuffer, " HAVING ", modelEntity, havingEntityCondition, viewHavingConditions,
havingEntityConditionParams, delegator);

// ORDER BY clause
List<String> orderByExpanded = new LinkedList<>();
Expand Down Expand Up @@ -859,7 +860,8 @@ public EntityListIterator selectListIteratorByCondition(Delegator delegator, Mod
protected StringBuilder makeConditionWhereString(ModelEntity modelEntity, EntityCondition whereEntityCondition,
List<EntityCondition> viewWhereConditions,
List<EntityConditionParam> whereEntityConditionParams) throws GenericEntityException {
return makeConditionWhereString(new StringBuilder(), "", modelEntity, whereEntityCondition, viewWhereConditions, whereEntityConditionParams);
return makeConditionWhereString(new StringBuilder(), "", modelEntity, whereEntityCondition, viewWhereConditions,
whereEntityConditionParams, null);
}

/**
Expand All @@ -870,12 +872,14 @@ protected StringBuilder makeConditionWhereString(ModelEntity modelEntity, Entity
* @param whereEntityCondition the where entity condition
* @param viewWhereConditions the view where conditions
* @param whereEntityConditionParams the where entity condition params
* @param delegator the delegator actually being used to run this query, or {@code null} if unavailable
* @return the string builder
* @throws GenericEntityException the generic entity exception
*/
protected StringBuilder makeConditionWhereString(StringBuilder whereString, String prefix, ModelEntity modelEntity,
EntityCondition whereEntityCondition, List<EntityCondition> viewWhereConditions,
List<EntityConditionParam> whereEntityConditionParams) throws GenericEntityException {
List<EntityConditionParam> whereEntityConditionParams, Delegator delegator)
throws GenericEntityException {
ModelViewEntity modelViewEntity = null;
if (modelEntity instanceof ModelViewEntity) {
modelViewEntity = (ModelViewEntity) modelEntity;
Expand All @@ -902,7 +906,7 @@ protected StringBuilder makeConditionWhereString(StringBuilder whereString, Stri
if (!conditions.isEmpty()) {
whereString.append(prefix);
whereString.append(EntityCondition.makeCondition(conditions, EntityOperator.AND).makeWhereString(modelEntity,
whereEntityConditionParams, this.datasource));
whereEntityConditionParams, this.datasource, delegator));
}

return whereString;
Expand All @@ -922,7 +926,7 @@ protected StringBuilder makeConditionHavingString(ModelEntity modelEntity, Entit
List<EntityCondition> viewHavingConditions,
List<EntityConditionParam> havingEntityConditionParams) throws GenericEntityException {
return makeConditionHavingString(new StringBuilder(), "", modelEntity, havingEntityCondition, viewHavingConditions,
havingEntityConditionParams);
havingEntityConditionParams, null);
}

/**
Expand All @@ -933,27 +937,29 @@ protected StringBuilder makeConditionHavingString(ModelEntity modelEntity, Entit
* @param havingEntityCondition the having entity condition
* @param viewHavingConditions the view having conditions
* @param havingEntityConditionParams the having entity condition params
* @param delegator the delegator actually being used to run this query, or {@code null} if unavailable
* @return the string builder
* @throws GenericEntityException the generic entity exception
*/
protected StringBuilder makeConditionHavingString(StringBuilder havingString, String prefix, ModelEntity modelEntity,
EntityCondition havingEntityCondition, List<EntityCondition> viewHavingConditions,
List<EntityConditionParam> havingEntityConditionParams) throws GenericEntityException {
List<EntityConditionParam> havingEntityConditionParams, Delegator delegator)
throws GenericEntityException {
ModelViewEntity modelViewEntity = null;
if (modelEntity instanceof ModelViewEntity) {
modelViewEntity = (ModelViewEntity) modelEntity;
}

String entityCondHavingString = "";
if (havingEntityCondition != null) {
entityCondHavingString = havingEntityCondition.makeWhereString(modelEntity, havingEntityConditionParams, this.datasource);
entityCondHavingString = havingEntityCondition.makeWhereString(modelEntity, havingEntityConditionParams, this.datasource, delegator);
}

String viewEntityCondHavingString = null;
if (modelViewEntity != null) {
EntityCondition viewHavingEntityCondition = EntityCondition.makeCondition(viewHavingConditions);
viewEntityCondHavingString = viewHavingEntityCondition.makeWhereString(modelEntity,
havingEntityConditionParams, this.datasource);
havingEntityConditionParams, this.datasource, delegator);
}

if (UtilValidate.isNotEmpty(entityCondHavingString) || UtilValidate.isNotEmpty(viewEntityCondHavingString)) {
Expand Down Expand Up @@ -1228,7 +1234,7 @@ public long selectCountByCondition(Delegator delegator, ModelEntity modelEntity,

// WHERE clause
List<EntityConditionParam> whereEntityConditionParams = new LinkedList<>();
makeConditionWhereString(sqlBuffer, " WHERE ", modelEntity, whereEntityCondition, viewWhereConditions, whereEntityConditionParams);
makeConditionWhereString(sqlBuffer, " WHERE ", modelEntity, whereEntityCondition, viewWhereConditions, whereEntityConditionParams, delegator);

// GROUP BY clause for view-entity
if (isGroupBy) {
Expand All @@ -1237,7 +1243,8 @@ public long selectCountByCondition(Delegator delegator, ModelEntity modelEntity,

// HAVING clause
List<EntityConditionParam> havingEntityConditionParams = new LinkedList<>();
makeConditionHavingString(sqlBuffer, " HAVING ", modelEntity, havingEntityCondition, viewHavingConditions, havingEntityConditionParams);
makeConditionHavingString(sqlBuffer, " HAVING ", modelEntity, havingEntityCondition, viewHavingConditions,
havingEntityConditionParams, delegator);

if (isGroupBy) {
sqlBuffer.append(") TEMP_NAME");
Expand Down Expand Up @@ -1368,7 +1375,7 @@ public int deleteByCondition(ModelEntity modelEntity, EntityCondition condition,

StringBuilder sql = new StringBuilder("DELETE FROM ").append(modelEntity.getTableName(this.datasource));

String whereCondition = condition.makeWhereString(modelEntity, null, this.datasource);
String whereCondition = condition.makeWhereString(modelEntity, null, this.datasource, sqlP.getDelegator());
if (UtilValidate.isNotEmpty(whereCondition)) {
sql.append(" WHERE ").append(whereCondition);
}
Expand Down
Loading