mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-11-18 22:59:25 +00:00
initial add of EbeanORM server based on v2.8.1
This commit is contained in:
1 parent
2b74d0f9d9
commit
96ce4c0ddf
1188 files changed
+135034
No files matched your search
File diff suppressed because it is too large.
Load diff
@@ -0,0 +1,235 @@
|
||||
/**
|
||||
* Copyright (C) 2006 Robin Bygrave
|
||||
*
|
||||
* 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.querydefn;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebeaninternal.api.BindParams;
|
||||
import com.avaje.ebeaninternal.api.SpiUpdate;
|
||||
import com.avaje.ebeaninternal.server.deploy.DeployNamedUpdate;
|
||||
|
||||
/**
|
||||
* Default implementation of OrmUpdate.
|
||||
*/
|
||||
public final class DefaultOrmUpdate<T> implements SpiUpdate<T>, Serializable {
|
||||
|
||||
private static final long serialVersionUID = -8791423602246515438L;
|
||||
|
||||
|
||||
private transient final EbeanServer server;
|
||||
|
||||
private final Class<?> beanType;
|
||||
|
||||
/**
|
||||
* The name of the update.
|
||||
*/
|
||||
private final String name;
|
||||
|
||||
/**
|
||||
* The parameters used to bind to the sql.
|
||||
*/
|
||||
private final BindParams bindParams = new BindParams();
|
||||
|
||||
/**
|
||||
* The sql update or delete statement.
|
||||
*/
|
||||
private final String updateStatement;
|
||||
|
||||
/**
|
||||
* Automatically detect the table being modified by this sql. This will
|
||||
* register this information so that eBean invalidates cached objects if
|
||||
* required.
|
||||
*/
|
||||
private boolean notifyCache = true;
|
||||
|
||||
private int timeout;
|
||||
|
||||
private String generatedSql;
|
||||
|
||||
private final String baseTable;
|
||||
|
||||
private final OrmUpdateType type;
|
||||
|
||||
/**
|
||||
* Create with a specific server. This means you can use the
|
||||
* UpdateSql.execute() method.
|
||||
*/
|
||||
public DefaultOrmUpdate(Class<?> beanType, EbeanServer server, String baseTable, String updateStatement) {
|
||||
this.beanType = beanType;
|
||||
this.server = server;
|
||||
this.baseTable = baseTable;
|
||||
this.name = "";
|
||||
this.updateStatement = updateStatement;
|
||||
this.type = deriveType(updateStatement);
|
||||
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate(Class<?> beanType, EbeanServer server, String baseTable, DeployNamedUpdate namedUpdate) {
|
||||
|
||||
this.beanType = beanType;
|
||||
this.server = server;
|
||||
this.baseTable = baseTable;
|
||||
this.name = namedUpdate.getName();
|
||||
this.notifyCache = namedUpdate.isNotifyCache();
|
||||
|
||||
// named updates are always converted to sql as part
|
||||
// of the initialisation
|
||||
this.updateStatement = namedUpdate.getSqlUpdateStatement();
|
||||
this.type = deriveType(updateStatement);
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate<T> setTimeout(int secs){
|
||||
this.timeout = secs;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Class<?> getBeanType() {
|
||||
return beanType;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the timeout in seconds.
|
||||
*/
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
private SpiUpdate.OrmUpdateType deriveType(String updateStatement) {
|
||||
|
||||
updateStatement = updateStatement.trim();
|
||||
int spacepos = updateStatement.indexOf(' ');
|
||||
if (spacepos == -1){
|
||||
return SpiUpdate.OrmUpdateType.UNKNOWN;
|
||||
|
||||
} else {
|
||||
String firstWord = updateStatement.substring(0, spacepos);
|
||||
if (firstWord.equalsIgnoreCase("update")){
|
||||
return SpiUpdate.OrmUpdateType.UPDATE;
|
||||
|
||||
} else if (firstWord.equalsIgnoreCase("insert")) {
|
||||
return SpiUpdate.OrmUpdateType.INSERT;
|
||||
|
||||
} else if (firstWord.equalsIgnoreCase("delete")) {
|
||||
return SpiUpdate.OrmUpdateType.DELETE;
|
||||
} else {
|
||||
return SpiUpdate.OrmUpdateType.UNKNOWN;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int execute() {
|
||||
return server.execute(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set this to false if you don't want eBean to automatically deduce the
|
||||
* table modification information and process it.
|
||||
* <p>
|
||||
* Set this to false if you don't want any cache invalidation or text index
|
||||
* management to occur. You may do this when say you update only one column
|
||||
* and you know that it is not important for cached objects or text indexes.
|
||||
* </p>
|
||||
*/
|
||||
public DefaultOrmUpdate<T> setNotifyCache(boolean notifyCache) {
|
||||
this.notifyCache = notifyCache;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the cache should be notified so that invalidates
|
||||
* appropriate objects.
|
||||
*/
|
||||
public boolean isNotifyCache() {
|
||||
return notifyCache;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getUpdateStatement() {
|
||||
return updateStatement;
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate<T> set(int position, Object value) {
|
||||
bindParams.setParameter(position, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate<T> setParameter(int position, Object value) {
|
||||
bindParams.setParameter(position, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate<T> setNull(int position, int jdbcType) {
|
||||
bindParams.setNullParameter(position, jdbcType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate<T> setNullParameter(int position, int jdbcType) {
|
||||
bindParams.setNullParameter(position, jdbcType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate<T> set(String name, Object value) {
|
||||
bindParams.setParameter(name, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate<T> setParameter(String name, Object param) {
|
||||
bindParams.setParameter(name, param);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate<T> setNull(String name, int jdbcType) {
|
||||
bindParams.setNullParameter(name, jdbcType);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultOrmUpdate<T> setNullParameter(String name, int jdbcType) {
|
||||
bindParams.setNullParameter(name, jdbcType);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the bind parameters.
|
||||
*/
|
||||
public BindParams getBindParams() {
|
||||
return bindParams;
|
||||
}
|
||||
|
||||
public String getGeneratedSql() {
|
||||
return generatedSql;
|
||||
}
|
||||
|
||||
public void setGeneratedSql(String generatedSql) {
|
||||
this.generatedSql = generatedSql;
|
||||
}
|
||||
|
||||
public String getBaseTable() {
|
||||
return baseTable;
|
||||
}
|
||||
|
||||
public OrmUpdateType getOrmUpdateType() {
|
||||
return type;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import java.sql.PreparedStatement;
|
||||
import java.sql.SQLException;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import com.avaje.ebean.EbeanServer;
|
||||
import com.avaje.ebean.SqlFutureList;
|
||||
import com.avaje.ebean.SqlQueryListener;
|
||||
import com.avaje.ebean.SqlRow;
|
||||
import com.avaje.ebeaninternal.api.BindParams;
|
||||
import com.avaje.ebeaninternal.api.SpiSqlQuery;
|
||||
|
||||
/**
|
||||
* Default implementation of SQuery - SQL Query.
|
||||
*/
|
||||
public class DefaultRelationalQuery implements SpiSqlQuery {
|
||||
|
||||
private static final long serialVersionUID = -1098305779779591068L;
|
||||
|
||||
private transient EbeanServer server;
|
||||
|
||||
private transient SqlQueryListener queryListener;
|
||||
|
||||
private String query;
|
||||
|
||||
private int firstRow;
|
||||
|
||||
private int maxRows;
|
||||
|
||||
private int timeout;
|
||||
|
||||
private boolean futureFetch;
|
||||
|
||||
private boolean cancelled;
|
||||
|
||||
/**
|
||||
* For the purposes of cancelling the query.
|
||||
*/
|
||||
private transient PreparedStatement pstmt;
|
||||
|
||||
/**
|
||||
* The rows after which the fetch continues in a bg thread.
|
||||
*/
|
||||
private int backgroundFetchAfter;
|
||||
|
||||
private int bufferFetchSizeHint;
|
||||
|
||||
/**
|
||||
* The property used to get the key value for a Map.
|
||||
*/
|
||||
private String mapKey;
|
||||
|
||||
/**
|
||||
* Bind parameters when using the query language.
|
||||
*/
|
||||
private BindParams bindParams = new BindParams();
|
||||
|
||||
/**
|
||||
* Additional supply a query detail object.
|
||||
*/
|
||||
public DefaultRelationalQuery(EbeanServer server, String query) {
|
||||
this.server = server;
|
||||
this.query = query;
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setQuery(String query) {
|
||||
this.query = query;
|
||||
return this;
|
||||
}
|
||||
|
||||
public List<SqlRow> findList() {
|
||||
return server.findList(this, null);
|
||||
}
|
||||
|
||||
public Set<SqlRow> findSet() {
|
||||
return server.findSet(this, null);
|
||||
}
|
||||
|
||||
public Map<?,SqlRow> findMap() {
|
||||
return server.findMap(this, null);
|
||||
}
|
||||
|
||||
public SqlRow findUnique() {
|
||||
return server.findUnique(this, null);
|
||||
}
|
||||
|
||||
public SqlFutureList findFutureList() {
|
||||
return server.findFutureList(this, null);
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setParameter(int position, Object value) {
|
||||
bindParams.setParameter(position, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setParameter(String paramName, Object value) {
|
||||
bindParams.setParameter(paramName, value);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the findListener is one has been set.
|
||||
*/
|
||||
public SqlQueryListener getListener() {
|
||||
return queryListener;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a listener. This is designed for large fetches
|
||||
* where lots are rows are to be processed and instead of
|
||||
* returning all the rows they are processed one at a time.
|
||||
* <p>
|
||||
* Note that the returning List Set or Map will be empty.
|
||||
* </p>
|
||||
*/
|
||||
public DefaultRelationalQuery setListener(SqlQueryListener queryListener) {
|
||||
this.queryListener = queryListener;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
return "SqlQuery ["+query+"]";
|
||||
}
|
||||
|
||||
public int getFirstRow() {
|
||||
return firstRow;
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setFirstRow(int firstRow) {
|
||||
this.firstRow = firstRow;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getMaxRows() {
|
||||
return maxRows;
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setMaxRows(int maxRows) {
|
||||
this.maxRows = maxRows;
|
||||
return this;
|
||||
}
|
||||
|
||||
public String getMapKey() {
|
||||
return mapKey;
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setMapKey(String mapKey) {
|
||||
this.mapKey = mapKey;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getBackgroundFetchAfter() {
|
||||
return backgroundFetchAfter;
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setBackgroundFetchAfter(int backgroundFetchAfter) {
|
||||
this.backgroundFetchAfter = backgroundFetchAfter;
|
||||
return this;
|
||||
}
|
||||
|
||||
public int getTimeout() {
|
||||
return timeout;
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setTimeout(int secs) {
|
||||
this.timeout = secs;
|
||||
return this;
|
||||
}
|
||||
|
||||
public BindParams getBindParams() {
|
||||
return bindParams;
|
||||
}
|
||||
|
||||
public DefaultRelationalQuery setBufferFetchSizeHint(int bufferFetchSizeHint){
|
||||
this.bufferFetchSizeHint = bufferFetchSizeHint;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
public int getBufferFetchSizeHint() {
|
||||
return bufferFetchSizeHint;
|
||||
}
|
||||
|
||||
public String getQuery() {
|
||||
return query;
|
||||
}
|
||||
|
||||
public boolean isFutureFetch() {
|
||||
return futureFetch;
|
||||
}
|
||||
|
||||
public void setFutureFetch(boolean futureFetch) {
|
||||
this.futureFetch = futureFetch;
|
||||
}
|
||||
|
||||
public void setPreparedStatement(PreparedStatement pstmt){
|
||||
synchronized (this) {
|
||||
this.pstmt = pstmt;
|
||||
}
|
||||
}
|
||||
|
||||
public void cancel() {
|
||||
synchronized (this) {
|
||||
this.cancelled = true;
|
||||
if (pstmt != null){
|
||||
try {
|
||||
pstmt.cancel();
|
||||
} catch (SQLException e) {
|
||||
String msg = "Error cancelling query";
|
||||
throw new PersistenceException(msg, e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean isCancelled() {
|
||||
synchronized (this) {
|
||||
return cancelled;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
public class NaturalKeyBindParam {
|
||||
|
||||
private final String name;
|
||||
|
||||
private final Object value;
|
||||
|
||||
public NaturalKeyBindParam(String name, Object value) {
|
||||
this.name = name;
|
||||
this.value = value;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public Object getValue() {
|
||||
return value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,484 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.ebean.event.BeanQueryRequest;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanDescriptor;
|
||||
import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssoc;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyDeploy;
|
||||
import com.avaje.ebeaninternal.server.el.ElPropertyValue;
|
||||
import com.avaje.ebeaninternal.server.query.SplitName;
|
||||
|
||||
/**
|
||||
* Represents the internal structure of an Object Relational query.
|
||||
* <p>
|
||||
* Holds the select() and join() details of a ORM query.
|
||||
* </p>
|
||||
* <p>
|
||||
* It is worth noting that for autoFetch a "tuned fetch info" builds an instance
|
||||
* of OrmQueryDetail. Tuning a query is a matter of replacing an instance of
|
||||
* this class with one that has been tuned with select() and join() set.
|
||||
* </p>
|
||||
*/
|
||||
public class OrmQueryDetail implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -2510486880141461807L;
|
||||
|
||||
/**
|
||||
* Root level properties.
|
||||
*/
|
||||
private OrmQueryProperties baseProps = new OrmQueryProperties();
|
||||
|
||||
/**
|
||||
* Contains the fetch/lazy/query joins and their properties.
|
||||
*/
|
||||
private LinkedHashMap<String, OrmQueryProperties> fetchPaths = new LinkedHashMap<String, OrmQueryProperties>(8);
|
||||
|
||||
private LinkedHashSet<String> includes = new LinkedHashSet<String>(8);
|
||||
|
||||
/**
|
||||
* Return a deep copy of the OrmQueryDetail.
|
||||
*/
|
||||
public OrmQueryDetail copy() {
|
||||
OrmQueryDetail copy = new OrmQueryDetail();
|
||||
copy.baseProps = baseProps.copy();
|
||||
for (Map.Entry<String, OrmQueryProperties> entry : fetchPaths.entrySet()) {
|
||||
copy.fetchPaths.put(entry.getKey(), entry.getValue().copy());
|
||||
}
|
||||
copy.includes = new LinkedHashSet<String>(includes);
|
||||
return copy;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the hash for the query plan.
|
||||
*/
|
||||
public int queryPlanHash(BeanQueryRequest<?> request) {
|
||||
|
||||
int hc = (baseProps == null ? 1 : baseProps.queryPlanHash(request));
|
||||
|
||||
if (fetchPaths != null) {
|
||||
for (OrmQueryProperties p : fetchPaths.values()) {
|
||||
hc = hc * 31 + p.queryPlanHash(request);
|
||||
}
|
||||
}
|
||||
|
||||
return hc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if equal in terms of autofetch (select and joins).
|
||||
*/
|
||||
public boolean isAutoFetchEqual(OrmQueryDetail otherDetail) {
|
||||
return autofetchPlanHash() == otherDetail.autofetchPlanHash();
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the hash for the query plan.
|
||||
*/
|
||||
private int autofetchPlanHash() {
|
||||
|
||||
int hc = (baseProps == null ? 1 : baseProps.autofetchPlanHash());
|
||||
|
||||
if (fetchPaths != null) {
|
||||
for (OrmQueryProperties p : fetchPaths.values()) {
|
||||
hc = hc * 31 + p.autofetchPlanHash();
|
||||
}
|
||||
}
|
||||
|
||||
return hc;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
if (baseProps != null) {
|
||||
sb.append("select ").append(baseProps);
|
||||
}
|
||||
if (fetchPaths != null) {
|
||||
for (OrmQueryProperties join : fetchPaths.values()) {
|
||||
sb.append(" fetch ").append(join);
|
||||
}
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public int hashCode() {
|
||||
throw new RuntimeException("should not use");
|
||||
}
|
||||
|
||||
/**
|
||||
* set the properties to include on the base / root entity.
|
||||
*/
|
||||
public void select(String columns) {
|
||||
baseProps = new OrmQueryProperties(null, columns);
|
||||
}
|
||||
|
||||
public boolean containsProperty(String property) {
|
||||
if (baseProps == null) {
|
||||
return true;
|
||||
} else {
|
||||
return baseProps.isIncluded(property);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the base / root query properties.
|
||||
*/
|
||||
public void setBase(OrmQueryProperties baseProps) {
|
||||
this.baseProps = baseProps;
|
||||
}
|
||||
|
||||
public List<OrmQueryProperties> removeSecondaryQueries() {
|
||||
return removeSecondaryQueries(false);
|
||||
}
|
||||
|
||||
public List<OrmQueryProperties> removeSecondaryLazyQueries() {
|
||||
return removeSecondaryQueries(true);
|
||||
}
|
||||
|
||||
private List<OrmQueryProperties> removeSecondaryQueries(boolean lazyQuery) {
|
||||
|
||||
ArrayList<String> matchingPaths = new ArrayList<String>(2);
|
||||
|
||||
for (OrmQueryProperties chunk : fetchPaths.values()) {
|
||||
boolean match = lazyQuery ? chunk.isLazyFetch() : chunk.isQueryFetch();
|
||||
if (match) {
|
||||
matchingPaths.add(chunk.getPath());
|
||||
}
|
||||
}
|
||||
|
||||
if (matchingPaths.size() == 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
// sort into depth order to remove
|
||||
Collections.sort(matchingPaths);
|
||||
|
||||
// the list of secondary queries
|
||||
ArrayList<OrmQueryProperties> props = new ArrayList<OrmQueryProperties>(2);
|
||||
|
||||
for (int i = 0; i < matchingPaths.size(); i++) {
|
||||
String path = matchingPaths.get(i);
|
||||
includes.remove(path);
|
||||
OrmQueryProperties secQuery = fetchPaths.remove(path);
|
||||
if (secQuery == null) {
|
||||
// the path has already been removed by another
|
||||
// secondary query
|
||||
|
||||
} else {
|
||||
props.add(secQuery);
|
||||
|
||||
// remove any child properties for this path
|
||||
Iterator<OrmQueryProperties> pass2It = fetchPaths.values().iterator();
|
||||
while (pass2It.hasNext()) {
|
||||
OrmQueryProperties pass2Prop = pass2It.next();
|
||||
if (secQuery.isChild(pass2Prop)) {
|
||||
// remove join to secondary query from the main query
|
||||
// and add to this secondary query
|
||||
pass2It.remove();
|
||||
includes.remove(pass2Prop.getPath());
|
||||
secQuery.add(pass2Prop);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Add the secondary queries as select properties
|
||||
// to the parent chunk to ensure the foreign keys
|
||||
// are included in the query
|
||||
for (int i = 0; i < props.size(); i++) {
|
||||
String path = props.get(i).getPath();
|
||||
// split into parent and property
|
||||
String[] split = SplitName.split(path);
|
||||
// add property to parent chunk
|
||||
OrmQueryProperties chunk = getChunk(split[0], true);
|
||||
chunk.addSecondaryQueryJoin(split[1]);
|
||||
}
|
||||
|
||||
return props;
|
||||
}
|
||||
|
||||
public boolean tuneFetchProperties(OrmQueryDetail tunedDetail) {
|
||||
|
||||
boolean tuned = false;
|
||||
|
||||
OrmQueryProperties tunedRoot = tunedDetail.getChunk(null, false);
|
||||
if (tunedRoot != null && tunedRoot.hasProperties()) {
|
||||
tuned = true;
|
||||
baseProps.setTunedProperties(tunedRoot);
|
||||
|
||||
for (OrmQueryProperties tunedChunk : tunedDetail.fetchPaths.values()) {
|
||||
OrmQueryProperties chunk = getChunk(tunedChunk.getPath(), false);
|
||||
if (chunk != null) {
|
||||
// set the properties to select
|
||||
chunk.setTunedProperties(tunedChunk);
|
||||
} else {
|
||||
// add a missing join
|
||||
putFetchPath(tunedChunk.copy());
|
||||
}
|
||||
}
|
||||
}
|
||||
return tuned;
|
||||
}
|
||||
|
||||
/**
|
||||
* Matches a join() method of the query.
|
||||
*/
|
||||
public void putFetchPath(OrmQueryProperties chunk) {
|
||||
String path = chunk.getPath();
|
||||
fetchPaths.put(path, chunk);
|
||||
includes.add(path);
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove all joins and properties.
|
||||
* <p>
|
||||
* Typically for the row count query.
|
||||
* </p>
|
||||
*/
|
||||
public void clear() {
|
||||
includes.clear();
|
||||
fetchPaths.clear();
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the fetch properties and configuration for a given path.
|
||||
*
|
||||
* @param path
|
||||
* the property to join
|
||||
* @param partialProps
|
||||
* the properties on the join property to include
|
||||
*/
|
||||
public OrmQueryProperties addFetch(String path, String partialProps, FetchConfig fetchConfig) {
|
||||
|
||||
OrmQueryProperties chunk = getChunk(path, true);
|
||||
chunk.setProperties(partialProps);
|
||||
chunk.setFetchConfig(fetchConfig);
|
||||
return chunk;
|
||||
}
|
||||
|
||||
public void sortFetchPaths(BeanDescriptor<?> d) {
|
||||
|
||||
LinkedHashMap<String, OrmQueryProperties> sorted = new LinkedHashMap<String, OrmQueryProperties>(fetchPaths.size());
|
||||
|
||||
for (OrmQueryProperties p : fetchPaths.values()) {
|
||||
sortFetchPaths(d, p, sorted);
|
||||
}
|
||||
|
||||
fetchPaths = sorted;
|
||||
}
|
||||
|
||||
private void sortFetchPaths(BeanDescriptor<?> d, OrmQueryProperties p,
|
||||
LinkedHashMap<String, OrmQueryProperties> sorted) {
|
||||
|
||||
String path = p.getPath();
|
||||
if (!sorted.containsKey(path)) {
|
||||
String parentPath = p.getParentPath();
|
||||
if (parentPath == null || sorted.containsKey(parentPath)) {
|
||||
// off root path or parent already ahead in fetch order
|
||||
sorted.put(path, p);
|
||||
} else {
|
||||
OrmQueryProperties parentProp = fetchPaths.get(parentPath);
|
||||
if (parentProp == null) {
|
||||
ElPropertyValue el = d.getElGetValue(parentPath);
|
||||
if (el == null) {
|
||||
String msg = "Path [" + parentPath + "] not valid from " + d.getFullName();
|
||||
throw new PersistenceException(msg);
|
||||
}
|
||||
// add a missing parent path just fetching the Id property
|
||||
BeanPropertyAssoc<?> assocOne = (BeanPropertyAssoc<?>) el.getBeanProperty();
|
||||
parentProp = new OrmQueryProperties(parentPath, assocOne.getTargetIdProperty());
|
||||
}
|
||||
if (parentProp != null) {
|
||||
sortFetchPaths(d, parentProp, sorted);
|
||||
}
|
||||
sorted.put(path, p);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert 'fetch joins' to 'many' properties over to 'query joins'.
|
||||
*/
|
||||
public void convertManyFetchJoinsToQueryJoins(BeanDescriptor<?> beanDescriptor, String lazyLoadManyPath,
|
||||
boolean allowOne, int queryBatch) {
|
||||
|
||||
ArrayList<OrmQueryProperties> manyChunks = new ArrayList<OrmQueryProperties>(3);
|
||||
|
||||
// the name of the many fetch property if there is one
|
||||
String manyFetchProperty = null;
|
||||
|
||||
// flag that is set once the many fetch property is chosen
|
||||
boolean fetchJoinFirstMany = allowOne;
|
||||
|
||||
sortFetchPaths(beanDescriptor);
|
||||
|
||||
for (String fetchPath : fetchPaths.keySet()) {
|
||||
ElPropertyDeploy elProp = beanDescriptor.getElPropertyDeploy(fetchPath);
|
||||
if (elProp.containsManySince(manyFetchProperty)) {
|
||||
|
||||
// this is a join to a *ToMany
|
||||
OrmQueryProperties chunk = fetchPaths.get(fetchPath);
|
||||
if (chunk.isFetchJoin()
|
||||
&& !isLazyLoadManyRoot(lazyLoadManyPath, chunk)
|
||||
&& !hasParentSecJoin(lazyLoadManyPath, chunk)) {
|
||||
// this is a 'fetch join' (included in main query)
|
||||
if (fetchJoinFirstMany) {
|
||||
// letting the first one remain a 'fetch join'
|
||||
fetchJoinFirstMany = false;
|
||||
manyFetchProperty = fetchPath;
|
||||
} else {
|
||||
// convert this one over to a 'query join'
|
||||
manyChunks.add(chunk);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < manyChunks.size(); i++) {
|
||||
// convert 'fetch joins' over to 'query joins'
|
||||
manyChunks.get(i).setQueryFetch(queryBatch, true);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this is actually the root level of a +query/+lazy loading
|
||||
* query.
|
||||
*/
|
||||
private boolean isLazyLoadManyRoot(String lazyLoadManyPath, OrmQueryProperties chunk) {
|
||||
if (lazyLoadManyPath != null && lazyLoadManyPath.equals(chunk.getPath())) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* If the chunk has a parent that is a query or lazy join. In this case it
|
||||
* does not need to be converted.
|
||||
*/
|
||||
private boolean hasParentSecJoin(String lazyLoadManyPath, OrmQueryProperties chunk) {
|
||||
OrmQueryProperties parent = getParent(chunk);
|
||||
if (parent == null) {
|
||||
return false;
|
||||
} else {
|
||||
if (lazyLoadManyPath != null && lazyLoadManyPath.equals(parent.getPath())) {
|
||||
return false;
|
||||
} else if (!parent.isFetchJoin()) {
|
||||
return true;
|
||||
} else {
|
||||
return hasParentSecJoin(lazyLoadManyPath, parent);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the parent chunk.
|
||||
*/
|
||||
private OrmQueryProperties getParent(OrmQueryProperties chunk) {
|
||||
String parentPath = chunk.getParentPath();
|
||||
return parentPath == null ? null : fetchPaths.get(parentPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set any default select clauses for the main bean and any joins that have
|
||||
* not explicitly defined a select clause.
|
||||
* <p>
|
||||
* That is this will use FetchType.LAZY to exclude some properties by
|
||||
* default.
|
||||
* </p>
|
||||
*/
|
||||
public void setDefaultSelectClause(BeanDescriptor<?> desc) {
|
||||
|
||||
if (desc.hasDefaultSelectClause() && !hasSelectClause()) {
|
||||
if (baseProps == null) {
|
||||
baseProps = new OrmQueryProperties();
|
||||
}
|
||||
baseProps.setDefaultProperties(desc.getDefaultSelectClause(), desc.getDefaultSelectClauseSet());
|
||||
}
|
||||
|
||||
for (OrmQueryProperties joinProps : fetchPaths.values()) {
|
||||
if (!joinProps.hasSelectClause()) {
|
||||
BeanDescriptor<?> assocDesc = desc.getBeanDescriptor(joinProps.getPath());
|
||||
if (assocDesc.hasDefaultSelectClause()) {
|
||||
// use the default select clause
|
||||
joinProps.setDefaultProperties(assocDesc.getDefaultSelectClause(), assocDesc.getDefaultSelectClauseSet());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public boolean hasSelectClause() {
|
||||
return (baseProps != null && baseProps.hasSelectClause());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the query detail has neither select properties specified
|
||||
* or any joins defined.
|
||||
*/
|
||||
public boolean isEmpty() {
|
||||
return fetchPaths.isEmpty() && (baseProps == null || !baseProps.hasProperties());
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if there are no joins.
|
||||
*/
|
||||
public boolean isJoinsEmpty() {
|
||||
return fetchPaths.isEmpty();
|
||||
}
|
||||
|
||||
/**
|
||||
* Add the explicit bean join.
|
||||
* <p>
|
||||
* This is also used to Exclude the matching property from the parent select
|
||||
* (aka remove the foreign key) because it is now included in it's on node
|
||||
* in the SqlTree.
|
||||
* </p>
|
||||
*/
|
||||
public void includeBeanJoin(String parentPath, String propertyName) {
|
||||
OrmQueryProperties parentChunk = getChunk(parentPath, true);
|
||||
parentChunk.includeBeanJoin(propertyName);
|
||||
}
|
||||
|
||||
public OrmQueryProperties getChunk(String path, boolean create) {
|
||||
if (path == null) {
|
||||
return baseProps;
|
||||
}
|
||||
OrmQueryProperties props = fetchPaths.get(path);
|
||||
if (create && props == null) {
|
||||
props = new OrmQueryProperties(path);
|
||||
putFetchPath(props);
|
||||
return props;
|
||||
|
||||
} else {
|
||||
return props;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if the property is included.
|
||||
*/
|
||||
public boolean includes(String path) {
|
||||
|
||||
OrmQueryProperties chunk = fetchPaths.get(path);
|
||||
|
||||
// may not have fetch properties if just +cache etc
|
||||
return chunk != null && !chunk.isCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the property includes for this detail.
|
||||
*/
|
||||
public HashSet<String> getIncludes() {
|
||||
return includes;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import javax.persistence.PersistenceException;
|
||||
|
||||
/**
|
||||
* Parses a Object relational query statement into a OrmQueryDetail and
|
||||
* OrmQueryAttributes.
|
||||
* <p>
|
||||
* The reason they are split into detail and attributes is that the autoFetch
|
||||
* feature is used to replace the OrmQueryDetail leaving the attributes
|
||||
* unchanged.
|
||||
* </p>
|
||||
*/
|
||||
public class OrmQueryDetailParser {
|
||||
|
||||
private final OrmQueryDetail detail = new OrmQueryDetail();
|
||||
|
||||
private int maxRows;
|
||||
|
||||
private int firstRow;
|
||||
|
||||
private String rawWhereClause;
|
||||
|
||||
private String rawOrderBy;
|
||||
|
||||
private final SimpleTextParser parser;
|
||||
|
||||
public OrmQueryDetailParser(String oql) {
|
||||
this.parser = new SimpleTextParser(oql);
|
||||
}
|
||||
|
||||
public void parse() throws PersistenceException {
|
||||
|
||||
parser.nextWord();
|
||||
processInitial();
|
||||
}
|
||||
|
||||
protected void assign(DefaultOrmQuery<?> query) {
|
||||
query.setOrmQueryDetail(detail);
|
||||
query.setFirstRow(firstRow);
|
||||
query.setMaxRows(maxRows);
|
||||
query.setRawWhereClause(rawWhereClause);
|
||||
query.order(rawOrderBy);
|
||||
}
|
||||
|
||||
private void processInitial() {
|
||||
if (parser.isMatch("find")) {
|
||||
OrmQueryProperties props = readFindFetch();
|
||||
detail.setBase(props);
|
||||
} else {
|
||||
process();
|
||||
}
|
||||
while (!parser.isFinished()) {
|
||||
process();
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isFetch() {
|
||||
return parser.isMatch("fetch") || parser.isMatch("join");
|
||||
}
|
||||
|
||||
private void process() {
|
||||
if (isFetch()) {
|
||||
OrmQueryProperties props = readFindFetch();
|
||||
detail.putFetchPath(props);
|
||||
|
||||
} else if (parser.isMatch("where")) {
|
||||
readWhere();
|
||||
|
||||
} else if (parser.isMatch("order", "by")) {
|
||||
readOrderBy();
|
||||
|
||||
} else if (parser.isMatch("limit")) {
|
||||
readLimit();
|
||||
|
||||
} else {
|
||||
throw new PersistenceException("Query expected 'fetch', 'where','order by' or 'limit' keyword but got ["
|
||||
+ parser.getWord() + "] \r " + parser.getOql());
|
||||
}
|
||||
}
|
||||
|
||||
private void readLimit() {
|
||||
try {
|
||||
String maxLimit = parser.nextWord();
|
||||
maxRows = Integer.parseInt(maxLimit);
|
||||
|
||||
String offsetKeyword = parser.nextWord();
|
||||
if (offsetKeyword != null) {
|
||||
if (!parser.isMatch("offset")) {
|
||||
throw new PersistenceException("expected offset keyword but got " + parser.getWord());
|
||||
}
|
||||
String firstRowLimit = parser.nextWord();
|
||||
firstRow = Integer.parseInt(firstRowLimit);
|
||||
parser.nextWord();
|
||||
}
|
||||
} catch (NumberFormatException e) {
|
||||
String msg = "Expected an integer for maxRows or firstRows in limit offset clause";
|
||||
throw new PersistenceException(msg, e);
|
||||
}
|
||||
}
|
||||
|
||||
private void readOrderBy() {
|
||||
// read the by
|
||||
parser.nextWord();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while (parser.nextWord() != null) {
|
||||
if (parser.isMatch("limit")) {
|
||||
break;
|
||||
} else {
|
||||
String w = parser.getWord();
|
||||
if (!w.startsWith("(")) {
|
||||
sb.append(" ");
|
||||
}
|
||||
sb.append(w);
|
||||
}
|
||||
}
|
||||
rawOrderBy = sb.toString().trim();
|
||||
|
||||
if (!parser.isFinished()) {
|
||||
readLimit();
|
||||
}
|
||||
}
|
||||
|
||||
private void readWhere() {
|
||||
|
||||
int nextMode = 0;
|
||||
StringBuilder sb = new StringBuilder();
|
||||
while ((parser.nextWord()) != null) {
|
||||
if (parser.isMatch("order", "by")) {
|
||||
nextMode = 1;
|
||||
break;
|
||||
|
||||
} else if (parser.isMatch("limit")) {
|
||||
nextMode = 2;
|
||||
break;
|
||||
|
||||
} else {
|
||||
sb.append(" ").append(parser.getWord());
|
||||
}
|
||||
}
|
||||
String whereClause = sb.toString().trim();
|
||||
if (whereClause.length() > 0) {
|
||||
rawWhereClause = whereClause;
|
||||
}
|
||||
|
||||
if (nextMode == 1) {
|
||||
readOrderBy();
|
||||
} else if (nextMode == 2) {
|
||||
readLimit();
|
||||
}
|
||||
}
|
||||
|
||||
private OrmQueryProperties readFindFetch() {
|
||||
|
||||
boolean readAlias = false;
|
||||
|
||||
String props = null;
|
||||
String path = parser.nextWord();
|
||||
String token = null;
|
||||
while ((token = parser.nextWord()) != null) {
|
||||
if (!readAlias && parser.isMatch("as")) {
|
||||
// next token is alias
|
||||
parser.nextWord();
|
||||
readAlias = true;
|
||||
|
||||
} else if ('(' == token.charAt(0)) {
|
||||
props = token;
|
||||
parser.nextWord();
|
||||
break;
|
||||
|
||||
} else if (isFindFetchEnd()) {
|
||||
break;
|
||||
|
||||
} else if (!readAlias) {
|
||||
readAlias = true;
|
||||
|
||||
} else {
|
||||
throw new PersistenceException("Expected (props) or new 'fetch' 'where' but got " + token);
|
||||
}
|
||||
}
|
||||
if (props != null) {
|
||||
props = props.substring(1, props.length() - 1);
|
||||
}
|
||||
return new OrmQueryProperties(path, props);
|
||||
}
|
||||
|
||||
private boolean isFindFetchEnd() {
|
||||
if (isFetch()) {
|
||||
return true;
|
||||
}
|
||||
if (parser.isMatch("where")) {
|
||||
return true;
|
||||
}
|
||||
if (parser.isMatch("order", "by")) {
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import com.avaje.ebean.config.dbplatform.DatabasePlatform;
|
||||
import com.avaje.ebean.config.dbplatform.SqlLimitRequest;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
|
||||
public class OrmQueryLimitRequest implements SqlLimitRequest {
|
||||
|
||||
final SpiQuery<?> ormQuery;
|
||||
final DatabasePlatform dbPlatform;
|
||||
|
||||
final String sql;
|
||||
|
||||
final String sqlOrderBy;
|
||||
|
||||
public OrmQueryLimitRequest(String sql, String sqlOrderBy, SpiQuery<?> ormQuery, DatabasePlatform dbPlatform) {
|
||||
this.sql = sql;
|
||||
this.sqlOrderBy = sqlOrderBy;
|
||||
this.ormQuery = ormQuery;
|
||||
this.dbPlatform = dbPlatform;
|
||||
}
|
||||
|
||||
public String getDbOrderBy() {
|
||||
return sqlOrderBy;
|
||||
}
|
||||
|
||||
public String getDbSql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
public int getFirstRow() {
|
||||
return ormQuery.getFirstRow();
|
||||
}
|
||||
|
||||
public int getMaxRows() {
|
||||
return ormQuery.getMaxRows();
|
||||
}
|
||||
|
||||
public boolean isDistinct() {
|
||||
return ormQuery.isDistinct();
|
||||
}
|
||||
|
||||
public SpiQuery<?> getOrmQuery() {
|
||||
return ormQuery;
|
||||
}
|
||||
|
||||
public DatabasePlatform getDbPlatform() {
|
||||
return dbPlatform;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,642 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import com.avaje.ebean.ExpressionFactory;
|
||||
import com.avaje.ebean.FetchConfig;
|
||||
import com.avaje.ebean.OrderBy;
|
||||
import com.avaje.ebean.OrderBy.Property;
|
||||
import com.avaje.ebean.Query;
|
||||
import com.avaje.ebean.event.BeanQueryRequest;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionFactory;
|
||||
import com.avaje.ebeaninternal.api.SpiExpressionList;
|
||||
import com.avaje.ebeaninternal.api.SpiQuery;
|
||||
import com.avaje.ebeaninternal.server.expression.FilterExprPath;
|
||||
import com.avaje.ebeaninternal.server.lib.util.StringHelper;
|
||||
import com.avaje.ebeaninternal.server.query.SplitName;
|
||||
import com.avaje.ebeaninternal.util.FilterExpressionList;
|
||||
|
||||
/**
|
||||
* Represents the Properties of an Object Relational query.
|
||||
*/
|
||||
public class OrmQueryProperties implements Serializable {
|
||||
|
||||
private static final long serialVersionUID = -8785582703966455658L;
|
||||
|
||||
private String parentPath;
|
||||
private String path;
|
||||
|
||||
private String properties;
|
||||
|
||||
private String trimmedProperties;
|
||||
|
||||
/**
|
||||
* NB: -1 means no +query, 0 means use the default batch size.
|
||||
*/
|
||||
private int queryFetchBatch = -1;
|
||||
private boolean queryFetchAll;
|
||||
|
||||
/**
|
||||
* NB: -1 means no +lazy, 0 means use the default batch size.
|
||||
*/
|
||||
private int lazyFetchBatch = -1;
|
||||
|
||||
private FetchConfig fetchConfig;
|
||||
|
||||
private boolean cache;
|
||||
|
||||
private boolean readOnly;
|
||||
|
||||
/**
|
||||
* Note this SHOULD be a LinkedHashSet to preserve order of the properties.
|
||||
* This is to make using SqlSelect easier with predictable property/column
|
||||
* ordering.
|
||||
*/
|
||||
private Set<String> included;
|
||||
|
||||
/**
|
||||
* Included bean joins.
|
||||
*/
|
||||
private Set<String> includedBeanJoin;
|
||||
|
||||
/**
|
||||
* Add these properties to the select so that the foreign key columns are
|
||||
* included in the query.
|
||||
*/
|
||||
private Set<String> secondaryQueryJoins;
|
||||
|
||||
private List<OrmQueryProperties> secondaryChildren;
|
||||
|
||||
/**
|
||||
* OrderBy properties that where on the main query
|
||||
* but moved here as they relate to this (query join).
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
private OrderBy orderBy;
|
||||
|
||||
/**
|
||||
* A filter that can be applied to the fetch of this path in the object graph.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
private SpiExpressionList filterMany;
|
||||
|
||||
/**
|
||||
* Construct with a given path (null == root path).
|
||||
*/
|
||||
public OrmQueryProperties(String path) {
|
||||
this.path = path;
|
||||
this.parentPath = SplitName.parent(path);
|
||||
}
|
||||
|
||||
public OrmQueryProperties() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used by query language parser.
|
||||
*/
|
||||
public OrmQueryProperties(String path, String properties) {
|
||||
this(path);
|
||||
setProperties(properties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Move a OrderBy.Property from the main query to this query join.
|
||||
*/
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void addSecJoinOrderProperty(OrderBy.Property orderProp) {
|
||||
if (orderBy == null){
|
||||
orderBy = new OrderBy();
|
||||
}
|
||||
orderBy.add(orderProp);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the Fetch configuration options for this path.
|
||||
*/
|
||||
public void setFetchConfig(FetchConfig fetchConfig) {
|
||||
if (fetchConfig != null) {
|
||||
this.fetchConfig = fetchConfig;
|
||||
lazyFetchBatch = fetchConfig.getLazyBatchSize();
|
||||
queryFetchBatch = fetchConfig.getQueryBatchSize();
|
||||
queryFetchAll = fetchConfig.isQueryAll();
|
||||
}
|
||||
}
|
||||
|
||||
public FetchConfig getFetchConfig() {
|
||||
return fetchConfig;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the comma delimited properties to fetch for this path.
|
||||
* <p>
|
||||
* This can include the +query and +lazy type hints.
|
||||
* </p>
|
||||
*/
|
||||
public void setProperties(String properties) {
|
||||
this.properties = properties;
|
||||
this.trimmedProperties = properties;
|
||||
parseProperties();
|
||||
|
||||
if (!isAllProperties()) {
|
||||
Set<String> parsed = parseIncluded(trimmedProperties);
|
||||
if (parsed.contains("*")){
|
||||
this.included = null;
|
||||
} else {
|
||||
this.included = parsed;
|
||||
}
|
||||
} else {
|
||||
this.included = null;
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isAllProperties() {
|
||||
return (trimmedProperties == null) || (trimmedProperties.length() == 0) || "*".equals(trimmedProperties);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the expressions used to filter on this path.
|
||||
* This should be a many path to use this method.
|
||||
*/
|
||||
@SuppressWarnings({ "rawtypes", "unchecked" })
|
||||
public <T> SpiExpressionList<T> filterMany(Query<T> rootQuery) {
|
||||
if (filterMany == null){
|
||||
FilterExprPath exprPath = new FilterExprPath(path);
|
||||
SpiExpressionFactory queryEf = (SpiExpressionFactory)rootQuery.getExpressionFactory();
|
||||
ExpressionFactory filterEf = queryEf.createExpressionFactory(exprPath);
|
||||
filterMany = new FilterExpressionList(exprPath, filterEf, rootQuery);
|
||||
// by default we need to make this a 'query join' now
|
||||
queryFetchAll = true;
|
||||
queryFetchBatch = 100;
|
||||
lazyFetchBatch = 100;
|
||||
}
|
||||
return filterMany;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the filterMany expression list (can be null).
|
||||
*/
|
||||
public SpiExpressionList<?> getFilterManyTrimPath(int trimPath) {
|
||||
if (filterMany == null){
|
||||
return null;
|
||||
}
|
||||
filterMany.trimPath(trimPath);
|
||||
return filterMany;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return the filterMany expression list (can be null).
|
||||
*/
|
||||
public SpiExpressionList<?> getFilterMany() {
|
||||
return filterMany;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the filterMany expression list.
|
||||
*/
|
||||
public void setFilterMany(SpiExpressionList<?> filterMany) {
|
||||
this.filterMany = filterMany;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the properties from deployment default FetchTypes.
|
||||
*/
|
||||
public void setDefaultProperties(String properties, Set<String> included) {
|
||||
this.properties = properties;
|
||||
this.trimmedProperties = properties;
|
||||
this.included = included;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the properties from a matching autofetch tuned properties.
|
||||
*/
|
||||
public void setTunedProperties(OrmQueryProperties tunedProperties) {
|
||||
this.properties = tunedProperties.properties;
|
||||
this.trimmedProperties = tunedProperties.trimmedProperties;
|
||||
this.included = tunedProperties.included;
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the select and joins for this query.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void configureBeanQuery(SpiQuery<?> query) {
|
||||
|
||||
if (trimmedProperties != null && trimmedProperties.length() > 0) {
|
||||
query.select(trimmedProperties);
|
||||
if (filterMany != null){
|
||||
query.setFilterMany(path, filterMany);
|
||||
}
|
||||
}
|
||||
|
||||
if (secondaryChildren != null) {
|
||||
int trimPath = path.length() + 1;
|
||||
for (int i = 0; i < secondaryChildren.size(); i++) {
|
||||
OrmQueryProperties p = secondaryChildren.get(i);
|
||||
String path = p.getPath();
|
||||
path = path.substring(trimPath);
|
||||
query.fetch(path, p.getProperties(), p.getFetchConfig());
|
||||
query.setFilterMany(path, p.getFilterManyTrimPath(trimPath));
|
||||
}
|
||||
}
|
||||
|
||||
if (orderBy != null){
|
||||
List<Property> orderByProps = orderBy.getProperties();
|
||||
for (int i = 0; i < orderByProps.size(); i++) {
|
||||
orderByProps.get(i).trim(path);
|
||||
}
|
||||
query.setOrder(orderBy);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Define the select and joins for this query.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public void configureManyQuery(SpiQuery<?> query) {
|
||||
|
||||
if (trimmedProperties != null && trimmedProperties.length() > 0) {
|
||||
query.fetch(query.getLazyLoadManyPath(), trimmedProperties);
|
||||
}
|
||||
if (filterMany != null){
|
||||
query.setFilterMany(path, filterMany);
|
||||
}
|
||||
if (secondaryChildren != null) {
|
||||
|
||||
int trimlen = path.length() - query.getLazyLoadManyPath().length();
|
||||
|
||||
for (int i = 0; i < secondaryChildren.size(); i++) {
|
||||
OrmQueryProperties p = secondaryChildren.get(i);
|
||||
String path = p.getPath();
|
||||
path = path.substring(trimlen);
|
||||
query.fetch(path, p.getProperties(), p.getFetchConfig());
|
||||
query.setFilterMany(path, p.getFilterManyTrimPath(trimlen));
|
||||
}
|
||||
}
|
||||
|
||||
if (orderBy != null){
|
||||
query.setOrder(orderBy);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a copy of the OrmQueryProperties.
|
||||
*/
|
||||
public OrmQueryProperties copy() {
|
||||
OrmQueryProperties copy = new OrmQueryProperties();
|
||||
copy.parentPath = parentPath;
|
||||
copy.path = path;
|
||||
copy.properties = properties;
|
||||
copy.cache = cache;
|
||||
copy.readOnly = readOnly;
|
||||
copy.queryFetchAll = queryFetchAll;
|
||||
copy.queryFetchBatch = queryFetchBatch;
|
||||
copy.lazyFetchBatch = lazyFetchBatch;
|
||||
copy.filterMany = filterMany;
|
||||
if (included != null) {
|
||||
copy.included = new HashSet<String>(included);
|
||||
}
|
||||
if (includedBeanJoin != null) {
|
||||
copy.includedBeanJoin = new HashSet<String>(includedBeanJoin);
|
||||
}
|
||||
return copy;
|
||||
}
|
||||
|
||||
public boolean hasSelectClause() {
|
||||
if ("*".equals(trimmedProperties)) {
|
||||
// explicitly selected all properties
|
||||
return true;
|
||||
}
|
||||
// explicitly selected some properties
|
||||
return included != null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
String s = "";
|
||||
if (path != null) {
|
||||
s += path + " ";
|
||||
}
|
||||
if (properties != null) {
|
||||
s += "(" + properties + ") ";
|
||||
} else if (included != null) {
|
||||
s += "(" + included + ") ";
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public boolean isChild(OrmQueryProperties possibleChild) {
|
||||
return possibleChild.getPath().startsWith(path + ".");
|
||||
}
|
||||
|
||||
/**
|
||||
* For secondary queries add a child element.
|
||||
*/
|
||||
public void add(OrmQueryProperties child) {
|
||||
if (secondaryChildren == null) {
|
||||
secondaryChildren = new ArrayList<OrmQueryProperties>();
|
||||
}
|
||||
secondaryChildren.add(child);
|
||||
}
|
||||
|
||||
public int autofetchPlanHash() {
|
||||
|
||||
int hc = (path != null ? path.hashCode() : 1);
|
||||
if (properties != null){
|
||||
hc = hc * 31 + properties.hashCode();
|
||||
} else {
|
||||
hc = hc * 31 + (included != null ? included.hashCode() : 1);
|
||||
}
|
||||
|
||||
return hc;
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the query plan hash.
|
||||
*/
|
||||
@SuppressWarnings("unchecked")
|
||||
public int queryPlanHash(BeanQueryRequest<?> request) {
|
||||
|
||||
int hc = (path != null ? path.hashCode() : 1);
|
||||
if (properties != null){
|
||||
hc = hc * 31 + properties.hashCode();
|
||||
} else {
|
||||
hc = hc * 31 + (included != null ? included.hashCode() : 1);
|
||||
}
|
||||
hc = hc * 31 + (filterMany != null ? filterMany.queryPlanHash(request) : 1);
|
||||
|
||||
return hc;
|
||||
}
|
||||
|
||||
public String getProperties() {
|
||||
return properties;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this has properties.
|
||||
*/
|
||||
public boolean hasProperties() {
|
||||
return properties != null || included != null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Return true if this property is included as a bean join.
|
||||
* <p>
|
||||
* If a property is included as a bean join then it should not be included
|
||||
* as a reference/proxy to avoid duplication.
|
||||
* </p>
|
||||
*/
|
||||
public boolean isIncludedBeanJoin(String propertyName) {
|
||||
if (includedBeanJoin == null) {
|
||||
return false;
|
||||
} else {
|
||||
return includedBeanJoin.contains(propertyName);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add a bean join property.
|
||||
*/
|
||||
public void includeBeanJoin(String propertyName) {
|
||||
if (includedBeanJoin == null) {
|
||||
includedBeanJoin = new HashSet<String>();
|
||||
}
|
||||
includedBeanJoin.add(propertyName);
|
||||
}
|
||||
|
||||
public boolean allProperties() {
|
||||
return included == null;
|
||||
}
|
||||
|
||||
/**
|
||||
* This excludes the bean joined properties.
|
||||
* <p>
|
||||
* This is because bean joins will have there own node in the SqlTree.
|
||||
* </p>
|
||||
*/
|
||||
public Iterator<String> getSelectProperties() {
|
||||
|
||||
if (secondaryQueryJoins == null) {
|
||||
return included.iterator();
|
||||
}
|
||||
|
||||
LinkedHashSet<String> temp = new LinkedHashSet<String>(secondaryQueryJoins.size() + included.size());
|
||||
temp.addAll(included);
|
||||
temp.addAll(secondaryQueryJoins);
|
||||
return temp.iterator();
|
||||
}
|
||||
|
||||
public void addSecondaryQueryJoin(String property) {
|
||||
if (secondaryQueryJoins == null) {
|
||||
secondaryQueryJoins = new HashSet<String>(4);
|
||||
}
|
||||
secondaryQueryJoins.add(property);
|
||||
}
|
||||
|
||||
/**
|
||||
* Return all the properties including the bean joins. This is the set that
|
||||
* will be used by EntityBeanIntercept to determine if a property needs to
|
||||
* be lazy loaded.
|
||||
*/
|
||||
public Set<String> getAllIncludedProperties() {
|
||||
|
||||
if (included == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
if (includedBeanJoin == null && secondaryQueryJoins == null) {
|
||||
return new LinkedHashSet<String>(included);
|
||||
}
|
||||
|
||||
LinkedHashSet<String> s = new LinkedHashSet<String>(2 * (included.size() + 5));
|
||||
if (included != null) {
|
||||
s.addAll(included);
|
||||
}
|
||||
if (includedBeanJoin != null) {
|
||||
s.addAll(includedBeanJoin);
|
||||
}
|
||||
if (secondaryQueryJoins != null) {
|
||||
s.addAll(secondaryQueryJoins);
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public boolean isIncluded(String propName) {
|
||||
|
||||
if (includedBeanJoin != null && includedBeanJoin.contains(propName)) {
|
||||
return false;
|
||||
}
|
||||
if (included == null) {
|
||||
// all properties included
|
||||
return true;
|
||||
}
|
||||
return included.contains(propName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to convert this join to a query join.
|
||||
*
|
||||
* @param queryJoinBatch
|
||||
* where -1 means not a query join and 0 means use the default
|
||||
* batch size.
|
||||
*/
|
||||
public OrmQueryProperties setQueryFetchBatch(int queryFetchBatch) {
|
||||
this.queryFetchBatch = queryFetchBatch;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OrmQueryProperties setQueryFetchAll(boolean queryFetchAll) {
|
||||
this.queryFetchAll = queryFetchAll;
|
||||
return this;
|
||||
}
|
||||
|
||||
public OrmQueryProperties setQueryFetch(int batch, boolean queryFetchAll) {
|
||||
this.queryFetchBatch = batch;
|
||||
this.queryFetchAll = queryFetchAll;
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the lazy loading batch size.
|
||||
*/
|
||||
public OrmQueryProperties setLazyFetchBatch(int lazyFetchBatch) {
|
||||
this.lazyFetchBatch = lazyFetchBatch;
|
||||
return this;
|
||||
}
|
||||
|
||||
public boolean isFetchJoin() {
|
||||
return !isQueryFetch() && !isLazyFetch();
|
||||
}
|
||||
|
||||
public boolean isQueryFetch() {
|
||||
return queryFetchBatch > -1;
|
||||
}
|
||||
|
||||
public int getQueryFetchBatch() {
|
||||
return queryFetchBatch;
|
||||
}
|
||||
|
||||
public boolean isQueryFetchAll() {
|
||||
return queryFetchAll;
|
||||
}
|
||||
|
||||
public boolean isLazyFetch() {
|
||||
return lazyFetchBatch > -1;
|
||||
}
|
||||
|
||||
public int getLazyFetchBatch() {
|
||||
return lazyFetchBatch;
|
||||
}
|
||||
|
||||
public boolean isReadOnly() {
|
||||
return readOnly;
|
||||
}
|
||||
|
||||
public boolean isCache() {
|
||||
return cache;
|
||||
}
|
||||
|
||||
public String getParentPath() {
|
||||
return parentPath;
|
||||
}
|
||||
|
||||
public String getPath() {
|
||||
return path;
|
||||
}
|
||||
|
||||
private void parseProperties() {
|
||||
if (trimmedProperties == null) {
|
||||
return;
|
||||
}
|
||||
int pos = trimmedProperties.indexOf("+readonly");
|
||||
if (pos > -1) {
|
||||
trimmedProperties = StringHelper.replaceString(trimmedProperties, "+readonly", "");
|
||||
this.readOnly = true;
|
||||
}
|
||||
pos = trimmedProperties.indexOf("+cache");
|
||||
if (pos > -1) {
|
||||
trimmedProperties = StringHelper.replaceString(trimmedProperties, "+cache", "");
|
||||
this.cache = true;
|
||||
}
|
||||
pos = trimmedProperties.indexOf("+query");
|
||||
if (pos > -1) {
|
||||
queryFetchBatch = parseBatchHint(pos, "+query");
|
||||
}
|
||||
pos = trimmedProperties.indexOf("+lazy");
|
||||
if (pos > -1) {
|
||||
lazyFetchBatch = parseBatchHint(pos, "+lazy");
|
||||
}
|
||||
|
||||
trimmedProperties = trimmedProperties.trim();
|
||||
while (trimmedProperties.startsWith(",")) {
|
||||
trimmedProperties = trimmedProperties.substring(1).trim();
|
||||
}
|
||||
}
|
||||
|
||||
private int parseBatchHint(int pos, String option) {
|
||||
|
||||
int startPos = pos + option.length();
|
||||
|
||||
int endPos = findEndPos(startPos, trimmedProperties);
|
||||
if (endPos == -1) {
|
||||
trimmedProperties = StringHelper.replaceString(trimmedProperties, option, "");
|
||||
return 0;
|
||||
|
||||
} else {
|
||||
|
||||
String batchParam = trimmedProperties.substring(startPos + 1, endPos);
|
||||
|
||||
if (endPos + 1 >= trimmedProperties.length()) {
|
||||
trimmedProperties = trimmedProperties.substring(0, pos);
|
||||
} else {
|
||||
trimmedProperties = trimmedProperties.substring(0, pos) + trimmedProperties.substring(endPos + 1);
|
||||
}
|
||||
return Integer.parseInt(batchParam);
|
||||
}
|
||||
}
|
||||
|
||||
private int findEndPos(int pos, String props) {
|
||||
|
||||
if (pos < props.length()) {
|
||||
if (props.charAt(pos) == '(') {
|
||||
int endPara = props.indexOf(')', pos + 1);
|
||||
if (endPara == -1) {
|
||||
String m = "Error could not find ')' in " + props + " after position " + pos;
|
||||
throw new RuntimeException(m);
|
||||
}
|
||||
return endPara;
|
||||
}
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the include separating by comma or semicolon.
|
||||
*/
|
||||
private static Set<String> parseIncluded(String rawList) {
|
||||
|
||||
String[] res = rawList.split(",");
|
||||
|
||||
LinkedHashSet<String> set = new LinkedHashSet<String>(res.length + 3);
|
||||
|
||||
String temp = null;
|
||||
for (int i = 0; i < res.length; i++) {
|
||||
temp = res[i].trim();
|
||||
if (temp.length() > 0) {
|
||||
set.add(temp);
|
||||
}
|
||||
}
|
||||
|
||||
if (set.isEmpty()){
|
||||
return null;
|
||||
}
|
||||
|
||||
return Collections.unmodifiableSet(set);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,177 @@
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
|
||||
public class SimpleTextParser {
|
||||
|
||||
private final String oql;
|
||||
private final char[] chars;
|
||||
private final int eof;
|
||||
|
||||
private int pos;
|
||||
private String word;
|
||||
private String lowerWord;
|
||||
|
||||
private int openParenthesisCount;
|
||||
|
||||
public SimpleTextParser(String oql) {
|
||||
this.oql = oql;
|
||||
this.chars = oql.toCharArray();
|
||||
this.eof = oql.length();
|
||||
}
|
||||
|
||||
public int getPos() {
|
||||
return pos;
|
||||
}
|
||||
|
||||
public String getOql() {
|
||||
return oql;
|
||||
}
|
||||
|
||||
public String getWord() {
|
||||
return word;
|
||||
}
|
||||
|
||||
public String peekNextWord() {
|
||||
int origPos = pos;
|
||||
String nw = nextWordInternal();
|
||||
pos = origPos;
|
||||
return nw;
|
||||
}
|
||||
|
||||
/**
|
||||
* Match the current and the next word.
|
||||
*/
|
||||
public boolean isMatch(String lowerMatch, String nextWordMatch) {
|
||||
|
||||
if (isMatch(lowerMatch)) {
|
||||
String nw = peekNextWord();
|
||||
if (nw != null) {
|
||||
nw = nw.toLowerCase();
|
||||
return nw.equals(nextWordMatch);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
public boolean isFinished() {
|
||||
return word == null;
|
||||
}
|
||||
|
||||
public int findWordLower(String lowerMatch, int afterPos) {
|
||||
this.pos = afterPos;
|
||||
return findWordLower(lowerMatch);
|
||||
}
|
||||
|
||||
public int findWordLower(String lowerMatch) {
|
||||
do {
|
||||
if (nextWord() == null) {
|
||||
return -1;
|
||||
}
|
||||
if (lowerMatch.equals(lowerWord)) {
|
||||
return pos - lowerWord.length();
|
||||
}
|
||||
} while (true);
|
||||
}
|
||||
|
||||
/**
|
||||
* Match the current word.
|
||||
*/
|
||||
public boolean isMatch(String lowerMatch) {
|
||||
return lowerMatch.equals(lowerWord);
|
||||
}
|
||||
|
||||
public String nextWord() {
|
||||
word = nextWordInternal();
|
||||
if (word != null) {
|
||||
lowerWord = word.toLowerCase();
|
||||
}
|
||||
return word;
|
||||
}
|
||||
|
||||
private String nextWordInternal() {
|
||||
trimLeadingWhitespace();
|
||||
if (pos >= eof) {
|
||||
return null;
|
||||
}
|
||||
int start = pos;
|
||||
if (chars[pos] == '(') {
|
||||
moveToClose();
|
||||
} else {
|
||||
moveToEndOfWord();
|
||||
}
|
||||
return oql.substring(start, pos);
|
||||
}
|
||||
|
||||
private void moveToClose() {
|
||||
|
||||
pos++;
|
||||
openParenthesisCount = 0;
|
||||
|
||||
for (; pos < eof; pos++) {
|
||||
char c = chars[pos];
|
||||
if (c == '(') {
|
||||
// count nested parenthesis
|
||||
openParenthesisCount++;
|
||||
|
||||
} else if (c == ')') {
|
||||
if (openParenthesisCount > 0) {
|
||||
// still in nested parenthesis
|
||||
--openParenthesisCount;
|
||||
} else {
|
||||
// we have found the end
|
||||
pos++;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void moveToEndOfWord() {
|
||||
char c = chars[pos];
|
||||
boolean isOperator = isOperator(c);
|
||||
for (; pos < eof; pos++) {
|
||||
c = chars[pos];
|
||||
if (isWordTerminator(c, isOperator)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private boolean isWordTerminator(char c, boolean isOperator) {
|
||||
if (Character.isWhitespace(c)) {
|
||||
return true;
|
||||
}
|
||||
if (isOperator(c)) {
|
||||
return !isOperator;
|
||||
}
|
||||
if (c == '('){
|
||||
return true;
|
||||
}
|
||||
|
||||
return isOperator;
|
||||
}
|
||||
|
||||
private boolean isOperator(char c) {
|
||||
switch (c) {
|
||||
case '<':
|
||||
return true;
|
||||
case '>':
|
||||
return true;
|
||||
case '=':
|
||||
return true;
|
||||
case '!':
|
||||
return true;
|
||||
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
private void trimLeadingWhitespace() {
|
||||
for (; pos < eof; pos++) {
|
||||
char c = chars[pos];
|
||||
if (!Character.isWhitespace(c)) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
/**
|
||||
* Objects to define the ORM query itself.
|
||||
*/
|
||||
package com.avaje.ebeaninternal.server.querydefn;
|
||||
Reference in new issue
Block a user