コード例 #1
0
 private static void VerifySynchronousExecutionMode(DynamicExecutionMode executionMode)
 {
     if (executionMode == DynamicExecutionMode.Asynchronous)
     {
         throw new NotSupportedException(asyncParameterDirectionError);
     }
 }
コード例 #2
0
        public DynamicStoredProcedure(IDbConnection connection,
                                      IEnumerable <IDataTransformer> transformers,
                                      CancellationToken token,
                                      int timeout,
                                      DynamicExecutionMode executionMode,
                                      bool hasResults)
        {
            Contract.Requires(connection != null);
            Contract.Requires(transformers != null);

            this.connection    = connection;
            this.transformers  = transformers;
            this.token         = token;
            this.timeout       = timeout;
            this.executionMode = executionMode;
            this.hasResults    = hasResults;
        }
コード例 #3
0
        private DynamicStoredProcedure(IDbConnection connection,
                                       IEnumerable <IDataTransformer> transformers,
                                       CancellationToken token,
                                       int timeout,
                                       string schema,
                                       DynamicExecutionMode executionMode,
                                       bool hasResults)
        {
            Contract.Requires(connection != null);
            Contract.Requires(transformers != null);
            Contract.Requires(!string.IsNullOrEmpty(schema));

            this.connection    = connection;
            this.transformers  = transformers;
            this.schema        = schema;
            this.token         = token;
            this.timeout       = timeout;
            this.executionMode = executionMode;
            this.hasResults    = hasResults;
        }
コード例 #4
0
        public DynamicStoredProcedureResults(
            IDbConnection connection,
            string schema,
            string name,
            int timeout,
            List <IStoredProcedureParameter> parameters,
            IEnumerable <IDataTransformer> transformers,
            DynamicExecutionMode executionMode,
            bool hasResults,
            CancellationToken token)
        {
            Contract.Requires(connection != null);
            Contract.Requires(!string.IsNullOrEmpty(schema));
            Contract.Requires(!string.IsNullOrEmpty(name));
            Contract.Requires(parameters != null);
            Contract.Requires(transformers != null);

            this.executionMode    = executionMode;
            this.command          = connection.CreateCommand(schema, name, timeout, out this.connection);
            this.parameters       = parameters;
            this.transformers     = transformers;
            this.token            = token;
            this.continueOnCaller = true;

            foreach (var p in parameters)
            {
                command.Parameters.Add(p.CreateDbDataParameter(command));
            }

            if (!hasResults)
            {
                if (executionMode == DynamicExecutionMode.Synchronous)
                {
                    command.ExecuteNonQuery();
                    TransferOutputParameters(parameters, token);
                    Dispose();
                }
                else
                {
#if !NET40
                    var sqlCommand = command as SqlCommand;
                    if (sqlCommand != null)
                    {
                        nonQueryTask = sqlCommand.ExecuteNonQueryAsync(token);
                    }
                    else
#endif
                    nonQueryTask = Task.Factory.StartNew(() => command.ExecuteNonQuery(),
                                                         token,
                                                         TaskCreationOptions.None,
                                                         TaskScheduler.Default);

                    nonQueryTask = nonQueryTask.ContinueWith(_ => TransferOutputParameters(parameters, token), token);
                }
            }
            else if (executionMode == DynamicExecutionMode.Synchronous)
            {
                var tcs = new TaskCompletionSource <IDataReader>();

                token.ThrowIfCancellationRequested();
                var res = command.ExecuteReader();

                // If there are any result sets, this won't actually have populated the parameters
                TransferOutputParameters(parameters, token, throwIfNonMatchingTypes: false);

                if (token.IsCancellationRequested)
                {
                    res.Dispose();
                    token.ThrowIfCancellationRequested();
                }
                else
                {
                    tcs.SetResult(res);
                    resultTask = tcs.Task;
                }
            }
            else
            {
#if !NET40
                var sqlCommand = command as SqlCommand;
                if (sqlCommand != null)
                {
                    resultTask = sqlCommand.ExecuteReaderAsync(token).ContinueWith(r => (IDataReader)r.Result, token);
                }
                else
#endif
                resultTask = Task.Factory.StartNew(() => command.ExecuteReader(),
                                                   token,
                                                   TaskCreationOptions.None,
                                                   TaskScheduler.Default);

                resultTask = resultTask.ContinueWith(r =>
                {
                    // If there are any result sets, the parameters won't actually have been set yet
                    TransferOutputParameters(parameters, token, throwIfNonMatchingTypes: false);
                    return(r.Result);
                }, token);
            }
        }