Skip to content

Commit

Permalink
refactor and introduce SqlViewSelect
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Nov 12, 2017
1 parent eb6070e commit 9c088cc
Show file tree
Hide file tree
Showing 7 changed files with 525 additions and 377 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
import javax.annotation.Nullable;

import org.adempiere.ad.dao.IQueryBuilder;
import org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound;
import org.adempiere.ad.expression.api.IStringExpression;
import org.adempiere.ad.service.IADReferenceDAO;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.DBException;
Expand Down Expand Up @@ -52,8 +50,10 @@
import de.metas.ui.web.view.ViewId;
import de.metas.ui.web.view.ViewRowIdsOrderedSelection;
import de.metas.ui.web.view.ViewRowIdsOrderedSelectionFactory;
import de.metas.ui.web.view.descriptor.SqlAndParams;
import de.metas.ui.web.view.descriptor.SqlViewBinding;
import de.metas.ui.web.view.descriptor.SqlViewRowIdsConverter;
import de.metas.ui.web.view.descriptor.SqlViewSelect;
import de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.ui.web.window.datatypes.WindowId;
Expand Down Expand Up @@ -95,7 +95,7 @@ public class SqlHUEditorViewRepository implements HUEditorViewRepository

private final SqlViewBinding sqlViewBinding;
private final ViewRowIdsOrderedSelectionFactory viewSelectionFactory;
private final IStringExpression sqlSelectHUIdsByPage;
private final SqlViewSelect sqlViewSelect;

@Builder
private SqlHUEditorViewRepository(
Expand All @@ -111,7 +111,7 @@ private SqlHUEditorViewRepository(

this.sqlViewBinding = sqlViewBinding;
viewSelectionFactory = SqlViewRowIdsOrderedSelectionFactory.of(sqlViewBinding);
sqlSelectHUIdsByPage = sqlViewBinding.getSqlSelectByPage();
sqlViewSelect = sqlViewBinding.getSqlViewSelect();
}

@Override
Expand Down Expand Up @@ -431,20 +431,20 @@ else if (huIdsFilter.getMustHUIds() != null)
@Override
public Page<Integer> retrieveHUIdsPage(final ViewRowIdsOrderedSelection selection, final int firstRow, final int maxRows)
{
final int firstSeqNo = firstRow + 1; // NOTE: firstRow is 0-based while SeqNo are 1-based
final int lastSeqNo = firstRow + maxRows;

final ViewEvaluationCtx viewEvalCtx = ViewEvaluationCtx.of(Env.getCtx());
final String sql = sqlSelectHUIdsByPage.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail);
final Object[] sqlParams = new Object[] { selection.getSelectionId(), firstSeqNo, lastSeqNo };
final SqlAndParams sqlAndParams = sqlViewSelect.selectByPage()
.viewEvalCtx(ViewEvaluationCtx.of(Env.getCtx()))
.viewId(selection.getViewId())
.firstRowZeroBased(firstRow)
.pageLength(maxRows)
.build();

PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, ITrx.TRXNAME_ThreadInherited);
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
pstmt.setMaxRows(maxRows);
DB.setParameters(pstmt, sqlParams);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());

rs = pstmt.executeQuery();

