Skip to content

Commit

Permalink
Merge pull request #41 from gcheng/dev-bookmark
Browse files Browse the repository at this point in the history
Making filter immutable, add filter to TableQuery, Rename RawStringFilte...
  • Loading branch information
Albert Cheng committed Apr 9, 2012
2 parents 640093e + 4d82414 commit 9cafac5
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,13 +75,12 @@
import com.microsoft.windowsazure.services.table.models.GetServicePropertiesResult;
import com.microsoft.windowsazure.services.table.models.GetTableResult;
import com.microsoft.windowsazure.services.table.models.InsertEntityResult;
import com.microsoft.windowsazure.services.table.models.LitteralFilter;
import com.microsoft.windowsazure.services.table.models.Query;
import com.microsoft.windowsazure.services.table.models.PropertyNameFilter;
import com.microsoft.windowsazure.services.table.models.QueryEntitiesOptions;
import com.microsoft.windowsazure.services.table.models.QueryEntitiesResult;
import com.microsoft.windowsazure.services.table.models.QueryStringFilter;
import com.microsoft.windowsazure.services.table.models.QueryTablesOptions;
import com.microsoft.windowsazure.services.table.models.QueryTablesResult;
import com.microsoft.windowsazure.services.table.models.RawStringFilter;
import com.microsoft.windowsazure.services.table.models.ServiceProperties;
import com.microsoft.windowsazure.services.table.models.TableServiceOptions;
import com.microsoft.windowsazure.services.table.models.UnaryFilter;
Expand Down Expand Up @@ -185,26 +184,29 @@ private WebResource addOptionalQueryParam(WebResource webResource, String key, O
return PipelineHelpers.addOptionalQueryParam(webResource, key, value);
}

