8
8
using Microsoft . Azure . WebJobs . Host . Bindings . Path ;
9
9
using Microsoft . WindowsAzure . Storage ;
10
10
using Microsoft . WindowsAzure . Storage . Table ;
11
+ using Newtonsoft . Json ;
11
12
using Newtonsoft . Json . Linq ;
12
13
13
14
namespace Microsoft . Azure . WebJobs . Script
@@ -16,14 +17,27 @@ internal class TableBinding : Binding
16
17
{
17
18
private readonly BindingTemplate _partitionKeyBindingTemplate ;
18
19
private readonly BindingTemplate _rowKeyBindingTemplate ;
20
+ private readonly TableQuery _tableQuery ;
19
21
20
- public TableBinding ( JobHostConfiguration config , string name , string tableName , string partitionKey , string rowKey , FileAccess fileAccess ) : base ( config , name , "queue" , fileAccess , false )
22
+ public TableBinding ( JobHostConfiguration config , string name , string tableName , string partitionKey , string rowKey , FileAccess fileAccess , TableQuery tableQuery = null ) : base ( config , name , "queue" , fileAccess , false )
21
23
{
22
24
TableName = tableName ;
23
25
PartitionKey = partitionKey ;
24
26
RowKey = rowKey ;
25
27
_partitionKeyBindingTemplate = BindingTemplate . FromString ( PartitionKey ) ;
26
- _rowKeyBindingTemplate = BindingTemplate . FromString ( RowKey ) ;
28
+ if ( ! string . IsNullOrEmpty ( RowKey ) )
29
+ {
30
+ _rowKeyBindingTemplate = BindingTemplate . FromString ( RowKey ) ;
31
+ }
32
+
33
+ _tableQuery = tableQuery ;
34
+ if ( _tableQuery == null )
35
+ {
36
+ _tableQuery = new TableQuery
37
+ {
38
+ TakeCount = 50
39
+ } ;
40
+ }
27
41
}
28
42
29
43
public string TableName { get ; private set ; }
@@ -35,7 +49,7 @@ public override bool HasBindingParameters
35
49
get
36
50
{
37
51
return _partitionKeyBindingTemplate . ParameterNames . Any ( ) ||
38
- _rowKeyBindingTemplate . ParameterNames . Any ( ) ;
52
+ ( _rowKeyBindingTemplate != null && _rowKeyBindingTemplate . ParameterNames . Any ( ) ) ;
39
53
}
40
54
}
41
55
@@ -46,11 +60,17 @@ public override async Task BindAsync(IBinder binder, Stream stream, IReadOnlyDic
46
60
if ( bindingData != null )
47
61
{
48
62
boundPartitionKey = _partitionKeyBindingTemplate . Bind ( bindingData ) ;
49
- boundRowKey = _rowKeyBindingTemplate . Bind ( bindingData ) ;
63
+ if ( _rowKeyBindingTemplate != null )
64
+ {
65
+ boundRowKey = _rowKeyBindingTemplate . Bind ( bindingData ) ;
66
+ }
50
67
}
51
68
52
69
boundPartitionKey = Resolve ( boundPartitionKey ) ;
53
- boundRowKey = Resolve ( boundRowKey ) ;
70
+ if ( ! string . IsNullOrEmpty ( boundRowKey ) )
71
+ {
72
+ boundRowKey = Resolve ( boundRowKey ) ;
73
+ }
54
74
55
75
if ( FileAccess == FileAccess . Write )
56
76
{
@@ -77,25 +97,81 @@ public override async Task BindAsync(IBinder binder, Stream stream, IReadOnlyDic
77
97
}
78
98
else
79
99
{
80
- DynamicTableEntity tableEntity = binder . Bind < DynamicTableEntity > ( new TableAttribute ( TableName , boundPartitionKey , boundRowKey ) ) ;
81
- if ( tableEntity != null )
100
+ if ( ! string . IsNullOrEmpty ( boundPartitionKey ) &&
101
+ ! string . IsNullOrEmpty ( boundRowKey ) )
82
102
{
83
- OperationContext context = new OperationContext ( ) ;
84
- var entityProperties = tableEntity . WriteEntity ( context ) ;
103
+ // singleton
104
+ DynamicTableEntity tableEntity = binder . Bind < DynamicTableEntity > ( new TableAttribute ( TableName , boundPartitionKey , boundRowKey ) ) ;
105
+ if ( tableEntity != null )
106
+ {
107
+ string json = ConvertEntityToJObject ( tableEntity ) . ToString ( ) ;
108
+ using ( StreamWriter sw = new StreamWriter ( stream ) )
109
+ {
110
+ await sw . WriteAsync ( json ) ;
111
+ }
112
+ }
113
+ }
114
+ else
115
+ {
116
+ // binding to entire table (query multiple table entities)
117
+ CloudTable table = binder . Bind < CloudTable > ( new TableAttribute ( TableName , boundPartitionKey , boundRowKey ) ) ;
118
+ var entities = table . ExecuteQuery ( _tableQuery ) ;
85
119
86
- JObject jsonObject = new JObject ( ) ;
87
- foreach ( var entityProperty in entityProperties )
120
+ JArray entityArray = new JArray ( ) ;
121
+ foreach ( var entity in entities )
88
122
{
89
- jsonObject . Add ( entityProperty . Key , entityProperty . Value . StringValue ) ;
123
+ entityArray . Add ( ConvertEntityToJObject ( entity ) ) ;
90
124
}
91
- string json = jsonObject . ToString ( ) ;
92
125
126
+ string json = entityArray . ToString ( Formatting . None ) ;
93
127
using ( StreamWriter sw = new StreamWriter ( stream ) )
94
128
{
95
129
await sw . WriteAsync ( json ) ;
96
130
}
97
131
}
98
132
}
99
133
}
134
+
135
+ private static JObject ConvertEntityToJObject ( DynamicTableEntity tableEntity )
136
+ {
137
+ OperationContext context = new OperationContext ( ) ;
138
+ var entityProperties = tableEntity . WriteEntity ( context ) ;
139
+
140
+ JObject jsonObject = new JObject ( ) ;
141
+ foreach ( var entityProperty in entityProperties )
142
+ {
143
+ JValue value = null ;
144
+ switch ( entityProperty . Value . PropertyType )
145
+ {
146
+ case EdmType . String :
147
+ value = new JValue ( entityProperty . Value . StringValue ) ;
148
+ break ;
149
+ case EdmType . Int32 :
150
+ value = new JValue ( entityProperty . Value . Int32Value ) ;
151
+ break ;
152
+ case EdmType . Int64 :
153
+ value = new JValue ( entityProperty . Value . Int64Value ) ;
154
+ break ;
155
+ case EdmType . DateTime :
156
+ value = new JValue ( entityProperty . Value . DateTime ) ;
157
+ break ;
158
+ case EdmType . Boolean :
159
+ value = new JValue ( entityProperty . Value . BooleanValue ) ;
160
+ break ;
161
+ case EdmType . Guid :
162
+ value = new JValue ( entityProperty . Value . GuidValue ) ;
163
+ break ;
164
+ case EdmType . Double :
165
+ value = new JValue ( entityProperty . Value . DoubleValue ) ;
166
+ break ;
167
+ case EdmType . Binary :
168
+ value = new JValue ( entityProperty . Value . BinaryValue ) ;
169
+ break ;
170
+ }
171
+
172
+ jsonObject . Add ( entityProperty . Key , value ) ;
173
+ }
174
+ return jsonObject ;
175
+ }
100
176
}
101
177
}
0 commit comments