12
12
using System . Threading . Tasks ;
13
13
using Azure . DataApiBuilder . Service . Configurations ;
14
14
using Azure . DataApiBuilder . Service . Exceptions ;
15
+ using Azure . DataApiBuilder . Service . Models ;
15
16
using Microsoft . AspNetCore . Http ;
16
17
using Microsoft . Extensions . Logging ;
17
18
using Polly ;
@@ -62,7 +63,7 @@ public QueryExecutor(DbExceptionParser dbExceptionParser,
62
63
public virtual async Task < TResult ? > ExecuteQueryAsync < TResult > (
63
64
string sqltext ,
64
65
IDictionary < string , object ? > parameters ,
65
- Func < DbDataReader , List < string > ? , Task < TResult ? > > ? dataReaderHandler ,
66
+ Func < DbDataReader , List < string > ? , Task < TResult > > ? dataReaderHandler ,
66
67
HttpContext ? httpContext = null ,
67
68
List < string > ? args = null )
68
69
{
@@ -134,7 +135,7 @@ await ExecuteQueryAgainstDbAsync(conn,
134
135
TConnection conn ,
135
136
string sqltext ,
136
137
IDictionary < string , object ? > parameters ,
137
- Func < DbDataReader , List < string > ? , Task < TResult ? > > ? dataReaderHandler ,
138
+ Func < DbDataReader , List < string > ? , Task < TResult > > ? dataReaderHandler ,
138
139
HttpContext ? httpContext ,
139
140
List < string > ? args = null )
140
141
{
@@ -208,20 +209,20 @@ public async Task<bool> ReadAsync(DbDataReader reader)
208
209
}
209
210
210
211
/// <inheritdoc />
211
- public async Task < Tuple < Dictionary < string , object ? > ? , Dictionary < string , object > > ? >
212
+ public async Task < DbOperationResultRow >
212
213
ExtractRowFromDbDataReader ( DbDataReader dbDataReader , List < string > ? args = null )
213
214
{
214
- Dictionary < string , object ? > row = new ( ) ;
215
-
216
- Dictionary < string , object > propertiesOfResult = GetResultProperties ( dbDataReader ) . Result ?? new ( ) ;
215
+ DbOperationResultRow dbOperationResultRow = new (
216
+ columns : new ( ) ,
217
+ resultProperties : GetResultProperties ( dbDataReader ) . Result ?? new ( ) ) ;
217
218
218
219
if ( await ReadAsync ( dbDataReader ) )
219
220
{
220
221
if ( dbDataReader . HasRows )
221
222
{
222
223
DataTable ? schemaTable = dbDataReader . GetSchemaTable ( ) ;
223
224
224
- if ( schemaTable != null )
225
+ if ( schemaTable is not null )
225
226
{
226
227
foreach ( DataRow schemaRow in schemaTable . Rows )
227
228
{
@@ -235,43 +236,37 @@ public async Task<bool> ReadAsync(DbDataReader reader)
235
236
int colIndex = dbDataReader . GetOrdinal ( columnName ) ;
236
237
if ( ! dbDataReader . IsDBNull ( colIndex ) )
237
238
{
238
- row . Add ( columnName , dbDataReader [ columnName ] ) ;
239
+ dbOperationResultRow . Columns . Add ( columnName , dbDataReader [ columnName ] ) ;
239
240
}
240
241
else
241
242
{
242
- row . Add ( columnName , value : null ) ;
243
+ dbOperationResultRow . Columns . Add ( columnName , value : null ) ;
243
244
}
244
245
}
245
246
}
246
247
}
247
248
}
248
249
249
- // no row was read
250
- if ( row . Count == 0 )
251
- {
252
- return new Tuple < Dictionary < string , object ? > ? , Dictionary < string , object > > ( null , propertiesOfResult ) ;
253
- }
254
-
255
- return new Tuple < Dictionary < string , object ? > ? , Dictionary < string , object > > ( row , propertiesOfResult ) ;
250
+ return dbOperationResultRow ;
256
251
}
257
252
258
253
/// <inheritdoc />
259
254
/// <Note>This function is a DbDataReader handler of type Func<DbDataReader, List<string>?, Task<TResult?>>
260
255
/// The parameter args is not used but is added to conform to the signature of the DbDataReader handler
261
256
/// function argument of ExecuteQueryAsync.</Note>
262
- public async Task < JsonArray ? > GetJsonArrayAsync (
257
+ public async Task < JsonArray > GetJsonArrayAsync (
263
258
DbDataReader dbDataReader ,
264
259
List < string > ? args = null )
265
260
{
266
- Tuple < Dictionary < string , object ? > ? , Dictionary < string , object > > ? resultRowAndProperties ;
261
+ DbOperationResultRow dbOperationResultRow = await ExtractRowFromDbDataReader ( dbDataReader ) ;
267
262
JsonArray resultArray = new ( ) ;
268
263
269
- while ( ( resultRowAndProperties = await ExtractRowFromDbDataReader ( dbDataReader ) ) is not null &&
270
- resultRowAndProperties . Item1 is not null )
264
+ while ( dbOperationResultRow . Columns . Count > 0 )
271
265
{
272
266
JsonElement result =
273
- JsonSerializer . Deserialize < JsonElement > ( JsonSerializer . Serialize ( resultRowAndProperties . Item1 ) ) ;
267
+ JsonSerializer . Deserialize < JsonElement > ( JsonSerializer . Serialize ( dbOperationResultRow . Columns ) ) ;
274
268
resultArray . Add ( result ) ;
269
+ dbOperationResultRow = await ExtractRowFromDbDataReader ( dbDataReader ) ;
275
270
}
276
271
277
272
return resultArray ;
@@ -306,21 +301,20 @@ public async Task<bool> ReadAsync(DbDataReader reader)
306
301
/// <inheritdoc />
307
302
/// <Note>This function is a DbDataReader handler of type
308
303
/// Func<DbDataReader, List<string>?, Task<TResult?>></Note>
309
- public async Task < Tuple < Dictionary < string , object ? > ? , Dictionary < string , object > > ? > GetMultipleResultSetsIfAnyAsync (
304
+ public async Task < DbOperationResultRow > GetMultipleResultSetsIfAnyAsync (
310
305
DbDataReader dbDataReader , List < string > ? args = null )
311
306
{
312
- Tuple < Dictionary < string , object ? > ? , Dictionary < string , object > > ? resultRecordWithProperties
307
+ DbOperationResultRow dbOperationResultRow
313
308
= await ExtractRowFromDbDataReader ( dbDataReader ) ;
314
309
315
310
/// Processes a second result set from DbDataReader if it exists.
316
311
/// In MsSQL upsert:
317
312
/// result set #1: result of the UPDATE operation.
318
313
/// result set #2: result of the INSERT operation.
319
- if ( resultRecordWithProperties is not null && resultRecordWithProperties . Item1 is not null )
314
+ if ( dbOperationResultRow . Columns . Count > 0 )
320
315
{
321
- resultRecordWithProperties . Item2 . Add ( SqlMutationEngine . IS_FIRST_RESULT_SET , true ) ;
322
- return new Tuple < Dictionary < string , object ? > ? , Dictionary < string , object > >
323
- ( resultRecordWithProperties . Item1 , resultRecordWithProperties . Item2 ) ;
316
+ dbOperationResultRow . ResultProperties . Add ( SqlMutationEngine . IS_FIRST_RESULT_SET , true ) ;
317
+ return dbOperationResultRow ;
324
318
}
325
319
else if ( await dbDataReader . NextResultAsync ( ) )
326
320
{
@@ -345,20 +339,22 @@ public async Task<bool> ReadAsync(DbDataReader reader)
345
339
}
346
340
}
347
341
348
- return null ;
342
+ return dbOperationResultRow ;
349
343
}
350
344
351
345
/// <inheritdoc />
352
346
/// <Note>This function is a DbDataReader handler of type
353
347
/// Func<DbDataReader, List<string>?, Task<TResult?>></Note>
354
- public Task < Dictionary < string , object > ? > GetResultProperties (
348
+ public Task < Dictionary < string , object > > GetResultProperties (
355
349
DbDataReader dbDataReader ,
356
350
List < string > ? columnNames = null )
357
351
{
358
- Dictionary < string , object > ? propertiesOfResult = new ( ) ;
359
- propertiesOfResult . Add ( nameof ( dbDataReader . RecordsAffected ) , dbDataReader . RecordsAffected ) ;
360
- propertiesOfResult . Add ( nameof ( dbDataReader . HasRows ) , dbDataReader . HasRows ) ;
361
- return Task . FromResult ( ( Dictionary < string , object > ? ) propertiesOfResult ) ;
352
+ Dictionary < string , object > resultProperties = new ( )
353
+ {
354
+ { nameof ( dbDataReader . RecordsAffected ) , dbDataReader . RecordsAffected } ,
355
+ { nameof ( dbDataReader . HasRows ) , dbDataReader . HasRows }
356
+ } ;
357
+ return Task . FromResult ( resultProperties ) ;
362
358
}
363
359
364
360
private async Task < string > GetJsonStringFromDbReader ( DbDataReader dbDataReader )
0 commit comments