Moved to the Attic the framework/sql component.

The framework/sql was an attempt to create a specialized SQL parser for the OFBiz entity engine. The idea was to use a standardized SQL string that the parser would convert to entity conditions and Delegator calls. So, you could use the same SQL string on any database. The component was never finished.


git-svn-id: https://svn.apache.org/repos/asf/ofbiz/trunk@1644469 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Jacopo Cappellato
2014-12-10 16:24:48 +00:00
parent afbf52ff23
commit ee209c08f6
68 changed files with 4 additions and 4195 deletions
@@ -1,90 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.entity.sql;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.condition.EntityFieldValue;
import org.ofbiz.entity.condition.EntityOperator;
import org.ofbiz.sql.BooleanCondition;
import org.ofbiz.sql.Condition;
import org.ofbiz.sql.ConditionList;
import org.ofbiz.sql.ConditionPlanner;
import org.ofbiz.sql.FieldValue;
import org.ofbiz.sql.Joiner;
import org.ofbiz.sql.NumberValue;
import org.ofbiz.sql.ParameterValue;
import org.ofbiz.sql.ParameterizedConditionException;
import org.ofbiz.sql.StringValue;
import org.ofbiz.sql.Value;
public class EntityConditionPlanner implements ConditionPlanner<EntityCondition> {
public EntityCondition parse(Condition condition, Map<String, ? extends Object> params) throws ParameterizedConditionException {
if (condition == null) return null;
if (condition instanceof BooleanCondition) {
BooleanCondition bc = (BooleanCondition) condition;
return EntityCondition.makeCondition(buildFieldValue(bc.getLeft()), EntityOperator.lookupComparison(bc.getOp()), buildValue(bc.getRight(), params));
} else if (condition instanceof ConditionList) {
ConditionList cl = (ConditionList) condition;
List<EntityCondition> conditions = new LinkedList<EntityCondition>();
for (Condition subCondition: cl) {
conditions.add(parse(subCondition, params));
}
return EntityCondition.makeCondition(conditions, cl.getJoiner() == Joiner.AND ? EntityOperator.AND : EntityOperator.OR);
} else {
throw new UnsupportedOperationException(condition.toString());
}
}
private static EntityFieldValue buildFieldValue(Value value) {
if (value instanceof FieldValue) {
FieldValue fv = (FieldValue) value;
return EntityFieldValue.makeFieldValue(fv.getFieldName(), fv.getTableName(), null, null);
}
throw new UnsupportedOperationException(value.toString());
}
private static Object buildValue(Object value, Map<String, ? extends Object> params) throws ParameterizedConditionException {
if (value instanceof NumberValue<?>) {
return ((NumberValue<?>) value).getNumber();
} else if (value instanceof StringValue) {
return ((StringValue) value).getString();
} else if (value instanceof FieldValue) {
FieldValue fv = (FieldValue) value;
return EntityFieldValue.makeFieldValue(fv.getFieldName(), fv.getTableName(), null, null);
} else if (value instanceof List<?>) {
List<Object> values = new LinkedList<Object>();
for (Object sqlValue: (List<?>) value) {
values.add(buildValue(sqlValue, params));
}
return values;
} else if (value == Value.NULL) {
return null;
} else if (value instanceof ParameterValue) {
if (params == null) {
throw new ParameterizedConditionException();
}
return params.get(((ParameterValue) value).getName());
}
throw new UnsupportedOperationException(value.getClass().getName() + ":" + value.toString());
}
}
@@ -1,29 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.entity.sql;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.sql.ConditionPlan;
import org.ofbiz.sql.DeletePlan;
public class EntityDeletePlan extends DeletePlan<EntityDeletePlan, EntityCondition> {
public EntityDeletePlan(ConditionPlan<EntityCondition> wherePlan) {
super(wherePlan);
}
}
@@ -1,24 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.entity.sql;
import org.ofbiz.sql.InsertPlan;
public class EntityInsertPlan extends InsertPlan<EntityInsertPlan> {
}
@@ -1,240 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.entity.sql;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.model.DynamicViewEntity;
import org.ofbiz.entity.model.ModelKeyMap;
import org.ofbiz.entity.model.ModelViewEntity.ComplexAlias;
import org.ofbiz.entity.model.ModelViewEntity.ComplexAliasField;
import org.ofbiz.entity.model.ModelViewEntity.ComplexAliasMember;
import org.ofbiz.sql.ConstantValue;
import org.ofbiz.sql.FieldAll;
import org.ofbiz.sql.FieldDef;
import org.ofbiz.sql.FieldValue;
import org.ofbiz.sql.FunctionCall;
import org.ofbiz.sql.Joined;
import org.ofbiz.sql.KeyMap;
import org.ofbiz.sql.MathValue;
import org.ofbiz.sql.NumberValue;
import org.ofbiz.sql.OrderByItem;
import org.ofbiz.sql.Planner;
import org.ofbiz.sql.Relation;
import org.ofbiz.sql.SQLDelete;
import org.ofbiz.sql.SQLInsert;
import org.ofbiz.sql.SQLSelect;
import org.ofbiz.sql.SQLUpdate;
import org.ofbiz.sql.SQLView;
import org.ofbiz.sql.SelectGroup;
import org.ofbiz.sql.StaticValue;
import org.ofbiz.sql.StringValue;
import org.ofbiz.sql.Table;
import org.ofbiz.sql.TableName;
import org.ofbiz.sql.Unioned;
import org.ofbiz.sql.Value;
public class EntityPlanner extends Planner<EntityPlanner, EntityCondition, EntityDeletePlan, EntityInsertPlan, EntitySelectPlan, EntityUpdatePlan, EntityViewPlan> {
public EntityPlanner() {
super(new EntityConditionPlanner());
}
@Override
public EntityDeletePlan planDelete(SQLDelete deleteStatement) {
return null;
}
@Override
public EntityInsertPlan planInsert(SQLInsert insertStatement) {
return null;
}
@Override
public EntitySelectPlan planSelect(SQLSelect selectStatement) {
DynamicViewEntity dve = new DynamicViewEntity();
Unioned unioned = selectStatement.getUnioned();
if (unioned != null) {
throw new IllegalArgumentException("union views not yet supported");
}
SelectGroup selectGroup = unioned.getGroup();
Table table = selectGroup.getTable();
addMember(dve, table.getTableName());
addJoined(dve, table.getTableName().getAlias(), table.getJoined());
if (selectGroup.getFieldAlls() != null) {
for (FieldAll fieldAll: selectGroup.getFieldAlls()) {
List<String> excludes = new LinkedList<String>();
for (String exclude: fieldAll) {
excludes.add(exclude);
}
if (excludes.isEmpty()) {
excludes = null;
}
dve.addAliasAll(fieldAll.getAlias(), null, excludes);
}
}
if (selectStatement.getRelations() != null) {
for (Relation relation: selectStatement.getRelations().values()) {
dve.addRelation(relation.getType(), relation.getTitle(), relation.getEntityName(), buildKeyMaps(relation));
}
}
List<String> groupBy = selectGroup.getGroupBy();
if (groupBy == null) {
groupBy = Collections.emptyList();
}
if (selectGroup.getFieldDefs() != null) {
for (FieldDef fieldDef: selectGroup.getFieldDefs()) {
addFieldDef(dve, groupBy, fieldDef.getAlias(), fieldDef);
}
}
List<String> orderBy;
if (selectStatement.getOrderBy() == null) {
orderBy = null;
} else {
orderBy = new LinkedList<String>();
for (OrderByItem orderByItem: selectStatement.getOrderBy()) {
orderBy.add(orderByItem.toString());
}
}
return new EntitySelectPlan(dve, plan(selectGroup.getWhereCondition()), plan(selectGroup.getHavingCondition()), orderBy);
}
@Override
public EntityUpdatePlan planUpdate(SQLUpdate updateStatement) {
return null;
}
@Override
public EntityViewPlan planView(SQLView viewStatement) {
return null;
}
private static void addFieldDef(DynamicViewEntity dve, List<String> groupBy, String alias, FieldDef fieldDef) {
StaticValue value = fieldDef.getValue();
if (value instanceof FieldValue) {
addFieldDef(dve, groupBy, fieldDef.getAlias(), (FieldValue) value, null);
} else if (value instanceof ConstantValue) {
addFieldDef(dve, groupBy, fieldDef.getAlias(), value);
} else {
throw new UnsupportedOperationException(alias + "[" + fieldDef + "]:" + fieldDef.getClass());
}
}
private static void addFieldDef(DynamicViewEntity dve, List<String> groupBy, String alias, FieldValue fieldValue, String function) {
dve.addAlias(fieldValue.getTableName(), alias, fieldValue.getFieldName(), null, null, groupBy.contains(alias), function);
}
private static void addFieldDef(DynamicViewEntity dve, List<String> groupBy, String alias, ComplexAliasMember member) {
dve.addAlias(null, alias, null, null, null, groupBy.contains(alias), null, member);
}
private static void addFieldDef(DynamicViewEntity dve, List<String> groupBy, String alias, ConstantValue value) {
if (value instanceof FieldValue) {
addFieldDef(dve, groupBy, alias, (FieldValue) value, null);
return;
} else if (value instanceof FunctionCall) {
FunctionCall fc = (FunctionCall) value;
String name = fc.getName().toLowerCase();
Iterator<Value> it = fc.iterator();
if (it.hasNext()) {
Value firstValue = it.next();
if (!it.hasNext()) {
if (firstValue instanceof FieldValue) {
addFieldDef(dve, groupBy, alias, (FieldValue) firstValue, name);
return;
}
}
}
}
addFieldDef(dve, groupBy, alias, buildComplexMember(value));
}
private static ComplexAliasMember buildComplexMember(Value value) {
if (value instanceof FieldValue) {
FieldValue fv = (FieldValue) value;
return new ComplexAliasField(fv.getTableName(), fv.getFieldName(), null, null);
} else if (value instanceof FunctionCall) {
FunctionCall fc = (FunctionCall) value;
String name = fc.getName().toLowerCase();
if (fc.getArgCount() == 1) {
Value firstValue = fc.iterator().next();
if (firstValue instanceof FieldValue) {
FieldValue fv = (FieldValue) firstValue;
return new ComplexAliasField(fv.getTableName(), fv.getFieldName(), null, name);
} else if (firstValue instanceof FunctionCall) {
FunctionCall fc2 = (FunctionCall) firstValue;
if (fc2.getName().equalsIgnoreCase("coalesce") && fc2.getArgCount() == 2) {
Iterator<Value> it = fc2.iterator();
Value f1 = it.next(), f2 = it.next();
if (f1 instanceof FieldValue) {
FieldValue fv = (FieldValue) f1;
if (f2 instanceof NumberValue<?>) {
return new ComplexAliasField(fv.getTableName(), fv.getFieldName(), ((NumberValue<?>) f2).getNumber().toString(), name);
} else if (f2 instanceof StringValue) {
return new ComplexAliasField(fv.getTableName(), fv.getFieldName(), "'" + ((StringValue) f2).getString() + "'", name);
}
}
}
}
} else if (fc.getName().equalsIgnoreCase("coalesce") && fc.getArgCount() == 2) {
Iterator<Value> it = fc.iterator();
Value f1 = it.next(), f2 = it.next();
if (f1 instanceof FieldValue) {
FieldValue fv = (FieldValue) f1;
if (f2 instanceof NumberValue<?>) {
return new ComplexAliasField(fv.getTableName(), fv.getFieldName(), ((NumberValue<?>) f2).getNumber().toString(), null);
} else if (f2 instanceof StringValue) {
return new ComplexAliasField(fv.getTableName(), fv.getFieldName(), "'" + ((StringValue) f2).getString() + "'", null);
}
}
}
} else if (value instanceof MathValue) {
MathValue mv = (MathValue) value;
ComplexAlias complexAlias = new ComplexAlias(mv.getOp());
for (ConstantValue constantValue: mv) {
complexAlias.addComplexAliasMember(buildComplexMember(constantValue));
}
return complexAlias;
}
throw new UnsupportedOperationException(value + ":" + value.getClass());
}
private static void addMember(DynamicViewEntity dve, TableName member) {
dve.addMemberEntity(member.getAlias(), member.getTableName());
}
private static void addJoined(DynamicViewEntity dve, String leftAlias, Joined joined) {
if (joined == null) return;
addMember(dve, joined.getTableName());
dve.addViewLink(leftAlias, joined.getTableName().getAlias(), joined.isOptional(), buildKeyMaps(joined));
addJoined(dve, joined.getTableName().getAlias(), joined.getJoined());
}
private static List<ModelKeyMap> buildKeyMaps(Iterable<KeyMap> keyMaps) {
List<ModelKeyMap> entityKeyMaps = new ArrayList<ModelKeyMap>();
for (KeyMap keyMap: keyMaps) {
entityKeyMaps.add(new ModelKeyMap(keyMap.getLeftFieldName(), keyMap.getRightFieldName()));
}
return entityKeyMaps;
}
}
@@ -1,121 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.entity.sql;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import org.ofbiz.entity.Delegator;
import org.ofbiz.entity.GenericEntityException;
import org.ofbiz.entity.GenericValue;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.entity.model.DynamicViewEntity;
import org.ofbiz.entity.transaction.TransactionUtil;
import org.ofbiz.entity.util.EntityListIterator;
import org.ofbiz.sql.ConditionPlan;
import org.ofbiz.sql.ParameterizedConditionException;
import org.ofbiz.sql.SelectPlan;
public final class EntitySelectPlan extends SelectPlan<EntitySelectPlan, EntityCondition> {
private final DynamicViewEntity dve;
private final List<String> orderBy;
private final int offset = -1;
private final int limit = -1;
public EntitySelectPlan(DynamicViewEntity dve, ConditionPlan<EntityCondition> wherePlan, ConditionPlan<EntityCondition> havingPlan, List<String> orderBy) {
super(wherePlan, havingPlan);
this.dve = dve;
this.orderBy = orderBy;
//this.offset = offset;
//this.limit = limit;
}
public EntityListIterator getEntityListIterator(Delegator delegator, Map<String, ? extends Object> params) throws GenericEntityException {
EntityCondition whereCondition;
EntityCondition havingCondition;
try {
whereCondition = getWherePlan().getCondition(params);
havingCondition = getHavingPlan().getCondition(params);
} catch (ParameterizedConditionException e) {
throw (GenericEntityException) new GenericEntityException(e.getMessage()).initCause(e);
}
return delegator.findListIteratorByCondition(dve, whereCondition, havingCondition, null, orderBy, null);
}
public List<GenericValue> getAll(final Delegator delegator, final Map<String, ? extends Object> params) throws GenericEntityException {
return TransactionUtil.doTransaction(new Callable<List<GenericValue>>() {
public List<GenericValue> call() throws Exception {
EntityListIterator it = null;
try {
it = getEntityListIterator(delegator, params);
return it.getCompleteList();
} finally {
if (it != null) it.close();
}
}
}, "sql select", 0, true);
}
public DynamicViewEntity getDynamicViewEntity() {
return dve;
}
public List<String> getOrderBy() {
return orderBy;
}
public int getOffset() {
return offset;
}
public int getLimit() {
return limit;
}
@Override
public StringBuilder appendTo(StringBuilder sb) {
sb.append("dve=").append(dve);
if (getWherePlan() != null) {
if (sb.length() > 0) sb.append(", ");
sb.append("where=(");
getWherePlan().appendTo(sb);
sb.append(")");
}
if (getHavingPlan() != null) {
if (sb.length() > 0) sb.append(", ");
sb.append("having=(");
getHavingPlan().appendTo(sb);
sb.append(")");
}
/*TODO death code can be removed ?
if (offset != -1) {
if (sb.length() > 0) sb.append(", ");
sb.append("offset=").append(offset);
}
if (limit != -1) {
if (sb.length() > 0) sb.append(", ");
sb.append("limit=").append(limit);
}
*/
sb.append("]");
sb.insert(0, "[");
return sb;
}
}
@@ -1,29 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.entity.sql;
import org.ofbiz.entity.condition.EntityCondition;
import org.ofbiz.sql.ConditionPlan;
import org.ofbiz.sql.UpdatePlan;
public class EntityUpdatePlan extends UpdatePlan<EntityUpdatePlan, EntityCondition> {
public EntityUpdatePlan(ConditionPlan<EntityCondition> wherePlan) {
super(wherePlan);
}
}
@@ -1,24 +0,0 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.ofbiz.entity.sql;
import org.ofbiz.sql.ViewPlan;
public final class EntityViewPlan extends ViewPlan<EntityViewPlan> {
}