use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.
the class SqlDocumentQueryBuilder method buildSqlWhereClause.
private IPair<IStringExpression, List<Object>> buildSqlWhereClause() {
final SqlParamsCollector sqlParams = SqlParamsCollector.newInstance();
final CompositeStringExpression.Builder sqlWhereClauseBuilder = IStringExpression.composer();
//
// Entity's WHERE clause
{
final IStringExpression entityWhereClauseExpression = entityBinding.getSqlWhereClause();
if (!entityWhereClauseExpression.isNullExpression()) {
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* entity where clause */ (").append(entityWhereClauseExpression).append(")");
}
}
//
// Key column
// FIXME: handle AD_Reference/AD_Ref_List(s). In that case the recordId will be AD_Ref_List.Value,
// so the SQL where clause which is currently build is AD_Ref_List_ID=<the AD_Ref_List.Value>.
// The build SQL where clause shall be something like AD_Reference_ID=<the reference, i think we shall fetch it somehow from Lookup> AND Value=<the value, which currently is the recordId>
final DocumentId recordId = getRecordId();
if (recordId != null) {
final List<SqlDocumentFieldDataBindingDescriptor> keyFields = entityBinding.getKeyFields();
if (keyFields.isEmpty()) {
throw new AdempiereException("Failed building where clause because there is no Key Column defined in " + entityBinding);
} else // Single primary key
if (keyFields.size() == 1) {
final String keyColumnName = keyFields.get(0).getColumnName();
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* key */ ").append(keyColumnName).append("=").append(sqlParams.placeholder(recordId.toInt()));
} else // Composed primary key
{
final Map<String, Object> keyColumnName2value = extractComposedKey(recordId, keyFields);
keyColumnName2value.forEach((keyColumnName, value) -> {
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* key */ ").append(keyColumnName).append("=").append(sqlParams.placeholder(value));
});
}
}
//
// Parent link where clause (if any)
final Document parentDocument = getParentDocument();
if (parentDocument != null) {
final String parentLinkColumnName = entityBinding.getParentLinkColumnName();
final String linkColumnName = entityBinding.getLinkColumnName();
if (parentLinkColumnName != null && linkColumnName != null) {
final IDocumentFieldView parentLinkField = parentDocument.getFieldView(parentLinkColumnName);
final Object parentLinkValue = parentLinkField.getValue();
final DocumentFieldWidgetType parentLinkWidgetType = parentLinkField.getWidgetType();
final Class<?> targetClass = entityBinding.getFieldByFieldName(linkColumnName).getSqlValueClass();
final Object sqlParentLinkValue = SqlDocumentsRepository.convertValueToPO(parentLinkValue, parentLinkColumnName, parentLinkWidgetType, targetClass);
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* parent link */ ").append(linkColumnName).append("=").append(sqlParams.placeholder(sqlParentLinkValue));
}
}
//
// Document filters
{
final String sqlFilters = SqlDocumentFilterConverters.createEntityBindingEffectiveConverter(entityBinding).getSql(sqlParams, getDocumentFilters(), SqlOptions.usingTableAlias(entityBinding.getTableAlias()));
if (!Check.isEmpty(sqlFilters, true)) {
sqlWhereClauseBuilder.appendIfNotEmpty("\n AND ");
sqlWhereClauseBuilder.append(" /* filters */ (\n").append(sqlFilters).append(")\n");
}
}
// Build the final SQL where clause
return ImmutablePair.of(sqlWhereClauseBuilder.build(), Collections.unmodifiableList(sqlParams.toList()));
}
use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.
the class SqlDocumentQueryBuilder method buildSql.
private final IPair<IStringExpression, List<Object>> buildSql() {
final List<Object> sqlParams = new ArrayList<>();
final CompositeStringExpression.Builder sqlBuilder = IStringExpression.composer();
//
// SELECT ... FROM ...
sqlBuilder.append(getSqlSelectFrom());
// NOTE: no need to add security here because it was already embedded in SqlSelectFrom
//
// WHERE
{
final IPair<IStringExpression, List<Object>> sqlWhereClauseAndParams = getSqlWhereAndParams();
final IStringExpression sqlWhereClause = sqlWhereClauseAndParams.getLeft();
if (!sqlWhereClause.isNullExpression()) {
sqlBuilder.append("\n WHERE ").append(sqlWhereClause);
sqlParams.addAll(sqlWhereClauseAndParams.getRight());
}
}
// ORDER BY
if (isSorting()) {
final IStringExpression sqlOrderBy = getSqlOrderByEffective();
if (sqlOrderBy != null && !sqlOrderBy.isNullExpression()) {
sqlBuilder.append("\n ORDER BY ").append(sqlOrderBy);
}
}
//
// LIMIT/OFFSET
{
final int firstRow = getFirstRow();
if (firstRow > 0) {
sqlBuilder.append("\n OFFSET ?");
sqlParams.add(firstRow);
}
final int pageLength = getPageLength();
if (pageLength > 0) {
sqlBuilder.append("\n LIMIT ?");
sqlParams.add(pageLength);
}
}
//
return ImmutablePair.of(sqlBuilder.build(), Collections.unmodifiableList(sqlParams));
}
use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.
the class SqlDocumentQueryBuilder method getSql.
/**
* @return SQL to fully load the documents matched by this query.
*/
public String getSql(final List<Object> outSqlParams) {
final Evaluatee evalCtx = getEvaluationContext();
final IPair<IStringExpression, List<Object>> sqlAndParams = getSqlAndParams();
final String sql = sqlAndParams.getLeft().evaluate(evalCtx, OnVariableNotFound.Fail);
final List<Object> sqlParams = sqlAndParams.getRight();
outSqlParams.addAll(sqlParams);
return sql;
}
use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.
the class SqlDocumentQueryBuilder method getSqlWhere.
private String getSqlWhere(final List<Object> sqlParams) {
final IPair<IStringExpression, List<Object>> sqlWhereAndParams = getSqlWhereAndParams();
final Evaluatee evalCtx = getEvaluationContext();
final String sqlWhere = sqlWhereAndParams.getLeft().evaluate(evalCtx, OnVariableNotFound.Fail);
sqlParams.addAll(sqlWhereAndParams.getRight());
return sqlWhere;
}
use of org.adempiere.ad.expression.api.IStringExpression in project metasfresh-webui-api by metasfresh.
the class SqlDocumentQueryBuilder method getSqlSelectParentId.
/**
* SQL to fetch the parent's ID based on given child document.
*
* @return SELECT KeyColumnName from ParentTableName WHERE ....
*/
public String getSqlSelectParentId(final List<Object> outSqlParams, final DocumentEntityDescriptor parentEntityDescriptor) {
final String linkColumnName = entityBinding.getLinkColumnName();
final String parentLinkColumnName = entityBinding.getParentLinkColumnName();
if (parentLinkColumnName == null || linkColumnName == null) {
throw new AdempiereException("Selecting parent ID is not possible because this entity does not have a parent link").setParameter("linkColumnName", linkColumnName).setParameter("parentLinkColumnName", parentLinkColumnName).setParameter("entityBinding", entityBinding);
}
//
// SELECT linkColumnName from current(child) tableName
final List<Object> sqlSelectLinkColumnNameParams = new ArrayList<>();
final CompositeStringExpression.Builder sqlSelectLinkColumnName = IStringExpression.composer();
{
final IPair<IStringExpression, List<Object>> sqlWhereAndParams = getSqlWhereAndParams();
final IStringExpression sqlWhere = sqlWhereAndParams.getLeft();
final List<Object> sqlWhereParams = sqlWhereAndParams.getRight();
sqlSelectLinkColumnName.append("SELECT " + linkColumnName).append(// NOTE: we need table alias because the where clause is using it
" FROM " + entityBinding.getTableName() + " " + entityBinding.getTableAlias()).append("\n WHERE ").append(sqlWhere);
sqlSelectLinkColumnNameParams.addAll(sqlWhereParams);
}
//
//
final String parentKeyColumnName = extractSingleKeyColumnName(parentEntityDescriptor);
if (Objects.equals(parentKeyColumnName, parentLinkColumnName)) {
final Evaluatee evalCtx = getEvaluationContext();
final String sql = sqlSelectLinkColumnName.build().evaluate(evalCtx, OnVariableNotFound.Fail);
outSqlParams.addAll(sqlSelectLinkColumnNameParams);
return sql;
} else {
final Evaluatee evalCtx = getEvaluationContext();
final String sql = IStringExpression.composer().append("SELECT " + parentKeyColumnName + " FROM " + parentEntityDescriptor.getTableName()).append("\n WHERE " + parentLinkColumnName + " IN (").append(sqlSelectLinkColumnName).append(")").build().evaluate(evalCtx, OnVariableNotFound.Fail);
outSqlParams.addAll(sqlSelectLinkColumnNameParams);
return sql;
}
}
Aggregations