* Tests the building of the SQL query WHERE clause from the Gviz query.
*/
public void testBuildWhereClause() {
Query query = new Query();
QueryFilter queryFilter1 = new ColumnColumnFilter(new SimpleColumn("ID"),
new SimpleColumn("Salary"), ComparisonFilter.Operator.EQ);
QueryFilter queryFilter2 = new ColumnValueFilter(new SimpleColumn("ID"),
new NumberValue(1), ComparisonFilter.Operator.GE);
QueryFilter queryFilter3 = new ColumnValueFilter(new SimpleColumn("Fname"),
new TextValue("Mi"), ComparisonFilter.Operator.STARTS_WITH);
QueryFilter queryFilter4 = new ColumnValueFilter(new SimpleColumn("Lname"),
new TextValue("SH"), ComparisonFilter.Operator.CONTAINS);
QueryFilter queryFilter5 = new ColumnValueFilter(new SimpleColumn("Lname"),
new TextValue("tz"), ComparisonFilter.Operator.ENDS_WITH);
List<QueryFilter> subFiltersList1 = Lists.newArrayList();
subFiltersList1.add(queryFilter1);
subFiltersList1.add(queryFilter2);
QueryFilter queryCompoundFilter1 =
new CompoundFilter(CompoundFilter.LogicalOperator.AND, subFiltersList1);
List<QueryFilter> subFiltersList2 = Lists.newArrayList();
subFiltersList2.add(queryFilter3);
subFiltersList2.add(queryFilter4);
subFiltersList2.add(queryFilter5);
QueryFilter queryCompoundFilter2 =
new CompoundFilter(CompoundFilter.LogicalOperator.AND, subFiltersList2);
List<QueryFilter> subFiltersList3 = Lists.newArrayList();
subFiltersList3.add(queryCompoundFilter1);
subFiltersList3.add(queryCompoundFilter2);
QueryFilter queryCompoundFilter3 =
new CompoundFilter(CompoundFilter.LogicalOperator.OR, subFiltersList3);
query.setFilter(queryCompoundFilter3);
StrBuilder queryStringBuilder = new StrBuilder();
SqlDataSourceHelper.appendWhereClause(query, queryStringBuilder);
assertEquals(queryStringBuilder.toString(),
"WHERE (((`ID`=`Salary`) AND (`ID`>=1.0)) OR ((`Fname` LIKE \"Mi%\") "
+ "AND (`Lname` LIKE \"%SH%\") AND (`Lname` LIKE \"%tz\"))) ",
queryStringBuilder.toString());
// Check empty compound filters.
List<QueryFilter> subFiltersList4 = Lists.newArrayList();
QueryFilter queryCompoundFilter4 =
new CompoundFilter(CompoundFilter.LogicalOperator.OR, subFiltersList4);
query.setFilter(queryCompoundFilter4);
queryStringBuilder = new StrBuilder();
SqlDataSourceHelper.appendWhereClause(query, queryStringBuilder);
assertEquals(queryStringBuilder.toString(), "WHERE false ");
QueryFilter queryCompoundFilter5 =
new CompoundFilter(CompoundFilter.LogicalOperator.AND, subFiltersList4);
query.setFilter(queryCompoundFilter5);
queryStringBuilder = new StrBuilder();
SqlDataSourceHelper.appendWhereClause(query, queryStringBuilder);
assertEquals(queryStringBuilder.toString(), "WHERE true ");
// Check compound filter with one sub-filter.
List<QueryFilter> subFiltersList6 = Lists.newArrayList(
(QueryFilter) new ColumnColumnFilter(new SimpleColumn("ID"),
new SimpleColumn("Salary"), ComparisonFilter.Operator.EQ));
QueryFilter queryCompoundFilter6 =
new CompoundFilter(CompoundFilter.LogicalOperator.OR, subFiltersList6);
query.setFilter(queryCompoundFilter6);
queryStringBuilder = new StrBuilder();
SqlDataSourceHelper.appendWhereClause(query, queryStringBuilder);
assertEquals(queryStringBuilder.toString(), "WHERE ((`ID`=`Salary`)) ",