Validated query builder
This commit is contained in:
parent
3657057e48
commit
a113390933
|
@ -117,10 +117,11 @@ public class Query extends PostgreSQLQuery {
|
||||||
return date;
|
return date;
|
||||||
}
|
}
|
||||||
|
|
||||||
//TODO need to check duplicated keys and put all values in OR
|
|
||||||
protected void addFilters() throws SQLException {
|
protected void addFilters() throws SQLException {
|
||||||
if(filters!=null && filters.size()>0) {
|
if(filters!=null && filters.size()>0) {
|
||||||
stringBuffer.append("(");
|
if(filters.size()>1) {
|
||||||
|
stringBuffer.append("(");
|
||||||
|
}
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for(String key : filters.keySet()) {
|
for(String key : filters.keySet()) {
|
||||||
if(first) {
|
if(first) {
|
||||||
|
@ -130,13 +131,17 @@ public class Query extends PostgreSQLQuery {
|
||||||
}
|
}
|
||||||
addOrConditions(key, filters.get(key));
|
addOrConditions(key, filters.get(key));
|
||||||
}
|
}
|
||||||
stringBuffer.append(")");
|
if(filters.size()>1) {
|
||||||
|
stringBuffer.append(")");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addOrConditions(String key, Collection<String> values) throws SQLException {
|
protected void addOrConditions(String key, Collection<String> values) throws SQLException {
|
||||||
if(values!=null && values.size()>0) {
|
if(values!=null && values.size()>0) {
|
||||||
stringBuffer.append("(");
|
if(values.size()>1) {
|
||||||
|
stringBuffer.append("(");
|
||||||
|
}
|
||||||
boolean first = true;
|
boolean first = true;
|
||||||
for(String value : values) {
|
for(String value : values) {
|
||||||
if(first) {
|
if(first) {
|
||||||
|
@ -148,7 +153,9 @@ public class Query extends PostgreSQLQuery {
|
||||||
stringBuffer.append("=");
|
stringBuffer.append("=");
|
||||||
appendValue(value);
|
appendValue(value);
|
||||||
}
|
}
|
||||||
stringBuffer.append(")");
|
if(values.size()>1) {
|
||||||
|
stringBuffer.append(")");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -251,7 +258,7 @@ public class Query extends PostgreSQLQuery {
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addTemporalConstraintToQuery() throws SQLException {
|
protected void addTemporalConstraintToQuery() throws SQLException {
|
||||||
stringBuffer.append(" WHERE ");
|
stringBuffer.append(" WHERE (");
|
||||||
String tableField = getTableField(AggregatedRecord.START_TIME);
|
String tableField = getTableField(AggregatedRecord.START_TIME);
|
||||||
stringBuffer.append(tableField);
|
stringBuffer.append(tableField);
|
||||||
stringBuffer.append(" > ");
|
stringBuffer.append(" > ");
|
||||||
|
@ -262,6 +269,7 @@ public class Query extends PostgreSQLQuery {
|
||||||
stringBuffer.append(tableField);
|
stringBuffer.append(tableField);
|
||||||
stringBuffer.append(" < ");
|
stringBuffer.append(" < ");
|
||||||
appendValue(temporalConstraint.getAlignedEndTime());
|
appendValue(temporalConstraint.getAlignedEndTime());
|
||||||
|
stringBuffer.append(")");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected void addDateGroupBy(boolean addNested) {
|
protected void addDateGroupBy(boolean addNested) {
|
||||||
|
|
|
@ -11,7 +11,6 @@ import java.util.Set;
|
||||||
import org.gcube.accounting.analytics.Filter;
|
import org.gcube.accounting.analytics.Filter;
|
||||||
import org.gcube.accounting.analytics.TemporalConstraint;
|
import org.gcube.accounting.analytics.TemporalConstraint;
|
||||||
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
|
import org.gcube.accounting.analytics.TemporalConstraint.AggregationMode;
|
||||||
import org.gcube.accounting.analytics.persistence.AccountingPersistenceBackendQuery;
|
|
||||||
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
import org.gcube.accounting.analytics.persistence.AccountingPersistenceQuery;
|
||||||
import org.gcube.accounting.datamodel.UsageRecord;
|
import org.gcube.accounting.datamodel.UsageRecord;
|
||||||
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
import org.gcube.accounting.datamodel.aggregation.AggregatedServiceUsageRecord;
|
||||||
|
@ -58,6 +57,8 @@ public class QueryTest extends ContextTest {
|
||||||
Set<Filter> filters = new HashSet<>();
|
Set<Filter> filters = new HashSet<>();
|
||||||
Filter filter = new Filter(UsageRecord.CONSUMER_ID, getRandomUser());
|
Filter filter = new Filter(UsageRecord.CONSUMER_ID, getRandomUser());
|
||||||
filters.add(filter);
|
filters.add(filter);
|
||||||
|
filter = new Filter(UsageRecord.CONSUMER_ID, getRandomUser());
|
||||||
|
filters.add(filter);
|
||||||
|
|
||||||
TemporalConstraint temporalConstraint = new TemporalConstraint(startTimeCalendar.getTimeInMillis(), entTimeCalendar.getTimeInMillis(), AggregationMode.MONTHLY);
|
TemporalConstraint temporalConstraint = new TemporalConstraint(startTimeCalendar.getTimeInMillis(), entTimeCalendar.getTimeInMillis(), AggregationMode.MONTHLY);
|
||||||
Query query = new Query(AggregatedServiceUsageRecord.class);
|
Query query = new Query(AggregatedServiceUsageRecord.class);
|
||||||
|
@ -67,9 +68,9 @@ public class QueryTest extends ContextTest {
|
||||||
logger.debug(ret);
|
logger.debug(ret);
|
||||||
|
|
||||||
|
|
||||||
String currentScope = AccountingPersistenceBackendQuery.getScopeToQuery();
|
|
||||||
Set<String> contexts = new HashSet<>();
|
Set<String> contexts = new HashSet<>();
|
||||||
contexts.add(currentScope);
|
contexts.add(DEFAULT_TEST_SCOPE);
|
||||||
|
contexts.add(ALTERNATIVE_TEST_SCOPE);
|
||||||
query.setContexts(contexts);
|
query.setContexts(contexts);
|
||||||
ret = query.getTimeSeriesQuery();
|
ret = query.getTimeSeriesQuery();
|
||||||
logger.debug(ret);
|
logger.debug(ret);
|
||||||
|
@ -89,7 +90,7 @@ public class QueryTest extends ContextTest {
|
||||||
@Test
|
@Test
|
||||||
public void testGetRecordQuery() throws Exception {
|
public void testGetRecordQuery() throws Exception {
|
||||||
Query query = new Query(AggregatedServiceUsageRecord.class);
|
Query query = new Query(AggregatedServiceUsageRecord.class);
|
||||||
query.setRecordId("fa573711-ceb6-44ba-9c83-bd47e0915b80");
|
query.setRecordId("7c3fcb31-6909-451d-bae0-633b3bec0c21");
|
||||||
String ret = query.getRecordQuery();
|
String ret = query.getRecordQuery();
|
||||||
logger.debug(ret);
|
logger.debug(ret);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue