Beispiel #1
0
        /// <summary>
        ///     Builds an update command.
        /// </summary>
        /// <param name="oldRow"> Old value of the row being updated. </param>
        /// <param name="newRow"> New value for the row being updated. </param>
        /// <param name="processor"> Context for the table containing row. </param>
        /// <returns> Update command. </returns>
        internal UpdateCommand BuildUpdateCommand(
            PropagatorResult oldRow,
            PropagatorResult newRow, TableChangeProcessor processor)
        {
            // If we're updating a row, the row may not need to be touched (e.g., no concurrency validation required)
            var rowMustBeTouched = false;

            var target = GetTarget(processor);

            // Create set clauses and returning parameter
            Dictionary <int, string> outputIdentifiers;
            DbExpression             returning;
            var setClauses = new List <DbModificationClause>();

            foreach (var clause in BuildSetClauses(
                         target, newRow, oldRow, processor, /* insertMode */ false, out outputIdentifiers, out returning,
                         ref rowMustBeTouched))
            {
                setClauses.Add(clause);
            }

            // Construct predicate identifying the row to modify
            var predicate = BuildPredicate(target, oldRow, newRow, processor, ref rowMustBeTouched);

            if (0 == setClauses.Count)
            {
                if (rowMustBeTouched)
                {
                    var stateEntries = new List <IEntityStateEntry>();
                    stateEntries.AddRange(
                        SourceInterpreter.GetAllStateEntries(
                            oldRow, m_translator, processor.Table));
                    stateEntries.AddRange(
                        SourceInterpreter.GetAllStateEntries(
                            newRow, m_translator, processor.Table));
                    if (stateEntries.All(it => (it.State == EntityState.Unchanged)))
                    {
                        rowMustBeTouched = false;
                    }
                }

                // Determine if there is nothing to do (i.e., no values to set,
                // no computed columns, and no concurrency validation required)
                if (!rowMustBeTouched)
                {
                    return(null);
                }
            }

            // Initialize DML command tree
            var commandTree =
                new DbUpdateCommandTree(
                    m_translator.MetadataWorkspace, DataSpace.SSpace, target, predicate, setClauses.AsReadOnly(), returning);

            // Create command
            UpdateCommand command = new DynamicUpdateCommand(
                processor, m_translator, ModificationOperator.Update, oldRow, newRow, commandTree, outputIdentifiers);

            return(command);
        }
Beispiel #2
0
        /// <summary>
        ///     Builds insert command.
        /// </summary>
        /// <param name="newRow"> Row to insert. </param>
        /// <param name="processor"> Context for the table we're inserting into. </param>
        /// <returns> Insert command. </returns>
        internal UpdateCommand BuildInsertCommand(PropagatorResult newRow, TableChangeProcessor processor)
        {
            // Bind the insert target
            var target = GetTarget(processor);

            // Create set clauses and returning parameter
            Dictionary <int, string> outputIdentifiers;
            DbExpression             returning;
            var rowMustBeTouched = true; // for inserts, the row must always be touched
            var setClauses       = new List <DbModificationClause>();

            foreach (var clause in BuildSetClauses(
                         target, newRow, null, processor, /* insertMode */ true, out outputIdentifiers,
                         out returning, ref rowMustBeTouched))
            {
                setClauses.Add(clause);
            }

            // Initialize DML command tree
            var commandTree =
                new DbInsertCommandTree(m_translator.MetadataWorkspace, DataSpace.SSpace, target, setClauses.AsReadOnly(), returning);

            // Create command
            UpdateCommand command = new DynamicUpdateCommand(
                processor, m_translator, ModificationOperator.Insert, null, newRow, commandTree, outputIdentifiers);

            return(command);
        }
