Ejemplo n.º 1
0
        public virtual IEnumerable <CommandPart> CreatePersistParts(SqlPersistTask task, string parameterNamePrefix)
        {
            ArgumentValidator.EnsureArgumentNotNull(task, "task");
            ArgumentValidator.EnsureArgumentNotNullOrEmpty(parameterNamePrefix, "parameterNamePrefix");

            var result         = new List <CommandPart>();
            int parameterIndex = 0;

            foreach (var request in task.RequestSequence)
            {
                var tuple             = task.Tuple;
                var compilationResult = request.GetCompiledStatement();
                var configuration     = new SqlPostCompilerConfiguration();
                var part = new CommandPart();

                foreach (var binding in request.ParameterBindings)
                {
                    var parameterValue = GetParameterValue(task, binding);
                    if (binding.BindingType == PersistParameterBindingType.VersionFilter && IsHandledLikeNull(parameterValue))
                    {
                        configuration.AlternativeBranches.Add(binding);
                    }
                    else
                    {
                        var parameterName = GetParameterName(parameterNamePrefix, ref parameterIndex);
                        configuration.PlaceholderValues.Add(binding, Driver.BuildParameterReference(parameterName));
                        AddParameter(part, binding, parameterName, parameterValue);
                    }
                }

                part.Statement = compilationResult.GetCommandText(configuration);
                result.Add(part);
            }
            return(result);
        }
Ejemplo n.º 2
0
 private void ProcessUnbatchedTask(SqlPersistTask task, CommandProcessorContext context)
 {
     if (context.ActiveCommand.Count > 0)
     {
         context.ActiveCommand.ExecuteNonQuery();
         ReleaseCommand(context);
         AllocateCommand(context);
     }
     ExecuteUnbatchedTask(task);
 }
Ejemplo n.º 3
0
        private object GetParameterValue(SqlPersistTask task, PersistParameterBinding binding)
        {
            switch (binding.BindingType)
            {
            case PersistParameterBindingType.Regular:
                return(task.Tuple.GetValueOrDefault(binding.FieldIndex));

            case PersistParameterBindingType.VersionFilter:
                return(task.OriginalTuple.GetValueOrDefault(binding.FieldIndex));

            default:
                throw new ArgumentOutOfRangeException("binding.Source");
            }
        }
Ejemplo n.º 4
0
        void ISqlTaskProcessor.ProcessTask(SqlPersistTask task, CommandProcessorContext context)
        {
            if (task.ValidateRowCount)
            {
                ProcessUnbatchedTask(task, context);
                return;
            }
            var sequence = Factory.CreatePersistParts(task, GetParameterPrefix(context));

            foreach (var part in sequence)
            {
                context.ActiveCommand.AddPart(part);
            }
        }
Ejemplo n.º 5
0
        private void ExecuteUnbatchedTask(SqlPersistTask task)
        {
            var sequence = Factory.CreatePersistParts(task);

            foreach (var part in sequence)
            {
                using (var command = Factory.CreateCommand()) {
                    command.AddPart(part);
                    var affectedRowsCount = command.ExecuteNonQuery();
                    if (affectedRowsCount == 0)
                    {
                        throw new VersionConflictException(string.Format(
                                                               Strings.ExVersionOfEntityWithKeyXDiffersFromTheExpectedOne, task.EntityKey));
                    }
                }
            }
        }
Ejemplo n.º 6
0
        void ISqlTaskProcessor.ProcessTask(SqlPersistTask task, CommandProcessorContext context)
        {
            if (task.ValidateRowCount)
            {
                ProcessUnbatchedTask(task, context);
                context.CurrentTask = null;
                return;
            }
            var sequence = Factory.CreatePersistParts(task, GetParameterPrefix(context)).ToChainedBuffer();

            if (PendCommandParts(context.ActiveCommand, sequence))
            {
                // higly recommended to no tear apart persist actions if they are batchable
                return;
            }
            foreach (var part in sequence)
            {
                context.ActiveCommand.AddPart(part);
            }

            context.CurrentTask = null;
        }
        void ISqlTaskProcessor.ProcessTask(SqlPersistTask task, CommandProcessorContext context)
        {
            var sequence = Factory.CreatePersistParts(task);

            foreach (var part in sequence)
            {
                try {
                    context.ActiveCommand.AddPart(part);
                    var affectedRowsCount = context.ActiveCommand.ExecuteNonQuery();
                    if (task.ValidateRowCount && affectedRowsCount == 0)
                    {
                        throw new VersionConflictException(string.Format(
                                                               Strings.ExVersionOfEntityWithKeyXDiffersFromTheExpectedOne, task.EntityKey));
                    }
                }
                finally {
                    context.ActiveCommand.DisposeSafely();
                    ReleaseCommand(context);
                    AllocateCommand(context);
                }
            }
        }
Ejemplo n.º 8
0
 public IEnumerable <CommandPart> CreatePersistParts(SqlPersistTask task)
 {
     return(CreatePersistParts(task, DefaultParameterNamePrefix));
 }