Repository navigation
Expand file tree
/
Copy pathTableQuery.cs
More file actions
101 lines (92 loc) · 4.58 KB
/
Copy pathTableQuery.cs
File metadata and controls
101 lines (92 loc) · 4.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System;
using System.Text;
using UnityEngine;
/// <summary>
/// Implemention of Query records operation https://msdn.microsoft.com/en-us/library/azure/jj677199.aspx
/// By default there is a maximum of 50 records returned in a query - use top and skip params to return additional pages of results.
/// NB: The `$inlinecount` param (which returns total count of all items without paging applied) is not set here as it alters the data model shape.
/// Rather the '$inlinecount=allpages' param is automatically set when using the table's Query method and wrapping your data model with the NestedResults object.
/// </summary>
namespace Azure.AppServices {
[Flags]
public enum TableSystemProperty {
nil = 0x0,
createdAt = 0x1,
updatedAt = 0x2,
version = 0x4,
deleted = 0x8
}
public class TableQuery {
public string Filter;
public OrderBy[] OrderBy;
public string Select;
public uint Skip;
public uint Top;
// other params
public bool IncludeDeleted;
public TableSystemProperty SystemProperties;
public bool NoScript = false; // When a value of `true` is supplied, the execution of registered scripts is suppressed.
// NB: To suppress script execution, you must also supply the service master key with the "X-ZUMO-MASTER" header.
// For more info and security note refer to: https://msdn.microsoft.com/en-us/library/azure/jj677199.aspx
public TableQuery(string filterPredicate = "", uint top = 0, uint skip = 0, string selectColumns = null, TableSystemProperty systemProperties = TableSystemProperty.nil, bool includeDeleted = false, params OrderBy[] orderBy) {
// supports the following subset of the query option parameters defined by the Open Data Protocol (OData)
this.Filter = filterPredicate; // return only rows that satisfy the specified filter predicate
this.OrderBy = orderBy; // sort column by one or more columns: order can be specified in 'desc' or 'asc' order ('asc' is default)
this.Select = selectColumns; // defines new projection of data by specifying the columns
this.Skip = skip; // the n of records to skip (used for paging results)
this.Top = top; // return the top n entities for any query
this.IncludeDeleted = includeDeleted; // if table has soft delete enabled then deleted records will be included in the results
this.SystemProperties = systemProperties; // list of system properties to be included in the response
}
public static TableQuery CreateWithOrderBy(params OrderBy[] orderBy) {
return new TableQuery("", 0, 0, null, TableSystemProperty.nil, false, orderBy);
}
public override string ToString() {
StringBuilder sb = new StringBuilder("?");
if (!string.IsNullOrEmpty(Filter)) {
sb.Append(string.Format("$filter=({0})&", WWW.EscapeURL(Filter)));
}
if (OrderBy != null && OrderBy.Length > 0) {
sb.Append(string.Format("$orderby="));
foreach (OrderBy order in OrderBy) {
sb.Append(order.ToString() + ",");
}
sb.Remove(sb.Length - 1, 1); // remove trailing ','
sb.Append(string.Format("&"));
}
if (Top > 0) {
sb.Append(string.Format("$top={0}&", Top.ToString()));
}
if (Skip > 0) {
sb.Append(string.Format("$skip={0}&", Skip.ToString()));
}
if (!string.IsNullOrEmpty(Select)) {
sb.Append(string.Format("$select={0}&", WWW.EscapeURL(Select)));
if (SystemProperties != TableSystemProperty.nil) {
// NB: setting `__systemproperties` param doesn't appear to do anything so I've added a workaround to append these values onto the 'select' param.
sb.Remove(sb.Length - 1, 1); // remove trailing '&'
sb.Append(string.Format(",{0}&", SystemPropertiesValues(this.SystemProperties)));
}
}
if (SystemProperties != TableSystemProperty.nil) {
sb.Append(string.Format("__systemproperties={0}&", SystemPropertiesValues(SystemProperties)));
}
if (this.IncludeDeleted) {
sb.Append("__includeDeleted=true&");
}
if (this.NoScript) {
sb.Append("noscript=true&");
}
sb.Remove(sb.Length - 1, 1); // remove trailing '&'
return sb.ToString();
}
private string SystemPropertiesValues(TableSystemProperty systemProperties) {
if (systemProperties == TableSystemProperty.nil) {
return "";
}
return systemProperties.ToString().Replace(" ", ""); // remove spaces from string
}
}
}