Beispiel #3
0
 internal DynamicUpdateCommand(
     TableChangeProcessor processor,
     UpdateTranslator translator,
     ModificationOperator modificationOperator,
     PropagatorResult originalValues,
     PropagatorResult currentValues,
     DbModificationCommandTree tree,
     Dictionary <int, string> outputIdentifiers)
     : base(translator, originalValues, currentValues)
 {
     this._processor = processor;
     this._operator  = modificationOperator;
     this._modificationCommandTree = tree;
     this._outputIdentifiers       = outputIdentifiers;
     if (ModificationOperator.Insert != modificationOperator && modificationOperator != ModificationOperator.Update)
     {
         return;
     }
     this._inputIdentifiers = new List <KeyValuePair <int, DbSetClause> >(2);
     foreach (KeyValuePair <EdmMember, PropagatorResult> pairEnumeration in Helper.PairEnumerations <EdmMember, PropagatorResult>(TypeHelpers.GetAllStructuralMembers((EdmType)this.CurrentValues.StructuralType), (IEnumerable <PropagatorResult>) this.CurrentValues.GetMemberValues()))
     {
         int         identifier = pairEnumeration.Value.Identifier;
         DbSetClause setter;
         if (-1 != identifier && DynamicUpdateCommand.TryGetSetterExpression(tree, pairEnumeration.Key, modificationOperator, out setter))
         {
             foreach (int principal in translator.KeyManager.GetPrincipals(identifier))
             {
                 this._inputIdentifiers.Add(new KeyValuePair <int, DbSetClause>(principal, setter));
             }
         }
     }
 }
Beispiel #4
0
        internal override int CompareToType(UpdateCommand otherCommand)
        {
            DynamicUpdateCommand dynamicUpdateCommand = (DynamicUpdateCommand)otherCommand;
            int num1 = (int)(this.Operator - dynamicUpdateCommand.Operator);

            if (num1 != 0)
            {
                return(num1);
            }
            int num2 = StringComparer.Ordinal.Compare(this._processor.Table.Name, dynamicUpdateCommand._processor.Table.Name);

            if (num2 != 0)
            {
                return(num2);
            }
            int num3 = StringComparer.Ordinal.Compare(this._processor.Table.EntityContainer.Name, dynamicUpdateCommand._processor.Table.EntityContainer.Name);

            if (num3 != 0)
            {
                return(num3);
            }
            PropagatorResult propagatorResult1 = this.Operator == ModificationOperator.Delete ? this.OriginalValues : this.CurrentValues;
            PropagatorResult propagatorResult2 = dynamicUpdateCommand.Operator == ModificationOperator.Delete ? dynamicUpdateCommand.OriginalValues : dynamicUpdateCommand.CurrentValues;

            for (int index = 0; index < this._processor.KeyOrdinals.Length; ++index)
            {
                int    keyOrdinal   = this._processor.KeyOrdinals[index];
                object simpleValue1 = propagatorResult1.GetMemberValue(keyOrdinal).GetSimpleValue();
                object simpleValue2 = propagatorResult2.GetMemberValue(keyOrdinal).GetSimpleValue();
                num3 = ByValueComparer.Default.Compare(simpleValue1, simpleValue2);
                if (num3 != 0)
                {
                    return(num3);
                }
            }
            for (int index = 0; index < this._processor.KeyOrdinals.Length; ++index)
            {
                int keyOrdinal = this._processor.KeyOrdinals[index];
                num3 = propagatorResult1.GetMemberValue(keyOrdinal).Identifier - propagatorResult2.GetMemberValue(keyOrdinal).Identifier;
                if (num3 != 0)
                {
                    return(num3);
                }
            }
            return(num3);
        }
