Skip to content

Commit cbed358

Browse files
committed
Query support. Resolves #iss25
1 parent 7be3703 commit cbed358

File tree

9 files changed

+353
-51
lines changed

9 files changed

+353
-51
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
using System.Collections;
2+
using System.Collections.Generic;
3+
using System;
4+
using RestSharp;
5+
6+
namespace Unity3dAzure.AppServices
7+
{
8+
[CLSCompliant(false)]
9+
public interface INestedResults
10+
{
11+
}
12+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace Unity3dAzure.AppServices
5+
{
6+
[CLSCompliant(false)]
7+
[Serializable]
8+
public class NestedResults<T> : INestedResults
9+
{
10+
public uint count { get; set; }
11+
public List<T> results { get; set; }
12+
}
13+
}

Assets/AppServices/table/IAzureMobileServiceTable.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public interface IAzureMobileServiceTable
4141
/// https://msdn.microsoft.com/en-us/library/azure/jj677199.aspx
4242
/// </summary>
4343
void Query<T>(CustomQuery query, Action<IRestResponse<List<T>>> callback = null) where T : new();
44+
void NestedQuery<T>(CustomQuery query, Action<IRestResponse<T>> callback = null) where T : INestedResults, new();
4445

4546
/// <summary>
4647
/// Get an item's data using id property.

Assets/AppServices/table/MobileServiceTable.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,20 @@ public override string ToString()
4646
{
4747
string uri = string.Format("{0}{1}{2}", URI_TABLES, _name, query);
4848
ZumoRequest request = new ZumoRequest(_client, uri, Method.GET);
49-
Debug.Log( "Query Request: " + uri );
50-
_client.ExecuteAsync<List<T>>(request, callback);
49+
Debug.Log( "Query Request: " + uri );
50+
_client.ExecuteAsync<List<T>> (request, callback);
5151
}
52+
53+
public void NestedQuery<T>(CustomQuery query, Action<IRestResponse<T>> callback = null) where T : INestedResults, new()
54+
{
55+
string queryResults = query.ToString ();
56+
string q = queryResults.Length > 0 ? "&" : "?";
57+
queryResults += string.Format("{0}$inlinecount=allpages", q);
58+
string uri = string.Format("{0}{1}{2}", URI_TABLES, _name, queryResults);
59+
ZumoRequest request = new ZumoRequest(_client, uri, Method.GET);
60+
Debug.Log( "Query Request: " + uri +" Query:"+ queryResults );
61+
_client.ExecuteAsync<T> (request, callback);
62+
}
5263

5364
public void Update<T>(T item, Action<IRestResponse<T>> callback = null) where T : new()
5465
{

Assets/AppServices/table/query/CustomQuery.cs

Lines changed: 86 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,46 @@
22
using System.Text;
33
using UnityEngine;
44

5+
/// <summary>
6+
/// Query records operation https://msdn.microsoft.com/en-us/library/azure/jj677199.aspx
7+
/// There is a maximum of 50 records returned in a query - use top and skip params to return additional pages of results.
8+
/// </summary>
59
namespace Unity3dAzure.AppServices
610
{
11+
[Flags]
12+
public enum MobileServiceSystemProperty
13+
{
14+
nil = 0x0,
15+
createdAt = 0x1,
16+
updatedAt = 0x2,
17+
version = 0x4,
18+
deleted = 0x8
19+
}
20+
721
[CLSCompliant(false)]
822
public class CustomQuery
923
{
24+
// query option parameters defined by the Open Data Protocol (OData)
1025
private string _filter;
1126
private string _orderBy;
12-
private UInt32 _top;
27+
private uint _top;
28+
private uint _skip;
29+
// private bool _inlineCount; // NB: removed this as it changes the data model which is linked to rest
30+
private string _select;
31+
// other params
32+
private MobileServiceSystemProperty _systemProperties;
33+
private bool _includeDeleted;
1334

14-
public CustomQuery(string filter, string orderBy=null, UInt32 top=0)
35+
public CustomQuery(string filter, string orderBy=null, uint top=0, uint skip=0, string select=null, MobileServiceSystemProperty systemProperties=MobileServiceSystemProperty.nil, bool includeDeleted=false)
1536
{
16-
_filter = filter; // return only rows that satisty the specified filter
17-
_orderBy = orderBy; // sort column by 'desc' or 'asc' order
18-
_top = top; // return the top n entities for any query,
37+
_filter = filter; // return only rows that satisty the specified filter predicate
38+
_orderBy = orderBy; // sort column by one or more columns: order can be specified in 'desc' or 'asc' order ('asc' is default)
39+
_top = top; // return the top n entities for any query
40+
_skip = skip; // the n of records to skip (used for paging results)
41+
//_inlineCount = inlineCount; // returns count of all items (without paging applied)
42+
_select = select; // defines new projection of data by specifying the columns
43+
_systemProperties = systemProperties; // list of system properties to be included in the response
44+
_includeDeleted = includeDeleted; // if table has soft delete enabled then deleted records will be included in the results
1945
}
2046

2147
public static CustomQuery OrderBy(string orderBy) {
@@ -26,24 +52,72 @@ public override string ToString()
2652
{
2753
string queryString = "";
2854
string q = "?";
29-
if (!string.IsNullOrEmpty(_filter)){
30-
queryString += string.Format("{0}$filter=({1})", q, encode(_filter));
55+
if (!string.IsNullOrEmpty(_filter)) {
56+
queryString += string.Format("{0}$filter=({1})", q, _filter);
3157
q = "&";
3258
}
33-
if (!string.IsNullOrEmpty(_orderBy)){
34-
queryString += string.Format("{0}$orderby={1}", q, encode(_orderBy));
59+
if (!string.IsNullOrEmpty(_orderBy)) {
60+
queryString += string.Format("{0}$orderby={1}", q, _orderBy);
3561
q = "&";
3662
}
3763
if (_top > 0) {
3864
queryString += string.Format("{0}$top={1}", q, _top.ToString());
65+
q = "&";
66+
}
67+
if (_skip > 0) {
68+
queryString += string.Format("{0}$skip={1}", q, _skip.ToString());
69+
q = "&";
70+
}
71+
// if (_inlineCount) {
72+
// queryString += string.Format("{0}$inlinecount=allpages", q);
73+
// q = "&";
74+
// }
75+
if (!string.IsNullOrEmpty(_select)) {
76+
queryString += string.Format("{0}$select={1}", q, _select);
77+
q = "&";
78+
}
79+
if (_systemProperties!=MobileServiceSystemProperty.nil) {
80+
// NB: setting __systemproperties param doesn't seem to do anything different as these properties are all included by default, but we can append values to the 'select' param.
81+
if (!string.IsNullOrEmpty (_select)) {
82+
queryString += string.Format (",{0}", SystemPropertiesValues (_systemProperties));
83+
}
84+
queryString += string.Format("{0}__systemproperties={1}", q, SystemPropertiesValues(_systemProperties));
85+
q = "&";
86+
}
87+
if (_includeDeleted) {
88+
queryString += string.Format("{0}__includeDeleted=true", q);
3989
}
40-
return queryString;
90+
return EscapeURL(queryString);
4191
}
4292

43-
private string encode(string url)
93+
private string EscapeURL(string query)
4494
{
45-
return WWW.EscapeURL(url.Replace("+", "%20")); // NB: replace space with '%20' and not '+'
95+
string q = WWW.EscapeURL (query);
96+
StringBuilder sb = new StringBuilder (q);
97+
sb.Replace ("+", "%20"); // NB: replace space with '%20' instead of '+'
98+
// keep query params: ?&=$
99+
sb.Replace ("%3f", "?");
100+
sb.Replace ("%26", "&");
101+
sb.Replace ("%3d", "=");
102+
sb.Replace ("%24", "$");
103+
return sb.ToString();
46104
}
47105

106+
private string SystemPropertiesValues(MobileServiceSystemProperty systemProperties)
107+
{
108+
if (systemProperties == MobileServiceSystemProperty.nil) {
109+
return "";
110+
}
111+
return systemProperties.ToString().Replace(", ","%2C"); // remove spaces from string and escape comma
112+
}
113+
114+
/// <summary>
115+
/// If _inlineCount is true, then results will be nested inside "results" array
116+
/// </summary>
117+
// public bool IsNestedResults()
118+
// {
119+
// return _inlineCount;
120+
// }
121+
48122
}
49123
}

Assets/Scenes/HighscoresDemo.unity

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3090,8 +3090,8 @@ MonoBehaviour:
30903090
m_TargetGraphic: {fileID: 1064423870}
30913091
m_HandleRect: {fileID: 1064423869}
30923092
m_Direction: 0
3093-
m_Value: 0
3094-
m_Size: 1
3093+
m_Value: 1
3094+
m_Size: 0.9999999
30953095
m_NumberOfSteps: 0
30963096
m_OnValueChanged:
30973097
m_PersistentCalls:

0 commit comments

Comments
 (0)