|
1 | | -namespace Unity3dAzure.AppServices |
| 1 | +using System; |
| 2 | +using RestSharp.Contrib; |
| 3 | + |
| 4 | +namespace Unity3dAzure.AppServices |
2 | 5 | { |
| 6 | + [CLSCompliant(false)] |
3 | 7 | public class CustomQuery |
4 | 8 | { |
5 | 9 | private string _filter; |
6 | 10 | private string _orderBy; |
| 11 | + private UInt32 _top; |
7 | 12 |
|
8 | | - public CustomQuery(string filter, string orderBy=null) |
| 13 | + public CustomQuery(string filter, string orderBy=null, UInt32 top=0) |
9 | 14 | { |
10 | | - _filter = filter; |
11 | | - _orderBy = orderBy; |
| 15 | + _filter = filter; // return only rows that satisty the specified filter |
| 16 | + _orderBy = orderBy; // sort column by 'desc' or 'asc' order |
| 17 | + _top = top; // return the top n entities for any query, |
12 | 18 | } |
13 | 19 |
|
| 20 | + public static CustomQuery OrderBy(string orderBy) { |
| 21 | + return new CustomQuery("", orderBy); |
| 22 | + } |
| 23 | + |
14 | 24 | public override string ToString() |
15 | 25 | { |
16 | 26 | string queryString = ""; |
17 | 27 | string q = "?"; |
18 | 28 | if (!string.IsNullOrEmpty(_filter)){ |
19 | | - queryString += string.Format("{0}$filter=({1})", q, escape(_filter)); |
| 29 | + queryString += string.Format("{0}$filter=({1})", q, encode(_filter)); |
20 | 30 | q = "&"; |
21 | 31 | } |
22 | 32 | if (!string.IsNullOrEmpty(_orderBy)){ |
23 | | - queryString += string.Format("{0}$orderby={1}", q, escape(_orderBy)); |
| 33 | + queryString += string.Format("{0}$orderby={1}", q, encode(_orderBy)); |
24 | 34 | q = "&"; |
25 | 35 | } |
| 36 | + if (_top > 0) { |
| 37 | + queryString += string.Format("{0}$top={1}", q, _top.ToString()); |
| 38 | + } |
26 | 39 | return queryString; |
27 | 40 | } |
28 | 41 |
|
29 | | - public string escape(string s) |
| 42 | + private string encode(string url) |
30 | 43 | { |
31 | | - return s.Replace(" ","%20"); |
| 44 | + url = url.Replace ("'", "%27"); // replace "'" with '%27' |
| 45 | + return HttpUtility.UrlPathEncode (url); // replaces " " with '%20' and not '+' |
32 | 46 | } |
33 | 47 |
|
34 | 48 | } |
|
0 commit comments