Beispiel #5
0
        /// <summary>
        ///     Builds a delete command.
        /// </summary>
        /// <param name="oldRow"> Value of the row being deleted. </param>
        /// <param name="processor"> Context for the table containing row. </param>
        /// <returns> Delete command. </returns>
        internal UpdateCommand BuildDeleteCommand(PropagatorResult oldRow, TableChangeProcessor processor)
        {
            // If we're deleting a row, the row must always be touched
            var rowMustBeTouched = true;

            // Initialize DML command tree
            var target = GetTarget(processor);

            // Create delete predicate
            var predicate   = BuildPredicate(target, oldRow, null, processor, ref rowMustBeTouched);
            var commandTree = new DbDeleteCommandTree(m_translator.MetadataWorkspace, DataSpace.SSpace, target, predicate);

            // Set command
            // Initialize delete command
            UpdateCommand command = new DynamicUpdateCommand(
                processor, m_translator, ModificationOperator.Delete, oldRow, null, commandTree, null);

            return(command);
        }
        /// <summary>
        ///     Builds a delete command.
        /// </summary>
        /// <param name="oldRow"> Value of the row being deleted. </param>
        /// <param name="processor"> Context for the table containing row. </param>
        /// <returns> Delete command. </returns>
        internal UpdateCommand BuildDeleteCommand(PropagatorResult oldRow, TableChangeProcessor processor)
        {
            // If we're deleting a row, the row must always be touched
            var rowMustBeTouched = true;

            // Initialize DML command tree
            var target = GetTarget(processor);

            // Create delete predicate
            var predicate = BuildPredicate(target, oldRow, null, processor, ref rowMustBeTouched);
            var commandTree = new DbDeleteCommandTree(m_translator.MetadataWorkspace, DataSpace.SSpace, target, predicate);

            // Set command
            // Initialize delete command
            UpdateCommand command = new DynamicUpdateCommand(
                processor, m_translator, ModificationOperator.Delete, oldRow, null, commandTree, null);

            return command;
        }
Beispiel #7
0
        protected virtual DbCommand CreateCommand(Dictionary <int, object> identifierValues)
        {
            DbModificationCommandTree modificationCommandTree = this._modificationCommandTree;

            if (this._inputIdentifiers != null)
            {
                Dictionary <DbSetClause, DbSetClause> clauseMappings = new Dictionary <DbSetClause, DbSetClause>();
                for (int index = 0; index < this._inputIdentifiers.Count; ++index)
                {
                    KeyValuePair <int, DbSetClause> inputIdentifier = this._inputIdentifiers[index];
                    object obj;
                    if (identifierValues.TryGetValue(inputIdentifier.Key, out obj))
                    {
                        DbSetClause dbSetClause = new DbSetClause(inputIdentifier.Value.Property, (DbExpression)DbExpressionBuilder.Constant(obj));
                        clauseMappings[inputIdentifier.Value] = dbSetClause;
                        this._inputIdentifiers[index]         = new KeyValuePair <int, DbSetClause>(inputIdentifier.Key, dbSetClause);
                    }
                }
                modificationCommandTree = DynamicUpdateCommand.RebuildCommandTree(modificationCommandTree, clauseMappings);
            }
            return(this.Translator.CreateCommand(modificationCommandTree));
        }
