From c6632bbc257cfa231802d015172186e7531ba0ae Mon Sep 17 00:00:00 2001 From: rbygrave Date: Wed, 13 May 2015 22:51:28 +1200 Subject: [PATCH] No effective change - code tidy, remove unused --- .../server/deploy/DbReadContext.java | 102 ++-- .../ebeaninternal/server/query/CQuery.java | 33 +- .../server/query/CQueryEngine.java | 13 +- .../server/query/CQueryFetchIds.java | 443 +++++++++--------- .../server/query/CQueryIteratorSimple.java | 3 +- .../query/CQueryIteratorWithBuffer.java | 2 +- .../server/query/SqlTreeNode.java | 1 - .../server/query/SqlTreeNodeBean.java | 7 +- .../server/query/SqlTreeNodeExtraJoin.java | 156 +++--- .../query/SqlTreeNodeManyWhereJoin.java | 146 +++--- .../server/query/SqlTreeNodeRoot.java | 34 +- .../server/query/SqlTreeProperties.java | 109 ++--- 12 files changed, 502 insertions(+), 547 deletions(-) diff --git a/src/main/java/com/avaje/ebeaninternal/server/deploy/DbReadContext.java b/src/main/java/com/avaje/ebeaninternal/server/deploy/DbReadContext.java index 4893272cb..68f7b8404 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/deploy/DbReadContext.java +++ b/src/main/java/com/avaje/ebeaninternal/server/deploy/DbReadContext.java @@ -13,70 +13,70 @@ import com.avaje.ebeaninternal.server.type.DataReader; * Context provided when a BeanProperty reads from a ResultSet. */ public interface DbReadContext { - + /** * Return the state of the object graph. */ - public Boolean isReadOnly(); - + Boolean isReadOnly(); + /** * Propagate the state to the bean. */ - public void propagateState(Object e); - + void propagateState(Object e); + /** * Return the DataReader. */ - public DataReader getDataReader(); - - /** - * Return true if the query is using supplied SQL rather than generated SQL. - */ - public boolean isRawSql(); - - /** - * Set the JoinNode - used by proxy/reference beans for profiling. - */ - public void setCurrentPrefix(String currentPrefix, Map pathMap); + DataReader getDataReader(); - /** - * Return true if we are profiling this query. - */ - public boolean isAutoFetchProfiling(); - - /** - * Add autoFetch profiling for a loaded entity bean. - */ - public void profileBean(EntityBeanIntercept ebi, String prefix); - - /** - * Return the persistence context. - */ - public PersistenceContext getPersistenceContext(); + /** + * Return true if the query is using supplied SQL rather than generated SQL. + */ + boolean isRawSql(); - /** - * Register a reference for lazy loading. - */ - public void register(String path, EntityBeanIntercept ebi); + /** + * Set the JoinNode - used by proxy/reference beans for profiling. + */ + void setCurrentPrefix(String currentPrefix, Map pathMap); - /** - * Register a collection for lazy loading. - */ - public void register(String path, BeanCollection bc); + /** + * Return true if we are profiling this query. + */ + boolean isAutoFetchProfiling(); - /** - * Return the property that is associated with the many. There can only be - * one. This can be null. - */ - public BeanPropertyAssocMany getManyProperty(); + /** + * Add autoFetch profiling for a loaded entity bean. + */ + void profileBean(EntityBeanIntercept ebi, String prefix); - /** - * Set back the bean that has just been loaded with its id. - */ - public void setLazyLoadedChildBean(EntityBean loadedBean, Object parentId); + /** + * Return the persistence context. + */ + PersistenceContext getPersistenceContext(); - /** - * Return the query mode. - */ - public SpiQuery.Mode getQueryMode(); + /** + * Register a reference for lazy loading. + */ + void register(String path, EntityBeanIntercept ebi); + + /** + * Register a collection for lazy loading. + */ + void register(String path, BeanCollection bc); + + /** + * Return the property that is associated with the many. There can only be + * one. This can be null. + */ + BeanPropertyAssocMany getManyProperty(); + + /** + * Set back the bean that has just been loaded with its id. + */ + void setLazyLoadedChildBean(EntityBean loadedBean, Object parentId); + + /** + * Return the query mode. + */ + SpiQuery.Mode getQueryMode(); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/CQuery.java b/src/main/java/com/avaje/ebeaninternal/server/query/CQuery.java index 656e37ab7..de1c4f235 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/CQuery.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/CQuery.java @@ -44,7 +44,7 @@ public class CQuery implements DbReadContext, CancelableQuery { private static final Logger logger = LoggerFactory.getLogger(CQuery.class); - private static final int GLOBAL_ROW_LIMIT = Integer.valueOf(System.getProperty("ebean.query.globallimit","1000000")); + private static final int GLOBAL_ROW_LIMIT = Integer.valueOf(System.getProperty("ebean.query.globallimit", "1000000")); /** * The resultSet rows read. @@ -159,7 +159,7 @@ public class CQuery implements DbReadContext, CancelableQuery { private final ObjectGraphNode objectGraphNode; private final AutoFetchManager autoFetchManager; - + private final WeakReference autoFetchManagerRef; private final Boolean readOnly; @@ -288,7 +288,7 @@ public class CQuery implements DbReadContext, CancelableQuery { // prepare SpiTransaction t = request.getTransaction(); Connection conn = t.getInternalConnection(); - + if (query.isRawSql()) { ResultSet suppliedResultSet = query.getRawSql().getResultSet(); if (suppliedResultSet != null) { @@ -298,13 +298,13 @@ public class CQuery implements DbReadContext, CancelableQuery { return true; } } - + if (forwardOnlyHint) { // Use forward only hints for large resultset processing (Issue 56, MySql specific) pstmt = conn.prepareStatement(sql, ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY); pstmt.setFetchSize(Integer.MIN_VALUE); } else { - pstmt = conn.prepareStatement(sql); + pstmt = conn.prepareStatement(sql); } if (query.getTimeout() > 0) { @@ -364,16 +364,16 @@ public class CQuery implements DbReadContext, CancelableQuery { public void setLazyLoadedChildBean(EntityBean bean, Object lazyLoadParentId) { - if (lazyLoadParentId != null) { - if (!lazyLoadParentId.equals(this.lazyLoadParentId)) { - // get the appropriate parent bean from the persistence context - this.lazyLoadParentBean = (EntityBean)getPersistenceContext().get(lazyLoadManyProperty.getBeanDescriptor().getBeanType(), lazyLoadParentId); - this.lazyLoadParentId = lazyLoadParentId; - } - - // add the loadedBean to the appropriate collection of lazyLoadParentBean - lazyLoadManyProperty.addBeanToCollectionWithCreate(lazyLoadParentBean, bean); + if (lazyLoadParentId != null) { + if (!lazyLoadParentId.equals(this.lazyLoadParentId)) { + // get the appropriate parent bean from the persistence context + this.lazyLoadParentBean = (EntityBean) getPersistenceContext().get(lazyLoadManyProperty.getBeanDescriptor().getBeanType(), lazyLoadParentId); + this.lazyLoadParentId = lazyLoadParentId; } + + // add the loadedBean to the appropriate collection of lazyLoadParentBean + lazyLoadManyProperty.addBeanToCollectionWithCreate(lazyLoadParentBean, bean); + } } /** @@ -613,12 +613,11 @@ public class CQuery implements DbReadContext, CancelableQuery { * Create a PersistenceException including interesting information like the * bindLog and sql used. */ - public static PersistenceException createPersistenceException(SQLException e, SpiTransaction t, - String bindLog, String sql) { + public static PersistenceException createPersistenceException(SQLException e, SpiTransaction t, String bindLog, String sql) { if (t.isLogSummary()) { // log the error to the transaction log - String errMsg = StringHelper.replaceStringMulti(e.getMessage(), new String[] { "\r", "\n" }, "\\n "); + String errMsg = StringHelper.replaceStringMulti(e.getMessage(), new String[]{"\r", "\n"}, "\\n "); String msg = "ERROR executing query: bindLog[" + bindLog + "] error[" + errMsg + "]"; t.logSummary(msg); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryEngine.java b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryEngine.java index a7262eeb8..1c671e317 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryEngine.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryEngine.java @@ -48,7 +48,6 @@ public class CQueryEngine { CQueryFetchIds rcQuery = queryBuilder.buildFetchIdsQuery(request); try { - BeanIdList list = rcQuery.findIds(); if (request.isLogSql()) { @@ -83,13 +82,13 @@ public class CQueryEngine { CQueryRowCount rcQuery = queryBuilder.buildRowCountQuery(request); try { - + int rowCount = rcQuery.findRowCount(); if (request.isLogSql()) { String logSql = rcQuery.getGeneratedSql(); if (TransactionManager.SQL_LOGGER.isTraceEnabled()) { - logSql= Str.add(logSql, "; --bind(", rcQuery.getBindLog(), ")"); + logSql = Str.add(logSql, "; --bind(", rcQuery.getBindLog(), ")"); } request.logSql(logSql); } @@ -168,7 +167,7 @@ public class CQueryEngine { logger.trace("Future fetch already cancelled"); return null; } - + if (request.isLogSql()) { logSql(cquery); } @@ -192,7 +191,7 @@ public class CQueryEngine { } catch (SQLException e) { throw cquery.createPersistenceException(e); - + } finally { if (cquery != null) { cquery.close(); @@ -233,7 +232,7 @@ public class CQueryEngine { request.executeSecondaryQueries(); - return (T)bean; + return (T) bean; } catch (SQLException e) { throw cquery.createPersistenceException(e); @@ -250,7 +249,7 @@ public class CQueryEngine { String sql = query.getGeneratedSql(); if (TransactionManager.SQL_LOGGER.isTraceEnabled()) { - sql= Str.add(sql, "; --bind(", query.getBindLog(), ")"); + sql = Str.add(sql, "; --bind(", query.getBindLog(), ")"); } query.getTransaction().logSql(sql); } diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryFetchIds.java b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryFetchIds.java index f6ed21636..8f2016869 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryFetchIds.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryFetchIds.java @@ -1,17 +1,5 @@ package com.avaje.ebeaninternal.server.query; -import java.sql.Connection; -import java.sql.PreparedStatement; -import java.sql.ResultSet; -import java.sql.SQLException; -import java.util.ArrayList; -import java.util.Collections; -import java.util.List; -import java.util.Map; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - import com.avaje.ebean.bean.BeanCollection; import com.avaje.ebean.bean.EntityBean; import com.avaje.ebean.bean.EntityBeanIntercept; @@ -21,259 +9,262 @@ import com.avaje.ebeaninternal.api.SpiQuery; import com.avaje.ebeaninternal.api.SpiQuery.Mode; import com.avaje.ebeaninternal.api.SpiTransaction; import com.avaje.ebeaninternal.server.core.OrmQueryRequest; -import com.avaje.ebeaninternal.server.core.SpiOrmQueryRequest; import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany; import com.avaje.ebeaninternal.server.deploy.DbReadContext; import com.avaje.ebeaninternal.server.type.DataBind; import com.avaje.ebeaninternal.server.type.DataReader; import com.avaje.ebeaninternal.server.type.RsetDataReader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.sql.Connection; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collections; +import java.util.List; +import java.util.Map; /** * Executes the select row count query. */ public class CQueryFetchIds { - private static final Logger logger = LoggerFactory.getLogger(CQueryFetchIds.class); + private static final Logger logger = LoggerFactory.getLogger(CQueryFetchIds.class); - /** - * The overall find request wrapper object. - */ - private final OrmQueryRequest request; + /** + * The overall find request wrapper object. + */ + private final OrmQueryRequest request; - private final BeanDescriptor desc; + private final BeanDescriptor desc; - private final SpiQuery query; + private final SpiQuery query; - /** - * Where clause predicates. - */ - private final CQueryPredicates predicates; + /** + * Where clause predicates. + */ + private final CQueryPredicates predicates; - /** - * The final sql that is generated. - */ - private final String sql; + /** + * The final sql that is generated. + */ + private final String sql; - private RsetDataReader dataReader; - - /** - * The statement used to create the resultSet. - */ - private PreparedStatement pstmt; + private RsetDataReader dataReader; - private String bindLog; + /** + * The statement used to create the resultSet. + */ + private PreparedStatement pstmt; - private long startNano; - - private int executionTimeMicros; + private String bindLog; - private int rowCount; - - private final int maxRows; - - /** - * Create the Sql select based on the request. - */ - public CQueryFetchIds(OrmQueryRequest request, CQueryPredicates predicates, String sql) { + private int executionTimeMicros; - this.request = request; - this.query = request.getQuery(); - this.sql = sql; - this.maxRows = query.getMaxRows(); + private int rowCount; - query.setGeneratedSql(sql); + private final int maxRows; - this.desc = request.getBeanDescriptor(); - this.predicates = predicates; + /** + * Create the Sql select based on the request. + */ + public CQueryFetchIds(OrmQueryRequest request, CQueryPredicates predicates, String sql) { - } - - /** - * Return a summary description of this query. - */ - public String getSummary() { - StringBuilder sb = new StringBuilder(); - sb.append("FindIds exeMicros[").append(executionTimeMicros) - .append("] rows[").append(rowCount) - .append("] type[").append(desc.getName()) - .append("] predicates[").append(predicates.getLogWhereSql()) - .append("] bind[").append(bindLog).append("]"); - - return sb.toString(); - } + this.request = request; + this.query = request.getQuery(); + this.sql = sql; + this.maxRows = query.getMaxRows(); - /** - * Return the bind log. - */ - public String getBindLog() { - return bindLog; + query.setGeneratedSql(sql); + + this.desc = request.getBeanDescriptor(); + this.predicates = predicates; + } + + /** + * Return a summary description of this query. + */ + public String getSummary() { + StringBuilder sb = new StringBuilder(80); + sb.append("FindIds exeMicros[").append(executionTimeMicros) + .append("] rows[").append(rowCount) + .append("] type[").append(desc.getName()) + .append("] predicates[").append(predicates.getLogWhereSql()) + .append("] bind[").append(bindLog).append("]"); + + return sb.toString(); + } + + /** + * Return the bind log. + */ + public String getBindLog() { + return bindLog; + } + + /** + * Return the generated sql. + */ + public String getGeneratedSql() { + return sql; + } + + /** + * Execute the query returning the row count. + */ + public BeanIdList findIds() throws SQLException { + + long startNano = System.nanoTime(); + + try { + // get the list that we are going to put the id's into. + // This was already set so that it is available to be + // read by other threads (it is a synchronised list) + List idList = query.getIdList(); + if (idList == null) { + // running in foreground thread (not FutureIds query) + idList = Collections.synchronizedList(new ArrayList()); + query.setIdList(idList); + } + + BeanIdList result = new BeanIdList(idList); + + SpiTransaction t = request.getTransaction(); + Connection conn = t.getInternalConnection(); + pstmt = conn.prepareStatement(sql); + + if (query.getBufferFetchSizeHint() > 0) { + pstmt.setFetchSize(query.getBufferFetchSizeHint()); + } + + if (query.getTimeout() > 0) { + pstmt.setQueryTimeout(query.getTimeout()); + } + + bindLog = predicates.bind(new DataBind(pstmt)); + + ResultSet rset = pstmt.executeQuery(); + dataReader = new RsetDataReader(rset); + + boolean hitMaxRows = false; + boolean hasMoreRows = false; + rowCount = 0; + + DbReadContext ctx = new DbContext(); + + while (rset.next()) { + Object idValue = desc.getIdBinder().read(ctx); + idList.add(idValue); + // reset back to 0 + dataReader.resetColumnPosition(); + rowCount++; + + if (maxRows > 0 && rowCount == maxRows) { + hitMaxRows = true; + hasMoreRows = rset.next(); + break; + + } + } + + if (hitMaxRows) { + result.setHasMore(hasMoreRows); + } + + long exeNano = System.nanoTime() - startNano; + executionTimeMicros = (int) exeNano / 1000; + + return result; + + } finally { + close(); + } + } + + /** + * Close the resources. + *

+ * The jdbc resultSet and statement need to be closed. Its important that + * this method is called. + *

+ */ + private void close() { + try { + if (dataReader != null) { + dataReader.close(); + dataReader = null; + } + } catch (SQLException e) { + logger.error(null, e); + } + try { + if (pstmt != null) { + pstmt.close(); + pstmt = null; + } + } catch (SQLException e) { + logger.error(null, e); + } + } + + + class DbContext implements DbReadContext { + + public void propagateState(Object e) { + throw new RuntimeException("Not Called"); } - /** - * Return the generated sql. - */ - public String getGeneratedSql() { - return sql; - } - - public SpiOrmQueryRequest getQueryRequest() { - return request; - } + public Mode getQueryMode() { + return Mode.NORMAL; + } - /** - * Execute the query returning the row count. - */ - public BeanIdList findIds() throws SQLException { + public DataReader getDataReader() { + return dataReader; + } - startNano = System.nanoTime(); - - try { - // get the list that we are going to put the id's into. - // This was already set so that it is available to be - // read by other threads (it is a synchronised list) - List idList = query.getIdList(); - if (idList == null){ - // running in foreground thread (not FutureIds query) - idList = Collections.synchronizedList(new ArrayList()); - query.setIdList(idList); - } - - BeanIdList result = new BeanIdList(idList); - - SpiTransaction t = request.getTransaction(); - Connection conn = t.getInternalConnection(); - pstmt = conn.prepareStatement(sql); - - if (query.getBufferFetchSizeHint() > 0){ - pstmt.setFetchSize(query.getBufferFetchSizeHint()); - } - - if (query.getTimeout() > 0){ - pstmt.setQueryTimeout(query.getTimeout()); - } - - bindLog = predicates.bind(new DataBind(pstmt)); - - ResultSet rset = pstmt.executeQuery(); - dataReader = new RsetDataReader(rset); - - boolean hitMaxRows = false; - boolean hasMoreRows = false; - rowCount = 0; - - DbReadContext ctx = new DbContext(); - - while (rset.next()){ - Object idValue = desc.getIdBinder().read(ctx); - idList.add(idValue); - // reset back to 0 - dataReader.resetColumnPosition(); - rowCount++; - - if (maxRows > 0 && rowCount == maxRows) { - hitMaxRows = true; - hasMoreRows = rset.next(); - break; + public Boolean isReadOnly() { + return Boolean.FALSE; + } - } - } - - if (hitMaxRows){ - result.setHasMore(hasMoreRows); - } - - long exeNano = System.nanoTime() - startNano; - executionTimeMicros = (int)exeNano/1000; + public boolean isRawSql() { + return false; + } - return result; - - } finally { - close(); - } - } + public void register(String path, EntityBeanIntercept ebi) { + } - /** - * Close the resources. - *

- * The jdbc resultSet and statement need to be closed. Its important that - * this method is called. - *

- */ - private void close() { - try { - if (dataReader != null) { - dataReader.close(); - dataReader = null; - } - } catch (SQLException e) { - logger.error(null, e); - } - try { - if (pstmt != null) { - pstmt.close(); - pstmt = null; - } - } catch (SQLException e) { - logger.error(null, e); - } - } + public void register(String path, BeanCollection bc) { + } - - class DbContext implements DbReadContext { + public BeanPropertyAssocMany getManyProperty() { + // always null + return null; + } - public void propagateState(Object e) { - throw new RuntimeException("Not Called"); - } + public PersistenceContext getPersistenceContext() { + // always null + return null; + } - public Mode getQueryMode() { - return Mode.NORMAL; - } - - public DataReader getDataReader() { - return dataReader; - } + public boolean isAutoFetchProfiling() { + return false; + } - public Boolean isReadOnly() { - return Boolean.FALSE; - } - - public boolean isRawSql() { - return false; - } + public void profileBean(EntityBeanIntercept ebi, String prefix) { + // no-op + } - public void register(String path, EntityBeanIntercept ebi){ - } + public void setCurrentPrefix(String currentPrefix, Map pathMap) { + // no-op + } - public void register(String path, BeanCollection bc){ - } + public void setLazyLoadedChildBean(EntityBean loadedBean, Object lazyLoadParentId) { + // no-op + } - public BeanPropertyAssocMany getManyProperty() { - // always null - return null; - } + } - public PersistenceContext getPersistenceContext() { - // always null - return null; - } - - public boolean isAutoFetchProfiling() { - return false; - } - - public void profileBean(EntityBeanIntercept ebi, String prefix) { - // no-op - } - - public void setCurrentPrefix(String currentPrefix,Map pathMap) { - // no-op - } - - public void setLazyLoadedChildBean(EntityBean loadedBean, Object lazyLoadParentId) { - // no-op - } - - } - } diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorSimple.java b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorSimple.java index bf4b877f8..88dba323a 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorSimple.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorSimple.java @@ -13,6 +13,7 @@ import com.avaje.ebeaninternal.server.core.OrmQueryRequest; class CQueryIteratorSimple implements QueryIterator { private final CQuery cquery; + private final OrmQueryRequest request; CQueryIteratorSimple(CQuery cquery, OrmQueryRequest request) { @@ -31,7 +32,7 @@ class CQueryIteratorSimple implements QueryIterator { @SuppressWarnings("unchecked") public T next() { - return (T)cquery.next(); + return (T) cquery.next(); } public void close() { diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorWithBuffer.java b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorWithBuffer.java index 673c0e2e0..ff1907b53 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorWithBuffer.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/CQueryIteratorWithBuffer.java @@ -37,7 +37,7 @@ class CQueryIteratorWithBuffer implements QueryIterator { int i = -1; while (moreToLoad && ++i < bufferSize) { if (cquery.hasNext()) { - buffer.add((T)cquery.next()); + buffer.add((T) cquery.next()); } else { moreToLoad = false; } diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNode.java b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNode.java index 55587b738..ae6f15b6f 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNode.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNode.java @@ -35,7 +35,6 @@ public interface SqlTreeNode { * At a high level this actually controls the reading of the data from the * jdbc resultSet and putting it into the bean etc. *

- * */ EntityBean load(DbReadContext ctx, EntityBean localBean, EntityBean contextBean) throws SQLException; diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeBean.java b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeBean.java index f464e3050..8e88bbd9b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeBean.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeBean.java @@ -264,11 +264,7 @@ public class SqlTreeNodeBean implements SqlTreeNode { children[i].load(ctx, localBean, contextBean); } - if (lazyLoadMany) { - // special case where we load children - - } else if (localBean != null) { - + if (!lazyLoadMany && localBean != null) { ctx.setCurrentPrefix(prefix, pathMap); if (readId) { createListProxies(localDesc, ctx, localBean); @@ -303,6 +299,7 @@ public class SqlTreeNodeBean implements SqlTreeNode { ctx.profileBean(ebi, prefix); } } + if (parentBean != null) { // set this back to the parentBean nodeBeanProp.setValue(parentBean, contextBean); diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeExtraJoin.java b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeExtraJoin.java index add74e788..b6e248f7b 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeExtraJoin.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeExtraJoin.java @@ -20,72 +20,72 @@ import com.avaje.ebeaninternal.server.deploy.TableJoin; *

*/ public class SqlTreeNodeExtraJoin implements SqlTreeNode { - - private final BeanPropertyAssoc assocBeanProperty; - - private final String prefix; - - private final boolean manyJoin; - - private List children; - - public SqlTreeNodeExtraJoin(String prefix, BeanPropertyAssoc assocBeanProperty) { - this.prefix = prefix; - this.assocBeanProperty = assocBeanProperty; - this.manyJoin = assocBeanProperty instanceof BeanPropertyAssocMany; - } - public void buildSelectExpressionChain(List selectChain) { - // nothing to add - } + private final BeanPropertyAssoc assocBeanProperty; - /** - * Return true if the extra join is a many join. - *

- * This means we need to add distinct to the sql query. - *

- */ - public boolean isManyJoin() { - return manyJoin; - } + private final String prefix; + + private final boolean manyJoin; + + private List children; + + public SqlTreeNodeExtraJoin(String prefix, BeanPropertyAssoc assocBeanProperty) { + this.prefix = prefix; + this.assocBeanProperty = assocBeanProperty; + this.manyJoin = assocBeanProperty instanceof BeanPropertyAssocMany; + } + + public void buildSelectExpressionChain(List selectChain) { + // nothing to add + } + + /** + * Return true if the extra join is a many join. + *

+ * This means we need to add distinct to the sql query. + *

+ */ + public boolean isManyJoin() { + return manyJoin; + } - public String getName() { - return prefix; - } - - public void addChild(SqlTreeNodeExtraJoin child){ - if (children == null){ - children = new ArrayList(); - } - children.add(child); - } - - public void appendFrom(DbSqlContext ctx, SqlJoinType joinType) { - - boolean manyToMany = false; - - if (assocBeanProperty instanceof BeanPropertyAssocMany){ - BeanPropertyAssocMany manyProp = (BeanPropertyAssocMany)assocBeanProperty; - if (manyProp.isManyToMany()){ - - manyToMany = true; - - String alias = ctx.getTableAlias(prefix); - String[] split = SplitName.split(prefix); - String parentAlias = ctx.getTableAlias(split[0]); - String alias2 = alias+"z_"; - - TableJoin manyToManyJoin = manyProp.getIntersectionTableJoin(); - manyToManyJoin.addJoin(joinType, parentAlias, alias2, ctx); - - assocBeanProperty.addJoin(joinType, alias2, alias, ctx); - } - } - - if (!manyToMany){ - assocBeanProperty.addJoin(joinType, prefix, ctx); - } + public String getName() { + return prefix; + } + + public void addChild(SqlTreeNodeExtraJoin child) { + if (children == null) { + children = new ArrayList(); + } + children.add(child); + } + + public void appendFrom(DbSqlContext ctx, SqlJoinType joinType) { + + boolean manyToMany = false; + + if (assocBeanProperty instanceof BeanPropertyAssocMany) { + BeanPropertyAssocMany manyProp = (BeanPropertyAssocMany) assocBeanProperty; + if (manyProp.isManyToMany()) { + + manyToMany = true; + + String alias = ctx.getTableAlias(prefix); + String[] split = SplitName.split(prefix); + String parentAlias = ctx.getTableAlias(split[0]); + String alias2 = alias + "z_"; + + TableJoin manyToManyJoin = manyProp.getIntersectionTableJoin(); + manyToManyJoin.addJoin(joinType, parentAlias, alias2, ctx); + + assocBeanProperty.addJoin(joinType, alias2, alias, ctx); + } + } + + if (!manyToMany) { + assocBeanProperty.addJoin(joinType, prefix, ctx); + } if (children != null) { @@ -101,23 +101,23 @@ public class SqlTreeNodeExtraJoin implements SqlTreeNode { } } - /** - * Does nothing. - */ - public void appendSelect(DbSqlContext ctx, boolean subQuery) { - } + /** + * Does nothing. + */ + public void appendSelect(DbSqlContext ctx, boolean subQuery) { + } - /** - * Does nothing. - */ - public void appendWhere(DbSqlContext ctx) { - } + /** + * Does nothing. + */ + public void appendWhere(DbSqlContext ctx) { + } - /** - * Does nothing. - */ - public EntityBean load(DbReadContext ctx, EntityBean localBean, EntityBean parentBean) throws SQLException { - return null; - } + /** + * Does nothing. + */ + public EntityBean load(DbReadContext ctx, EntityBean localBean, EntityBean parentBean) throws SQLException { + return null; + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeManyWhereJoin.java b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeManyWhereJoin.java index 9a84dc546..21970c80c 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeManyWhereJoin.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeManyWhereJoin.java @@ -17,92 +17,80 @@ import com.avaje.ebeaninternal.server.deploy.TableJoin; */ public class SqlTreeNodeManyWhereJoin implements SqlTreeNode { - private final String parentPrefix; - - private final String prefix; + private final String parentPrefix; - private final BeanPropertyAssoc nodeBeanProp; - - /** - * Child joins. - */ - private final SqlTreeNode[] children; - - /** - * The many where join which is either INNER or OUTER. - */ - private final SqlJoinType manyJoinType; + private final String prefix; - public SqlTreeNodeManyWhereJoin(String prefix, BeanPropertyAssoc prop, SqlJoinType manyJoinType) { - - this.nodeBeanProp = prop; - this.prefix = prefix; - this.manyJoinType = manyJoinType; + private final BeanPropertyAssoc nodeBeanProp; - String[] split = SplitName.split(prefix); - this.parentPrefix = split[0]; - - List childrenList = new ArrayList(0); - this.children = childrenList.toArray(new SqlTreeNode[childrenList.size()]); + /** + * The many where join which is either INNER or OUTER. + */ + private final SqlJoinType manyJoinType; + + public SqlTreeNodeManyWhereJoin(String prefix, BeanPropertyAssoc prop, SqlJoinType manyJoinType) { + + this.nodeBeanProp = prop; + this.prefix = prefix; + this.manyJoinType = manyJoinType; + + String[] split = SplitName.split(prefix); + this.parentPrefix = split[0]; + } + + /** + * Append to the FROM clause for this node. + */ + @Override + public void appendFrom(DbSqlContext ctx, SqlJoinType currentJoinType) { + + // always use the join type as per this many where join + // (OUTER for disjunction and otherwise INNER) + appendFromBaseTable(ctx, manyJoinType); + } + + /** + * Join to base table for this node. This includes a join to the + * intersection table if this is a ManyToMany node. + */ + public void appendFromBaseTable(DbSqlContext ctx, SqlJoinType joinType) { + + String alias = ctx.getTableAliasManyWhere(prefix); + String parentAlias = ctx.getTableAliasManyWhere(parentPrefix); + + if (nodeBeanProp instanceof BeanPropertyAssocOne) { + nodeBeanProp.addJoin(joinType, parentAlias, alias, ctx); + + } else { + BeanPropertyAssocMany manyProp = (BeanPropertyAssocMany) nodeBeanProp; + if (!manyProp.isManyToMany()) { + manyProp.addJoin(joinType, parentAlias, alias, ctx); + + } else { + String alias2 = alias + "z_"; + + TableJoin manyToManyJoin = manyProp.getIntersectionTableJoin(); + manyToManyJoin.addJoin(joinType, parentAlias, alias2, ctx); + manyProp.addJoin(joinType, alias2, alias, ctx); + } } + } - /** - * Append to the FROM clause for this node. - */ - @Override - public void appendFrom(DbSqlContext ctx, SqlJoinType currentJoinType) { - - // always use the join type as per this many where join - // (OUTER for disjunction and otherwise INNER) - appendFromBaseTable(ctx, manyJoinType); - - for (int i = 0; i < children.length; i++) { - children[i].appendFrom(ctx, manyJoinType); - } - } + public void buildSelectExpressionChain(List selectChain) { + // nothing to add + } - /** - * Join to base table for this node. This includes a join to the - * intersection table if this is a ManyToMany node. - */ - public void appendFromBaseTable(DbSqlContext ctx, SqlJoinType joinType) { + public void appendSelect(DbSqlContext ctx, boolean subQuery) { + // nothing to do here + } - String alias = ctx.getTableAliasManyWhere(prefix); - String parentAlias = ctx.getTableAliasManyWhere(parentPrefix); + public void appendWhere(DbSqlContext ctx) { + // nothing to do here + } - if (nodeBeanProp instanceof BeanPropertyAssocOne){ - nodeBeanProp.addJoin(joinType, parentAlias, alias, ctx); - - } else { - BeanPropertyAssocMany manyProp = (BeanPropertyAssocMany)nodeBeanProp; - if (!manyProp.isManyToMany()) { - manyProp.addJoin(joinType, parentAlias, alias, ctx); - - } else { - String alias2 = alias + "z_"; - - TableJoin manyToManyJoin = manyProp.getIntersectionTableJoin(); - manyToManyJoin.addJoin(joinType, parentAlias, alias2, ctx); - manyProp.addJoin(joinType, alias2, alias, ctx); - } - } - } - - public void buildSelectExpressionChain(List selectChain) { - // nothing to add - } - - public void appendSelect(DbSqlContext ctx, boolean subQuery) { - // nothing to do here - } - - public void appendWhere(DbSqlContext ctx) { - // nothing to do here - } - - public EntityBean load(DbReadContext ctx, EntityBean localBean, EntityBean parentBean) throws SQLException { - // nothing to do here - return null; - } + public EntityBean load(DbReadContext ctx, EntityBean localBean, EntityBean parentBean) throws SQLException { + // nothing to do here + return null; + } } diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeRoot.java b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeRoot.java index 86a3e7fac..9ed916096 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeRoot.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeNodeRoot.java @@ -1,33 +1,31 @@ package com.avaje.ebeaninternal.server.query; -import java.util.List; - -import com.avaje.ebean.bean.EntityBean; import com.avaje.ebeaninternal.server.deploy.BeanDescriptor; import com.avaje.ebeaninternal.server.deploy.BeanPropertyAssocMany; -import com.avaje.ebeaninternal.server.deploy.DbReadContext; import com.avaje.ebeaninternal.server.deploy.DbSqlContext; import com.avaje.ebeaninternal.server.deploy.TableJoin; +import java.util.List; + /** * Represents the root node of the Sql Tree. */ public final class SqlTreeNodeRoot extends SqlTreeNodeBean { - private final TableJoin includeJoin; - - /** - * Specify for SqlSelect to include an Id property or not. - */ - public SqlTreeNodeRoot(BeanDescriptor desc, SqlTreeProperties props, List myList, boolean withId, TableJoin includeJoin, BeanPropertyAssocMany many){ - super(null, null, desc, props, myList, withId, many); - this.includeJoin = includeJoin; - } + private final TableJoin includeJoin; - public SqlTreeNodeRoot(BeanDescriptor desc, SqlTreeProperties props, List myList, boolean withId) { - super(null, null, desc, props, myList, withId, null); - this.includeJoin = null; - } + /** + * Specify for SqlSelect to include an Id property or not. + */ + public SqlTreeNodeRoot(BeanDescriptor desc, SqlTreeProperties props, List myList, boolean withId, TableJoin includeJoin, BeanPropertyAssocMany many) { + super(null, null, desc, props, myList, withId, many); + this.includeJoin = includeJoin; + } + + public SqlTreeNodeRoot(BeanDescriptor desc, SqlTreeProperties props, List myList, boolean withId) { + super(null, null, desc, props, myList, withId, null); + this.includeJoin = null; + } /** * For the root node there is no join type or on clause etc. @@ -46,5 +44,5 @@ public final class SqlTreeNodeRoot extends SqlTreeNodeBean { return joinType; } - + } diff --git a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeProperties.java b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeProperties.java index b514b4ca1..ae6b38a33 100644 --- a/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeProperties.java +++ b/src/main/java/com/avaje/ebeaninternal/server/query/SqlTreeProperties.java @@ -14,86 +14,69 @@ public class SqlTreeProperties { private static final TableJoin[] EMPTY_TABLE_JOINS = new TableJoin[0]; - /** - * True if this node of the tree should have read only entity beans. - */ - private boolean readOnly; + /** + * True if this node of the tree should have read only entity beans. + */ + private boolean readOnly; - /** - * set to false if the id field is not included. - */ - private boolean includeId = true; + private TableJoin[] tableJoins = EMPTY_TABLE_JOINS; - private TableJoin[] tableJoins = EMPTY_TABLE_JOINS; + /** + * The bean properties in order. + */ + private List propsList = new ArrayList(); - /** - * The bean properties in order. - */ - private List propsList = new ArrayList(); - - /** - * Maintain a list of property names to detect embedded bean additions. - */ - private LinkedHashSet propNames = new LinkedHashSet(); + /** + * Maintain a list of property names to detect embedded bean additions. + */ + private LinkedHashSet propNames = new LinkedHashSet(); private boolean allProperties; - - public SqlTreeProperties() { - } - - public boolean containsProperty(String propName){ - return propNames.contains(propName); - } - public void add(BeanProperty[] props) { - for (BeanProperty beanProperty : props) { - propsList.add(beanProperty); - } - } + public SqlTreeProperties() { + } - public void add(BeanProperty prop) { + public boolean containsProperty(String propName) { + return propNames.contains(propName); + } + + public void add(BeanProperty[] props) { + for (int i = 0; i < props.length; i++) { + propsList.add(props[i]); + } + } + + public void add(BeanProperty prop) { propsList.add(prop); - propNames.add(prop.getName()); - } - - public BeanProperty[] getProps() { - return propsList.toArray(new BeanProperty[propsList.size()]); - } + propNames.add(prop.getName()); + } - public boolean isIncludeId() { - return includeId; - } + public BeanProperty[] getProps() { + return propsList.toArray(new BeanProperty[propsList.size()]); + } - public void setIncludeId(boolean includeId) { - this.includeId = includeId; - } + public boolean isPartialObject() { + return !allProperties; + } - public boolean isPartialObject() { - return !allProperties; - } + public boolean isReadOnly() { + return readOnly; + } - public boolean isReadOnly() { - return readOnly; - } + public void setReadOnly(boolean readOnly) { + this.readOnly = readOnly; + } - public void setReadOnly(boolean readOnly) { - this.readOnly = readOnly; - } + public TableJoin[] getTableJoins() { + return tableJoins; + } - public TableJoin[] getTableJoins() { - return tableJoins; - } - - public void setTableJoins(TableJoin[] tableJoins) { - this.tableJoins = tableJoins; - } + public void setTableJoins(TableJoin[] tableJoins) { + this.tableJoins = tableJoins; + } public void setAllProperties(boolean allProperties) { this.allProperties = allProperties; } - public boolean isAllProperties() { - return allProperties; - } - } \ No newline at end of file