Skip to content

Commit

Permalink
Merge pull request #5 from EdwinBetanc0urt/bugfix/sql-error-with-filters
Browse files Browse the repository at this point in the history
fix: SQL error with filters.
  • Loading branch information
yamelsenih committed Aug 31, 2024
2 parents e9fc62a + 4edd65c commit bf69961
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 40 deletions.
78 changes: 45 additions & 33 deletions src/main/java/org/spin/report_engine/format/QueryDefinition.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ public class QueryDefinition {
private String whereClause;
private String completeQuery;
private String completeQueryCount;
private int limit;
private int limit;
private int offset;
private int instanceId;
public static final int NO_LIMIT = -1;
Expand Down Expand Up @@ -97,7 +97,7 @@ public QueryDefinition withLimit(int limit, int offset) {
this.offset = offset;
return this;
}

public List<Filter> getConditions() {
return conditions;
}
Expand Down Expand Up @@ -130,7 +130,7 @@ public List<Object> getParameters() {
}

public String getWhereClause() {
return whereClause;
return this.whereClause;
}

public QueryDefinition withWhereClause(String whereClause) {
Expand All @@ -157,6 +157,11 @@ public QueryDefinition withCompleteQuery(String completeQuery) {
}

public QueryDefinition buildQuery() {
// Add Query columns
StringBuffer completeQuery = new StringBuffer(getQuery());
StringBuffer completeQueryWithoutLimit = new StringBuffer(getQuery());

// Add Where restriction
// TODO: Add 1=1 to remove `if (whereClause.length() > 0)` and change stream with parallelStream
StringBuffer whereClause = new StringBuffer();
getConditions().stream()
Expand All @@ -177,46 +182,53 @@ public QueryDefinition buildQuery() {
whereClause.append(restriction);
}
});
// No Limit for Counter
StringBuffer completeQueryWithoutLimit = new StringBuffer(getQuery());
if(!Util.isEmpty(getWhereClause())) {
completeQueryWithoutLimit.append(" WHERE ").append(whereClause);
}
if(!Util.isEmpty(getGroupBy())) {
completeQueryWithoutLimit.append(" GROUP BY ").append(getGroupBy());
}
if(!Util.isEmpty(getOrderBy())) {
completeQueryWithoutLimit.append(" ORDER BY ").append(getOrderBy());
withWhereClause(whereClause.toString());
if(!Util.isEmpty(getWhereClause(), true)) {
completeQuery.append(" WHERE ").append(getWhereClause());
completeQueryWithoutLimit.append(" WHERE ").append(getWhereClause());
}
withCompleteQueryCount(completeQueryWithoutLimit.toString());
// Add Limit
if(limit != NO_LIMIT) {
if(limit == 0) {

// Add Limit records
if(this.limit != NO_LIMIT) {
if(this.limit == 0) {
withLimit(100, 0);
}
if (whereClause.length() > 0) {
whereClause.append(" AND ");
}
whereClause.append("ROWNUM <= ").append(limit);
// Add offset
if (whereClause.length() > 0) {
whereClause.append(" AND ");

StringBuffer limitClause = new StringBuffer()
// TODO: Implement with use https://github.com/adempiere/adempiere/pull/4142
// .append(" LIMIT ")
// .append(this.limit)
// .append(" OFFSET ")
// .append(this.offset)
// .append(completeQueryCount)
;

if(!Util.isEmpty(getWhereClause(), true)) {
limitClause.insert(0, " AND ");
} else {
limitClause.insert(0, " WHERE ");
}
whereClause.append("ROWNUM >= ").append(offset);
}
//
withWhereClause(whereClause.toString());
StringBuffer completeQuery = new StringBuffer(getQuery());
if(!Util.isEmpty(getWhereClause())) {
completeQuery.append(" WHERE ").append(getWhereClause());
limitClause.append(" ROWNUM <= ").append(this.limit);
limitClause.append(" AND ROWNUM >= ").append(this.offset);

completeQuery.append(limitClause.toString());
}
if(!Util.isEmpty(getGroupBy())) {

// Add Group By
if(!Util.isEmpty(getGroupBy(), true)) {
completeQuery.append(" GROUP BY ").append(getGroupBy());
completeQueryWithoutLimit.append(" GROUP BY ").append(getGroupBy());
}
if(!Util.isEmpty(getOrderBy())) {

// Add Order By
if(!Util.isEmpty(getOrderBy(), true)) {
completeQuery.append(" ORDER BY ").append(getOrderBy());
completeQueryWithoutLimit.append(" ORDER BY ").append(getOrderBy());
}

withCompleteQueryCount(completeQueryWithoutLimit.toString());
withCompleteQuery(completeQuery.toString());

return this;
}

Expand Down
26 changes: 21 additions & 5 deletions src/main/java/org/spin/report_engine/service/ReportBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,11 @@ public class ReportBuilder {
private ReportBuilder() {
conditions = new ArrayList<Filter>();
}


public List<Filter> getFilters() {
return this.conditions;
}

public ReportBuilder withFilters(List<Filter> filters) {
this.conditions = filters;
return this;
Expand All @@ -88,7 +92,7 @@ public ReportBuilder withParameter(String key, Object value) {
condition.put(Filter.VALUES, value);
Filter filter = new Filter(condition);
filter.setColumnName(key);
conditions.add(filter);
this.conditions.add(filter);
return this;
}

Expand Down Expand Up @@ -172,9 +176,19 @@ private ReportInfo get(String transactionName) {
Language language = Language.getLoginLanguage();
MPrintFormat printFormat = new MPrintFormat(Env.getCtx(), getPrintFormatId(), null);
PrintFormat format = PrintFormat.newInstance(printFormat);
QueryDefinition queryDefinition = format.getQuery().withConditions(conditions).withInstanceId(getInstanceId()).withLimit(limit, offset).buildQuery();
QueryDefinition queryDefinition = format.getQuery()
.withConditions(conditions)
.withInstanceId(getInstanceId())
.withLimit(limit, offset)
.buildQuery()
;
int count = CountUtil.countRecords(queryDefinition.getCompleteQueryCount(), format.getTableName(), queryDefinition.getParameters());
ReportInfo reportInfo = ReportInfo.newInstance(format, queryDefinition).withReportViewId(getReportViewId()).withInstanceId(getInstanceId()).withRecordCount(count).withSummary(isSummary());
ReportInfo reportInfo = ReportInfo.newInstance(format, queryDefinition)
.withReportViewId(getReportViewId())
.withInstanceId(getInstanceId())
.withRecordCount(count)
.withSummary(isSummary())
;
DB.runResultSet(transactionName, queryDefinition.getCompleteQuery(), queryDefinition.getParameters(), resulset -> {
while (resulset.next()) {
format.getItems().forEach(item -> {
Expand Down Expand Up @@ -263,7 +277,9 @@ private MPInstance generateProcessInstance() {
processInstance.setName(process.get_Translation(I_AD_Process.COLUMNNAME_Name));
processInstance.setRecord_ID(getRecordId());
processInstance.saveEx();
withInstanceId(processInstance.getAD_PInstance_ID());
if (!Util.isEmpty(process.getClassname(), true)) {
withInstanceId(processInstance.getAD_PInstance_ID());
}
// Add Parameters
AtomicInteger sequence = new AtomicInteger(0);
conditions.forEach(filter -> {
Expand Down
7 changes: 5 additions & 2 deletions src/main/java/org/spin/report_engine/service/Service.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
import org.spin.report_engine.format.QueryDefinition;
import org.spin.service.grpc.authentication.SessionManager;
import org.spin.service.grpc.util.db.LimitUtil;
import org.spin.service.grpc.util.query.Filter;
import org.spin.service.grpc.util.query.FilterManager;
import org.spin.service.grpc.util.value.ValueManager;

Expand Down Expand Up @@ -143,8 +144,10 @@ public static Report.Builder getReport(GetReportRequest request) {
int offset = (pageNumber - 1) * limit;
ReportBuilder reportBuilder = ReportBuilder.newInstance().withReportId(request.getReportId());
if(!Util.isEmpty(request.getFilters())) {
reportBuilder.withFilters(FilterManager.newInstance(request.getFilters())
.getConditions());
List<Filter> conditionsList = FilterManager.newInstance(request.getFilters())
.getConditions()
;
reportBuilder.withFilters(conditionsList);
}
reportBuilder.withPrintFormatId(request.getPrintFormatId()).withReportViewId(request.getReportViewId());
reportBuilder.withSummary(request.getIsSummary());
Expand Down

0 comments on commit bf69961

Please sign in to comment.