示例#1
0
        /// <summary>
        /// Execute the operation synchronously.
        /// </summary>
        /// <returns></returns>
        public override TArgument?Execute(object?state = null)
        {
            IReadOnlyDictionary <string, object?>?row = null;

            var executionToken = Prepare();
            var rowCount       = executionToken.Execute(cmd =>
            {
                using (var reader = cmd.ExecuteReader(CommandBehavior.SequentialAccess))
                {
                    row = reader.ReadDictionary();
                    return((row != null ? 1 : 0) + reader.RemainingRowCount());
                }
            }, state);

            if (rowCount == 0 || row == null)
            {
                throw new DataException("No rows were returned");
            }
            else if (rowCount > 1)
            {
                throw new DataException($"Expected 1 row but received {rowCount} rows.");
            }

            //update the ArgumentValue with any new keys, calculated fields, etc.
            MaterializerUtilities.PopulateComplexObject(row, m_CommandBuilder.ArgumentValue, null);

            return(m_CommandBuilder.ArgumentValue);
        }
示例#2
0
        /// <summary>
        /// Execute the operation asynchronously.
        /// </summary>
        /// <param name="cancellationToken">The cancellation token.</param>
        /// <param name="state">User defined state, usually used for logging.</param>
        /// <returns></returns>
        public override async Task <TArgument?> ExecuteAsync(CancellationToken cancellationToken, object?state = null)
        {
            IReadOnlyDictionary <string, object?>?row = null;

            var executionToken = Prepare();
            var rowCount       = await executionToken.ExecuteAsync(async cmd =>
            {
                using (var reader = await cmd.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken).ConfigureAwait(false))
                {
                    row = await reader.ReadDictionaryAsync().ConfigureAwait(false);
                    return((row != null ? 1 : 0) + await reader.RemainingRowCountAsync().ConfigureAwait(false));
                }
            }, cancellationToken, state).ConfigureAwait(false);

            if (rowCount == 0 || row == null)
            {
                throw new DataException("No rows were returned");
            }
            else if (rowCount > 1)
            {
                throw new DataException($"Expected 1 row but received {rowCount} rows.");
            }

            //update the ArgumentValue with any new keys, calculated fields, etc.
            MaterializerUtilities.PopulateComplexObject(row, m_CommandBuilder.ArgumentValue, null);

            return(m_CommandBuilder.ArgumentValue);
        }
        static TObject CaptureOutputParameters(CommandExecutionToken <TCommand, TParameter> commandToken)
        {
            var result = AsOutputsMaterializer <TCommand, TParameter> .CaptureOutputParameters(commandToken);

            var objectResult = new TObject();

            MaterializerUtilities.PopulateComplexObject(result, objectResult, null);
            return(objectResult);
        }
示例#4
0
 TObject ConstructObject(IReadOnlyDictionary <string, object?>?row, int?rowCount)
 {
     if (rowCount == 0 || row == null)
     {
         throw new MissingDataException($"No rows were returned. It was this expected, use `.ToObjectOrNull` instead of `.ToObject`.");
     }
     else if (rowCount > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows))
     {
         throw new UnexpectedDataException($"Expected 1 row but received {rowCount} rows. If this was expected, use `RowOptions.DiscardExtraRows`.");
     }
     return(MaterializerUtilities.ConstructObject <TObject>(row, Constructor));
 }
示例#5
0
 TObject?ConstructObject(IReadOnlyDictionary <string, object?>?row, int?rowCount)
 {
     if (rowCount == 0 || row == null)
     {
         if (!m_RowOptions.HasFlag(RowOptions.PreventEmptyResults))
         {
             return(null);
         }
         else
         {
             throw new MissingDataException($"No rows were returned and {nameof(RowOptions)}.{nameof(RowOptions.PreventEmptyResults)} was enabled.");
         }
     }
     else if (rowCount > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows))
     {
         throw new UnexpectedDataException($"Expected 1 row but received {rowCount} rows. If this was expected, use `RowOptions.DiscardExtraRows`.");
     }
     return(MaterializerUtilities.ConstructObject <TObject>(row, Constructor));
 }
示例#6
0
 private TObject ConstructObject(IReadOnlyDictionary <string, object> row, int?rowCount)
 {
     if (rowCount == 0)
     {
         if (m_RowOptions.HasFlag(RowOptions.AllowEmptyResults))
         {
             return(null);
         }
         else
         {
             throw new MissingDataException("No rows were returned");
         }
     }
     else if (rowCount > 1 && !m_RowOptions.HasFlag(RowOptions.DiscardExtraRows))
     {
         throw new UnexpectedDataException($"Expected 1 row but received {rowCount} rows");
     }
     return(MaterializerUtilities.ConstructObject <TObject>(row, ConstructorSignature));
 }
示例#7
0
        private T ConstructObject()
        {
            var constructorParameters = m_Constructor.ParameterNames;
            var parameters            = new object[constructorParameters.Length];

            for (var i = 0; i < constructorParameters.Length; i++)
            {
                parameters[i] = m_Dictionary[constructorParameters[i]];
            }

            var result = (T)m_Constructor.ConstructorInfo.Invoke(parameters);

            if (m_PopulateComplexObject)
            {
                MaterializerUtilities.PopulateComplexObject(m_Dictionary, result, null);
            }

            //Change tracking objects shouldn't be materialized as unchanged.
            (result as IChangeTracking)?.AcceptChanges();

            return(result);
        }