initial add of EbeanORM server based on v2.8.1

This commit is contained in:
rbygrave committed 2012-09-14 00:40:39 +12:00
1 parent 2b74d0f9d9
commit 96ce4c0ddf
1188 files changed
+135034

No files matched your search

@@ -0,0 +1,76 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.el.ElPropertyDeploy;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
/**
* Base class for simple expressions.
*
* @author rbygrave
*/
public abstract class AbstractExpression implements SpiExpression {
private static final long serialVersionUID = 4072786211853856174L;
protected final String propName;
protected final FilterExprPath pathPrefix;
protected AbstractExpression(FilterExprPath pathPrefix, String propName) {
this.pathPrefix = pathPrefix;
this.propName = propName;
}
public String getPropertyName() {
if (pathPrefix == null){
return propName;
} else {
String path = pathPrefix.getPath();
if (path == null || path.length() == 0){
return propName;
} else {
return path+"."+propName;
}
}
}
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
String propertyName = getPropertyName();
if (propertyName != null){
ElPropertyDeploy elProp = desc.getElPropertyDeploy(propertyName);
if (elProp != null && elProp.containsMany()){
manyWhereJoin.add(elProp);
}
}
}
protected ElPropertyValue getElProp(SpiExpressionRequest request) {
String propertyName = getPropertyName();
return request.getBeanDescriptor().getElGetValue(propertyName);
}
}
@@ -0,0 +1,133 @@
package com.avaje.ebeaninternal.server.expression;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.el.ElPropertyDeploy;
class AllEqualsExpression implements SpiExpression {
private static final long serialVersionUID = -8691773558205937025L;
private final Map<String, Object> propMap;
private final FilterExprPath pathPrefix;
AllEqualsExpression(FilterExprPath pathPrefix, Map<String, Object> propMap) {
this.pathPrefix = pathPrefix;
this.propMap = propMap;
}
protected String name(String propName) {
if (pathPrefix == null) {
return propName;
} else {
String path = pathPrefix.getPath();
if (path == null || path.length() == 0) {
return propName;
} else {
return path + "." + propName;
}
}
}
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
if (propMap != null) {
Iterator<String> it = propMap.keySet().iterator();
while (it.hasNext()) {
String propertyName = it.next();
ElPropertyDeploy elProp = desc.getElPropertyDeploy(name(propertyName));
if (elProp != null && elProp.containsMany()) {
manyWhereJoin.add(elProp);
}
}
}
}
public void addBindValues(SpiExpressionRequest request) {
if (propMap.isEmpty()) {
return;
}
Iterator<Object> it = propMap.values().iterator();
while (it.hasNext()) {
Object value = it.next();
if (value != null) {
request.addBindValue(value);
} else {
// null value uses is null clause
}
}
}
public void addSql(SpiExpressionRequest request) {
if (propMap.isEmpty()) {
return;
}
request.append("(");
Set<Entry<String, Object>> entries = propMap.entrySet();
Iterator<Entry<String, Object>> it = entries.iterator();
int count = 0;
while (it.hasNext()) {
Map.Entry<java.lang.String, java.lang.Object> entry = it.next();
Object value = entry.getValue();
String propName = entry.getKey();
if (count > 0) {
request.append("and ");
}
request.append(name(propName));
if (value == null) {
request.append(" is null ");
} else {
request.append(" = ? ");
}
count++;
}
request.append(")");
}
/**
* Based on the properties and whether they are null.
* <p>
* The null check is required due to the "is null" sql being generated.
* </p>
*/
public int queryAutoFetchHash() {
int hc = AllEqualsExpression.class.getName().hashCode();
Set<Entry<String, Object>> entries = propMap.entrySet();
Iterator<Entry<String, Object>> it = entries.iterator();
while (it.hasNext()) {
Map.Entry<java.lang.String, java.lang.Object> entry = it.next();
Object value = entry.getValue();
String propName = entry.getKey();
hc = hc * 31 + propName.hashCode();
hc = hc * 31 + (value == null ? 0 : 1);
}
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
return queryAutoFetchHash();
}
}
@@ -0,0 +1,48 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
class BetweenExpression extends AbstractExpression {
private static final long serialVersionUID = 2078918165221454910L;
private static final String BETWEEN = " between ";
private final Object valueHigh;
private final Object valueLow;
BetweenExpression(FilterExprPath pathPrefix, String propertyName, Object valLo, Object valHigh) {
super(pathPrefix, propertyName);
this.valueLow = valLo;
this.valueHigh = valHigh;
}
public void addBindValues(SpiExpressionRequest request) {
request.addBindValue(valueLow);
request.addBindValue(valueHigh);
}
public void addSql(SpiExpressionRequest request) {
request.append(getPropertyName()).append(BETWEEN).append(" ? and ? ");
}
public int queryAutoFetchHash() {
int hc = BetweenExpression.class.getName().hashCode();
hc = hc * 31 + propName.hashCode();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
int hc = valueLow.hashCode();
hc = hc * 31 + valueHigh.hashCode();
return hc;
}
}
@@ -0,0 +1,101 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.el.ElPropertyDeploy;
/**
* Between expression where a value is between two properties.
*
* @author rbygrave
*/
class BetweenPropertyExpression implements SpiExpression {
private static final long serialVersionUID = 2078918165221454910L;
private static final String BETWEEN = " between ";
private final FilterExprPath pathPrefix;
private final String lowProperty;
private final String highProperty;
private final Object value;
BetweenPropertyExpression(FilterExprPath pathPrefix, String lowProperty, String highProperty, Object value) {
this.pathPrefix = pathPrefix;
this.lowProperty = lowProperty;
this.highProperty = highProperty;
this.value = value;
}
protected String name(String propName) {
if (pathPrefix == null) {
return propName;
} else {
String path = pathPrefix.getPath();
if (path == null || path.length() == 0) {
return propName;
} else {
return path + "." + propName;
}
}
}
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
ElPropertyDeploy elProp = desc.getElPropertyDeploy(name(lowProperty));
if (elProp != null && elProp.containsMany()) {
manyWhereJoin.add(elProp);
}
elProp = desc.getElPropertyDeploy(name(highProperty));
if (elProp != null && elProp.containsMany()) {
manyWhereJoin.add(elProp);
}
}
public void addBindValues(SpiExpressionRequest request) {
request.addBindValue(value);
}
public void addSql(SpiExpressionRequest request) {
request.append(" ? ").append(BETWEEN).append(name(lowProperty)).append(" and ").append(name(highProperty));
}
public int queryAutoFetchHash() {
int hc = BetweenPropertyExpression.class.getName().hashCode();
hc = hc * 31 + lowProperty.hashCode();
hc = hc * 31 + highProperty.hashCode();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
return value.hashCode();
}
}
@@ -0,0 +1,56 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
class CaseInsensitiveEqualExpression extends AbstractExpression implements LuceneAwareExpression {
private static final long serialVersionUID = -6406036750998971064L;
private final String value;
CaseInsensitiveEqualExpression(FilterExprPath pathPrefix, String propertyName, String value) {
super(pathPrefix, propertyName);
this.value = value.toLowerCase();
}
public void addBindValues(SpiExpressionRequest request) {
ElPropertyValue prop = getElProp(request);
if (prop != null && prop.isDbEncrypted()) {
// bind the key as well as the value
String encryptKey = prop.getBeanProperty().getEncryptKey().getStringValue();
request.addBindValue(encryptKey);
}
request.addBindValue(value);
}
public void addSql(SpiExpressionRequest request) {
String propertyName = getPropertyName();
String pname = propertyName;
ElPropertyValue prop = getElProp(request);
if (prop != null && prop.isDbEncrypted()) {
pname = prop.getBeanProperty().getDecryptProperty(propertyName);
}
request.append("lower(").append(pname).append(") =? ");
}
public int queryAutoFetchHash() {
int hc = CaseInsensitiveEqualExpression.class.getName().hashCode();
hc = hc * 31 + propName.hashCode();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
return value.hashCode();
}
}
@@ -0,0 +1,249 @@
package com.avaje.ebeaninternal.server.expression;
import java.util.ArrayList;
import java.util.Iterator;
import com.avaje.ebean.ExampleExpression;
import com.avaje.ebean.LikeType;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.core.OrmQueryRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.BeanProperty;
/**
* A "Query By Example" type of expression.
* <p>
* Pass in an example entity and for each non-null scalar properties an
* expression is added.
* </p>
*
* <pre class="code">
* // create an example bean and set the properties
* // with the query parameters you want
* Customer example = new Customer();
* example.setName(&quot;Rob%&quot;);
* example.setNotes(&quot;%something%&quot;);
*
* List&lt;Customer&gt; list = Ebean.find(Customer.class).where()
* // pass the bean into the where() clause
* .exampleLike(example)
* // you can add other expressions to the same query
* .gt(&quot;id&quot;, 2).findList();
*
* </pre>
*/
public class DefaultExampleExpression implements SpiExpression, ExampleExpression {
private static final long serialVersionUID = 1L;
/**
* The example bean containing the properties.
*/
private final Object entity;
/**
* Set to true to use case insensitive expressions.
*/
private boolean caseInsensitive;
/**
* The type of like (RAW, STARTS_WITH, ENDS_WITH etc)
*/
private LikeType likeType;
/**
* By default zeros are excluded.
*/
private boolean includeZeros;
/**
* The non null bean properties and found and together added as a list of
* expressions (like or equal to expressions).
*/
private ArrayList<SpiExpression> list;
private final FilterExprPath pathPrefix;
/**
* Construct the query by example expression.
*
* @param entity
* the example entity with non null property values
* @param caseInsensitive
* if true use case insensitive expressions
* @param likeType
* the type of Like wild card used
*/
public DefaultExampleExpression(FilterExprPath pathPrefix, Object entity, boolean caseInsensitive, LikeType likeType) {
this.pathPrefix = pathPrefix;
this.entity = entity;
this.caseInsensitive = caseInsensitive;
this.likeType = likeType;
}
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins whereManyJoins) {
if (list != null) {
for (int i = 0; i < list.size(); i++) {
list.get(i).containsMany(desc, whereManyJoins);
}
}
}
public ExampleExpression includeZeros() {
includeZeros = true;
return this;
}
public ExampleExpression caseInsensitive() {
caseInsensitive = true;
return this;
}
public ExampleExpression useStartsWith() {
likeType = LikeType.STARTS_WITH;
return this;
}
public ExampleExpression useContains() {
likeType = LikeType.CONTAINS;
return this;
}
public ExampleExpression useEndsWith() {
likeType = LikeType.ENDS_WITH;
return this;
}
public ExampleExpression useEqualTo() {
likeType = LikeType.EQUAL_TO;
return this;
}
/**
* This will return null for this example expression.
*/
public String getPropertyName() {
return null;
}
/**
* Adds bind values to the request.
*/
public void addBindValues(SpiExpressionRequest request) {
for (int i = 0; i < list.size(); i++) {
SpiExpression item = list.get(i);
item.addBindValues(request);
}
}
/**
* Generates and adds the sql to the request.
*/
public void addSql(SpiExpressionRequest request) {
if (!list.isEmpty()) {
request.append("(");
for (int i = 0; i < list.size(); i++) {
SpiExpression item = list.get(i);
if (i > 0) {
request.append(" and ");
}
item.addSql(request);
}
request.append(") ");
}
}
/**
* Return a hash for autoFetch query identification.
*/
public int queryAutoFetchHash() {
// we have not yet built the list of expressions
// so just based on the class name
return DefaultExampleExpression.class.getName().hashCode();
}
/**
* Return a hash for query plan identification.
*/
public int queryPlanHash(BeanQueryRequest<?> request) {
// this is always called once, and always called before
// addSql() and addBindValues() methods
list = buildExpressions(request);
int hc = DefaultExampleExpression.class.getName().hashCode();
for (int i = 0; i < list.size(); i++) {
hc = hc * 31 + list.get(i).queryPlanHash(request);
}
return hc;
}
/**
* Return a hash for the actual bind values used.
*/
public int queryBindHash() {
int hc = DefaultExampleExpression.class.getName().hashCode();
for (int i = 0; i < list.size(); i++) {
hc = hc * 31 + list.get(i).queryBindHash();
}
return hc;
}
/**
* Build the List of expressions.
*/
private ArrayList<SpiExpression> buildExpressions(BeanQueryRequest<?> request) {
ArrayList<SpiExpression> list = new ArrayList<SpiExpression>();
OrmQueryRequest<?> r = (OrmQueryRequest<?>) request;
BeanDescriptor<?> beanDescriptor = r.getBeanDescriptor();
Iterator<BeanProperty> propIter = beanDescriptor.propertiesAll();
while (propIter.hasNext()) {
BeanProperty beanProperty = propIter.next();
String propName = beanProperty.getName();
Object value = beanProperty.getValue(entity);
if (beanProperty.isScalar() && value != null) {
if (value instanceof String) {
list.add(new LikeExpression(pathPrefix, propName, (String) value, caseInsensitive, likeType));
} else {
if (!includeZeros && isZero(value)) {
// exclude the zero values typically to weed out
// primitive int and long that initialise to 0
} else {
list.add(new SimpleExpression(pathPrefix, propName, SimpleExpression.Op.EQ, value));
}
}
}
}
return list;
}
/**
* Return true if the value is a numeric zero.
*/
private boolean isZero(Object value) {
if (value instanceof Number) {
Number num = (Number) value;
double doubleValue = num.doubleValue();
if (doubleValue == 0) {
return true;
}
}
return false;
}
}
@@ -0,0 +1,356 @@
package com.avaje.ebeaninternal.server.expression;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import com.avaje.ebean.ExampleExpression;
import com.avaje.ebean.Expression;
import com.avaje.ebean.ExpressionFactory;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.Junction;
import com.avaje.ebean.LikeType;
import com.avaje.ebean.Query;
import com.avaje.ebeaninternal.api.SpiExpressionFactory;
import com.avaje.ebeaninternal.api.SpiQuery;
/**
* Default Expression factory for creating standard expressions.
*/
public class DefaultExpressionFactory implements SpiExpressionFactory {
private static final Object[] EMPTY_ARRAY = new Object[] {};
private final FilterExprPath prefix;
public DefaultExpressionFactory() {
this(null);
}
public DefaultExpressionFactory(FilterExprPath prefix) {
this.prefix = prefix;
}
public ExpressionFactory createExpressionFactory(FilterExprPath prefix) {
return new DefaultExpressionFactory(prefix);
}
public String getLang() {
return "sql";
}
/**
* Equal To - property equal to the given value.
*/
public Expression eq(String propertyName, Object value) {
if (value == null) {
return isNull(propertyName);
}
return new SimpleExpression(prefix, propertyName, SimpleExpression.Op.EQ, value);
}
/**
* Not Equal To - property not equal to the given value.
*/
public Expression ne(String propertyName, Object value) {
if (value == null) {
return isNotNull(propertyName);
}
return new SimpleExpression(prefix, propertyName, SimpleExpression.Op.NOT_EQ, value);
}
/**
* Case Insensitive Equal To - property equal to the given value (typically
* using a lower() function to make it case insensitive).
*/
public Expression ieq(String propertyName, String value) {
if (value == null) {
return isNull(propertyName);
}
return new CaseInsensitiveEqualExpression(prefix, propertyName, value);
}
/**
* Between - property between the two given values.
*/
public Expression between(String propertyName, Object value1, Object value2) {
return new BetweenExpression(prefix, propertyName, value1, value2);
}
/**
* Between - value between two given properties.
*/
public Expression betweenProperties(String lowProperty, String highProperty, Object value) {
return new BetweenPropertyExpression(prefix, lowProperty, highProperty, value);
}
/**
* Greater Than - property greater than the given value.
*/
public Expression gt(String propertyName, Object value) {
return new SimpleExpression(prefix, propertyName, SimpleExpression.Op.GT, value);
}
/**
* Greater Than or Equal to - property greater than or equal to the given
* value.
*/
public Expression ge(String propertyName, Object value) {
return new SimpleExpression(prefix, propertyName, SimpleExpression.Op.GT_EQ, value);
}
/**
* Less Than - property less than the given value.
*/
public Expression lt(String propertyName, Object value) {
return new SimpleExpression(prefix, propertyName, SimpleExpression.Op.LT, value);
}
/**
* Less Than or Equal to - property less than or equal to the given value.
*/
public Expression le(String propertyName, Object value) {
return new SimpleExpression(prefix, propertyName, SimpleExpression.Op.LT_EQ, value);
}
/**
* Is Null - property is null.
*/
public Expression isNull(String propertyName) {
return new NullExpression(prefix, propertyName, false);
}
/**
* Is Not Null - property is not null.
*/
public Expression isNotNull(String propertyName) {
return new NullExpression(prefix, propertyName, true);
}
/**
* Case insensitive {@link #exampleLike(Object)}
*/
public ExampleExpression iexampleLike(Object example) {
return new DefaultExampleExpression(prefix, example, true, LikeType.RAW);
}
/**
* Create the query by Example expression which is case sensitive and using
* LikeType.RAW (you need to add you own wildcards % and _).
*/
public ExampleExpression exampleLike(Object example) {
return new DefaultExampleExpression(prefix, example, false, LikeType.RAW);
}
/**
* Create the query by Example expression specifying more options.
*/
public ExampleExpression exampleLike(Object example, boolean caseInsensitive, LikeType likeType) {
return new DefaultExampleExpression(prefix, example, caseInsensitive, likeType);
}
/**
* Like - property like value where the value contains the SQL wild card
* characters % (percentage) and _ (underscore).
*/
public Expression like(String propertyName, String value) {
return new LikeExpression(prefix, propertyName, value, false, LikeType.RAW);
}
/**
* Case insensitive Like - property like value where the value contains the
* SQL wild card characters % (percentage) and _ (underscore). Typically uses
* a lower() function to make the expression case insensitive.
*/
public Expression ilike(String propertyName, String value) {
return new LikeExpression(prefix, propertyName, value, true, LikeType.RAW);
}
/**
* Starts With - property like value%.
*/
public Expression startsWith(String propertyName, String value) {
return new LikeExpression(prefix, propertyName, value, false, LikeType.STARTS_WITH);
}
/**
* Case insensitive Starts With - property like value%. Typically uses a
* lower() function to make the expression case insensitive.
*/
public Expression istartsWith(String propertyName, String value) {
return new LikeExpression(prefix, propertyName, value, true, LikeType.STARTS_WITH);
}
/**
* Ends With - property like %value.
*/
public Expression endsWith(String propertyName, String value) {
return new LikeExpression(prefix, propertyName, value, false, LikeType.ENDS_WITH);
}
/**
* Case insensitive Ends With - property like %value. Typically uses a lower()
* function to make the expression case insensitive.
*/
public Expression iendsWith(String propertyName, String value) {
return new LikeExpression(prefix, propertyName, value, true, LikeType.ENDS_WITH);
}
/**
* Contains - property like %value%.
*/
public Expression contains(String propertyName, String value) {
return new LikeExpression(prefix, propertyName, value, false, LikeType.CONTAINS);
}
/**
* Case insensitive Contains - property like %value%. Typically uses a lower()
* function to make the expression case insensitive.
*/
public Expression icontains(String propertyName, String value) {
return new LikeExpression(prefix, propertyName, value, true, LikeType.CONTAINS);
}
/**
* In - property has a value in the array of values.
*/
public Expression in(String propertyName, Object[] values) {
return new InExpression(prefix, propertyName, values);
}
/**
* In - using a subQuery.
*/
public Expression in(String propertyName, Query<?> subQuery) {
return new InQueryExpression(prefix, propertyName, (SpiQuery<?>) subQuery);
}
/**
* In - property has a value in the collection of values.
*/
public Expression in(String propertyName, Collection<?> values) {
return new InExpression(prefix, propertyName, values);
}
/**
* Id Equal to - ID property is equal to the value.
*/
public Expression idEq(Object value) {
return new IdExpression(value);
}
/**
* Id IN a list of id values.
*/
public Expression idIn(List<?> idList) {
return new IdInExpression(idList);
}
/**
* All Equal - Map containing property names and their values.
* <p>
* Expression where all the property names in the map are equal to the
* corresponding value.
* </p>
*
* @param propertyMap
* a map keyed by property names.
*/
public Expression allEq(Map<String, Object> propertyMap) {
return new AllEqualsExpression(prefix, propertyMap);
}
/**
* Add raw expression with a single parameter.
* <p>
* The raw expression should contain a single ? at the location of the
* parameter.
* </p>
*/
public Expression raw(String raw, Object value) {
return new RawExpression(raw, new Object[] { value });
}
/**
* Add raw expression with an array of parameters.
* <p>
* The raw expression should contain the same number of ? as there are
* parameters.
* </p>
*/
public Expression raw(String raw, Object[] values) {
return new RawExpression(raw, values);
}
/**
* Add raw expression with no parameters.
*/
public Expression raw(String raw) {
return new RawExpression(raw, EMPTY_ARRAY);
}
/**
* And - join two expressions with a logical and.
*/
public Expression and(Expression expOne, Expression expTwo) {
return new LogicExpression.And(expOne, expTwo);
}
/**
* Or - join two expressions with a logical or.
*/
public Expression or(Expression expOne, Expression expTwo) {
return new LogicExpression.Or(expOne, expTwo);
}
/**
* Negate the expression (prefix it with NOT).
*/
public Expression not(Expression exp) {
return new NotExpression(exp);
}
/**
* Return a list of expressions that will be joined by AND's.
*/
public <T> Junction<T> conjunction(Query<T> query) {
return new JunctionExpression.Conjunction<T>(query, query.where());
}
/**
* Return a list of expressions that will be joined by OR's.
*/
public <T> Junction<T> disjunction(Query<T> query) {
return new JunctionExpression.Disjunction<T>(query, query.where());
}
/**
* Return a list of expressions that will be joined by AND's.
*/
public <T> Junction<T> conjunction(Query<T> query, ExpressionList<T> parent) {
return new JunctionExpression.Conjunction<T>(query, parent);
}
/**
* Return a list of expressions that will be joined by OR's.
*/
public <T> Junction<T> disjunction(Query<T> query, ExpressionList<T> parent) {
return new JunctionExpression.Disjunction<T>(query, parent);
}
}
@@ -0,0 +1,61 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.expression;
import java.io.Serializable;
/**
* This is the path prefix for filterMany.
* <p>
* The actual path can change due to FetchConfig query joins that proceed
* the query that includes the filterMany.
* </p>
*
* @author rbygrave
*/
public class FilterExprPath implements Serializable {
private static final long serialVersionUID = -6420905565372842018L;
/**
* The path of the filterMany.
*/
private String path;
public FilterExprPath(String path){
this.path = path;
}
/**
* Trim off leading part of the path due to a
* proceeding (earlier) query join etc.
*/
public void trimPath(int prefixTrim) {
path = path.substring(prefixTrim);
}
/**
* Return the path. This is a prefix used in the filterMany expressions.
*/
public String getPath() {
return path;
}
}
@@ -0,0 +1,66 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.util.DefaultExpressionRequest;
/**
* Slightly redundant as Query.setId() ultimately also does the same job.
*/
class IdExpression implements SpiExpression {
private static final long serialVersionUID = -3065936341718489842L;
private final Object value;
IdExpression(Object value) {
this.value = value;
}
/**
* Always returns false.
*/
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
}
public void addBindValues(SpiExpressionRequest request) {
// 'flatten' EmbeddedId and multiple Id cases
// into an array of the underlying scalar field values
DefaultExpressionRequest r = (DefaultExpressionRequest) request;
Object[] bindIdValues = r.getBeanDescriptor().getBindIdValues(value);
for (int i = 0; i < bindIdValues.length; i++) {
request.addBindValue(bindIdValues[i]);
}
}
public void addSql(SpiExpressionRequest request) {
DefaultExpressionRequest r = (DefaultExpressionRequest) request;
String idSql = r.getBeanDescriptor().getIdBinderIdSql();
request.append(idSql).append(" ");
}
/**
* No properties so this is just a unique static number.
*/
public int queryAutoFetchHash() {
// this number is unique for a given bean type
// which is all that is required
return IdExpression.class.getName().hashCode();
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
return value.hashCode();
}
}
@@ -0,0 +1,86 @@
package com.avaje.ebeaninternal.server.expression;
import java.util.List;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.server.deploy.id.IdBinder;
import com.avaje.ebeaninternal.util.DefaultExpressionRequest;
/**
* Slightly redundant as Query.setId() ultimately also does the same job.
*/
public class IdInExpression implements SpiExpression {
private static final long serialVersionUID = 1L;
private final List<?> idList;
public IdInExpression(List<?> idList) {
this.idList = idList;
}
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
}
public void addBindValues(SpiExpressionRequest request) {
// Bind the Id values including EmbeddedId and multiple Id
DefaultExpressionRequest r = (DefaultExpressionRequest) request;
BeanDescriptor<?> descriptor = r.getBeanDescriptor();
IdBinder idBinder = descriptor.getIdBinder();
for (int i = 0; i < idList.size(); i++) {
idBinder.addIdInBindValue(request, idList.get(i));
}
}
/**
* For use with deleting non attached detail beans during stateless update.
*/
public void addSqlNoAlias(SpiExpressionRequest request) {
DefaultExpressionRequest r = (DefaultExpressionRequest) request;
BeanDescriptor<?> descriptor = r.getBeanDescriptor();
IdBinder idBinder = descriptor.getIdBinder();
request.append(descriptor.getIdBinder().getBindIdInSql(null));
String inClause = idBinder.getIdInValueExpr(idList.size());
request.append(inClause);
}
public void addSql(SpiExpressionRequest request) {
DefaultExpressionRequest r = (DefaultExpressionRequest) request;
BeanDescriptor<?> descriptor = r.getBeanDescriptor();
IdBinder idBinder = descriptor.getIdBinder();
request.append(descriptor.getIdBinderInLHSSql());
String inClause = idBinder.getIdInValueExpr(idList.size());
request.append(inClause);
}
/**
* Incorporates the number of Id values to bind.
*/
public int queryAutoFetchHash() {
// this number is unique for a given bean type
// which is all that is required
int hc = IdInExpression.class.getName().hashCode();
hc = hc * 31 + idList.size();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
return idList.hashCode();
}
}
@@ -0,0 +1,100 @@
package com.avaje.ebeaninternal.server.expression;
import java.util.Collection;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
class InExpression extends AbstractExpression {
private static final long serialVersionUID = 3150665801693551260L;
private final Object[] values;
InExpression(FilterExprPath pathPrefix, String propertyName, Collection<?> coll) {
super(pathPrefix, propertyName);
values = coll.toArray(new Object[coll.size()]);
}
InExpression(FilterExprPath pathPrefix, String propertyName, Object[] array) {
super(pathPrefix, propertyName);
this.values = array;
}
public void addBindValues(SpiExpressionRequest request) {
ElPropertyValue prop = getElProp(request);
if (prop != null && !prop.isAssocId()) {
prop = null;
}
for (int i = 0; i < values.length; i++) {
if (prop == null) {
request.addBindValue(values[i]);
} else {
// extract the id values from the bean
Object[] ids = prop.getAssocOneIdValues(values[i]);
if (ids != null) {
for (int j = 0; j < ids.length; j++) {
request.addBindValue(ids[j]);
}
}
}
}
}
public void addSql(SpiExpressionRequest request) {
if (values.length == 0) {
// 'no match' for in empty collection
request.append("1=0");
return;
}
String propertyName = getPropertyName();
ElPropertyValue prop = getElProp(request);
if (prop != null && !prop.isAssocId()) {
prop = null;
}
if (prop != null) {
request.append(prop.getAssocIdInExpr(propertyName));
String inClause = prop.getAssocIdInValueExpr(values.length);
request.append(inClause);
} else {
request.append(propertyName);
request.append(" in (?");
for (int i = 1; i < values.length; i++) {
request.append(", ").append("?");
}
request.append(" ) ");
}
}
/**
* Based on the number of values in the in clause.
*/
public int queryAutoFetchHash() {
int hc = InExpression.class.getName().hashCode() + 31 * values.length;
hc = hc * 31 + propName.hashCode();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
int hc = 0;
for (int i = 1; i < values.length; i++) {
hc = 31 * hc + values[i].hashCode();
}
return hc;
}
}
@@ -0,0 +1,86 @@
package com.avaje.ebeaninternal.server.expression;
import java.util.List;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.SpiEbeanServer;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.api.SpiQuery;
import com.avaje.ebeaninternal.server.query.CQuery;
/**
* In expression using a sub query.
*
* @authors Mario and Rob
*/
class InQueryExpression extends AbstractExpression {
private static final long serialVersionUID = 666990277309851644L;
private final SpiQuery<?> subQuery;
private transient CQuery<?> compiledSubQuery;
public InQueryExpression(FilterExprPath pathPrefix, String propertyName, SpiQuery<?> subQuery) {
super(pathPrefix, propertyName);
this.subQuery = subQuery;
}
public int queryAutoFetchHash() {
int hc = InQueryExpression.class.getName().hashCode();
hc = hc * 31 + propName.hashCode();
hc = hc * 31 + subQuery.queryAutofetchHash();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
// queryPlanHash executes prior to addSql() or addBindValues()
// ... so compiledQuery will exist
compiledSubQuery = compileSubQuery(request);
int hc = InQueryExpression.class.getName().hashCode();
hc = hc * 31 + propName.hashCode();
hc = hc * 31 + subQuery.queryPlanHash(request);
return hc;
}
/**
* Compile/build the sub query.
*/
private CQuery<?> compileSubQuery(BeanQueryRequest<?> queryRequest) {
SpiEbeanServer ebeanServer = (SpiEbeanServer) queryRequest.getEbeanServer();
return ebeanServer.compileQuery(subQuery, queryRequest.getTransaction());
}
public int queryBindHash() {
return subQuery.queryBindHash();
}
public void addSql(SpiExpressionRequest request) {
String subSelect = compiledSubQuery.getGeneratedSql();
subSelect = subSelect.replace('\n', ' ');
String propertyName = getPropertyName();
request.append(" (");
request.append(propertyName);
request.append(") in (");
request.append(subSelect);
request.append(") ");
}
public void addBindValues(SpiExpressionRequest request) {
List<Object> bindParams = compiledSubQuery.getPredicates().getWhereExprBindValues();
if (bindParams == null) {
return;
}
for (int i = 0; i < bindParams.size(); i++) {
request.addBindValue(bindParams.get(i));
}
}
}
@@ -0,0 +1,421 @@
package com.avaje.ebeaninternal.server.expression;
import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.avaje.ebean.Expression;
import com.avaje.ebean.ExpressionList;
import com.avaje.ebean.FutureIds;
import com.avaje.ebean.FutureList;
import com.avaje.ebean.FutureRowCount;
import com.avaje.ebean.Junction;
import com.avaje.ebean.OrderBy;
import com.avaje.ebean.PagingList;
import com.avaje.ebean.QueryIterator;
import com.avaje.ebean.QueryListener;
import com.avaje.ebean.QueryResultVisitor;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
import com.avaje.ebeaninternal.util.DefaultExpressionList;
/**
* Junction implementation.
*/
abstract class JunctionExpression<T> implements Junction<T>, SpiExpression, ExpressionList<T> {
private static final long serialVersionUID = -7422204102750462676L;
private static final String OR = " or ";
private static final String AND = " and ";
static class Conjunction<T> extends JunctionExpression<T> {
private static final long serialVersionUID = -645619859900030678L;
Conjunction(com.avaje.ebean.Query<T> query, ExpressionList<T> parent) {
super(AND, query, parent);
}
}
static class Disjunction<T> extends JunctionExpression<T> {
private static final long serialVersionUID = -8464470066692221413L;
Disjunction(com.avaje.ebean.Query<T> query, ExpressionList<T> parent) {
super(OR, query, parent);
}
}
// private final ArrayList<SpiExpression> list = new
// ArrayList<SpiExpression>();
private final DefaultExpressionList<T> exprList;
private final String joinType;
JunctionExpression(String joinType, com.avaje.ebean.Query<T> query, ExpressionList<T> parent) {
this.joinType = joinType;
this.exprList = new DefaultExpressionList<T>(query, parent);
}
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
List<SpiExpression> list = exprList.internalList();
for (int i = 0; i < list.size(); i++) {
list.get(i).containsMany(desc, manyWhereJoin);
}
}
public Junction<T> add(Expression item) {
SpiExpression i = (SpiExpression) item;
exprList.add(i);
return this;
}
public Junction<T> addAll(ExpressionList<T> addList) {
exprList.addAll(addList);
return this;
}
public void addBindValues(SpiExpressionRequest request) {
List<SpiExpression> list = exprList.internalList();
for (int i = 0; i < list.size(); i++) {
SpiExpression item = list.get(i);
item.addBindValues(request);
}
}
public void addSql(SpiExpressionRequest request) {
List<SpiExpression> list = exprList.internalList();
if (!list.isEmpty()) {
request.append("(");
for (int i = 0; i < list.size(); i++) {
SpiExpression item = list.get(i);
if (i > 0) {
request.append(joinType);
}
item.addSql(request);
}
request.append(") ");
}
}
/**
* Based on Junction type and all the expression contained.
*/
public int queryAutoFetchHash() {
int hc = JunctionExpression.class.getName().hashCode();
hc = hc * 31 + joinType.hashCode();
List<SpiExpression> list = exprList.internalList();
for (int i = 0; i < list.size(); i++) {
hc = hc * 31 + list.get(i).queryAutoFetchHash();
}
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
int hc = JunctionExpression.class.getName().hashCode();
hc = hc * 31 + joinType.hashCode();
List<SpiExpression> list = exprList.internalList();
for (int i = 0; i < list.size(); i++) {
hc = hc * 31 + list.get(i).queryPlanHash(request);
}
return hc;
}
public int queryBindHash() {
int hc = JunctionExpression.class.getName().hashCode();
List<SpiExpression> list = exprList.internalList();
for (int i = 0; i < list.size(); i++) {
hc = hc * 31 + list.get(i).queryBindHash();
}
return hc;
}
public ExpressionList<T> endJunction() {
return exprList.endJunction();
}
public ExpressionList<T> allEq(Map<String, Object> propertyMap) {
return exprList.allEq(propertyMap);
}
public ExpressionList<T> and(Expression expOne, Expression expTwo) {
return exprList.and(expOne, expTwo);
}
public ExpressionList<T> between(String propertyName, Object value1, Object value2) {
return exprList.between(propertyName, value1, value2);
}
public ExpressionList<T> betweenProperties(String lowProperty, String highProperty, Object value) {
return exprList.betweenProperties(lowProperty, highProperty, value);
}
public Junction<T> conjunction() {
return exprList.conjunction();
}
public ExpressionList<T> contains(String propertyName, String value) {
return exprList.contains(propertyName, value);
}
public Junction<T> disjunction() {
return exprList.disjunction();
}
public ExpressionList<T> endsWith(String propertyName, String value) {
return exprList.endsWith(propertyName, value);
}
public ExpressionList<T> eq(String propertyName, Object value) {
return exprList.eq(propertyName, value);
}
public ExpressionList<T> exampleLike(Object example) {
return exprList.exampleLike(example);
}
public ExpressionList<T> filterMany(String prop) {
throw new RuntimeException("filterMany not allowed on Junction expression list");
}
public FutureIds<T> findFutureIds() {
return exprList.findFutureIds();
}
public FutureList<T> findFutureList() {
return exprList.findFutureList();
}
public FutureRowCount<T> findFutureRowCount() {
return exprList.findFutureRowCount();
}
public List<Object> findIds() {
return exprList.findIds();
}
public void findVisit(QueryResultVisitor<T> visitor) {
exprList.findVisit(visitor);
}
public QueryIterator<T> findIterate() {
return exprList.findIterate();
}
public List<T> findList() {
return exprList.findList();
}
public Map<?, T> findMap() {
return exprList.findMap();
}
public <K> Map<K, T> findMap(String keyProperty, Class<K> keyType) {
return exprList.findMap(keyProperty, keyType);
}
public PagingList<T> findPagingList(int pageSize) {
return exprList.findPagingList(pageSize);
}
public int findRowCount() {
return exprList.findRowCount();
}
public Set<T> findSet() {
return exprList.findSet();
}
public T findUnique() {
return exprList.findUnique();
}
public ExpressionList<T> ge(String propertyName, Object value) {
return exprList.ge(propertyName, value);
}
public ExpressionList<T> gt(String propertyName, Object value) {
return exprList.gt(propertyName, value);
}
public ExpressionList<T> having() {
throw new RuntimeException("having() not allowed on Junction expression list");
}
public ExpressionList<T> icontains(String propertyName, String value) {
return exprList.icontains(propertyName, value);
}
public ExpressionList<T> idEq(Object value) {
return exprList.idEq(value);
}
public ExpressionList<T> idIn(List<?> idValues) {
return exprList.idIn(idValues);
}
public ExpressionList<T> iendsWith(String propertyName, String value) {
return exprList.iendsWith(propertyName, value);
}
public ExpressionList<T> ieq(String propertyName, String value) {
return exprList.ieq(propertyName, value);
}
public ExpressionList<T> iexampleLike(Object example) {
return exprList.iexampleLike(example);
}
public ExpressionList<T> ilike(String propertyName, String value) {
return exprList.ilike(propertyName, value);
}
public ExpressionList<T> in(String propertyName, Collection<?> values) {
return exprList.in(propertyName, values);
}
public ExpressionList<T> in(String propertyName, Object... values) {
return exprList.in(propertyName, values);
}
public ExpressionList<T> in(String propertyName, com.avaje.ebean.Query<?> subQuery) {
return exprList.in(propertyName, subQuery);
}
public ExpressionList<T> isNotNull(String propertyName) {
return exprList.isNotNull(propertyName);
}
public ExpressionList<T> isNull(String propertyName) {
return exprList.isNull(propertyName);
}
public ExpressionList<T> istartsWith(String propertyName, String value) {
return exprList.istartsWith(propertyName, value);
}
public com.avaje.ebean.Query<T> join(String assocProperty, String assocProperties) {
return exprList.join(assocProperty, assocProperties);
}
public com.avaje.ebean.Query<T> join(String assocProperties) {
return exprList.join(assocProperties);
}
public ExpressionList<T> le(String propertyName, Object value) {
return exprList.le(propertyName, value);
}
public ExpressionList<T> like(String propertyName, String value) {
return exprList.like(propertyName, value);
}
public ExpressionList<T> lt(String propertyName, Object value) {
return exprList.lt(propertyName, value);
}
public ExpressionList<T> ne(String propertyName, Object value) {
return exprList.ne(propertyName, value);
}
public ExpressionList<T> not(Expression exp) {
return exprList.not(exp);
}
public ExpressionList<T> or(Expression expOne, Expression expTwo) {
return exprList.or(expOne, expTwo);
}
public OrderBy<T> order() {
return exprList.order();
}
public com.avaje.ebean.Query<T> order(String orderByClause) {
return exprList.order(orderByClause);
}
public OrderBy<T> orderBy() {
return exprList.orderBy();
}
public com.avaje.ebean.Query<T> orderBy(String orderBy) {
return exprList.orderBy(orderBy);
}
public com.avaje.ebean.Query<T> query() {
return exprList.query();
}
public ExpressionList<T> raw(String raw, Object value) {
return exprList.raw(raw, value);
}
public ExpressionList<T> raw(String raw, Object[] values) {
return exprList.raw(raw, values);
}
public ExpressionList<T> raw(String raw) {
return exprList.raw(raw);
}
public com.avaje.ebean.Query<T> select(String properties) {
return exprList.select(properties);
}
public com.avaje.ebean.Query<T> setBackgroundFetchAfter(int backgroundFetchAfter) {
return exprList.setBackgroundFetchAfter(backgroundFetchAfter);
}
public com.avaje.ebean.Query<T> setFirstRow(int firstRow) {
return exprList.setFirstRow(firstRow);
}
public com.avaje.ebean.Query<T> setListener(QueryListener<T> queryListener) {
return exprList.setListener(queryListener);
}
public com.avaje.ebean.Query<T> setMapKey(String mapKey) {
return exprList.setMapKey(mapKey);
}
public com.avaje.ebean.Query<T> setMaxRows(int maxRows) {
return exprList.setMaxRows(maxRows);
}
public com.avaje.ebean.Query<T> setOrderBy(String orderBy) {
return exprList.setOrderBy(orderBy);
}
public com.avaje.ebean.Query<T> setUseCache(boolean useCache) {
return exprList.setUseCache(useCache);
}
public ExpressionList<T> startsWith(String propertyName, String value) {
return exprList.startsWith(propertyName, value);
}
public ExpressionList<T> where() {
return exprList.where();
}
}
@@ -0,0 +1,98 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.LikeType;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
class LikeExpression extends AbstractExpression implements LuceneAwareExpression {
private static final long serialVersionUID = -5398151809111172380L;
private final String val;
private final boolean caseInsensitive;
private final LikeType type;
LikeExpression(FilterExprPath pathPrefix, String propertyName, String value, boolean caseInsensitive, LikeType type) {
super(pathPrefix, propertyName);
this.caseInsensitive = caseInsensitive;
this.type = type;
this.val = value;
}
public void addBindValues(SpiExpressionRequest request) {
ElPropertyValue prop = getElProp(request);
if (prop != null && prop.isDbEncrypted()) {
// bind the key as well as the value
String encryptKey = prop.getBeanProperty().getEncryptKey().getStringValue();
request.addBindValue(encryptKey);
}
String bindValue = getValue(val, caseInsensitive, type);
request.addBindValue(bindValue);
}
public void addSql(SpiExpressionRequest request) {
String propertyName = getPropertyName();
String pname = propertyName;
ElPropertyValue prop = getElProp(request);
if (prop != null && prop.isDbEncrypted()) {
pname = prop.getBeanProperty().getDecryptProperty(propertyName);
}
if (caseInsensitive) {
request.append("lower(").append(pname).append(")");
} else {
request.append(pname);
}
if (type.equals(LikeType.EQUAL_TO)) {
request.append(" = ? ");
} else {
request.append(" like ? ");
}
}
/**
* Based on caseInsensitive and the property name.
*/
public int queryAutoFetchHash() {
int hc = LikeExpression.class.getName().hashCode();
hc = hc * 31 + (caseInsensitive ? 0 : 1);
hc = hc * 31 + propName.hashCode();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
return val.hashCode();
}
private static String getValue(String value, boolean caseInsensitive, LikeType type) {
if (caseInsensitive) {
value = value.toLowerCase();
}
switch (type) {
case RAW:
return value;
case STARTS_WITH:
return value + "%";
case ENDS_WITH:
return "%" + value;
case CONTAINS:
return "%" + value + "%";
case EQUAL_TO:
return value;
default:
throw new RuntimeException("LikeType " + type + " missed?");
}
}
}
@@ -0,0 +1,92 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.Expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
/**
* A logical And or Or for joining two expressions.
*/
abstract class LogicExpression implements SpiExpression {
private static final long serialVersionUID = 616860781960645251L;
static final String AND = " and ";
static final String OR = " or ";
static class And extends LogicExpression {
private static final long serialVersionUID = -3832889676798526444L;
And(Expression expOne, Expression expTwo) {
super(AND, expOne, expTwo);
}
}
static class Or extends LogicExpression {
private static final long serialVersionUID = -6871993143194094819L;
Or(Expression expOne, Expression expTwo) {
super(OR, expOne, expTwo);
}
}
private final SpiExpression expOne;
private final SpiExpression expTwo;
private final String joinType;
LogicExpression(String joinType, Expression expOne, Expression expTwo) {
this.joinType = joinType;
this.expOne = (SpiExpression) expOne;
this.expTwo = (SpiExpression) expTwo;
}
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
expOne.containsMany(desc, manyWhereJoin);
expTwo.containsMany(desc, manyWhereJoin);
}
public void addBindValues(SpiExpressionRequest request) {
expOne.addBindValues(request);
expTwo.addBindValues(request);
}
public void addSql(SpiExpressionRequest request) {
request.append("(");
expOne.addSql(request);
request.append(joinType);
expTwo.addSql(request);
request.append(") ");
}
/**
* Based on the joinType plus the two expressions.
*/
public int queryAutoFetchHash() {
int hc = LogicExpression.class.getName().hashCode() + joinType.hashCode();
hc = hc * 31 + expOne.queryAutoFetchHash();
hc = hc * 31 + expTwo.queryAutoFetchHash();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
int hc = LogicExpression.class.getName().hashCode() + joinType.hashCode();
hc = hc * 31 + expOne.queryPlanHash(request);
hc = hc * 31 + expTwo.queryPlanHash(request);
return hc;
}
public int queryBindHash() {
int hc = expOne.queryBindHash();
hc = hc * 31 + expTwo.queryBindHash();
return hc;
}
}
@@ -0,0 +1,29 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.expression;
/**
* Marker interface for lucene aware expressions.
*
* @author rbygrave
*/
public interface LuceneAwareExpression {
}
@@ -0,0 +1,55 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.Expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
final class NotExpression implements SpiExpression, LuceneAwareExpression {
private static final long serialVersionUID = 5648926732402355781L;
private static final String NOT = "not (";
private final SpiExpression exp;
NotExpression(Expression exp) {
this.exp = (SpiExpression) exp;
}
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
exp.containsMany(desc, manyWhereJoin);
}
public void addBindValues(SpiExpressionRequest request) {
exp.addBindValues(request);
}
public void addSql(SpiExpressionRequest request) {
request.append(NOT);
exp.addSql(request);
request.append(") ");
}
/**
* Based on the expression.
*/
public int queryAutoFetchHash() {
int hc = NotExpression.class.getName().hashCode();
hc = hc * 31 + exp.queryAutoFetchHash();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
int hc = NotExpression.class.getName().hashCode();
hc = hc * 31 + exp.queryPlanHash(request);
return hc;
}
public int queryBindHash() {
return exp.queryBindHash();
}
}
@@ -0,0 +1,58 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
/**
* Slightly redundant as Query.setId() ultimately also does the same job.
*/
class NullExpression extends AbstractExpression {
private static final long serialVersionUID = 4246991057451128269L;
private final boolean notNull;
NullExpression(FilterExprPath pathPrefix, String propertyName, boolean notNull) {
super(pathPrefix, propertyName);
this.notNull = notNull;
}
public void addBindValues(SpiExpressionRequest request) {
}
public void addSql(SpiExpressionRequest request) {
String propertyName = getPropertyName();
String nullExpr = notNull ? " is not null " : " is null ";
ElPropertyValue prop = getElProp(request);
if (prop != null && prop.isAssocId()){
request.append(prop.getAssocOneIdExpr(propertyName, nullExpr));
return;
}
request.append(propertyName).append(nullExpr);
}
/**
* Based on notNull flag and the propertyName.
*/
public int queryAutoFetchHash() {
int hc = NullExpression.class.getName().hashCode();
hc = hc * 31 + (notNull ? 1 : 0);
hc = hc * 31 + propName.hashCode();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
return (notNull ? 1 : 0);
}
}
@@ -0,0 +1,35 @@
/**
* Copyright (C) 2009 Authors
*
* This file is part of Ebean.
*
* Ebean is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Ebean is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Ebean; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package com.avaje.ebeaninternal.server.expression;
import javax.persistence.PersistenceException;
/**
* Exception used to wrap Lucene parsing exceptions.
*/
public class PersistenceLuceneParseException extends PersistenceException {
private static final long serialVersionUID = 838790249273928392L;
public PersistenceLuceneParseException(Throwable e){
super(e);
}
}
@@ -0,0 +1,54 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.ManyWhereJoins;
import com.avaje.ebeaninternal.api.SpiExpression;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
class RawExpression implements SpiExpression {
private static final long serialVersionUID = 7973903141340334606L;
private final String sql;
private final Object[] values;
RawExpression(String sql, Object[] values) {
this.sql = sql;
this.values = values;
}
public void containsMany(BeanDescriptor<?> desc, ManyWhereJoins manyWhereJoin) {
}
public void addBindValues(SpiExpressionRequest request) {
if (values != null) {
for (int i = 0; i < values.length; i++) {
request.addBindValue(values[i]);
}
}
}
public void addSql(SpiExpressionRequest request) {
request.append(sql);
}
/**
* Based on the sql.
*/
public int queryAutoFetchHash() {
int hc = RawExpression.class.getName().hashCode();
hc = hc * 31 + sql.hashCode();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
return sql.hashCode();
}
}
@@ -0,0 +1,113 @@
package com.avaje.ebeaninternal.server.expression;
import com.avaje.ebean.event.BeanQueryRequest;
import com.avaje.ebeaninternal.api.SpiExpressionRequest;
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
public class SimpleExpression extends AbstractExpression implements LuceneAwareExpression {
private static final long serialVersionUID = -382881395755603790L;
enum Op {
EQ(" = ? ", " = "), NOT_EQ(" <> ? ", " <> "), LT(" < ? ", " < "), LT_EQ(" <= ? ", " <= "), GT(" > ? ", " > "), GT_EQ(" >= ? ", " >= ");
String exp;
String shortDesc;
Op(String exp, String shortDesc) {
this.exp = exp;
this.shortDesc = shortDesc;
}
public String bind() {
return exp;
}
public String shortDesc() {
return shortDesc;
}
}
private final Op type;
private final Object value;
public SimpleExpression(FilterExprPath pathPrefix, String propertyName, Op type, Object value) {
super(pathPrefix, propertyName);
this.type = type;
this.value = value;
}
public boolean isOpEquals() {
return Op.EQ.equals(type);
}
public void addBindValues(SpiExpressionRequest request) {
ElPropertyValue prop = getElProp(request);
if (prop != null) {
if (prop.isAssocId()) {
Object[] ids = prop.getAssocOneIdValues(value);
if (ids != null) {
for (int i = 0; i < ids.length; i++) {
request.addBindValue(ids[i]);
}
}
return;
}
if (prop.isDbEncrypted()) {
// bind the key as well as the value
String encryptKey = prop.getBeanProperty().getEncryptKey().getStringValue();
request.addBindValue(encryptKey);
} else if (prop.isLocalEncrypted()) {
// not supporting this for equals (but probably could)
// prop.getBeanProperty().getScalarType();
}
}
request.addBindValue(value);
}
public void addSql(SpiExpressionRequest request) {
String propertyName = getPropertyName();
ElPropertyValue prop = getElProp(request);
if (prop != null) {
if (prop.isAssocId()) {
request.append(prop.getAssocOneIdExpr(propertyName, type.bind()));
return;
}
if (prop.isDbEncrypted()) {
String dsql = prop.getBeanProperty().getDecryptSql();
request.append(dsql).append(type.bind());
return;
}
}
request.append(propertyName).append(type.bind());
}
/**
* Based on the type and propertyName.
*/
public int queryAutoFetchHash() {
int hc = SimpleExpression.class.getName().hashCode();
hc = hc * 31 + propName.hashCode();
hc = hc * 31 + type.name().hashCode();
return hc;
}
public int queryPlanHash(BeanQueryRequest<?> request) {
return queryAutoFetchHash();
}
public int queryBindHash() {
return value.hashCode();
}
public Object getValue() {
return value;
}
}
@@ -0,0 +1,36 @@
<HTML>
<HEAD>
<META HTTP-EQUIV="CONTENT-TYPE" CONTENT="text/html; charset=iso-8859-1">
<TITLE>Collection objects for lazy loading</TITLE>
</HEAD>
<Body BGCOLOR="#ffffff">
Expressions for building WHERE clauses.
<p>
You will most likely use expressions directly off the ExpressionList.
</p>
<pre class="code">
Query&lt;Order&gt; query = Ebean.createQuery(Order.class)
.where()
.like(&quot;customer.name&quot;,&quot;rob%&quot;)
.gt(&quot;orderDate&quot;,lastWeek);
List&lt;Order&gt; orderList = query.findList();
...
</pre>
<p>
In the code above the LIKE and GREATER THAN Expressions are added to the where clause.
The way this works is that where() returns an ExpressionList which has methods on
it to create the standard expressions (EQUAL TO, LIKE etc).
</p>
<p>
I expect most people to add their expressions in this way. The Expr expression factory
object also has the ability to create the standard expressions.
</p>
<p>
You can build your own Expression objects by implementing the Expression interface.
</p>
</Body>
</HTML>