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
+2 -2
View File
@@ -1312,7 +1312,7 @@ under the License.
<property name="sonar.sources" value="specialpurpose/appserver/src, framework/base/src, framework/bi/src,
framework/catalina/src, framework/common/src, framework/datafile/src, framework/entity/src, framework/entityext/src,
framework/example/src, framework/geronimo/src, framework/jetty/src,
framework/minilang/src, framework/security/src, framework/service/src, framework/sql/src, framework/start/src,
framework/minilang/src, framework/security/src, framework/service/src, framework/start/src,
framework/testtools/src, framework/webapp/src, framework/webtools/src, framework/widget/src,
applications/accounting/src, applications/commonext/src, applications/content/src, applications/humanres/src,
applications/manufacturing/src, applications/marketing/src, applications/order/src, applications/party/src,
@@ -1321,7 +1321,7 @@ under the License.
specialpurpose/googlecheckout/src, specialpurpose/hhfacility/src, specialpurpose/oagis/src, specialpurpose/pos/src,
specialpurpose/scrum/src, specialpurpose/birt/src" />
<property name="sonar.tests" value="framework/base/src/org/ofbiz/base/test, framework/entity/src/org/ofbiz/entity/test,
framework/service/src/org/ofbiz/service/test, framework/sql/src/org/ofbiz/sql/test,
framework/service/src/org/ofbiz/service/test,
applications/accounting/src/org/ofbiz/accounting/test, applications/content/src/org/ofbiz/content/test,
applications/product/src/org/ofbiz/product/test, applications/product/src/org/ofbiz/shipment/test,
applications/securityext/src/org/ofbiz/securityext/test" />
+2 -2
View File
@@ -20,7 +20,7 @@ under the License.
<project name="OFBiz Framework Build" default="build" basedir=".">
<filelist id="framework-builds" dir="framework"
files="start/build.xml,base/build.xml,sql/build.xml,
files="start/build.xml,base/build.xml,
entity/build.xml,geronimo/build.xml,
catalina/build.xml,
security/build.xml,service/build.xml,entityext/build.xml,
@@ -30,5 +30,5 @@ under the License.
testtools/build.xml,
webtools/build.xml"/>
<filelist id="test-builds" dir="framework" files="base/build.xml,sql/build.xml,entity/build.xml"/>
<filelist id="test-builds" dir="framework" files="base/build.xml,entity/build.xml"/>
</project>
-1
View File
@@ -22,7 +22,6 @@ under the License.
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/component-loader.xsd">
<load-component component-location="base"/>
<load-component component-location="geronimo"/>
<load-component component-location="sql"/>
<load-component component-location="entity"/>
<load-component component-location="security"/>
<load-component component-location="datafile"/>
-1
View File
@@ -37,7 +37,6 @@ under the License.
<fileset dir="../base/lib/j2eespecs" includes="*.jar"/>
<fileset dir="../base/lib/scripting" includes="*.jar"/>
<fileset dir="../base/build/lib" includes="*.jar"/>
<fileset dir="../sql/build/lib" includes="*.jar"/>
</path>
<patternset id="src.exc.set">
@@ -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> {
}
-74
View File
@@ -1,74 +0,0 @@
<?xml version="1.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.
-->
<project name="OFBiz - SQL" default="jar" basedir=".">
<import file="../../common.xml"/>
<!-- ================================================================== -->
<!-- Initialization of all property settings -->
<!-- ================================================================== -->
<property name="desc" value="SQL Component"/>
<property name="name" value="ofbiz-sql"/>
<property name="ofbiz.home.dir" value="../.."/>
<path id="local.class.path">
<fileset dir="../base/lib" includes="*.jar"/>
<fileset dir="../base/build/lib" includes="*.jar"/>
</path>
<path id="test.class.path">
<path refid="local.class.path"/>
<fileset dir="../base/lib" includes="*.jar"/>
<fileset dir="../base/lib/commons" includes="*.jar"/>
<fileset dir="../base/lib/j2eespecs" includes="*.jar"/>
<pathelement location="config"/>
<pathelement location="src"/>
</path>
<filelist id="test.classes" dir="${src.dir}">
<file name="org/ofbiz/sql/test/ValuesTest.java"/>
<file name="org/ofbiz/sql/test/ConditionsTest.java"/>
<file name="org/ofbiz/sql/test/SelectTest.java"/>
</filelist>
<patternset id="cobertura-src-dirs">
<include name="src"/>
</patternset>
<!-- ================================================================== -->
<!-- Compilation of the source files -->
<!-- ================================================================== -->
<target name="jar" depends="classes">
<main-jar/>
<test-jar/>
</target>
<!-- ================================================================== -->
<!-- Build JavaDoc -->
<!-- ================================================================== -->
<target name="docs" depends="prepare-docs">
<default-javadoc>
<fileset dir="${src.dir}" defaultexcludes="yes">
<patternset refid="src.inc.set"/>
<patternset refid="src.exc.set"/>
</fileset>
</default-javadoc>
</target>
</project>
-27
View File
@@ -1,27 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
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.
-->
<ofbiz-component name="sql"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/ofbiz-component.xsd">
<resource-loader name="main" type="component"/>
<classpath type="jar" location="build/lib/*"/>
</ofbiz-component>
@@ -1,78 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class AggregateFunction extends StaticValue {
public static final String module = AggregateFunction.class.getName();
private final String name;
private final boolean isDistinct;
private final StaticValue value;
public AggregateFunction(String name, boolean isDistinct, StaticValue value) {
this.name = name;
this.isDistinct = isDistinct;
this.value = value;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public String getName() {
return name;
}
public boolean isDistinct() {
return isDistinct;
}
@Override
public String getDefaultName() {
return name;
}
public StaticValue getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (o instanceof AggregateFunction) {
AggregateFunction other = (AggregateFunction) o;
return name.equals(other.name) && isDistinct == other.isDistinct && value.equals(other.value);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append(name).append('(');
if (isDistinct) {
sb.append("DISTINCT ");
}
value.appendTo(sb);
sb.append(')');
return sb;
}
}
-45
View File
@@ -1,45 +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.sql;
import java.util.Collection;
import java.util.Map;
import org.ofbiz.base.lang.Appender;
import org.ofbiz.base.util.UtilObject;
import org.ofbiz.base.util.UtilValidate;
public abstract class Atom implements Appender<StringBuilder> {
public static boolean equalsHelper(Object l, Object r) {
return UtilObject.equalsHelper(l, r);
}
public static <T extends Collection<I>, I> T checkEmpty(T col) {
return UtilValidate.isEmpty(col) ? null : col;
}
public static <T extends Map<K, V>, K, V> T checkEmpty(T map) {
return UtilValidate.isEmpty(map) ? null : map;
}
@Override
public String toString() {
return appendTo(new StringBuilder()).toString();
}
}
@@ -1,70 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class BetweenCondition extends Condition {
private final Value left;
private final Value r1;
private final Value r2;
public BetweenCondition(Value left, Value r1, Value r2) {
this.left = left;
this.r1 = r1;
this.r2 = r2;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public Value getLeft() {
return left;
}
public Value getR1() {
return r1;
}
public Value getR2() {
return r2;
}
@Override
public boolean equals(Object o) {
if (o instanceof BetweenCondition) {
BetweenCondition other = (BetweenCondition) o;
return left.equals(other.left) && r1.equals(other.r1) && r2.equals(other.r2);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
left.appendTo(sb);
sb.append(" BETWEEN ");
r1.appendTo(sb);
sb.append(" AND ");
r2.appendTo(sb);
return sb;
}
}
@@ -1,68 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class BooleanCondition extends Condition {
private final Value left;
private final String op;
private final Value right;
public BooleanCondition(Value left, String op, Value right) {
this.left = left;
this.op = op.toLowerCase();
this.right = right;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public Value getLeft() {
return left;
}
public String getOp() {
return op;
}
public Value getRight() {
return right;
}
@Override
public boolean equals(Object o) {
if (o instanceof BooleanCondition) {
BooleanCondition other = (BooleanCondition) o;
return left.equals(other.left) & op.equals(other.op) && right.equals(other.right);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
left.appendTo(sb);
sb.append(' ').append(op).append(' ');
right.appendTo(sb);
return sb;
}
}
@@ -1,44 +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.sql;
public abstract class Condition extends Atom {
public interface Visitor {
void visit(BetweenCondition condition);
void visit(BooleanCondition condition);
void visit(ConditionList condition);
void visit(ListCondition condition);
}
public static class BaseVisitor implements Visitor {
public void visit(BetweenCondition condition) {
}
public void visit(BooleanCondition condition) {
}
public void visit(ConditionList condition) {
}
public void visit(ListCondition condition) {
}
}
public abstract void accept(Visitor visitor);
}
@@ -1,70 +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.sql;
import java.util.Iterator;
import java.util.List;
import org.ofbiz.base.lang.SourceMonitored;
import org.ofbiz.base.util.StringUtil;
@SourceMonitored
public final class ConditionList extends Condition implements Iterable<Condition> {
private final Joiner joiner;
private final List<Condition> conditions;
public ConditionList(Joiner joiner, List<Condition> conditions) {
this.joiner = joiner;
this.conditions = conditions;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public void add(Condition condition) {
conditions.add(condition);
}
public Joiner getJoiner() {
return joiner;
}
public Iterator<Condition> iterator() {
return conditions.iterator();
}
@Override
public boolean equals(Object o) {
if (o instanceof ConditionList) {
ConditionList other = (ConditionList) o;
return joiner.equals(other.joiner) && conditions.equals(other.conditions);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append('(');
StringUtil.appendTo(sb, conditions, " ", " ", joiner.toString());
sb.append(')');
return sb;
}
}
@@ -1,52 +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.sql;
import java.util.Map;
public final class ConditionPlan<C> extends SQLPlan<ConditionPlan<C>> {
private final ConditionPlanner<C> planner;
private final Condition originalCondition;
private final C condition;
public ConditionPlan(ConditionPlanner<C> planner, Condition originalCondition, C condition) {
this.planner = planner;
this.originalCondition = originalCondition;
this.condition = condition;
}
public C getCondition(Map<String, ? extends Object> params) throws ParameterizedConditionException {
if (originalCondition != null) {
return planner.parse(originalCondition, params);
} else {
return condition;
}
}
@Override
public StringBuilder appendTo(StringBuilder sb) {
sb.append("ConditionPlan[");
if (originalCondition != null) {
sb.append("original=" + originalCondition);
} else {
sb.append("condition=" + condition);
}
return sb.append("]");
}
}
@@ -1,25 +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.sql;
import java.util.Map;
public interface ConditionPlanner<C> {
C parse(Condition originalCondition, Map<String, ? extends Object> params) throws ParameterizedConditionException;
}
@@ -1,22 +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.sql;
public abstract class ConstantValue extends Value {
}
@@ -1,63 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class CountAllFunction extends StaticValue {
private final String tableName;
public CountAllFunction(String tableName) {
this.tableName = tableName;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
@Override
public String getDefaultName() {
return "COUNT";
}
public String getTableName() {
return tableName;
}
@Override
public boolean equals(Object o) {
if (o instanceof CountAllFunction) {
CountAllFunction other = (CountAllFunction) o;
return equalsHelper(tableName, other.tableName);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("COUNT(");
if (tableName != null) {
sb.append(tableName).append('.');
}
sb.append("*)");
return sb;
}
}
@@ -1,31 +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.sql;
public abstract class DeletePlan<P extends DeletePlan<P, C>, C> extends SQLPlan<P> {
private final ConditionPlan<C> wherePlan;
protected DeletePlan(ConditionPlan<C> wherePlan) {
this.wherePlan = wherePlan;
}
public ConditionPlan<C> getWherePlan() {
return wherePlan;
}
}
@@ -1,64 +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.sql;
import java.util.Iterator;
import java.util.Set;
import org.ofbiz.base.lang.SourceMonitored;
import org.ofbiz.base.util.StringUtil;
@SourceMonitored
public final class FieldAll extends Atom implements Iterable<String> {
private final String alias;
private final Set<String> exclude;
public FieldAll(String alias, Set<String> exclude) {
this.alias = alias;
this.exclude = exclude;
}
public String getAlias() {
return alias;
}
public Iterator<String> iterator() {
return exclude.iterator();
}
@Override
public boolean equals(Object o) {
if (o instanceof FieldAll) {
FieldAll other = (FieldAll) o;
return alias.equals(other.alias) && exclude.equals(other.exclude);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append(alias).append(".*");
if (!exclude.isEmpty()) {
sb.append(" EXCLUDE (");
StringUtil.append(sb, exclude, null, null, ", ");
sb.append(')');
}
return sb;
}
}
@@ -1,62 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class FieldDef extends Atom {
private final String alias;
private final StaticValue value;
public FieldDef(StaticValue value, String alias) {
this.alias = alias;
this.value = value;
}
public final String getAlias() {
return alias;
}
public String getDefaultName() {
return alias == null ? value.getDefaultName() : alias;
}
public StaticValue getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (o instanceof FieldDef) {
FieldDef other = (FieldDef) o;
return equalsHelper(alias, other.alias) && value.equals(other.value);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
value.appendTo(sb);
if (alias != null) {
sb.append(" AS ").append(alias);
}
return sb;
}
}
@@ -1,72 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class FieldValue extends StaticValue {
private final String fieldName;
private final String tableName;
public FieldValue(String fieldName) {
this(null, fieldName);
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public FieldValue(String tableName, String fieldName) {
this.tableName = tableName;
this.fieldName = fieldName;
}
public final String getTableName() {
return tableName;
}
public final String getFieldName() {
return fieldName;
}
@Override
public String getDefaultName() {
return fieldName;
}
@Override
public boolean equals(Object o) {
if (o instanceof FieldValue) {
FieldValue other = (FieldValue) o;
return fieldName.equals(other.fieldName) && equalsHelper(tableName, other.tableName);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
if (tableName != null) {
sb.append(tableName).append('.');
}
sb.append(fieldName);
return sb;
}
}
@@ -1,75 +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.sql;
import java.util.Iterator;
import java.util.List;
import org.ofbiz.base.lang.SourceMonitored;
import org.ofbiz.base.util.StringUtil;
@SourceMonitored
public final class FunctionCall extends StaticValue implements Iterable<Value> {
private final String name;
private final List<Value> values;
public FunctionCall(String name, List<Value> values) {
this.name = name;
this.values = values;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public String getName() {
return name;
}
@Override
public String getDefaultName() {
return name;
}
public int getArgCount() {
return values.size();
}
public Iterator<Value> iterator() {
return values.iterator();
}
@Override
public boolean equals(Object o) {
if (o instanceof FunctionCall) {
FunctionCall other = (FunctionCall) o;
return name.equals(other.name) && values.equals(other.values);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append(name).append('(');
StringUtil.appendTo(sb, values, null, null, ", ");
sb.append(')');
return sb;
}
}
@@ -1,22 +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.sql;
public abstract class InsertPlan<P extends InsertPlan<P>> extends SQLPlan<P> {
}
@@ -1,53 +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.sql;
import java.util.Iterator;
import java.util.List;
import org.ofbiz.base.util.StringUtil;
public final class InsertRow extends Atom implements Iterable<Value> {
private final List<Value> values;
public InsertRow(List<Value> values) {
this.values = values;
}
public Iterator<Value> iterator() {
return values.iterator();
}
@Override
public boolean equals(Object o) {
if (o instanceof InsertRow) {
InsertRow other = (InsertRow) o;
return values.equals(other.values);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append('(');
StringUtil.appendTo(sb, values, null, null, ", ");
sb.append(')');
return sb;
}
}
@@ -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.sql;
import org.ofbiz.base.lang.Appender;
public interface InsertSource extends Appender<StringBuilder> {
}
@@ -1,52 +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.sql;
import java.util.Iterator;
import java.util.List;
import org.ofbiz.base.util.StringUtil;
public final class InsertValues extends Atom implements InsertSource, Iterable<InsertRow> {
private final List<InsertRow> rows;
public InsertValues(List<InsertRow> rows) {
this.rows = rows;
}
public Iterator<InsertRow> iterator() {
return rows.iterator();
}
@Override
public boolean equals(Object o) {
if (o instanceof InsertValues) {
InsertValues other = (InsertValues) o;
return rows.equals(other.rows);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("VALUES ");
StringUtil.appendTo(sb, rows, null, null, ", ");
return sb;
}
}
@@ -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.sql;
import java.util.Iterator;
import java.util.List;
public final class Joined extends Atom implements Iterable<KeyMap> {
private final boolean isOptional;
private final TableName tableName;
private final List<KeyMap> keyMaps;
private final Joined joined;
public Joined(boolean isOptional, TableName tableName, List<KeyMap> keyMaps) {
this(isOptional, tableName, keyMaps, null);
}
public Joined(boolean isOptional, TableName tableName, List<KeyMap> keyMaps, Joined joined) {
this.isOptional = isOptional;
this.tableName = tableName;
this.keyMaps = keyMaps;
this.joined = joined;
}
public boolean isOptional() {
return isOptional;
}
public TableName getTableName() {
return tableName;
}
public Iterator<KeyMap> iterator() {
return keyMaps.iterator();
}
public Joined getJoined() {
return joined;
}
@Override
public boolean equals(Object o) {
if (o instanceof Joined) {
Joined other = (Joined) o;
return tableName.equals(other.tableName) && keyMaps.equals(other.keyMaps) && equalsHelper(joined, other.joined);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
return appendTo("left", sb);
}
public StringBuilder appendTo(String leftAlias, StringBuilder sb) {
sb.append(isOptional ? " LEFT JOIN " : " JOIN ");
return appendToRest(leftAlias, sb);
}
public StringBuilder appendToRest(String leftAlias, StringBuilder sb) {
tableName.appendTo(sb);
sb.append(" ON ");
for (int i = 0; i < keyMaps.size(); i++) {
KeyMap keyMap = keyMaps.get(i);
if (i != 0) sb.append(" AND ");
sb.append(' ').append(leftAlias).append('.').append(keyMap.getLeftFieldName());
sb.append(" = ").append(tableName.getAlias()).append('.').append(keyMap.getRightFieldName());
}
if (joined != null) {
joined.appendTo(tableName.getAlias(), sb);
}
return sb;
}
}
@@ -1,23 +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.sql;
public enum Joiner {
AND, OR
}
@@ -1,47 +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.sql;
public final class KeyMap {
private final String leftFieldName;
private final String rightFieldName;
public KeyMap(String leftFieldName, String rightFieldName) {
this.leftFieldName = leftFieldName;
this.rightFieldName = rightFieldName;
}
public String getLeftFieldName() {
return leftFieldName;
}
public String getRightFieldName() {
return rightFieldName;
}
@Override
public boolean equals(Object o) {
if (o instanceof KeyMap) {
KeyMap other = (KeyMap) o;
return leftFieldName.equals(other.leftFieldName) && rightFieldName.equals(other.rightFieldName);
} else {
return false;
}
}
}
@@ -1,71 +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.sql;
import java.util.List;
import org.ofbiz.base.lang.SourceMonitored;
import org.ofbiz.base.util.StringUtil;
@SourceMonitored
public final class ListCondition extends Condition {
private final Value left;
private final String op;
private final List<Value> values;
public ListCondition(Value left, String op, List<Value> values) {
this.left = left;
this.op = op.toLowerCase();
this.values = values;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public Value getLeft() {
return left;
}
public String getOp() {
return op;
}
public List<Value> getValues() {
return values;
}
@Override
public boolean equals(Object o) {
if (o instanceof ListCondition) {
ListCondition other = (ListCondition) o;
return left.equals(other.left) && op.equals(other.op) && values.equals(other.values);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
left.appendTo(sb);
sb.append(' ').append(op).append(" (");
StringUtil.appendTo(sb, values, null, null, ", ");
return sb.append(')');
}
}
@@ -1,71 +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.sql;
import java.util.Iterator;
import java.util.List;
import org.ofbiz.base.lang.SourceMonitored;
import org.ofbiz.base.util.StringUtil;
@SourceMonitored
public final class MathValue extends StaticValue implements Iterable<ConstantValue> {
private final String op;
private final List<ConstantValue> values;
public MathValue(String op, List<ConstantValue> values) {
this.op = op.toLowerCase();
this.values = values;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public String getOp() {
return op;
}
@Override
public String getDefaultName() {
return null;
}
public Iterator<ConstantValue> iterator() {
return values.iterator();
}
@Override
public boolean equals(Object o) {
if (o instanceof MathValue) {
MathValue other = (MathValue) o;
return op.equals(other.op) && values.equals(other.values);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append('(');
StringUtil.appendTo(sb, values, null, null, " ", op, " ");
sb.append(')');
return sb;
}
}
@@ -1,62 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class NumberValue<N extends Number> extends ConstantValue {
private final N number;
public static NumberValue<Long> valueOf(long v) {
return new NumberValue<Long>(v);
}
public static NumberValue<Double> valueOf(double v) {
return new NumberValue<Double>(v);
}
public NumberValue(N number) {
this.number = number;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public N getNumber() {
return number;
}
@Override
public boolean equals(Object o) {
if (o instanceof NumberValue<?>) {
NumberValue<?> other = (NumberValue<?>) o;
return number.equals(other.number);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append(number);
return sb;
}
}
@@ -1,80 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class OrderByItem extends Atom {
public enum Order { DEFAULT, ASCENDING, DESCENDING }
public enum Nulls { DEFAULT, FIRST, LAST }
private final Order order;
private final Nulls nulls;
private final ConstantValue value;
public OrderByItem(Order order, Nulls nulls, ConstantValue value) {
this.order = order;
this.nulls = nulls;
this.value = value;
}
public final Order getOrder() {
return order;
}
public final Nulls getNulls() {
return nulls;
}
public final ConstantValue getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (o instanceof OrderByItem) {
OrderByItem other = (OrderByItem) o;
return order.equals(other.order) && nulls.equals(other.nulls) && value.equals(other.value);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
value.appendTo(sb);
switch (order) {
case ASCENDING:
sb.append(" ASC");
break;
case DESCENDING:
sb.append(" DESC");
break;
}
switch (nulls) {
case FIRST:
sb.append(" NULLS FIRST");
break;
case LAST:
sb.append(" NULLS LAST");
break;
}
return sb;
}
}
@@ -1,54 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class ParameterValue extends Value {
private final String name;
public ParameterValue(String name) {
this.name = name;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public String getName() {
return name;
}
@Override
public boolean equals(Object o) {
if (o instanceof ParameterValue) {
ParameterValue other = (ParameterValue) o;
return name.equals(other.name);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append('?').append(name);
return sb;
}
}
@@ -1,40 +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.sql;
import org.ofbiz.base.util.GeneralException;
@SuppressWarnings("serial")
public final class ParameterizedConditionException extends GeneralException {
public ParameterizedConditionException() {
super();
}
public ParameterizedConditionException(String msg) {
super(msg);
}
public ParameterizedConditionException(String msg, Throwable nested) {
super(msg, nested);
}
public ParameterizedConditionException(Throwable nested) {
super(nested);
}
}
@@ -1,54 +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.sql;
public abstract class Planner<P extends Planner<P, C, D, I, S, U, V>, C, D extends DeletePlan<D, C>, I extends InsertPlan<I>, S extends SelectPlan<S, C>, U extends UpdatePlan<U, C>, V extends ViewPlan<V>> {
private final ConditionPlanner<C> conditionPlanner;
protected Planner(ConditionPlanner<C> conditionPlanner) {
this.conditionPlanner = conditionPlanner;
}
public ConditionPlanner<C> getConditionPlanner() {
return conditionPlanner;
}
public ConditionPlan<C> plan(Condition condition) {
try {
return new ConditionPlan<C>(conditionPlanner, null, conditionPlanner.parse(condition, null));
} catch (ParameterizedConditionException e) {
return new ConditionPlan<C>(conditionPlanner, condition, null);
}
}
public <STMNT extends SQLStatement<?>> SQLPlan<?> plan(STMNT statement) {
if (statement instanceof SQLDelete) return planDelete((SQLDelete) statement);
if (statement instanceof SQLInsert) return planInsert((SQLInsert) statement);
if (statement instanceof SQLSelect) return planSelect((SQLSelect) statement);
if (statement instanceof SQLUpdate) return planUpdate((SQLUpdate) statement);
if (statement instanceof SQLView) return planView((SQLView) statement);
return null;
}
public abstract D planDelete(SQLDelete deleteStatement);
public abstract I planInsert(SQLInsert insertStatement);
public abstract S planSelect(SQLSelect selectStatement);
public abstract U planUpdate(SQLUpdate updateStatement);
public abstract V planView(SQLView viewStatement);
}
@@ -1,87 +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.sql;
import java.util.Iterator;
import java.util.List;
public final class Relation extends Atom implements Iterable<KeyMap> {
private final String name;
private final String type;
private final String title;
private final String entityName;
private final List<KeyMap> keyMaps;
public Relation(String type, String title, String entityName, List<KeyMap> keyMaps) {
this.type = type;
this.title = title;
this.entityName = entityName;
this.keyMaps = keyMaps;
this.name = title == null ? entityName : title + entityName;
}
public String getName() {
return name;
}
public String getType() {
return type;
}
public String getTitle() {
return title;
}
public String getEntityName() {
return entityName;
}
public Iterator<KeyMap> iterator() {
return keyMaps.iterator();
}
@Override
public boolean equals(Object o) {
if (o instanceof Relation) {
Relation other = (Relation) o;
return equalsHelper(type, other.type) && equalsHelper(title, other.title) && entityName.equals(other.entityName) && keyMaps.equals(other.keyMaps);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("RELATION");
if (type != null) {
sb.append(" TYPE ").append(type);
}
if (title != null) {
sb.append(" TITLE ").append(title);
}
sb.append(' ').append(entityName);
sb.append(" MAP");
for (int i = 0; i < keyMaps.size(); i++) {
KeyMap keyMap = keyMaps.get(i);
if (i != 0) sb.append(" AND ");
sb.append(' ').append(keyMap.getLeftFieldName());
sb.append(" = ").append(keyMap.getRightFieldName());
}
return sb;
}
}
@@ -1,67 +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.sql;
public final class SQLDelete extends SQLStatement<SQLDelete> {
private final Table table;
private final Condition whereCondition;
public SQLDelete(Table table, Condition whereCondition) {
this.table = table;
this.whereCondition = whereCondition;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public Table getTable() {
return table;
}
public Condition getWhereCondition() {
return whereCondition;
}
@Override
public boolean equals(Object o) {
if (o instanceof SQLDelete) {
SQLDelete other = (SQLDelete) o;
return table.equals(other.table) && equalsHelper(whereCondition, other.whereCondition);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("DELETE FROM ");
table.getTableName().appendTo(sb);
if (table.getJoined() != null) {
sb.append(" USING");
table.getJoined().appendToRest(table.getTableName().getAlias(), sb);
}
if (whereCondition != null) {
sb.append(" WHERE ");
whereCondition.appendTo(sb);
}
sb.append(';');
return sb;
}
}
@@ -1,93 +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.sql;
import java.util.List;
import org.ofbiz.base.util.StringUtil;
public final class SQLIndex extends SQLStatement<SQLIndex> {
private final boolean isUnique;
private final String name;
private final String table;
private final String using;
private final List<ConstantValue> values;
public SQLIndex(boolean isUnique, String name, String table, String using, List<ConstantValue> values) {
this.isUnique = isUnique;
this.name = name;
this.table = table;
this.using = using;
this.values = values;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public boolean getIsUnique() {
return isUnique;
}
public String getName() {
return name;
}
public String getTable() {
return table;
}
public String getUsing() {
return using;
}
public List<ConstantValue> getValues() {
return values;
}
@Override
public boolean equals(Object o) {
if (o instanceof SQLIndex) {
SQLIndex other = (SQLIndex) o;
return isUnique == other.isUnique && name.equals(other.name) && table.equals(other.table) && equalsHelper(using, other.using) && values.equals(other.values);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("CREATE");
if (isUnique) {
sb.append(" UNIQUE");
}
sb.append(" INDEX ");
sb.append(name);
sb.append(" ON ");
sb.append(table);
if (using != null) {
sb.append(" USING ").append(using);
}
sb.append(" (");
StringUtil.append(sb, values, null, null, ", ");
sb.append(')');
sb.append(';');
return sb;
}
}
@@ -1,77 +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.sql;
import java.util.Iterator;
import java.util.List;
import org.ofbiz.base.util.StringUtil;
import org.ofbiz.base.util.UtilValidate;
public final class SQLInsert extends SQLStatement<SQLInsert> {
private final TableName tableName;
private final InsertSource source;
private final List<String> columns;
public SQLInsert(TableName tableName, InsertSource source, List<String> columns) {
this.tableName = tableName;
this.source = source;
this.columns = columns;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public TableName getTableName() {
return tableName;
}
public InsertSource getSource() {
return source;
}
public Iterator<String> iterator() {
return columns.iterator();
}
@Override
public boolean equals(Object o) {
if (o instanceof SQLInsert) {
SQLInsert other = (SQLInsert) o;
return tableName.equals(other.tableName) && equalsHelper(columns, other.columns) && source.equals(other.source);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("INSERT INTO ");
tableName.appendTo(sb);
if (UtilValidate.isNotEmpty(columns)) {
sb.append(" (");
StringUtil.append(sb, columns, null, null, ", ");
sb.append(')');
}
sb.append(' ');
source.appendTo(sb);
return sb;
}
}
@@ -1,25 +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.sql;
public abstract class SQLPlan<P extends SQLPlan<P>> extends Atom {
public StringBuilder appendTo(StringBuilder sb) {
return sb.append("SQLPlan[").append(getClass().getName()).append("]");
}
}
@@ -1,113 +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.sql;
import java.util.List;
import java.util.Map;
import org.ofbiz.base.util.StringUtil;
public final class SQLSelect extends SQLStatement<SQLSelect> implements InsertSource {
private final SelectGroup selectGroup;
private final Unioned unioned;
private final Map<String, Relation> relations;
private final int offset;
private final int limit;
private final List<OrderByItem> orderBy;
public SQLSelect(boolean isDistinct, List<FieldAll> fieldAlls, Map<String, FieldDef> fieldDefs, Table table, Map<String, Relation> relations, Condition whereCondition, Condition havingCondition, List<String> groupBy, List<OrderByItem> orderBy, int offset, int limit) {
this(new SelectGroup(isDistinct, fieldAlls, fieldDefs, table, whereCondition, havingCondition, groupBy), null, relations, orderBy, offset, limit);
}
public SQLSelect(SelectGroup selectGroup, Unioned unioned, Map<String, Relation> relations, List<OrderByItem> orderBy, int offset, int limit) {
this.selectGroup = selectGroup;
this.unioned = unioned;
this.relations = checkEmpty(relations);
this.orderBy = orderBy;
this.offset = offset;
this.limit = limit;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public SelectGroup getSelectGroup() {
return selectGroup;
}
public Unioned getUnioned() {
return unioned;
}
public Map<String, Relation> getRelations() {
return relations;
}
public List<OrderByItem> getOrderBy() {
return orderBy;
}
public int getOffset() {
return offset;
}
public int getLimit() {
return limit;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof SQLSelect)) {
return false;
}
SQLSelect other = (SQLSelect) o;
return selectGroup.equals(other.selectGroup)
&& equalsHelper(unioned, other.unioned)
&& equalsHelper(relations, other.relations)
&& offset == other.offset
&& limit == other.limit
&& equalsHelper(orderBy, other.orderBy)
;
}
public StringBuilder appendTo(StringBuilder sb) {
selectGroup.appendTo(sb);
if (unioned != null) {
unioned.appendTo(sb);
}
if (relations != null) {
StringUtil.appendTo(sb, relations.values(), " ", null, null);
}
if (orderBy != null) {
sb.append(" ORDER BY ");
StringUtil.append(sb, orderBy, null, null, ", ");
}
if (offset != -1) {
sb.append(" OFFSET ").append(offset);
}
if (limit != -1) {
sb.append(" LIMIT ").append(limit);
}
sb.append(';');
return sb;
}
}
@@ -1,52 +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.sql;
public abstract class SQLStatement<S extends SQLStatement<S>> extends Atom {
public interface Visitor {
void visit(SQLDelete statement);
void visit(SQLIndex statement);
void visit(SQLInsert statement);
void visit(SQLSelect statement);
void visit(SQLUpdate statement);
void visit(SQLView statement);
}
public static class BaseVisitor implements Visitor {
public void visit(SQLDelete statement) {
}
public void visit(SQLIndex statement) {
}
public void visit(SQLInsert statement) {
}
public void visit(SQLSelect statement) {
}
public void visit(SQLUpdate statement) {
}
public void visit(SQLView statement) {
}
}
public abstract void accept(Visitor visitor);
}
@@ -1,106 +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.sql;
import java.util.Iterator;
import java.util.List;
import org.ofbiz.base.util.StringUtil;
public final class SQLUpdate extends SQLStatement<SQLUpdate> implements Iterable<SetField> {
private final Table table;
private final List<SetField> setFields;
private final Condition whereCondition;
public SQLUpdate(Table table, List<SetField> setFields, Condition whereCondition) {
this.table = table;
this.setFields = setFields;
this.whereCondition = whereCondition;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public Table getTable() {
return table;
}
public Iterator<SetField> iterator() {
return setFields.iterator();
}
public Condition getWhereCondition() {
return whereCondition;
}
@Override
public boolean equals(Object o) {
if (o instanceof SQLUpdate) {
SQLUpdate other = (SQLUpdate) o;
return table.equals(other.table) && setFields.equals(other.setFields) && equalsHelper(whereCondition, other.whereCondition);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("UPDATE ");
table.getTableName().appendTo(sb);
// n=4;1+(n-1)*2+3+(n-1)*2+1;3*n+(n-1)*2
// 17
// 18
sb.append(" SET ");
if (setFields.size() <= 3) {
StringUtil.appendTo(sb, setFields, null, null, ", ");
} else {
Iterator<SetField> it = setFields.iterator();
sb.append('(');
while (it.hasNext()) {
SetField setField = it.next();
sb.append(setField.getName());
if (it.hasNext()) {
sb.append(", ");
}
}
sb.append(") = (");
it = setFields.iterator();
while (it.hasNext()) {
SetField setField = it.next();
setField.getValue().appendTo(sb);
if (it.hasNext()) {
sb.append(", ");
}
}
sb.append(')');
it = setFields.iterator();
}
if (table.getJoined() != null) {
sb.append(" FROM ");
table.getJoined().appendToRest(table.getTableName().getAlias(), sb);
}
if (whereCondition != null) {
sb.append(" WHERE ");
whereCondition.appendTo(sb);
}
sb.append(';');
return sb;
}
}
@@ -1,58 +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.sql;
public final class SQLView extends SQLStatement<SQLView> {
private final String name;
private final SQLSelect sqlSelect;
public SQLView(String name, SQLSelect sqlSelect) {
this.name = name;
this.sqlSelect = sqlSelect;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public String getName() {
return name;
}
public SQLSelect getSelect() {
return sqlSelect;
}
@Override
public boolean equals(Object o) {
if (o instanceof SQLView) {
SQLView other = (SQLView) o;
return name.equals(other.name) && sqlSelect.equals(other.sqlSelect);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("CREATE VIEW ").append(name).append(" AS ");
sqlSelect.appendTo(sb);
return sb;
}
}
@@ -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.sql;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import org.ofbiz.base.util.StringUtil;
public final class SelectGroup extends Atom {
private final boolean isDistinct;
private final List<FieldAll> fieldAlls;
private final Map<String, FieldDef> fieldDefs;
private final Table table;
private final Condition whereCondition;
private final Condition havingCondition;
private final List<String> groupBy;
public SelectGroup(boolean isDistinct, List<FieldAll> fieldAlls, Map<String, FieldDef> fieldDefs, Table table, Condition whereCondition, Condition havingCondition, List<String> groupBy) {
this.isDistinct = isDistinct;
this.fieldAlls = checkEmpty(fieldAlls);
this.fieldDefs = checkEmpty(fieldDefs);
this.table = table;
this.whereCondition = whereCondition;
this.havingCondition = havingCondition;
this.groupBy = groupBy;
}
public boolean getIsDistinct() {
return isDistinct;
}
public Collection<FieldAll> getFieldAlls() {
return fieldAlls;
}
public Collection<FieldDef> getFieldDefs() {
return fieldDefs != null ? fieldDefs.values() : null;
}
public Table getTable() {
return table;
}
public Condition getWhereCondition() {
return whereCondition;
}
public Condition getHavingCondition() {
return havingCondition;
}
public List<String> getGroupBy() {
return groupBy;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof SelectGroup)) {
return false;
}
SelectGroup other = (SelectGroup) o;
return isDistinct == other.isDistinct
&& equalsHelper(fieldAlls, other.fieldAlls)
&& equalsHelper(fieldDefs, other.fieldDefs)
&& table.equals(other.table)
&& equalsHelper(whereCondition, other.whereCondition)
&& equalsHelper(havingCondition, other.havingCondition)
&& equalsHelper(groupBy, other.groupBy)
;
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append("SELECT");
if (isDistinct) {
sb.append(" DISTINCT");
}
if (fieldAlls != null) {
StringUtil.appendTo(sb, fieldAlls, " ", null, ",");
}
if (fieldAlls != null && fieldDefs != null) {
sb.append(',');
}
if (fieldDefs != null) {
StringUtil.appendTo(sb, fieldDefs.values(), " ", null, ",");
}
sb.append(" FROM ");
table.appendTo(sb);
if (whereCondition != null) {
sb.append(" WHERE ");
whereCondition.appendTo(sb);
}
if (havingCondition != null) {
sb.append(" HAVING ");
havingCondition.appendTo(sb);
}
if (groupBy != null) {
sb.append(" GROUP BY ");
StringUtil.append(sb, groupBy, null, null, ", ");
}
return sb;
}
}
@@ -1,37 +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.sql;
public abstract class SelectPlan<P extends SelectPlan<P, C>, C> extends SQLPlan<P> {
private final ConditionPlan<C> wherePlan;
private final ConditionPlan<C> havingPlan;
protected SelectPlan(ConditionPlan<C> wherePlan, ConditionPlan<C> havingPlan) {
this.wherePlan = wherePlan;
this.havingPlan = havingPlan;
}
public ConditionPlan<C> getWherePlan() {
return wherePlan;
}
public ConditionPlan<C> getHavingPlan() {
return havingPlan;
}
}
@@ -1,53 +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.sql;
public final class SetField extends Atom {
private final String name;
private final Value value;
public SetField(String name, Value value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public Value getValue() {
return value;
}
@Override
public boolean equals(Object o) {
if (o instanceof SetField) {
SetField other = (SetField) o;
return name.equals(other.name) && value.equals(other.value);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append(name).append(" = ");
value.appendTo(sb);
return sb;
}
}
@@ -1,23 +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.sql;
public abstract class StaticValue extends ConstantValue {
public abstract String getDefaultName();
}
@@ -1,54 +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.sql;
import org.ofbiz.base.lang.SourceMonitored;
@SourceMonitored
public final class StringValue extends ConstantValue {
private final String str;
public StringValue(String str) {
this.str = str;
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public String getString() {
return str;
}
@Override
public boolean equals(Object o) {
if (o instanceof StringValue) {
StringValue other = (StringValue) o;
return str.equals(other.str);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append('\'').append(str.replaceAll("'", "''")).append('\'');
return sb;
}
}
@@ -1,59 +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.sql;
public final class Table extends Atom {
private final TableName tableName;
private final Joined joined;
public Table(TableName tableName) {
this(tableName, null);
}
public Table(TableName tableName, Joined joined) {
this.tableName = tableName;
this.joined = joined;
}
public TableName getTableName() {
return tableName;
}
public Joined getJoined() {
return joined;
}
@Override
public boolean equals(Object o) {
if (o instanceof Table) {
Table other = (Table) o;
return tableName.equals(other.tableName) && equalsHelper(joined, other.joined);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
tableName.appendTo(sb);
if (joined != null) {
joined.appendTo(tableName.getAlias(), sb);
}
return sb;
}
}
@@ -1,55 +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.sql;
public final class TableName extends Atom {
private final String tableName;
private final String alias;
public TableName(String tableName, String alias) {
this.tableName = tableName;
this.alias = alias != null ? alias : tableName;
}
public final String getTableName() {
return tableName;
}
public final String getAlias() {
return alias;
}
@Override
public boolean equals(Object o) {
if (o instanceof TableName) {
TableName other = (TableName) o;
return tableName.equals(other.tableName) && alias.equals(other.alias);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append(tableName);
if (!alias.equals(tableName)) {
sb.append(' ').append(alias);
}
return sb;
}
}
@@ -1,64 +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.sql;
public final class Unioned extends Atom {
public enum Operator { UNION, UNION_ALL, INTERSECT, INTERSECT_ALL, EXCEPT, EXCEPT_ALL }
private final Operator operator;
private final SelectGroup group;
private final Unioned next;
public Unioned(Operator operator, SelectGroup group, Unioned next) {
this.operator = operator;
this.group = group;
this.next = next;
}
public Operator getOperator() {
return operator;
}
public SelectGroup getGroup() {
return group;
}
public Unioned getNext() {
return next;
}
@Override
public boolean equals(Object o) {
if (o instanceof Unioned) {
Unioned other = (Unioned) o;
return operator.equals(other.operator) && group.equals(other.group) && equalsHelper(next, other.next);
} else {
return false;
}
}
public StringBuilder appendTo(StringBuilder sb) {
sb.append(' ').append(operator.toString().replace('_', ' ')).append(' ');
group.appendTo(sb);
if (next != null) {
next.appendTo(sb);
}
return sb;
}
}
@@ -1,31 +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.sql;
public abstract class UpdatePlan<P extends UpdatePlan<P, C>, C> extends SQLPlan<P> {
private final ConditionPlan<C> wherePlan;
protected UpdatePlan(ConditionPlan<C> wherePlan) {
this.wherePlan = wherePlan;
}
public ConditionPlan<C> getWherePlan() {
return wherePlan;
}
}
@@ -1,79 +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.sql;
public abstract class Value extends Atom {
public static final class Null extends Value {
private Null() {
}
@Override
public void accept(Visitor visitor) {
visitor.visit(this);
}
public StringBuilder appendTo(StringBuilder sb) {
return sb.append("NULL");
}
}
public static final Null NULL = new Null();
public interface Visitor {
void visit(AggregateFunction value);
void visit(FieldValue value);
void visit(FunctionCall value);
void visit(MathValue value);
void visit(Null value);
void visit(NumberValue<?> value);
void visit(ParameterValue value);
void visit(StringValue value);
void visit(CountAllFunction value);
}
public static class BaseVisitor implements Visitor {
public void visit(AggregateFunction value) {
}
public void visit(FieldValue value) {
}
public void visit(FunctionCall value) {
}
public void visit(MathValue value) {
}
public void visit(Null value) {
}
public void visit(NumberValue<?> value) {
}
public void visit(ParameterValue value) {
}
public void visit(StringValue value) {
}
public void visit(CountAllFunction value) {
}
}
public abstract void accept(Visitor visitor);
}
@@ -1,22 +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.sql;
public abstract class ViewPlan<P extends ViewPlan<P>> extends SQLPlan<P> {
}
@@ -1,162 +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.sql.test;
import java.util.Collections;
import java.util.List;
import org.ofbiz.sql.BetweenCondition;
import org.ofbiz.sql.BooleanCondition;
import org.ofbiz.sql.Condition;
import org.ofbiz.sql.ConditionList;
import org.ofbiz.sql.Joiner;
import org.ofbiz.sql.ListCondition;
import org.ofbiz.sql.NumberValue;
import org.ofbiz.sql.Value;
import org.ofbiz.base.lang.SourceMonitored;
import org.ofbiz.base.test.GenericTestCaseBase;
@SourceMonitored
public class ConditionsTest extends GenericTestCaseBase {
private static final NumberValue<Long> l1 = NumberValue.valueOf(1);
private static final NumberValue<Long> l2 = NumberValue.valueOf(5);
private static final NumberValue<Long> l3 = NumberValue.valueOf(10);
public ConditionsTest(String name) {
super(name);
}
private static <C extends Condition> void basicTest(String label, Class<C> clz, C c, String s, C o, boolean matches) {
assertEquals(label + ":toString", s, c.toString());
assertNotEquals(label + ":not-equals-this", c, ConditionsTest.class);
if (o != null) {
if (matches) {
assertEquals(label + ":equals", o, c);
} else {
assertNotEquals(label + ":not-equals", o, c);
}
}
ConditionVisitorRecorder visitor = new ConditionVisitorRecorder();
c.accept(visitor);
c.accept(visitor);
assertEquals(label + ":visited", 2, visitor.counts.remove(clz).intValue());
assertTrue(label + ":nothing-else-visited", visitor.counts.isEmpty());
}
private static void betweenConditionTest(String label, BetweenCondition c, Value left, Value r1, Value r2, String s, BetweenCondition o, boolean matches) {
assertEquals(label + ":left", left, c.getLeft());
assertEquals(label + ":r1", r1, c.getR1());
assertEquals(label + ":r2", r2, c.getR2());
basicTest(label, BetweenCondition.class, c, s, o, matches);
}
public void testBetweenCondition() {
BetweenCondition c1 = new BetweenCondition(l1, l2, l3);
betweenConditionTest("c1", c1, l1, l2, l3, "1 BETWEEN 5 AND 10", null, false);
BetweenCondition c2 = new BetweenCondition(l3, l1, l2);
betweenConditionTest("c2", c2, l3, l1, l2, "10 BETWEEN 1 AND 5", c1, false);
BetweenCondition c3 = new BetweenCondition(l1, l2, l3);
betweenConditionTest("c3", c3, l1, l2, l3, "1 BETWEEN 5 AND 10", c1, true);
BetweenCondition c4 = new BetweenCondition(l1, l3, l2);
betweenConditionTest("c4", c4, l1, l3, l2, "1 BETWEEN 10 AND 5", c1, false);
BetweenCondition c5 = new BetweenCondition(l1, l2, l1);
betweenConditionTest("c5", c5, l1, l2, l1, "1 BETWEEN 5 AND 1", c1, false);
}
private static void booleanConditionTest(String label, BooleanCondition c, Value left, String op, Value right, String s, BooleanCondition o, boolean matches) {
assertEquals(label + ":left", left, c.getLeft());
assertEquals(label + ":op", op, c.getOp());
assertEquals(label + ":right", right, c.getRight());
basicTest(label, BooleanCondition.class, c, s, o, matches);
}
public void testBooleanCondition() {
BooleanCondition c1 = new BooleanCondition(l1, "=", l2);
booleanConditionTest("c1", c1, l1, "=", l2, "1 = 5", null, false);
BooleanCondition c2 = new BooleanCondition(l1, "=", l3);
booleanConditionTest("c2", c2, l1, "=", l3, "1 = 10", c1, false);
BooleanCondition c3 = new BooleanCondition(l1, "<", l2);
booleanConditionTest("c3", c3, l1, "<", l2, "1 < 5", c1, false);
BooleanCondition c4 = new BooleanCondition(l2, ">", l3);
booleanConditionTest("c4", c4, l2, ">", l3, "5 > 10", c1, false);
BooleanCondition c5 = new BooleanCondition(l1, "=", l2);
booleanConditionTest("c5", c5, l1, "=", l2, "1 = 5", c1, true);
}
private static void conditionListTest(String label, ConditionList c, Joiner joiner, List<? extends Condition> conditions, String s, ConditionList o, boolean matches) {
assertEquals(label + ":joiner", joiner, c.getJoiner());
basicTest(label, ConditionList.class, c, s, o, matches);
BooleanCondition b = new BooleanCondition(l1, "=", l3);
c.add(b);
assertEqualsIterable(label + ":iterable", conditions, Collections.<Condition>emptyList(), false, c, list(b), true);
}
public void testConditionList() {
BooleanCondition b1 = new BooleanCondition(l1, "=", l2);
BooleanCondition b2 = new BooleanCondition(l2, "=", l3);
ConditionList c1 = new ConditionList(Joiner.AND, GenericTestCaseBase.<Condition>list(b1));
conditionListTest("c1", c1, Joiner.AND, list(b1), "( 1 = 5 )", null, false);
ConditionList c2 = new ConditionList(Joiner.AND, GenericTestCaseBase.<Condition>list(b1, b2));
conditionListTest("c2", c2, Joiner.AND, list(b1, b2), "( 1 = 5 AND 5 = 10 )", c1, false);
ConditionList c3 = new ConditionList(Joiner.OR, GenericTestCaseBase.<Condition>list(b2, b1));
conditionListTest("c3", c3, Joiner.OR, list(b2, b1), "( 5 = 10 OR 1 = 5 )", c1, false);
ConditionList c4 = new ConditionList(Joiner.AND, GenericTestCaseBase.<Condition>list(b1));
conditionListTest("c4", c4, Joiner.AND, list(b1), "( 1 = 5 )", c1, true);
}
private static void listConditionTest(String label, ListCondition c, Value left, String op, List<? extends Value> values, String s, ListCondition o, boolean matches) {
assertEquals(label + ":left", left, c.getLeft());
assertEquals(label + ":op", op, c.getOp());
assertEquals(label + ":right", values, c.getValues());
basicTest(label, ListCondition.class, c, s, o, matches);
}
@SuppressWarnings("unchecked")
public void testListCondition() {
ListCondition c1 = new ListCondition(l1, "IN", GenericTestCaseBase.<Value>list(l2));
listConditionTest("c1", c1, l1, "in", list(l2), "1 in (5)", null, false);
ListCondition c2 = new ListCondition(l1, "NOT IN", GenericTestCaseBase.<Value>list(l2));
listConditionTest("c2", c2, l1, "not in", list(l2), "1 not in (5)", c1, false);
ListCondition c3 = new ListCondition(l2, "IN", GenericTestCaseBase.<Value>list(l3));
listConditionTest("c3", c3, l2, "in", list(l3), "5 in (10)", c1, false);
ListCondition c4 = new ListCondition(l1, "IN", GenericTestCaseBase.<Value>list(l2, l3));
listConditionTest("c4", c4, l1, "in", list(l2, l3), "1 in (5, 10)", c1, false);
ListCondition c5 = new ListCondition(l1, "IN", GenericTestCaseBase.<Value>list(l2));
listConditionTest("c5", c5, l1, "in", list(l2), "1 in (5)", c1, true);
}
public static class ConditionVisitorRecorder extends Recorder<Class<? extends Condition>> implements Condition.Visitor {
public void visit(BetweenCondition condition) {
record(BetweenCondition.class);
}
public void visit(BooleanCondition condition) {
record(BooleanCondition.class);
}
public void visit(ConditionList condition) {
record(ConditionList.class);
}
public void visit(ListCondition condition) {
record(ListCondition.class);
}
}
}
@@ -1,74 +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.
*/
SELECT
a.*,
b.* EXCLUDE (partyId, partyId),
c.* EXCLUDE (partyId),
d.roleTypeId,
d.description AS roleDescription,
SUM(a.partyId),
FOO(a.partyId, 1) AS baz,
(a.partyId || '-' || a.partyTypeId) AS one,
COUNT(a.partyId) AS cnt1,
COUNT(partyId) AS cnt2,
COUNT(DISTINCT a.partyId) AS cnt3
FROM
Party a LEFT JOIN Person b USING partyId
LEFT JOIN PartyGroup c ON b.partyId = c.partyId
JOIN PartyRole d ON c.partyId = d.partyId AND c.partyId = d.partyId
WHERE
a.partyTypeId = 'PERSON'
AND
b.lastName LIKE ?lastName
AND
b.birthDate BETWEEN '1974-12-01' AND '1974-12-31'
OR
(
b.partyId IN ('1', '2', '3', '4')
AND
b.gender = 'M'
)
HAVING
b.firstName LIKE '%foo%'
RELATION TYPE one TITLE MainA Person MAP partyId
RELATION TITLE MainB Person MAP partyId
RELATION TYPE one Person MAP partyId
RELATION PartyGroup MAP partyId
ORDER BY
LOWER(lastName), firstName, -birthDate
OFFSET 5
LIMIT 10
;
INSERT INTO Party (partyId, partyTypeId, statusId) VALUES
('a', 'PERSON', 'PARTY_DISABLED'),
(5, 'PARTY_GROUP', ?name);
INSERT INTO Person (partyId, firstName) SELECT partyId, (partyId || '-auto') AS firstName FROM Party WHERE partyId IN ('a', 'b');
UPDATE Person SET (lastName) = (('auto-' || partyId)) WHERE partyId IN ('a', 'b');
UPDATE Person SET (lastName, height, width) = (('auto-' || partyId), 5, 7) WHERE partyId IN ('a', 'b');
UPDATE Person SET lastName = ('auto-' || partyId), height = 6, width = 5, nickname = 'a' WHERE partyId IN ('a', 'b');
DELETE FROM Person WHERE partyId IN ('a', 'b');
DELETE FROM Party WHERE partyId IN ('a', 'b');
CREATE VIEW viewOne AS SELECT a.* FROM Party a;
CREATE INDEX testIndex ON Party USING btree (partyId);
/*
UPDATE Person SET firstName = partyId || '-auto' WHERE partyId IN ('a', 'b');
*/
@@ -1,36 +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.sql.test;
import java.util.HashMap;
import java.util.Map;
public abstract class Recorder<I> {
public Map<I, Integer> counts = new HashMap<I, Integer>();
protected void record(I key) {
Integer count = counts.get(key);
if (count == null) {
counts.put(key, Integer.valueOf(1));
} else {
counts.put(key, Integer.valueOf(count.intValue() + 1));
}
}
}
@@ -1,116 +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.sql.test;
import java.util.Collections;
import java.util.Set;
import org.ofbiz.base.lang.SourceMonitored;
import org.ofbiz.base.test.GenericTestCaseBase;
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.OrderByItem;
import org.ofbiz.sql.StaticValue;
import org.ofbiz.sql.Value;
@SourceMonitored
public class SelectTest extends GenericTestCaseBase {
private static final FieldValue fv1 = new FieldValue("partyId");
private static final FieldValue fv4 = new FieldValue(null, "firstName");
private static final FunctionCall fc1 = new FunctionCall("LOWER", GenericTestCaseBase.<Value>list(fv1));
public SelectTest(String name) {
super(name);
}
private static <V> void basicTest(String label, Class<V> clz, V v, V o, boolean matches) {
assertNotEquals(label + ":not-equals-this", v, SelectTest.class);
if (o != null) {
if (matches) {
assertEquals(label + ":equals", o, v);
} else {
assertNotEquals(label + ":not-equals", o, v);
}
}
}
private static void fieldAllTest(String label, FieldAll v, String alias, Set<String> exclude, FieldAll o, boolean matches) throws Exception {
assertEquals(label + ":alias", alias, v.getAlias());
assertEquals(label + ":exclude", exclude, set(v));
basicTest(label, FieldAll.class, v, o, matches);
}
public void testFieldAll() throws Exception {
FieldAll v1 = new FieldAll("a", set("a", "b"));
fieldAllTest("v1", v1, "a", set("a", "b"), null, false);
FieldAll v2 = new FieldAll("b", set("a", "b"));
fieldAllTest("v2", v2, "b", set("a", "b"), v1, false);
FieldAll v3 = new FieldAll("a", set("b", "c"));
fieldAllTest("v3", v3, "a", set("b", "c"), v1, false);
FieldAll v4 = new FieldAll("a", Collections.<String>emptySet());
fieldAllTest("v4", v4, "a", Collections.<String>emptySet(), v1, false);
FieldAll v5 = new FieldAll("a", set("b", "a"));
fieldAllTest("v5", v5, "a", set("b", "a"), v1, true);
}
private static void fieldDefTest(String label, FieldDef v, StaticValue value, String alias, String defaultName, FieldDef o, boolean matches) throws Exception {
assertEquals(label + ":value", value, v.getValue());
assertEquals(label + ":alias", alias, v.getAlias());
assertEquals(label + ":default-name", defaultName, v.getDefaultName());
basicTest(label, FieldDef.class, v, o, matches);
}
public void testFieldDef() throws Exception {
FieldDef v1 = new FieldDef(fv1, null);
fieldDefTest("v1", v1, fv1, null, "partyId", null, false);
FieldDef v2 = new FieldDef(fv1, "partyId");
fieldDefTest("v2", v2, fv1, "partyId", "partyId", v1, false);
FieldDef v3 = new FieldDef(fv4, null);
fieldDefTest("v3", v3, fv4, null, "firstName", v1, false);
FieldDef v4 = new FieldDef(fv4, "partyId");
fieldDefTest("v4", v4, fv4, "partyId", "partyId", v1, false);
FieldDef v5 = new FieldDef(fv1, null);
fieldDefTest("v5", v5, fv1, null, "partyId", v1, true);
}
private static void orderByItemTest(String label, OrderByItem v, OrderByItem.Order order, OrderByItem.Nulls nulls, ConstantValue value, OrderByItem o, boolean matches) throws Exception {
assertEquals(label + ":order", order, v.getOrder());
assertEquals(label + ":nulls", nulls, v.getNulls());
assertEquals(label + ":value", value, v.getValue());
basicTest(label, OrderByItem.class, v, o, matches);
}
public void testOrderByItem() throws Exception {
OrderByItem v1 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fv1);
orderByItemTest("v1", v1, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fv1, null, false);
OrderByItem v2 = new OrderByItem(OrderByItem.Order.ASCENDING, OrderByItem.Nulls.FIRST, fv1);
orderByItemTest("v3", v2, OrderByItem.Order.ASCENDING, OrderByItem.Nulls.FIRST, fv1, v1, false);
OrderByItem v3 = new OrderByItem(OrderByItem.Order.DESCENDING, OrderByItem.Nulls.LAST, fv1);
orderByItemTest("v2", v3, OrderByItem.Order.DESCENDING, OrderByItem.Nulls.LAST, fv1, v1, false);
OrderByItem v4 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fc1);
orderByItemTest("v4", v4, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fc1, v1, false);
OrderByItem v5 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fv4);
orderByItemTest("v5", v5, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.DEFAULT, fv4, v1, false);
OrderByItem v6 = new OrderByItem(OrderByItem.Order.DEFAULT, OrderByItem.Nulls.LAST, fv4);
orderByItemTest("v6", v6, OrderByItem.Order.DEFAULT, OrderByItem.Nulls.LAST, fv4, v1, false);
}
}
@@ -1,44 +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.
*/
SELECT
a.*,
b.firstName,
b.lastName
FROM
Party a JOIN Person b ON a.partyId = b.partyId
WHERE
a.partyTypeId = 'PERSON'
AND
b.lastName LIKE ?lastName
HAVING
b.firstName LIKE '%foo%'
OFFSET 5
LIMIT 10
;
SELECT
a.partyTypeId,
COUNT(a.partyId) AS count
FROM
Party a LEFT JOIN Person b USING partyId
LEFT JOIN PartyGroup c USING partyId
GROUP BY
partyTypeId
;
@@ -1,239 +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.sql.test;
import java.util.List;
import org.ofbiz.sql.ConstantValue;
import org.ofbiz.sql.CountAllFunction;
import org.ofbiz.sql.AggregateFunction;
import org.ofbiz.sql.FieldValue;
import org.ofbiz.sql.FunctionCall;
import org.ofbiz.sql.MathValue;
import org.ofbiz.sql.NumberValue;
import org.ofbiz.sql.ParameterValue;
import org.ofbiz.sql.StaticValue;
import org.ofbiz.sql.StringValue;
import org.ofbiz.sql.Value;
import org.ofbiz.base.lang.SourceMonitored;
import org.ofbiz.base.test.GenericTestCaseBase;
@SourceMonitored
public class ValuesTest extends GenericTestCaseBase {
private static final FieldValue fv1 = new FieldValue("partyId");
private static final FieldValue fv2 = new FieldValue("a", "partyId");
private static final FieldValue fv3 = new FieldValue(null, "partyId");
private static final FieldValue fv4 = new FieldValue(null, "firstName");
private static final NumberValue<Long> nv1 = NumberValue.valueOf(1);
private static final NumberValue<Double> nv2 = NumberValue.valueOf(2D);
private static final NumberValue<Long> nv3 = NumberValue.valueOf(3);
public ValuesTest(String name) {
super(name);
}
private static <V extends Value> void basicTest(String label, Class<V> clz, V v, String defaultName, String s, V o, boolean matches) {
if (v instanceof StaticValue) {
assertEquals(label + ":default-name", defaultName, ((StaticValue) v).getDefaultName());
}
assertEquals(label + ":toString", s, v.toString());
assertNotEquals(label + ":not-equals-this", v, ValuesTest.class);
if (o != null) {
if (matches) {
assertEquals(label + ":equals", o, v);
} else {
assertNotEquals(label + ":not-equals", o, v);
}
}
ValueVisitorRecorder visitor = new ValueVisitorRecorder();
v.accept(visitor);
v.accept(visitor);
assertEquals(label + ":visited", 2, visitor.counts.remove(clz).intValue());
assertTrue(label + ":nothing-else-visited", visitor.counts.isEmpty());
}
private static void countAllFunctionTest(String label, CountAllFunction v, String tableName, String s, CountAllFunction o, boolean matches) {
assertEquals(label + ":left", tableName, v.getTableName());
basicTest(label, CountAllFunction.class, v, "COUNT", s, o, matches);
}
public void testCountAllFunction() {
CountAllFunction v1 = new CountAllFunction("a");
countAllFunctionTest("v1", v1, "a", "COUNT(a.*)", null, false);
CountAllFunction v2 = new CountAllFunction(null);
countAllFunctionTest("v2", v2, null, "COUNT(*)", v1, false);
CountAllFunction v3 = new CountAllFunction("a");
countAllFunctionTest("v3", v3, "a", "COUNT(a.*)", v1, true);
}
private static void aggregateFunctionTest(String label, AggregateFunction v, String name, boolean isDistinct, FieldValue fv, String s, AggregateFunction o, boolean matches) {
assertEquals(label + ":name", name, v.getName());
assertEquals(label + ":left", isDistinct, v.isDistinct());
assertEquals(label + ":field-value", fv, v.getValue());
basicTest(label, AggregateFunction.class, v, name, s, o, matches);
}
public void testAggregateFunction() {
AggregateFunction v1 = new AggregateFunction("COUNT", false, fv2);
aggregateFunctionTest("v1", v1, "COUNT", false, fv2, "COUNT(a.partyId)", null, false);
AggregateFunction v2 = new AggregateFunction("COUNT", true, fv2);
aggregateFunctionTest("v2", v2, "COUNT", true, fv2, "COUNT(DISTINCT a.partyId)", v1, false);
AggregateFunction v3 = new AggregateFunction("COUNT", true, fv1);
aggregateFunctionTest("v3", v3, "COUNT", true, fv1, "COUNT(DISTINCT partyId)", v1, false);
AggregateFunction v4 = new AggregateFunction("COUNT", false, fv1);
aggregateFunctionTest("v4", v4, "COUNT", false, fv1, "COUNT(partyId)", v1, false);
AggregateFunction v5 = new AggregateFunction("MAX", false, fv2);
aggregateFunctionTest("v5", v5, "MAX", false, fv2, "MAX(a.partyId)", v1, false);
AggregateFunction v6 = new AggregateFunction("COUNT", false, fv2);
aggregateFunctionTest("v6", v6, "COUNT", false, fv2, "COUNT(a.partyId)", v1, true);
}
private static void fieldValueTest(String label, FieldValue v, String tableName, String fieldName, String s, FieldValue o, boolean matches) {
assertEquals(label + ":table-name", tableName, v.getTableName());
assertEquals(label + ":field-name", fieldName, v.getFieldName());
basicTest(label, FieldValue.class, v, fieldName, s, o, matches);
}
public void testFieldValue() {
fieldValueTest("fv1", fv1, null, "partyId", "partyId", null, false);
fieldValueTest("fv2", fv2, "a", "partyId", "a.partyId", fv1, false);
fieldValueTest("fv3", fv3, null, "partyId", "partyId", fv1, true);
fieldValueTest("fv4", fv4, null, "firstName", "firstName", fv1, false);
}
private static void functionCallTest(String label, FunctionCall v, String name, List<? extends Value> values, String s, FunctionCall o, boolean matches) {
assertEquals(label + ":name", name, v.getName());
assertEquals(label + ":arg-count", values.size(), v.getArgCount());
assertEqualsIterable(label, values, v);
basicTest(label, FunctionCall.class, v, name, s, o, matches);
}
@SuppressWarnings("unchecked")
public void testFunctionCall() {
FunctionCall v1 = new FunctionCall("LENGTH", GenericTestCaseBase.<Value>list(nv1, nv2));
functionCallTest("v1", v1, "LENGTH", list(nv1, nv2), "LENGTH(1, 2.0)", null, false);
FunctionCall v2 = new FunctionCall("LENGTH", GenericTestCaseBase.<Value>list(nv1, nv3));
functionCallTest("v2", v2, "LENGTH", list(nv1, nv3), "LENGTH(1, 3)", v1, false);
FunctionCall v3 = new FunctionCall("LENGTH", GenericTestCaseBase.<Value>list(nv1, nv2));
functionCallTest("v3", v3, "LENGTH", list(nv1, nv2), "LENGTH(1, 2.0)", v1, true);
FunctionCall v4 = new FunctionCall("TRIM", GenericTestCaseBase.<Value>list(nv1, nv2));
functionCallTest("v4", v4, "TRIM", list(nv1, nv2), "TRIM(1, 2.0)", v1, false);
}
private static void mathValueTest(String label, MathValue v, String op, List<? extends ConstantValue> values, String s, MathValue o, boolean matches) {
assertEquals(label + ":op", op, v.getOp());
assertEqualsIterable(label, values, v);
basicTest(label, MathValue.class, v, null, s, o, matches);
}
@SuppressWarnings("unchecked")
public void testMathValue() {
MathValue v1 = new MathValue("+", GenericTestCaseBase.<ConstantValue>list(nv1, nv2));
mathValueTest("v1", v1, "+", list(nv1, nv2), "(1 + 2.0)", null, false);
MathValue v2 = new MathValue("+", GenericTestCaseBase.<ConstantValue>list(nv1, nv3));
mathValueTest("v2", v2, "+", list(nv1, nv3), "(1 + 3)", v1, false);
MathValue v3 = new MathValue("+", GenericTestCaseBase.<ConstantValue>list(nv1, nv2));
mathValueTest("v3", v3, "+", list(nv1, nv2), "(1 + 2.0)", v1, true);
MathValue v4 = new MathValue("-", GenericTestCaseBase.<ConstantValue>list(nv1, nv3));
mathValueTest("v4", v4, "-", list(nv1, nv3), "(1 - 3)", v1, false);
}
public void testNull() {
basicTest("null", Value.Null.class, Value.NULL, null, "NULL", null, false);
}
private static <N extends Number> void numberValueTest(String label, NumberValue<N> v, N n, String s, NumberValue<?> o, boolean matches) {
assertEquals(label + ":number", n, v.getNumber());
basicTest(label, NumberValue.class, v, null, s, o, matches);
}
public void testNumberValue() {
numberValueTest("nv1", nv1, Long.valueOf(1), "1", null, false);
numberValueTest("nv2", nv2, Double.valueOf(2), "2.0", nv1, false);
NumberValue<Long> nv3 = NumberValue.valueOf(1);
numberValueTest("nv3", nv3, Long.valueOf(1), "1", nv1, true);
}
private static void parameterValueTest(String label, ParameterValue v, String name, String s, ParameterValue o, boolean matches) {
assertEquals(label + ":name", name, v.getName());
basicTest(label, ParameterValue.class, v, null, s, o, matches);
}
public void testParameterValue() {
ParameterValue v1 = new ParameterValue("a");
parameterValueTest("v1", v1, "a", "?a", null, false);
ParameterValue v2 = new ParameterValue("b");
parameterValueTest("v2", v2, "b", "?b", v1, false);
ParameterValue v3 = new ParameterValue("a");
parameterValueTest("v3", v3, "a", "?a", v1, true);
}
private static void stringValueTest(String label, StringValue v, String string, String s, StringValue o, boolean matches) {
assertEquals(label + ":name", string, v.getString());
basicTest(label, StringValue.class, v, null, s, o, matches);
}
public void testStringValue() {
StringValue v1 = new StringValue("foo");
stringValueTest("v1", v1, "foo", "'foo'", null, false);
StringValue v2 = new StringValue("b'r");
stringValueTest("v2", v2, "b'r", "'b''r'", v1, false);
StringValue v3 = new StringValue("foo");
stringValueTest("v3", v3, "foo", "'foo'", v1, true);
}
public static class ValueVisitorRecorder extends Recorder<Class<? extends Value>> implements Value.Visitor {
public void visit(AggregateFunction value) {
record(AggregateFunction.class);
}
public void visit(FieldValue value) {
record(FieldValue.class);
}
public void visit(FunctionCall value) {
record(FunctionCall.class);
}
public void visit(MathValue value) {
record(MathValue.class);
}
public void visit(Value.Null value) {
record(Value.Null.class);
}
public void visit(NumberValue<?> value) {
record(NumberValue.class);
}
public void visit(ParameterValue value) {
record(ParameterValue.class);
}
public void visit(StringValue value) {
record(StringValue.class);
}
public void visit(CountAllFunction value) {
record(CountAllFunction.class);
}
}
}