Skip to content

Commit 2733153

Browse files
committed
Implements Sql query parameters.
1 parent cc32cb0 commit 2733153

11 files changed

+737
-44
lines changed

src/com/microsoft/azure/documentdb/Constants.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ static class Properties {
105105

106106
static class ResourceKeys {
107107
static final String ATTACHMENTS = "Attachments";
108+
static final String CONFLICTS = "Conflicts";
108109
static final String DATABASES = "Databases";
109110
static final String DOCUMENTS = "Documents";
110111
static final String DOCUMENT_COLLECTIONS = "DocumentCollections";

src/com/microsoft/azure/documentdb/DocumentClient.java

Lines changed: 296 additions & 17 deletions
Large diffs are not rendered by default.

src/com/microsoft/azure/documentdb/DocumentServiceRequest.java

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,47 @@ public static DocumentServiceRequest create(ResourceType resourceType,
103103
}
104104
}
105105

106+
/**
107+
* Creates a DocumentServiceRequest with a query.
108+
*
109+
* @param resourceType the resource type.
110+
* @param relativePath the relative URI path.
111+
* @param query the query.
112+
* @param headers the request headers.
113+
* @return the created document service request.
114+
*/
115+
public static DocumentServiceRequest create(ResourceType resourceType,
116+
String relativePath,
117+
SqlQuerySpec querySpec,
118+
DocumentClient.QueryCompatibilityMode queryCompatibilityMode,
119+
Map<String, String> headers) {
120+
String queryText;
121+
switch (queryCompatibilityMode) {
122+
case SqlQuery:
123+
if (querySpec.getParameters() != null && querySpec.getParameters().size() > 0) {
124+
throw new IllegalArgumentException(
125+
String.format("Unsupported argument in query compatibility mode '{%s}'",
126+
queryCompatibilityMode.name()));
127+
}
128+
129+
queryText = querySpec.getQueryText();
130+
break;
131+
132+
case Default:
133+
case Query:
134+
default:
135+
queryText = querySpec.toString();
136+
break;
137+
}
138+
139+
try {
140+
HttpEntity body = new StringEntity(queryText);
141+
return new DocumentServiceRequest(resourceType, relativePath, body, headers);
142+
} catch (UnsupportedEncodingException e) {
143+
throw new IllegalArgumentException("Failed to get HttpEntity from resource.", e);
144+
}
145+
}
146+
106147
/**
107148
* Creates a DocumentServiceRequest without body.
108149
*

src/com/microsoft/azure/documentdb/DocumentServiceResponse.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ public InputStream getContentStream() {
136136
public static <T extends Resource> String getResourceKey(Class<T> c) {
137137
if (c.equals(Attachment.class)) {
138138
return Constants.ResourceKeys.ATTACHMENTS;
139+
} else if (c.equals(Conflict.class)) {
140+
return Constants.ResourceKeys.CONFLICTS;
139141
} else if (c.equals(Database.class)) {
140142
return Constants.ResourceKeys.DATABASES;
141143
} else if (c.equals(Document.class)) {

src/com/microsoft/azure/documentdb/GatewayProxy.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,12 @@ final class GatewayProxy {
4545
private HttpClient httpClient;
4646
private HttpClient mediaHttpClient;
4747
private PoolingClientConnectionManager connectionManager;
48+
private DocumentClient.QueryCompatibilityMode queryCompatibilityMode;
4849

4950
public GatewayProxy(URI serviceEndpoint,
5051
ConnectionPolicy connectionPolicy,
5152
ConsistencyLevel consistencyLevel,
53+
DocumentClient.QueryCompatibilityMode queryCompatibilityMode,
5254
String masterKey,
5355
Map<String, String> resourceTokens) {
5456
this.serviceEndpoint = serviceEndpoint;
@@ -66,6 +68,7 @@ public GatewayProxy(URI serviceEndpoint,
6668
}
6769

6870
this.connectionPolicy = connectionPolicy;
71+
this.queryCompatibilityMode = queryCompatibilityMode;
6972
this.masterKey = masterKey;
7073
this.resourceTokens = resourceTokens;
7174

@@ -109,8 +112,20 @@ public DocumentServiceResponse doReadFeed(DocumentServiceRequest request)
109112
public DocumentServiceResponse doSQLQuery(DocumentServiceRequest request)
110113
throws DocumentClientException {
111114
request.getHeaders().put(HttpConstants.HttpHeaders.IS_QUERY, "true");
112-
request.getHeaders().put(HttpConstants.HttpHeaders.CONTENT_TYPE,
113-
RuntimeConstants.MediaTypes.SQL);
115+
116+
switch (this.queryCompatibilityMode) {
117+
case SqlQuery:
118+
request.getHeaders().put(HttpConstants.HttpHeaders.CONTENT_TYPE,
119+
RuntimeConstants.MediaTypes.SQL);
120+
break;
121+
case Default:
122+
case Query:
123+
default:
124+
request.getHeaders().put(HttpConstants.HttpHeaders.CONTENT_TYPE,
125+
RuntimeConstants.MediaTypes.QUERY_JSON);
126+
break;
127+
}
128+
114129
return this.performPostRequest(request);
115130
}
116131

src/com/microsoft/azure/documentdb/JsonSerializable.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
package com.microsoft.azure.documentdb;
22

33
import java.io.IOException;
4+
import java.lang.reflect.Array;
45
import java.lang.reflect.InvocationTargetException;
56
import java.lang.reflect.Modifier;
67
import java.util.ArrayList;
8+
import java.util.Arrays;
79
import java.util.Collection;
810
import java.util.Iterator;
911
import java.util.List;
@@ -106,6 +108,21 @@ public void remove(String propertyName) {
106108
this.propertyBag.remove(propertyName);
107109
}
108110

111+
private static Object[] convertToObjectArray(Object array) {
112+
Class<?> ofArray = array.getClass().getComponentType();
113+
if (ofArray.isPrimitive()) {
114+
List<Object> ar = new ArrayList<Object>();
115+
int length = Array.getLength(array);
116+
for (int i = 0; i < length; i++) {
117+
ar.add(Array.get(array, i));
118+
}
119+
return ar.toArray();
120+
}
121+
else {
122+
return (Object[]) array;
123+
}
124+
}
125+
109126
/**
110127
* Sets the value of a property.
111128
*
@@ -123,6 +140,12 @@ public <T extends Object> void set(String propertyName, T value) {
123140
JSONArray jsonArray = new JSONArray();
124141
this.internalSetCollection(propertyName, (Collection)value, jsonArray);
125142
this.propertyBag.put(propertyName, jsonArray);
143+
} else if (value.getClass().isArray()) {
144+
// Array.
145+
JSONArray jsonArray = new JSONArray();
146+
this.internalSetCollection(propertyName,
147+
Arrays.asList(JsonSerializable.convertToObjectArray(value)), jsonArray);
148+
this.propertyBag.put(propertyName, jsonArray);
126149
} else if (value instanceof Number || value instanceof Boolean || value instanceof String ||
127150
value instanceof JSONObject) {
128151
// JSONObject, number (includes int, float, double etc), boolean, and string.

src/com/microsoft/azure/documentdb/RuntimeConstants.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,15 @@ class RuntimeConstants {
44
static class MediaTypes {
55
// http://www.iana.org/assignments/media-types/media-types.xhtml
66
public static final String ANY = "*/*";
7-
public static final String JSON = "application/json";
8-
public static final String XML = "application/xml";
9-
public static final String OCTET_STREAM = "application/octet-stream";
10-
public static final String SQL = "application/sql";
117
public static final String IMAGE_JPEG = "image/jpeg";
128
public static final String IMAGE_PNG = "image/png";
9+
public static final String JAVA_SCRIPT = "application/x-javascript";
10+
public static final String JSON = "application/json";
11+
public static final String OCTET_STREAM = "application/octet-stream";
12+
public static final String QUERY_JSON = "application/query+json";
13+
public static final String SQL = "application/sql";
1314
public static final String TEXT_HTML = "text/html";
1415
public static final String TEXT_PLAIN = "text/plain";
15-
public static final String JAVA_SCRIPT = "application/x-javascript";
16+
public static final String XML = "application/xml";
1617
}
1718
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.microsoft.azure.documentdb;
2+
3+
public final class SqlParameter extends JsonSerializable {
4+
5+
6+
/**
7+
* Initializes a new instance of the SqlParameter class.
8+
*/
9+
public SqlParameter() {
10+
super();
11+
}
12+
13+
/**
14+
* Initializes a new instance of the SqlParameter class with the name and value of the parameter.
15+
*
16+
* @param name the name of the parameter.
17+
* @param value the value of the parameter.
18+
*/
19+
public SqlParameter(String name, Object value) {
20+
super();
21+
this.setName(name);
22+
this.setValue(value);
23+
}
24+
25+
/**
26+
* Gets the name of the parameter.
27+
*
28+
* @return the name of the parameter.
29+
*/
30+
public String getName() {
31+
return super.getString("name");
32+
}
33+
34+
/**
35+
* Sets the name of the parameter.
36+
*
37+
* @param name the name of the parameter.
38+
*/
39+
public void setName(String name) {
40+
super.set("name", name);
41+
}
42+
43+
/**
44+
* Gets the value of the parameter.
45+
*
46+
* @param c the class of the parameter value.
47+
* @return the value of the parameter.
48+
*/
49+
public <T extends Object> Object getValue(Class<T> c) {
50+
return super.getObject("value", c);
51+
}
52+
53+
/**
54+
* Sets the value of the parameter.
55+
*
56+
* @param value the value of the parameter.
57+
*/
58+
public void setValue(Object value) {
59+
super.set("value", value);
60+
}
61+
}
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
package com.microsoft.azure.documentdb;
2+
3+
import java.util.ArrayList;
4+
import java.util.Arrays;
5+
import java.util.Collection;
6+
import java.util.Iterator;
7+
import java.util.List;
8+
9+
public final class SqlParameterCollection implements Collection<SqlParameter> {
10+
11+
private List<SqlParameter> parameters;
12+
13+
/**
14+
* Initializes a new instance of the SqlParameterCollection class.
15+
*/
16+
public SqlParameterCollection() {
17+
this.parameters = new ArrayList<SqlParameter>();
18+
}
19+
20+
/**
21+
* Initializes a new instance of the SqlParameterCollection class from an array of parameters.
22+
*
23+
* @param parameters the array of parameters.
24+
*/
25+
public SqlParameterCollection(SqlParameter... parameters) {
26+
if (parameters == null) {
27+
throw new IllegalArgumentException("parameters");
28+
}
29+
30+
this.parameters = Arrays.asList(parameters);
31+
}
32+
33+
/**
34+
* Initializes a new instance of the SqlParameterCollection class from a collection of parameters.
35+
*
36+
* @param parameters the collection of parameters.
37+
*/
38+
public SqlParameterCollection(Collection<SqlParameter> parameters) {
39+
if (parameters == null) {
40+
throw new IllegalArgumentException("parameters");
41+
}
42+
43+
this.parameters = new ArrayList<SqlParameter>(parameters);
44+
}
45+
46+
@Override
47+
public boolean add(SqlParameter parameter) {
48+
return this.parameters.add(parameter);
49+
}
50+
51+
@Override
52+
public boolean addAll(Collection<? extends SqlParameter> parameters) {
53+
return this.parameters.addAll(parameters);
54+
}
55+
56+
@Override
57+
public void clear() {
58+
this.parameters.clear();
59+
}
60+
61+
@Override
62+
public boolean contains(Object parameter) {
63+
return this.parameters.contains(parameter);
64+
}
65+
66+
@Override
67+
public boolean containsAll(Collection<?> parameters) {
68+
return this.parameters.containsAll(parameters);
69+
}
70+
71+
@Override
72+
public boolean isEmpty() {
73+
return this.parameters.isEmpty();
74+
}
75+
76+
@Override
77+
public Iterator<SqlParameter> iterator() {
78+
return this.parameters.iterator();
79+
}
80+
81+
@Override
82+
public boolean remove(Object parameter) {
83+
return this.parameters.remove(parameter);
84+
}
85+
86+
@Override
87+
public boolean removeAll(Collection<?> parameters) {
88+
return this.parameters.removeAll(parameters);
89+
}
90+
91+
@Override
92+
public boolean retainAll(Collection<?> parameters) {
93+
return this.parameters.retainAll(parameters);
94+
}
95+
96+
@Override
97+
public int size() {
98+
return this.parameters.size();
99+
}
100+
101+
@Override
102+
public Object[] toArray() {
103+
return this.parameters.toArray();
104+
}
105+
106+
@Override
107+
public <T> T[] toArray(T[] parameters) {
108+
return this.parameters.toArray(parameters);
109+
}
110+
}

0 commit comments

Comments
 (0)