22using System . Text ;
33using 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>
59namespace 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}
0 commit comments