-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathQueryExecutor.cs
More file actions
378 lines (340 loc) · 15.7 KB
/
QueryExecutor.cs
File metadata and controls
378 lines (340 loc) · 15.7 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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Net;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Threading.Tasks;
using Azure.DataApiBuilder.Service.Configurations;
using Azure.DataApiBuilder.Service.Exceptions;
using Azure.DataApiBuilder.Service.Models;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging;
using Polly;
using Polly.Retry;
namespace Azure.DataApiBuilder.Service.Resolvers
{
/// <summary>
/// Encapsulates query execution apis.
/// </summary>
public class QueryExecutor<TConnection> : IQueryExecutor
where TConnection : DbConnection, new()
{
protected DbExceptionParser DbExceptionParser { get; }
protected ILogger<IQueryExecutor> QueryExecutorLogger { get; }
private RuntimeConfigProvider ConfigProvider { get; }
// The maximum number of attempts that can be made to execute the query successfully in addition to the first attempt.
// So to say in case of transient exceptions, the query will be executed (_maxRetryCount + 1) times at max.
private static int _maxRetryCount = 5;
private AsyncRetryPolicy _retryPolicy;
public virtual DbConnectionStringBuilder ConnectionStringBuilder { get; set; }
public QueryExecutor(DbExceptionParser dbExceptionParser,
ILogger<IQueryExecutor> logger,
DbConnectionStringBuilder connectionStringBuilder,
RuntimeConfigProvider configProvider)
{
DbExceptionParser = dbExceptionParser;
QueryExecutorLogger = logger;
ConnectionStringBuilder = connectionStringBuilder;
ConfigProvider = configProvider;
_retryPolicy = Polly.Policy
.Handle<DbException>(DbExceptionParser.IsTransientException)
.WaitAndRetryAsync(
retryCount: _maxRetryCount,
sleepDurationProvider: (attempt) => TimeSpan.FromSeconds(Math.Pow(2, attempt)),
onRetry: (exception, backOffTime) =>
{
QueryExecutorLogger.LogError(exception.Message);
QueryExecutorLogger.LogError(exception.StackTrace);
});
}
/// <inheritdoc/>
public virtual async Task<TResult?> ExecuteQueryAsync<TResult>(
string sqltext,
IDictionary<string, object?> parameters,
Func<DbDataReader, List<string>?, Task<TResult>>? dataReaderHandler,
HttpContext? httpContext = null,
List<string>? args = null)
{
int retryAttempt = 0;
using TConnection conn = new()
{
ConnectionString = ConnectionStringBuilder.ConnectionString,
};
await SetManagedIdentityAccessTokenIfAnyAsync(conn);
return await _retryPolicy.ExecuteAsync(async () =>
{
retryAttempt++;
try
{
// When IsLateConfigured is true we are in a hosted scenario and do not reveal query information.
if (!ConfigProvider.IsLateConfigured)
{
QueryExecutorLogger.LogDebug($"Executing query: \n{sqltext}");
}
TResult? result =
await ExecuteQueryAgainstDbAsync(conn,
sqltext,
parameters,
dataReaderHandler,
httpContext,
args);
if (retryAttempt > 1)
{
// This implies that the request got successfully executed during one of retry attempts.
QueryExecutorLogger.LogInformation($"Request executed successfully in {retryAttempt} attempt of" +
$"{_maxRetryCount + 1} available attempts.");
}
return result;
}
catch (DbException e)
{
if (DbExceptionParser.IsTransientException((DbException)e) && retryAttempt < _maxRetryCount + 1)
{
throw e;
}
else
{
QueryExecutorLogger.LogError(e.Message);
QueryExecutorLogger.LogError(e.StackTrace);
// Throw custom DABException
throw DbExceptionParser.Parse(e);
}
}
});
}
/// <summary>
/// Method to execute sql query against the database.
/// </summary>
/// <param name="conn">Connection object used to connect to database.</param>
/// <param name="sqltext">Sql text to be executed.</param>
/// <param name="parameters">The parameters used to execute the SQL text.</param>
/// <param name="dataReaderHandler">The function to invoke to handle the results
/// in the DbDataReader obtained after executing the query.</param>
/// <param name="httpContext">Current user httpContext.</param>
/// <param name="args">List of string arguments to the DbDataReader handler.</param>
/// <returns>An object formed using the results of the query as returned by the given handler.</returns>
public virtual async Task<TResult?> ExecuteQueryAgainstDbAsync<TResult>(
TConnection conn,
string sqltext,
IDictionary<string, object?> parameters,
Func<DbDataReader, List<string>?, Task<TResult>>? dataReaderHandler,
HttpContext? httpContext,
List<string>? args = null)
{
await conn.OpenAsync();
DbCommand cmd = conn.CreateCommand();
cmd.CommandType = CommandType.Text;
// Add query to send user data from DAB to the underlying database to enable additional security the user might have configured
// at the database level.
string sessionParamsQuery = GetSessionParamsQuery(httpContext, parameters);
//"EXEC sp_set_session_context 'roles', 'Anonymous', @read_only =1 ;";
cmd.CommandText = sessionParamsQuery + sqltext;
if (parameters is not null)
{
foreach (KeyValuePair<string, object?> parameterEntry in parameters)
{
DbParameter parameter = cmd.CreateParameter();
parameter.ParameterName = parameterEntry.Key;
parameter.Value = parameterEntry.Value ?? DBNull.Value;
cmd.Parameters.Add(parameter);
}
}
try
{
using DbDataReader dbDataReader = await cmd.ExecuteReaderAsync(CommandBehavior.CloseConnection);
if (dataReaderHandler is not null && dbDataReader is not null)
{
return await dataReaderHandler(dbDataReader, args);
}
else
{
return default(TResult);
}
}
catch (DbException e)
{
QueryExecutorLogger.LogError(e.Message);
QueryExecutorLogger.LogError(e.StackTrace);
throw DbExceptionParser.Parse(e);
}
}
/// <inheritdoc />
public virtual string GetSessionParamsQuery(HttpContext? httpContext, IDictionary<string, object?> parameters)
{
return string.Empty;
}
/// <inheritdoc />
public virtual async Task SetManagedIdentityAccessTokenIfAnyAsync(DbConnection conn)
{
// no-op in the base class.
await Task.Yield();
}
/// <inheritdoc />
public async Task<bool> ReadAsync(DbDataReader reader)
{
try
{
return await reader.ReadAsync();
}
catch (DbException e)
{
QueryExecutorLogger.LogError(e.Message);
QueryExecutorLogger.LogError(e.StackTrace);
throw DbExceptionParser.Parse(e);
}
}
/// <inheritdoc />
public async Task<DbOperationResultRow>
ExtractRowFromDbDataReader(DbDataReader dbDataReader, List<string>? args = null)
{
DbOperationResultRow dbOperationResultRow = new(
columns: new(),
resultProperties: GetResultProperties(dbDataReader).Result ?? new());
if (await ReadAsync(dbDataReader))
{
if (dbDataReader.HasRows)
{
DataTable? schemaTable = dbDataReader.GetSchemaTable();
if (schemaTable is not null)
{
foreach (DataRow schemaRow in schemaTable.Rows)
{
string columnName = (string)schemaRow["ColumnName"];
if (args is not null && !args.Contains(columnName))
{
continue;
}
int colIndex = dbDataReader.GetOrdinal(columnName);
if (!dbDataReader.IsDBNull(colIndex))
{
dbOperationResultRow.Columns.Add(columnName, dbDataReader[columnName]);
}
else
{
dbOperationResultRow.Columns.Add(columnName, value: null);
}
}
}
}
}
return dbOperationResultRow;
}
/// <inheritdoc />
/// <Note>This function is a DbDataReader handler of type Func<DbDataReader, List<string>?, Task<TResult?>>
/// The parameter args is not used but is added to conform to the signature of the DbDataReader handler
/// function argument of ExecuteQueryAsync.</Note>
public async Task<JsonArray> GetJsonArrayAsync(
DbDataReader dbDataReader,
List<string>? args = null)
{
DbOperationResultRow dbOperationResultRow = await ExtractRowFromDbDataReader(dbDataReader);
JsonArray resultArray = new();
while (dbOperationResultRow.Columns.Count > 0)
{
JsonElement result =
JsonSerializer.Deserialize<JsonElement>(JsonSerializer.Serialize(dbOperationResultRow.Columns));
resultArray.Add(result);
dbOperationResultRow = await ExtractRowFromDbDataReader(dbDataReader);
}
return resultArray;
}
/// <inheritdoc />
/// <Note>This function is a DbDataReader handler of type Func<DbDataReader, List<string>?, Task<TResult?>>
/// The parameter args is not used but is added to conform to the signature of the DbDataReader handler
/// function argument of ExecuteQueryAsync.</Note>
public async Task<TResult?> GetJsonResultAsync<TResult>(
DbDataReader dbDataReader,
List<string>? args = null)
{
TResult? jsonResult = default;
// Parse Results into Json and return
if (dbDataReader.HasRows)
{
// Make sure to get the complete json string in case of large document.
jsonResult =
JsonSerializer.Deserialize<TResult>(
await GetJsonStringFromDbReader(dbDataReader));
}
else
{
QueryExecutorLogger.LogInformation("Did not return any rows in the JSON result.");
}
return jsonResult;
}
/// <inheritdoc />
/// <Note>This function is a DbDataReader handler of type
/// Func<DbDataReader, List<string>?, Task<TResult?>></Note>
public async Task<DbOperationResultRow> GetMultipleResultSetsIfAnyAsync(
DbDataReader dbDataReader, List<string>? args = null)
{
DbOperationResultRow dbOperationResultRow
= await ExtractRowFromDbDataReader(dbDataReader);
/// Processes a second result set from DbDataReader if it exists.
/// In MsSQL upsert:
/// result set #1: result of the UPDATE operation.
/// result set #2: result of the INSERT operation.
if (dbOperationResultRow.Columns.Count > 0)
{
dbOperationResultRow.ResultProperties.Add(SqlMutationEngine.IS_FIRST_RESULT_SET, true);
return dbOperationResultRow;
}
else if (await dbDataReader.NextResultAsync())
{
// Since no first result set exists, we return the second result set.
return await ExtractRowFromDbDataReader(dbDataReader);
}
else
{
// This is the case where UPDATE and INSERT both return no results.
// e.g. a situation where the item with the given PK doesn't exist so there's
// no update and PK is auto generated so no insert can happen.
if (args is not null && args.Count == 2)
{
string prettyPrintPk = args[0];
string entityName = args[1];
throw new DataApiBuilderException(
message: $"Cannot perform INSERT and could not find {entityName} " +
$"with primary key {prettyPrintPk} to perform UPDATE on.",
statusCode: HttpStatusCode.NotFound,
subStatusCode: DataApiBuilderException.SubStatusCodes.EntityNotFound);
}
}
return dbOperationResultRow;
}
/// <inheritdoc />
/// <Note>This function is a DbDataReader handler of type
/// Func<DbDataReader, List<string>?, Task<TResult?>></Note>
public Task<Dictionary<string, object>> GetResultProperties(
DbDataReader dbDataReader,
List<string>? columnNames = null)
{
Dictionary<string, object> resultProperties = new()
{
{ nameof(dbDataReader.RecordsAffected), dbDataReader.RecordsAffected },
{ nameof(dbDataReader.HasRows), dbDataReader.HasRows }
};
return Task.FromResult(resultProperties);
}
private async Task<string> GetJsonStringFromDbReader(DbDataReader dbDataReader)
{
StringBuilder jsonString = new();
// Even though we only return a single cell, we need this loop for
// MS SQL. Sadly it splits FOR JSON PATH output across multiple
// rows if the JSON consists of more than 2033 bytes:
// Sources:
// 1. https://docs.microsoft.com/en-us/sql/relational-databases/json/format-query-results-as-json-with-for-json-sql-server?view=sql-server-2017#output-of-the-for-json-clause
// 2. https://stackoverflow.com/questions/54973536/for-json-path-results-in-ssms-truncated-to-2033-characters/54973676
// 3. https://docs.microsoft.com/en-us/sql/relational-databases/json/use-for-json-output-in-sql-server-and-in-client-apps-sql-server?view=sql-server-2017#use-for-json-output-in-a-c-client-app
while (await ReadAsync(dbDataReader))
{
jsonString.Append(dbDataReader.GetString(0));
}
return jsonString.ToString();
}
}
}