Expand All @@ -464,7 +464,7 @@ public Page<Integer> retrieveHUIdsPage(final ViewRowIdsOrderedSelection selectio
catch (final SQLException ex)
{
throw DBException.wrapIfNeeded(ex)
.setSqlIfAbsent(sql, sqlParams);
.setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
}
finally
{
Expand Down
110 changes: 46 additions & 64 deletions src/main/java/de/metas/ui/web/view/SqlViewDataRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,13 @@

import org.adempiere.ad.dao.IQueryBL;
import org.adempiere.ad.dao.impl.TypedSqlQueryFilter;
import org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound;
import org.adempiere.ad.expression.api.IStringExpression;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DBException;
import org.adempiere.model.PlainContextAware;
import org.adempiere.util.Check;
import org.adempiere.util.Services;
import org.compiere.util.DB;
import org.compiere.util.Evaluatee;
import org.compiere.util.Evaluatees;
import org.slf4j.Logger;

import com.google.common.base.MoreObjects;
Expand All @@ -37,10 +33,11 @@
import de.metas.ui.web.document.filter.sql.SqlParamsCollector;
import de.metas.ui.web.exceptions.EntityNotFoundException;
import de.metas.ui.web.view.ViewRow.DefaultRowType;
import de.metas.ui.web.view.descriptor.SqlAndParams;
import de.metas.ui.web.view.descriptor.SqlViewBinding;
import de.metas.ui.web.view.descriptor.SqlViewRowFieldBinding;
import de.metas.ui.web.view.descriptor.SqlViewRowFieldBinding.SqlViewRowFieldLoader;
import de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder;
import de.metas.ui.web.view.descriptor.SqlViewSelect;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
import de.metas.ui.web.window.datatypes.WindowId;
Expand Down Expand Up @@ -75,10 +72,7 @@ class SqlViewDataRepository implements IViewDataRepository
private static final Logger logger = LogManager.getLogger(SqlViewDataRepository.class);

private final String tableName;
private final IStringExpression sqlSelectById;
private final IStringExpression sqlSelectByPage;
private final IStringExpression sqlSelectRowIdsByPage;
private final IStringExpression sqlSelectLinesByRowIds;
private final SqlViewSelect sqlViewSelect;
private final ViewRowIdsOrderedSelectionFactory viewRowIdsOrderedSelectionFactory;
private final DocumentFilterDescriptorsProvider viewFilterDescriptors;
private final List<DocumentQueryOrderBy> defaultOrderBys;
Expand All @@ -91,10 +85,7 @@ class SqlViewDataRepository implements IViewDataRepository
SqlViewDataRepository(@NonNull final SqlViewBinding sqlBindings)
{
tableName = sqlBindings.getTableName();
sqlSelectById = sqlBindings.getSqlSelectById();
sqlSelectByPage = sqlBindings.getSqlSelectByPage();
sqlSelectRowIdsByPage = sqlBindings.getSqlSelectRowIdsByPage();
sqlSelectLinesByRowIds = sqlBindings.getSqlSelectLinesByRowIds();
sqlViewSelect = sqlBindings.getSqlViewSelect();
viewFilterDescriptors = sqlBindings.getViewFilterDescriptors();
viewRowIdsOrderedSelectionFactory = SqlViewRowIdsOrderedSelectionFactory.of(sqlBindings);
defaultOrderBys = sqlBindings.getDefaultOrderBys();
Expand Down Expand Up @@ -149,11 +140,11 @@ public String getSqlWhereClause(final ViewId viewId, final List<DocumentFilter>
sqlWhereClause.append("(").append(sqlFilters).append(")");
}
}

// Filter by rowIds
{
final String sqlFilterByRowIds = viewRowIdsOrderedSelectionFactory.getSqlWhereClause(viewId, rowIds);
if(!Check.isEmpty(sqlFilterByRowIds, true))
if (!Check.isEmpty(sqlFilterByRowIds, true))
{
if (sqlWhereClause.length() > 0)
{
Expand All @@ -162,7 +153,7 @@ public String getSqlWhereClause(final ViewId viewId, final List<DocumentFilter>
sqlWhereClause.append("(").append(sqlFilterByRowIds).append(")");
}
}

return sqlWhereClause.toString();
}

Expand All @@ -181,19 +172,20 @@ public ViewRowIdsOrderedSelection createOrderedSelectionFromSelection(final View
@Override
public IViewRow retrieveById(final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final DocumentId rowId)
{
final String viewSelectionId = viewId.getViewId();

final String sql = sqlSelectById.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail);
final SqlAndParams sqlAndParams = sqlViewSelect.selectById()
.viewEvalCtx(viewEvalCtx)
.viewId(viewId)
.rowId(rowId)
.build();

final Object[] sqlParams = new Object[] { viewSelectionId, rowId.toInt() };
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
final int limit = 2;
pstmt = DB.prepareStatement(sql, ITrx.TRXNAME_ThreadInherited);
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
pstmt.setMaxRows(limit);
DB.setParameters(pstmt, sqlParams);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());

rs = pstmt.executeQuery();

Expand All @@ -215,7 +207,7 @@ else if (documents.size() > 1)
catch (final SQLException | DBException e)
{
throw DBException.wrapIfNeeded(e)
.setSqlIfAbsent(sql, sqlParams);
.setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
}
finally
{
Expand Down Expand Up @@ -277,7 +269,7 @@ private ViewRow.Builder loadViewRow(final ResultSet rs, final WindowId windowId,
{
final ViewRow.Builder viewRowBuilder = ViewRow.builder(windowId);

final int parentIdInt = rs.getInt(SqlViewSelectionQueryBuilder.COLUMNNAME_Paging_Parent_ID);
final int parentIdInt = rs.getInt(SqlViewSelect.COLUMNNAME_Paging_Parent_ID);
if (!rs.wasNull())
{
viewRowBuilder.setParentRowId(DocumentId.of(parentIdInt));
Expand Down Expand Up @@ -321,36 +313,32 @@ public DocumentFilterDescriptorsProvider getViewFilterDescriptors()
public List<IViewRow> retrievePage(final ViewEvaluationCtx viewEvalCtx, final ViewRowIdsOrderedSelection orderedSelection, final int firstRow, final int pageLength) throws DBException
{
logger.debug("Getting page: firstRow={}, pageLength={} - {}", firstRow, pageLength, this);

Check.assume(firstRow >= 0, "firstRow >= 0 but it was {}", firstRow);
Check.assume(pageLength > 0, "pageLength > 0 but it was {}", pageLength);

logger.debug("Using: {}", orderedSelection);

final ViewId viewId = orderedSelection.getViewId();
final String viewSelectionId = viewId.getViewId();
final SqlAndParams sqlAndParams = sqlViewSelect.selectByPage()
.viewEvalCtx(viewEvalCtx)
.viewId(viewId)
.firstRowZeroBased(firstRow)
.pageLength(pageLength)
.build();

final int firstSeqNo = firstRow + 1; // NOTE: firstRow is 0-based while SeqNo are 1-based
final int lastSeqNo = firstRow + pageLength;

final String sql = sqlSelectByPage.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail);
final Object[] sqlParams = new Object[] { viewSelectionId, firstSeqNo, lastSeqNo };
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, ITrx.TRXNAME_ThreadInherited);
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
pstmt.setMaxRows(pageLength);
DB.setParameters(pstmt, sqlParams);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());

rs = pstmt.executeQuery();

final List<IViewRow> page = loadViewRows(rs, viewEvalCtx, viewId, pageLength);
return page;
}
catch (final SQLException | DBException e)
{
throw DBException.wrapIfNeeded(e)
.setSqlIfAbsent(sql, sqlParams);
.setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
}
finally
{
Expand All @@ -362,32 +350,29 @@ public List<IViewRow> retrievePage(final ViewEvaluationCtx viewEvalCtx, final Vi
public List<DocumentId> retrieveRowIdsByPage(final ViewEvaluationCtx viewEvalCtx, final ViewRowIdsOrderedSelection orderedSelection, final int firstRow, final int pageLength)
{
logger.debug("Getting page: firstRow={}, pageLength={} - {}", firstRow, pageLength, this);

Check.assume(firstRow >= 0, "firstRow >= 0 but it was {}", firstRow);
Check.assume(pageLength > 0, "pageLength > 0 but it was {}", pageLength);
logger.debug("Using: {}", orderedSelection);

SqlViewRowFieldLoader rowIdFieldLoader = rowFieldLoaders.get(keyFieldName);
if (rowIdFieldLoader == null)
{
throw new AdempiereException("No rowId loader found in " + this);
}

logger.debug("Using: {}", orderedSelection);
final ViewId viewId = orderedSelection.getViewId();
final String viewSelectionId = viewId.getViewId();

final int firstSeqNo = firstRow + 1; // NOTE: firstRow is 0-based while SeqNo are 1-based
final int lastSeqNo = firstRow + pageLength;

final String sql = sqlSelectRowIdsByPage.evaluate(viewEvalCtx.toEvaluatee(), OnVariableNotFound.Fail);
final Object[] sqlParams = new Object[] { viewSelectionId, firstSeqNo, lastSeqNo };
final SqlAndParams sqlAndParams = sqlViewSelect.selectRowIdsByPage()
.viewEvalCtx(viewEvalCtx)
.viewId(viewId)
.firstRowZeroBased(firstRow)
.pageLength(pageLength)
.build();

PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, ITrx.TRXNAME_ThreadInherited);
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
pstmt.setMaxRows(pageLength);
DB.setParameters(pstmt, sqlParams);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());

rs = pstmt.executeQuery();

Expand All @@ -410,7 +395,7 @@ public List<DocumentId> retrieveRowIdsByPage(final ViewEvaluationCtx viewEvalCtx
catch (final SQLException | DBException e)
{
throw DBException.wrapIfNeeded(e)
.setSqlIfAbsent(sql, sqlParams);
.setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
}
finally
{
Expand All @@ -421,23 +406,20 @@ public List<DocumentId> retrieveRowIdsByPage(final ViewEvaluationCtx viewEvalCtx
private List<IViewRow> retrieveRowLines(final ViewEvaluationCtx viewEvalCtx, final ViewId viewId, final DocumentIdsSelection rowIds)
{
logger.debug("Getting row lines: rowId={} - {}", rowIds, this);

logger.debug("Using: {}", viewId);
final String viewSelectionId = viewId.getViewId();

// TODO: apply the ORDER BY from orderedSelection
final String sqlRecordIds = DB.buildSqlList(rowIds.toIntSet());
final Evaluatee viewEvalCtxEffective = Evaluatees.ofSingleton(SqlViewSelectionQueryBuilder.Paging_Record_IDsPlaceholder.getName(), sqlRecordIds)
.andComposeWith(viewEvalCtx.toEvaluatee());

final String sql = sqlSelectLinesByRowIds.evaluate(viewEvalCtxEffective, OnVariableNotFound.Fail);
final Object[] sqlParams = new Object[] { viewSelectionId };
final SqlAndParams sqlAndParams = sqlViewSelect.selectIncludedLines()
.viewEvalCtx(viewEvalCtx)
.viewId(viewId)
.rowIds(rowIds)
.build();

PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement(sql, ITrx.TRXNAME_ThreadInherited);
DB.setParameters(pstmt, sqlParams);
pstmt = DB.prepareStatement(sqlAndParams.getSql(), ITrx.TRXNAME_ThreadInherited);
DB.setParameters(pstmt, sqlAndParams.getSqlParams());

rs = pstmt.executeQuery();

Expand All @@ -447,7 +429,7 @@ private List<IViewRow> retrieveRowLines(final ViewEvaluationCtx viewEvalCtx, fin
catch (final SQLException | DBException e)
{
throw DBException.wrapIfNeeded(e)
.setSqlIfAbsent(sql, sqlParams);
.setSqlIfAbsent(sqlAndParams.getSql(), sqlAndParams.getSqlParams());
}
finally
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@

import de.metas.logging.LogManager;
import de.metas.ui.web.document.filter.DocumentFilter;
import de.metas.ui.web.view.descriptor.SqlAndParams;
import de.metas.ui.web.view.descriptor.SqlViewBinding;
import de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder;
import de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder.SqlAndParams;
import de.metas.ui.web.view.descriptor.SqlViewSelectionQueryBuilder.SqlCreateSelection;
import de.metas.ui.web.window.datatypes.DocumentId;
import de.metas.ui.web.window.datatypes.DocumentIdsSelection;
Expand Down
Loading

0 comments on commit 9c088cc

Please sign in to comment.