private WebResource addOptionalQuery(WebResource webResource, Query query) {
if (query == null)
private WebResource addOptionalQueryEntitiesOptions(WebResource webResource,
QueryEntitiesOptions queryEntitiesOptions) {
if (queryEntitiesOptions == null)
return webResource;

if (query.getSelectFields() != null && query.getSelectFields().size() > 0) {
if (queryEntitiesOptions.getSelectFields() != null && queryEntitiesOptions.getSelectFields().size() > 0) {
webResource = addOptionalQueryParam(webResource, "$select",
CommaStringBuilder.join(encodeODataURIValues(query.getSelectFields())));
CommaStringBuilder.join(encodeODataURIValues(queryEntitiesOptions.getSelectFields())));
}

if (query.getTop() != null) {
webResource = addOptionalQueryParam(webResource, "$top", encodeODataURIValue(query.getTop().toString()));
if (queryEntitiesOptions.getTop() != null) {
webResource = addOptionalQueryParam(webResource, "$top", encodeODataURIValue(queryEntitiesOptions.getTop()
.toString()));
}

if (query.getFilter() != null) {
webResource = addOptionalQueryParam(webResource, "$filter", buildFilterExpression(query.getFilter()));
if (queryEntitiesOptions.getFilter() != null) {
webResource = addOptionalQueryParam(webResource, "$filter",
buildFilterExpression(queryEntitiesOptions.getFilter()));
}

if (query.getOrderByFields() != null) {
if (queryEntitiesOptions.getOrderByFields() != null) {
webResource = addOptionalQueryParam(webResource, "$orderby",
CommaStringBuilder.join(encodeODataURIValues(query.getOrderByFields())));
CommaStringBuilder.join(encodeODataURIValues(queryEntitiesOptions.getOrderByFields())));
}

return webResource;
Expand All @@ -220,8 +222,8 @@ private void buildFilterExpression(Filter filter, StringBuilder sb) {
if (filter == null)
return;

if (filter instanceof LitteralFilter) {
sb.append(((LitteralFilter) filter).getLitteral());
if (filter instanceof PropertyNameFilter) {
sb.append(((PropertyNameFilter) filter).getPropertyName());
}
else if (filter instanceof ConstantFilter) {
Object value = ((ConstantFilter) filter).getValue();
Expand Down Expand Up @@ -285,8 +287,8 @@ else if (filter instanceof BinaryFilter) {
buildFilterExpression(((BinaryFilter) filter).getRight(), sb);
sb.append(")");
}
else if (filter instanceof RawStringFilter) {
sb.append(((RawStringFilter) filter).getRawString());
else if (filter instanceof QueryStringFilter) {
sb.append(((QueryStringFilter) filter).getQueryString());
}
}

Expand Down Expand Up @@ -383,19 +385,25 @@ public QueryTablesResult queryTables() throws ServiceException {

@Override
public QueryTablesResult queryTables(QueryTablesOptions options) throws ServiceException {
Query query = new Query();
Filter queryFilter = options.getFilter();
String nextTableName = options.getNextTableName();
String prefix = options.getPrefix();

if (prefix != null) {
// Append Max char to end '{' is 1 + 'z' in AsciiTable ==> upperBound is prefix + '{'
Filter prefixFilter = Filter.and(Filter.ge(Filter.litteral("TableName"), Filter.constant(prefix)),
Filter.le(Filter.litteral("TableName"), Filter.constant(prefix + "{")));
query.setFilter(prefixFilter);
Filter prefixFilter = Filter.and(Filter.ge(Filter.propertyName("TableName"), Filter.constant(prefix)),
Filter.le(Filter.propertyName("TableName"), Filter.constant(prefix + "{")));

if (queryFilter == null) {
queryFilter = prefixFilter;
}
else {
queryFilter = Filter.and(queryFilter, prefixFilter);
}
}

WebResource webResource = getResource(options).path("Tables");
webResource = addOptionalQuery(webResource, query);
webResource = addOptionalQueryParam(webResource, "$filter", buildFilterExpression(queryFilter));
webResource = addOptionalQueryParam(webResource, "NextTableName", nextTableName);

WebResource.Builder builder = webResource.getRequestBuilder();
Expand Down Expand Up @@ -613,7 +621,7 @@ public QueryEntitiesResult queryEntities(String table, QueryEntitiesOptions opti
options = new QueryEntitiesOptions();

WebResource webResource = getResource(options).path(table);
webResource = addOptionalQuery(webResource, options.getQuery());
webResource = addOptionalQueryEntitiesOptions(webResource, options);
webResource = addOptionalQueryParam(webResource, "NextPartitionKey",
encodeODataURIValue(options.getNextPartitionKey()));
webResource = addOptionalQueryParam(webResource, "NextRowKey", encodeODataURIValue(options.getNextRowKey()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,26 @@
package com.microsoft.windowsazure.services.table.models;

public class BinaryFilter extends Filter {
private String operator;
private Filter left;
private Filter right;
private final String operator;
private final Filter left;
private final Filter right;

public String getOperator() {
return operator;
public BinaryFilter(Filter left, String operator, Filter right) {
this.left = left;
this.operator = operator;
this.right = right;
}

public BinaryFilter setOperator(String operator) {
this.operator = operator;
return this;
public String getOperator() {
return operator;
}

public Filter getLeft() {
return left;
}

public BinaryFilter setLeft(Filter left) {
this.left = left;
return this;
}

public Filter getRight() {
return right;
}

public BinaryFilter setRight(Filter right) {
this.right = right;
return this;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,13 @@
package com.microsoft.windowsazure.services.table.models;

public class ConstantFilter extends Filter {
private Object value;
private final Object value;

public Object getValue() {
return value;
public ConstantFilter(Object value) {
this.value = value;
}

public ConstantFilter setValue(Object value) {
this.value = value;
return this;
public Object getValue() {
return value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,50 +16,50 @@

public class Filter {
public static UnaryFilter not(Filter operand) {
return new UnaryFilter().setOperator("not").setOperand(operand);
return new UnaryFilter("not", operand);
}

public static BinaryFilter and(Filter left, Filter right) {
return new BinaryFilter().setOperator("and").setLeft(left).setRight(right);
return new BinaryFilter(left, "and", right);
}

public static BinaryFilter or(Filter left, Filter right) {
return new BinaryFilter().setOperator("or").setLeft(left).setRight(right);
return new BinaryFilter(left, "or", right);
}

public static BinaryFilter eq(Filter left, Filter right) {
return new BinaryFilter().setOperator("eq").setLeft(left).setRight(right);
return new BinaryFilter(left, "eq", right);
}

public static BinaryFilter ne(Filter left, Filter right) {
return new BinaryFilter().setOperator("ne").setLeft(left).setRight(right);
return new BinaryFilter(left, "ne", right);
}

public static BinaryFilter ge(Filter left, Filter right) {
return new BinaryFilter().setOperator("ge").setLeft(left).setRight(right);
return new BinaryFilter(left, "ge", right);
}

public static BinaryFilter gt(Filter left, Filter right) {
return new BinaryFilter().setOperator("gt").setLeft(left).setRight(right);
return new BinaryFilter(left, "gt", right);
}

public static BinaryFilter lt(Filter left, Filter right) {
return new BinaryFilter().setOperator("lt").setLeft(left).setRight(right);
return new BinaryFilter(left, "lt", right);
}

public static BinaryFilter le(Filter left, Filter right) {
return new BinaryFilter().setOperator("le").setLeft(left).setRight(right);
return new BinaryFilter(left, "le", right);
}

public static ConstantFilter constant(Object value) {
return new ConstantFilter().setValue(value);
return new ConstantFilter(value);
}

public static LitteralFilter litteral(String value) {
return new LitteralFilter().setLitteral(value);
public static PropertyNameFilter propertyName(String value) {
return new PropertyNameFilter(value);
}

public static RawStringFilter rawString(String value) {
return new RawStringFilter().setRawString(value);
public static QueryStringFilter queryString(String value) {
return new QueryStringFilter(value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,15 @@
*/
package com.microsoft.windowsazure.services.table.models;

public class RawStringFilter extends Filter {
private String rawString;
public class PropertyNameFilter extends Filter {
private final String propertyName;

public String getRawString() {
return rawString;
public PropertyNameFilter(String propertyName) {
this.propertyName = propertyName;
}

public RawStringFilter setRawString(String rawString) {
this.rawString = rawString;
return this;
public String getPropertyName() {
return propertyName;
}

}

This file was deleted.

Loading

0 comments on commit 9cafac5

Please sign in to comment.