Beispiel #8
0
        private static DbModificationCommandTree RebuildCommandTree(
            DbModificationCommandTree originalTree,
            Dictionary <DbSetClause, DbSetClause> clauseMappings)
        {
            if (clauseMappings.Count == 0)
            {
                return(originalTree);
            }
            DbModificationCommandTree modificationCommandTree;

            if (originalTree.CommandTreeKind == DbCommandTreeKind.Insert)
            {
                DbInsertCommandTree insertCommandTree = (DbInsertCommandTree)originalTree;
                modificationCommandTree = (DbModificationCommandTree) new DbInsertCommandTree(insertCommandTree.MetadataWorkspace, insertCommandTree.DataSpace, insertCommandTree.Target, new ReadOnlyCollection <DbModificationClause>((IList <DbModificationClause>)DynamicUpdateCommand.ReplaceClauses(insertCommandTree.SetClauses, clauseMappings)), insertCommandTree.Returning);
            }
            else
            {
                DbUpdateCommandTree updateCommandTree = (DbUpdateCommandTree)originalTree;
                modificationCommandTree = (DbModificationCommandTree) new DbUpdateCommandTree(updateCommandTree.MetadataWorkspace, updateCommandTree.DataSpace, updateCommandTree.Target, updateCommandTree.Predicate, new ReadOnlyCollection <DbModificationClause>((IList <DbModificationClause>)DynamicUpdateCommand.ReplaceClauses(updateCommandTree.SetClauses, clauseMappings)), updateCommandTree.Returning);
            }
            return(modificationCommandTree);
        }
        /// <summary>
        ///     Builds an update command.
        /// </summary>
        /// <param name="oldRow"> Old value of the row being updated. </param>
        /// <param name="newRow"> New value for the row being updated. </param>
        /// <param name="processor"> Context for the table containing row. </param>
        /// <returns> Update command. </returns>
        internal UpdateCommand BuildUpdateCommand(
            PropagatorResult oldRow,
            PropagatorResult newRow, TableChangeProcessor processor)
        {
            // If we're updating a row, the row may not need to be touched (e.g., no concurrency validation required)
            var rowMustBeTouched = false;

            var target = GetTarget(processor);

            // Create set clauses and returning parameter
            Dictionary<int, string> outputIdentifiers;
            DbExpression returning;
            var setClauses = new List<DbModificationClause>();
            foreach (var clause in BuildSetClauses(
                target, newRow, oldRow, processor, /* insertMode */ false, out outputIdentifiers, out returning,
                ref rowMustBeTouched))
            {
                setClauses.Add(clause);
            }

            // Construct predicate identifying the row to modify
            var predicate = BuildPredicate(target, oldRow, newRow, processor, ref rowMustBeTouched);

            if (0 == setClauses.Count)
            {
                if (rowMustBeTouched)
                {
                    var stateEntries = new List<IEntityStateEntry>();
                    stateEntries.AddRange(
                        SourceInterpreter.GetAllStateEntries(
                            oldRow, m_translator, processor.Table));
                    stateEntries.AddRange(
                        SourceInterpreter.GetAllStateEntries(
                            newRow, m_translator, processor.Table));
                    if (stateEntries.All(it => (it.State == EntityState.Unchanged)))
                    {
                        rowMustBeTouched = false;
                    }
                }

                // Determine if there is nothing to do (i.e., no values to set, 
                // no computed columns, and no concurrency validation required)
                if (!rowMustBeTouched)
                {
                    return null;
                }
            }

            // Initialize DML command tree
            var commandTree =
                new DbUpdateCommandTree(
                    m_translator.MetadataWorkspace, DataSpace.SSpace, target, predicate, setClauses.AsReadOnly(), returning);

            // Create command
            UpdateCommand command = new DynamicUpdateCommand(
                processor, m_translator, ModificationOperator.Update, oldRow, newRow, commandTree, outputIdentifiers);

            return command;
        }
        /// <summary>
        ///     Builds insert command.
        /// </summary>
        /// <param name="newRow"> Row to insert. </param>
        /// <param name="processor"> Context for the table we're inserting into. </param>
        /// <returns> Insert command. </returns>
        internal UpdateCommand BuildInsertCommand(PropagatorResult newRow, TableChangeProcessor processor)
        {
            // Bind the insert target
            var target = GetTarget(processor);

            // Create set clauses and returning parameter
            Dictionary<int, string> outputIdentifiers;
            DbExpression returning;
            var rowMustBeTouched = true; // for inserts, the row must always be touched
            var setClauses = new List<DbModificationClause>();
            foreach (var clause in BuildSetClauses(
                target, newRow, null, processor, /* insertMode */ true, out outputIdentifiers,
                out returning, ref rowMustBeTouched))
            {
                setClauses.Add(clause);
            }

            // Initialize DML command tree
            var commandTree =
                new DbInsertCommandTree(m_translator.MetadataWorkspace, DataSpace.SSpace, target, setClauses.AsReadOnly(), returning);

            // Create command
            UpdateCommand command = new DynamicUpdateCommand(
                processor, m_translator, ModificationOperator.Insert, null, newRow, commandTree, outputIdentifiers);

            return command;
        }