Ejemplo n.º 1
0
        /// <summary>
        ///     Executes the command generated by <see cref="CreateStoreCommand" /> against a
        ///     database using the given connection.
        /// </summary>
        /// <param name="connection"> The connection to the database to update. </param>
        public override void Execute(IRelationalConnection connection)
        {
            Check.NotNull(connection, nameof(connection));

            var storeCommand = CreateStoreCommand();

            try
            {
                using var dataReader = storeCommand.RelationalCommand.ExecuteReader(
                          new RelationalCommandParameterObject(
                              connection,
                              storeCommand.ParameterValues,
                              null,
                              Dependencies.CurrentContext.Context,
                              Dependencies.Logger));
                Consume(dataReader);
            }
            catch (DbUpdateException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DbUpdateException(
                          RelationalStrings.UpdateStoreException,
                          ex,
                          ModificationCommands.SelectMany(c => c.Entries).ToList());
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        ///     Executes the command generated by <see cref="CreateStoreCommand" /> against a
        ///     database using the given connection.
        /// </summary>
        /// <param name="connection"> The connection to the database to update. </param>
        /// <param name="cancellationToken"> A <see cref="CancellationToken" /> to observe while waiting for the task to complete. </param>
        /// <returns> A task that represents the asynchronous operation. </returns>
        /// <exception cref="OperationCanceledException"> If the <see cref="CancellationToken"/> is canceled. </exception>
        public override async Task ExecuteAsync(
            IRelationalConnection connection,
            CancellationToken cancellationToken = default)
        {
            Check.NotNull(connection, nameof(connection));

            var storeCommand = CreateStoreCommand();

            try
            {
                await using var dataReader = await storeCommand.RelationalCommand.ExecuteReaderAsync(
                                new RelationalCommandParameterObject (
                                    connection,
                                    storeCommand.ParameterValues,
                                    null,
                                    Dependencies.CurrentContext.Context,
                                    Dependencies.Logger),
                                cancellationToken).ConfigureAwait(false);
                await ConsumeAsync(dataReader, cancellationToken).ConfigureAwait(false);
            }
            catch (DbUpdateException)
            {
                throw;
            }
            catch (Exception ex)
            {
                throw new DbUpdateException(
                          RelationalStrings.UpdateStoreException,
                          ex,
                          ModificationCommands.SelectMany(c => c.Entries).ToList());
            }
        }
Ejemplo n.º 3
0
        protected virtual IRelationalCommand CreateStoreCommand()
        {
            var commandBuilder = _commandBuilderFactory
                                 .Create()
                                 .Append(GetCommandText());

            foreach (var columnModification in ModificationCommands.SelectMany(t => t.ColumnModifications))
            {
                if (columnModification.ParameterName != null)
                {
                    commandBuilder.AddParameter(
                        SqlGenerationHelper.GenerateParameterName(columnModification.ParameterName),
                        columnModification.Value,
                        columnModification.Property);
                }

                if (columnModification.OriginalParameterName != null)
                {
                    commandBuilder.AddParameter(
                        SqlGenerationHelper.GenerateParameterName(columnModification.OriginalParameterName),
                        columnModification.OriginalValue,
                        columnModification.Property);
                }
            }

            return(commandBuilder.Build());
        }
Ejemplo n.º 4
0
        protected override RawSqlCommand CreateStoreCommand()
        {
            var commandBuilder = _commandBuilderFactory
                                 .Create()
                                 .Append(GetCommandText());

            var parameterValues = new Dictionary <string, object>(GetParameterCount());

            foreach (var columnModification in ModificationCommands.SelectMany(t => t.ColumnModifications))
            {
                if (columnModification.UseCurrentValueParameter)
                {
                    commandBuilder.AddParameter(
                        columnModification.ParameterName,
                        SqlGenerationHelper.GenerateParameterName(columnModification.ParameterName),
                        columnModification.Property);

                    parameterValues.Add(
                        columnModification.ParameterName,
                        columnModification.Value);
                }

                if (columnModification.UseOriginalValueParameter)
                {
                    commandBuilder.AddParameter(
                        columnModification.OriginalParameterName,
                        SqlGenerationHelper.GenerateParameterName(columnModification.OriginalParameterName),
                        columnModification.Property);

                    parameterValues.Add(
                        columnModification.OriginalParameterName,
                        columnModification.OriginalValue);
                }
            }

            for (var i = 1; i < _cursorPosition; i++)
            {
                var nameParameter = $"cur{i}";

                commandBuilder.AddRawParameter(
                    nameParameter,
                    new OracleParameter(
                        nameParameter,
                        OracleDbType.RefCursor,
                        DBNull.Value,
                        ParameterDirection.Output));
            }

            return(new RawSqlCommand(
                       commandBuilder.Build(),
                       parameterValues));
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 创建存储过程命令
        /// </summary>
        /// <returns></returns>
        protected override RawSqlCommand CreateStoreCommand()
        {
            try
            {
                if (Check.IsTraceEnabled(m_oracleLogger?.Logger))
                {
                    Trace <DbLoggerCategory.Update> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Entry, OracleTraceClassName.OracleModificationCommandBatch, OracleTraceFuncName.CreateStoreCommand);
                }

                var commandText     = GetCommandText();
                var ommandBuilder   = _commandBuilderFactory.Create().Append(commandText);
                var parameterValues = new Dictionary <string, object>(GetParameterCount());

                foreach (ColumnModification item in ModificationCommands.SelectMany((ModificationCommand t) => t.ColumnModifications))
                {
                    if (item.UseCurrentValueParameter)
                    {
                        ommandBuilder.AddParameter(item.ParameterName, SqlGenerationHelper.GenerateParameterName(item.ParameterName), item.Property);
                        parameterValues.Add(item.ParameterName, item.Value);
                    }
                    if (item.UseOriginalValueParameter)
                    {
                        ommandBuilder.AddParameter(item.OriginalParameterName, SqlGenerationHelper.GenerateParameterName(item.OriginalParameterName), item.Property);
                        parameterValues.Add(item.OriginalParameterName, item.OriginalValue);
                    }
                }
                for (int i = 1; i < _cursorPosition; i++)
                {
                    var parameterName = $"cur{i}";
                    ommandBuilder.AddRawParameter(parameterName, (DbParameter) new OracleParameter(parameterName, OracleDbType.RefCursor, (object)DBNull.Value, ParameterDirection.Output));
                }
                return(new RawSqlCommand(ommandBuilder.Build(), parameterValues));
            }
            catch (Exception ex)
            {
                if (Check.IsErrorEnabled(m_oracleLogger?.Logger))
                {
                    Trace <DbLoggerCategory.Update> .Write(m_oracleLogger, LogLevel.Error, OracleTraceTag.Error, OracleTraceClassName.OracleModificationCommandBatch, OracleTraceFuncName.CreateStoreCommand, ex.ToString());
                }
                throw;
            }
            finally
            {
                if (Check.IsTraceEnabled(m_oracleLogger?.Logger))
                {
                    Trace <DbLoggerCategory.Update> .Write(m_oracleLogger, LogLevel.Trace, OracleTraceTag.Exit, OracleTraceClassName.OracleModificationCommandBatch, OracleTraceFuncName.CreateStoreCommand);
                }
            }
        }
Ejemplo n.º 6
0
        protected virtual DbCommand CreateStoreCommand(
            [NotNull] string commandText,
            [NotNull] DbTransaction transaction,
            [NotNull] RelationalTypeMapper typeMapper)
        {
            var command = transaction.Connection.CreateCommand();

            command.CommandType = CommandType.Text;
            command.CommandText = commandText;
            command.Transaction = transaction;

            foreach (var columnModification in ModificationCommands.SelectMany(t => t.ColumnModifications))
            {
                PopulateParameters(command, columnModification, typeMapper);
            }

            return(command);
        }
        protected virtual RawSqlCommand CreateStoreCommand()
        {
            var commandBuilder = _commandBuilderFactory
                                 .Create()
                                 .Append(GetCommandText());

            var parameterValues = new Dictionary <string, object>(GetParameterCount());

            foreach (var columnModification in ModificationCommands.SelectMany(t => t.ColumnModifications))
            {
                if (columnModification.ParameterName != null)
                {
                    commandBuilder.AddParameter(
                        columnModification.ParameterName,
                        SqlGenerationHelper.GenerateParameterName(columnModification.ParameterName),
                        columnModification.Property);

                    parameterValues.Add(
                        columnModification.ParameterName,
                        columnModification.Value);
                }

                if (columnModification.OriginalParameterName != null)
                {
                    commandBuilder.AddParameter(
                        columnModification.OriginalParameterName,
                        SqlGenerationHelper.GenerateParameterName(columnModification.OriginalParameterName),
                        columnModification.Property);

                    parameterValues.Add(
                        columnModification.OriginalParameterName,
                        columnModification.OriginalValue);
                }
            }

            return(new RawSqlCommand(
                       commandBuilder.Build(),
                       parameterValues));
        }