Skip to content

Commit

Permalink
refactor sql order by building; misc refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
teosarca committed Apr 29, 2017
1 parent c3ff463 commit 621d1c4
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 42 deletions.
3 changes: 1 addition & 2 deletions src/main/java/de/metas/ui/web/view/SqlViewRepository.java
Original file line number Diff line number Diff line change
Expand Up @@ -112,14 +112,13 @@ public ViewRowIdsOrderedSelection createOrderedSelection(final SqlViewEvaluation
final Stopwatch stopwatch = Stopwatch.createStarted();
final long rowsCount = DB.executeUpdateEx(sql, sqlParams.toArray(), ITrx.TRXNAME_ThreadInherited);
stopwatch.stop();
final boolean queryLimitHit = queryLimit > 0 && rowsCount >= queryLimit;
logger.trace("Created selection {}, rowsCount={}, duration={} \n SQL: {} -- {}", viewId, rowsCount, stopwatch, sql, sqlParams);

return ViewRowIdsOrderedSelection.builder()
.setViewId(viewId)
.setSize(rowsCount)
.setOrderBys(sqlBindings.getDefaultOrderBys())
.setQueryLimit(queryLimit, queryLimitHit)
.setQueryLimit(queryLimit)
.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ public static final class Builder
private List<DocumentQueryOrderBy> orderBys;

private int queryLimit;
private boolean queryLimitHit;

private Builder()
{
Expand Down Expand Up @@ -149,7 +148,7 @@ public Builder setSize(final long size)

private ImmutableList<DocumentQueryOrderBy> getOrderBys()
{
return ImmutableList.copyOf(orderBys);
return orderBys == null ? ImmutableList.of() : ImmutableList.copyOf(orderBys);
}

public Builder setOrderBys(final List<DocumentQueryOrderBy> orderBys)
Expand All @@ -158,10 +157,9 @@ public Builder setOrderBys(final List<DocumentQueryOrderBy> orderBys)
return this;
}

public Builder setQueryLimit(final int queryLimit, final boolean queryLimitHit)
public Builder setQueryLimit(final int queryLimit)
{
this.queryLimit = queryLimit;
this.queryLimitHit = queryLimitHit;
return this;
}

Expand All @@ -172,7 +170,9 @@ private int getQueryLimit()

private boolean isQueryLimitHit()
{
return queryLimitHit;
return queryLimit > 0
&& size > 0
&& size >= queryLimit;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,7 @@ public String getSqlCreateSelectionFrom( //
//
// SELECT ... FROM ... WHERE 1=1
{
IStringExpression sqlOrderBy = SqlDocumentOrderByBuilder.newInstance(this).buildSqlOrderBy(defaultOrderBys);
IStringExpression sqlOrderBy = SqlDocumentOrderByBuilder.newInstance(this::getFieldOrderBy).buildSqlOrderBy(defaultOrderBys);
if (sqlOrderBy.isNullExpression())
{
sqlOrderBy = ConstantStringExpression.of(keyColumnNameFQ);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
package de.metas.ui.web.view.descriptor;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.adempiere.ad.expression.api.IExpressionEvaluator.OnVariableNotFound;
import org.adempiere.ad.expression.api.IStringExpression;
import org.adempiere.ad.expression.api.impl.ConstantStringExpression;
import org.adempiere.ad.trx.api.ITrx;
import org.adempiere.exceptions.DBException;
import org.adempiere.util.Check;
import org.compiere.util.DB;
import org.compiere.util.Evaluatees;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableMap;
Expand All @@ -17,6 +20,7 @@
import de.metas.ui.web.view.ViewRowIdsOrderedSelection;
import de.metas.ui.web.window.datatypes.WindowId;
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import de.metas.ui.web.window.model.sql.SqlDocumentOrderByBuilder;

/*
* #%L
Expand All @@ -31,11 +35,11 @@
*
* This program 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
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
Expand Down Expand Up @@ -66,45 +70,41 @@ public ViewRowIdsOrderedSelection createFromView(final ViewRowIdsOrderedSelectio
{
final WindowId windowId = fromView.getWindowId();
final String fromSelectionId = fromView.getSelectionId();

final ViewId newViewId = ViewId.random(windowId);
final String newSelectionId = newViewId.getViewId();
final String sqlOrderBys = buildOrderBys(orderBys); // NOTE: we assume it's not empty!
final String sqlFinal = sqlCreateFromViewId.replace(SqlViewBinding.PLACEHOLDER_OrderBy, sqlOrderBys);
final int rowCount = DB.executeUpdateEx(sqlFinal, new Object[] { newSelectionId, fromSelectionId }, ITrx.TRXNAME_ThreadInherited);

final List<Object> sqlParams = new ArrayList<>();
final String sqlFinal = buildSqlCreateFromViewId(sqlParams, newSelectionId, fromSelectionId, orderBys);
final int rowsCount = DB.executeUpdateEx(sqlFinal, sqlParams.toArray(), ITrx.TRXNAME_ThreadInherited);

return ViewRowIdsOrderedSelection.builder()
.setViewId(newViewId)
.setSize(rowCount)
.setSize(rowsCount)
.setOrderBys(orderBys)
.setQueryLimit(fromView.getQueryLimit(), fromView.isQueryLimitHit())
.setQueryLimit(fromView.getQueryLimit())
.build();
}

private final String buildOrderBys(final List<DocumentQueryOrderBy> orderBys)
private String buildSqlCreateFromViewId(final List<Object> sqlParams, final String newSelectionId, final String fromSelectionId, final List<DocumentQueryOrderBy> orderBys)
{
if (orderBys == null || orderBys.isEmpty())
{
return "";
}

return orderBys
.stream()
.map(orderBy -> buildOrderBy(orderBy))
.filter(orderBy -> !Check.isEmpty(orderBy, true))
.collect(Collectors.joining(", "));
final String sqlOrderBys = SqlDocumentOrderByBuilder.newInstance(this::getFieldOrderBy)
.buildSqlOrderBy(orderBys)
.evaluate(Evaluatees.empty(), OnVariableNotFound.Fail); // assume constant string
final String sqlFinal = sqlCreateFromViewId.replace(SqlViewBinding.PLACEHOLDER_OrderBy, sqlOrderBys);
sqlParams.add(newSelectionId);
sqlParams.add(fromSelectionId);
return sqlFinal;
}

private final String buildOrderBy(final DocumentQueryOrderBy orderBy)
private final IStringExpression getFieldOrderBy(final String fieldName)
{
final String fieldName = orderBy.getFieldName();
final String fieldSql = sqlOrderBysByFieldName.get(fieldName);
if (fieldSql == null)
{
throw new DBException("No SQL field mapping found for: " + fieldName + ". Available fields are: " + sqlOrderBysByFieldName.keySet());
}

return "(" + fieldSql + ") " + (orderBy.isAscending() ? " ASC" : " DESC");
return ConstantStringExpression.of(fieldSql.trim());
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package de.metas.ui.web.window.descriptor.sql;

import org.adempiere.ad.expression.api.IStringExpression;

/*
* #%L
* metasfresh-webui-api
Expand Down Expand Up @@ -30,4 +32,10 @@ public interface SqlEntityBinding

/** @return field binding or throws exception in case it was not found */
SqlEntityFieldBinding getFieldByFieldName(String fieldName);

default IStringExpression getFieldOrderBy(String fieldName)
{
return getFieldByFieldName(fieldName).getSqlOrderBy();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.adempiere.ad.expression.api.IStringExpression;

import de.metas.ui.web.window.descriptor.sql.SqlEntityBinding;
import de.metas.ui.web.window.descriptor.sql.SqlEntityFieldBinding;
import de.metas.ui.web.window.model.DocumentQueryOrderBy;
import lombok.NonNull;

Expand Down Expand Up @@ -35,14 +34,25 @@ public class SqlDocumentOrderByBuilder
{
public static final SqlDocumentOrderByBuilder newInstance(final SqlEntityBinding entityBinding)
{
return new SqlDocumentOrderByBuilder(entityBinding);
return new SqlDocumentOrderByBuilder(entityBinding::getFieldOrderBy);
}

public static final SqlDocumentOrderByBuilder newInstance(final SqlOrderByBindings bindings)
{
return new SqlDocumentOrderByBuilder(bindings);
}


public static interface SqlOrderByBindings
{
IStringExpression getFieldOrderBy(String fieldName);
}

private final SqlEntityBinding entityBinding;
private final SqlOrderByBindings bindings;

private SqlDocumentOrderByBuilder(@NonNull final SqlEntityBinding entityBinding)
private SqlDocumentOrderByBuilder(@NonNull final SqlOrderByBindings bindings)
{
this.entityBinding = entityBinding;
this.bindings = bindings;
}

public IStringExpression buildSqlOrderBy(final List<DocumentQueryOrderBy> orderBys)
Expand All @@ -64,8 +74,7 @@ public IStringExpression buildSqlOrderBy(final List<DocumentQueryOrderBy> orderB
private final IStringExpression buildSqlOrderBy(final DocumentQueryOrderBy orderBy)
{
final String fieldName = orderBy.getFieldName();
final SqlEntityFieldBinding fieldBinding = entityBinding.getFieldByFieldName(fieldName);
final IStringExpression sqlExpression = fieldBinding.getSqlOrderBy();
final IStringExpression sqlExpression = bindings.getFieldOrderBy(fieldName);
return SqlDocumentOrderByBuilder.buildSqlOrderBy(sqlExpression, orderBy.isAscending());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public List<DocumentQueryOrderBy> getOrderBysEffective()
public IStringExpression getSqlOrderByEffective()
{
final List<DocumentQueryOrderBy> orderBys = getOrderBysEffective();
return SqlDocumentOrderByBuilder.newInstance(entityBinding).buildSqlOrderBy(orderBys);
return SqlDocumentOrderByBuilder.newInstance(entityBinding::getFieldOrderBy).buildSqlOrderBy(orderBys);
}

public SqlDocumentEntityDataBindingDescriptor getEntityBinding()
Expand Down

0 comments on commit 621d1c4

Please sign in to comment.