public override void Visit(DbScanExpression expression) { Check.NotNull(expression, "expression"); // we know we won't hit this code unless there is no function defined for this // ModificationOperation, so if this EntitySet is using a DefiningQuery, instead // of a table, that is an error MetadataProperty definingQuery; if (expression.Target.MetadataProperties.TryGetValue("DefiningQuery", false, out definingQuery) && null != definingQuery.Value) { string missingCudElement; var ict = _commandTree as DbInsertCommandTree; var dct = _commandTree as DbDeleteCommandTree; #if DEBUG var uct = _commandTree as DbUpdateCommandTree; #endif if (null != dct) { missingCudElement = "DeleteFunction" /*StorageMslConstructs.DeleteFunctionElement*/; } else if (null != ict) { missingCudElement = "InsertFunction" /*StorageMslConstructs.InsertFunctionElement*/; } else { #if DEBUG Debug.Assert(null != uct, "did you add a new option?"); #endif missingCudElement = "UpdateFunction" /*StorageMslConstructs.UpdateFunctionElement*/; } throw ADP1.Update( EntityRes.GetString( EntityRes.Update_SqlEntitySetWithoutDmlFunctions, expression.Target.Name, missingCudElement, "ModificationFunctionMapping" /*StorageMslConstructs.ModificationFunctionMappingElement*/), null); } _commandText.Append(SqlGenerator.GetTargetTSql(expression.Target)); }
internal static string[] GenerateUpdateSql(DbUpdateCommandTree tree, out List <DbParameter> parameters, bool isLocalProvider) { var commandTexts = new List <String>(); var commandText = new StringBuilder(s_commandTextBuilderInitialCapacity); var translator = new ExpressionTranslator(commandText, tree, null != tree.Returning, isLocalProvider); // update [schemaName].[tableName] commandText.Append("update "); tree.Target.Expression.Accept(translator); commandText.AppendLine(); // set c1 = ..., c2 = ..., ... var first = true; commandText.Append("set "); foreach (DbSetClause setClause in tree.SetClauses) { if (first) { first = false; } else { commandText.Append(", "); } setClause.Property.Accept(translator); commandText.Append(" = "); setClause.Value.Accept(translator); } if (first) { // If first is still true, it indicates there were no set // clauses. // - we acquire the appropriate locks // - server-gen columns (e.g. timestamp) get recomputed // // Fix #13533 : A fake update DML updating some column item // with the same value as before to acquire the lock on the table // while updating some columns in another table. This happens when // both the table are dependent on an entity and the members of entity // which is mapped to one table is being updated and the other table // needs to be locked for consistency. string updatableColumnName; if (GetUpdatableColumn(tree, out updatableColumnName)) { commandText.Append("["); commandText.Append(CommonUtils.EscapeSquareBraceNames(updatableColumnName)); commandText.Append("] "); commandText.Append(" = "); commandText.Append("["); commandText.Append(CommonUtils.EscapeSquareBraceNames(updatableColumnName)); commandText.Append("] "); } else { // Throw some meaningful error throw ADP1.Update( EntityRes.GetString(EntityRes.UpdateStatementCannotBeGeneratedForAcquiringLock), null); } } commandText.AppendLine(); // where c1 = ..., c2 = ... commandText.Append("where "); tree.Predicate.Accept(translator); commandText.AppendLine(); commandTexts.Add(commandText.ToString()); commandText.Length = 0; // generate returning sql GenerateReturningSql(commandText, tree, translator, tree.Returning); if (!String.IsNullOrEmpty(commandText.ToString())) { commandTexts.Add(commandText.ToString()); } parameters = translator.Parameters; return(commandTexts.ToArray()); }