internal string Print(DbDeleteCommandTree tree) { DebugCheck.NotNull(tree); DebugCheck.NotNull(tree.Predicate); TreeNode targetNode; if (tree.Target != null) { targetNode = _visitor.VisitBinding("Target", tree.Target); } else { targetNode = new TreeNode("Target"); } TreeNode predicateNode; if (tree.Predicate != null) { predicateNode = _visitor.VisitExpression("Predicate", tree.Predicate); } else { predicateNode = new TreeNode("Predicate"); } return(Print( new TreeNode( "DbDeleteCommandTree", CreateParametersNode(tree), targetNode, predicateNode))); }
internal string Print(DbDeleteCommandTree tree) { // Predicate should not be null since DbDeleteCommandTree initializes it to DbConstantExpression(true) Debug.Assert(tree != null && tree.Predicate != null, "Invalid DbDeleteCommandTree"); TreeNode targetNode; if (tree.Target != null) { targetNode = _visitor.VisitBinding("Target", tree.Target); } else { targetNode = new TreeNode("Target"); } TreeNode predicateNode; if (tree.Predicate != null) { predicateNode = _visitor.VisitExpression("Predicate", tree.Predicate); } else { predicateNode = new TreeNode("Predicate"); } return(this.Print(new TreeNode( "DbDeleteCommandTree", CreateParametersNode(tree), targetNode, predicateNode))); }
/// <summary> /// In case of a delete command we always filter based on the tenantId /// </summary> private static void InterceptDeleteCommand(DbCommandTreeInterceptionContext interceptionContext, string tenantValue) { var deleteCommand = interceptionContext.Result as DbDeleteCommandTree; if (deleteCommand == null) { return; } var column = TenantAttribute.GetTenantColumnName(deleteCommand.Target.VariableType.EdmType); if (string.IsNullOrEmpty(column)) { return; } // Create the variable reference in order to create the property var variableReference = deleteCommand.Target.VariableType.Variable(deleteCommand.Target.VariableName); // Create the property to which will assign the correct value var tenantProperty = variableReference.Property(column); var tenantIdWherePredicate = tenantProperty.Equal(DbExpression.FromString(tenantValue)); // The initial predicate is the sql where statement var initialPredicate = deleteCommand.Predicate; // Add to the initial statement the tenantId statement which translates in sql AND TenantId = 'value' var finalPredicate = initialPredicate.And(tenantIdWherePredicate); var newDeleteCommand = new DbDeleteCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, finalPredicate); interceptionContext.Result = newDeleteCommand; }
internal static string GenerateDeleteSql( DbDeleteCommandTree tree, SqlGenerator sqlGenerator, out List <DbParameter> parameters, bool createParameters = true) { StringBuilder commandText = new StringBuilder(s_commandTextBuilderInitialCapacity); ExpressionTranslator translator = new ExpressionTranslator( commandText, tree, false, sqlGenerator, "DeleteFunction", createParameters); // delete [schemaName].[tableName] commandText.Append("DELETE FROM "); tree.Target.Expression.Accept(translator); commandText.AppendLine(); // where c1 = ... AND c2 = ... commandText.Append("WHERE "); tree.Predicate.Accept(translator); parameters = translator.Parameters; commandText.AppendLine(" "); return(commandText.ToString()); }
/// <summary> /// In case of a delete command we always filter based on the userId /// </summary> static void InterceptDeleteCommand(DbCommandTreeInterceptionContext interceptionContext) { var deleteCommand = interceptionContext.Result as DbDeleteCommandTree; if (deleteCommand != null) { var column = UserAwareAttribute.GetUserColumnName(deleteCommand.Target.VariableType.EdmType); if (!string.IsNullOrEmpty(column)) { // Get the userId (throw an exception if there is none) var userId = GetCurrentUserId(); // Create the variable reference in order to create the property var variableReference = DbExpressionBuilder.Variable(deleteCommand.Target.VariableType, deleteCommand.Target.VariableName); // Create the property to which will assign the correct value var userProperty = DbExpressionBuilder.Property(variableReference, column); var userIdWherePredicate = DbExpressionBuilder.Equal(userProperty, DbExpression.FromInt32(userId)); // The initial predicate is the sql where statement var initialPredicate = deleteCommand.Predicate; // Add to the initial statement the userId statement which translates in sql AND userId = 'value' var finalPredicate = initialPredicate.And(userIdWherePredicate); var newDeleteCommand = new DbDeleteCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, finalPredicate); interceptionContext.Result = newDeleteCommand; } } }
private DbCommandTree HandleDeleteCommand(DbDeleteCommandTree deleteCommand) { var setClauses = new List <DbModificationClause>(); var table = (EntityType)deleteCommand.Target.VariableType.EdmType; if (table.Properties.All(p => p.Name != Constants.IS_DELETED_COLUMN_NAME)) { return(deleteCommand); } var now = m_DateTimeProvider.GetUtcNow(); setClauses.Add(DbExpressionBuilder.SetClause( deleteCommand.Target.VariableType.Variable(deleteCommand.Target.VariableName).Property(Constants.IS_DELETED_COLUMN_NAME), DbExpression.FromBoolean(true))); setClauses.Add(DbExpressionBuilder.SetClause( deleteCommand.Target.VariableType.Variable(deleteCommand.Target.VariableName).Property(Constants.DELETED_COLUMN_NAME), DbExpression.FromDateTime(now))); return(new DbUpdateCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, deleteCommand.Predicate, setClauses.AsReadOnly(), null)); }
internal static string GenerateDeleteSql( DbDeleteCommandTree tree, SqlGenerator sqlGenerator, out List <SqlParameter> parameters, bool upperCaseKeywords = true, bool createParameters = true) { var commandText = new SqlStringBuilder(CommandTextBuilderInitialCapacity) { UpperCaseKeywords = upperCaseKeywords }; var translator = new ExpressionTranslator( commandText, tree, false, sqlGenerator, createParameters: createParameters); // delete [schemaName].[tableName] commandText.AppendKeyword("delete "); tree.Target.Expression.Accept(translator); commandText.AppendLine(); // where c1 = ... AND c2 = ... commandText.AppendKeyword("where "); tree.Predicate.Accept(translator); parameters = translator.Parameters; return(commandText.ToString()); }
private static DbCommandTree HandleDeleteCommand(DbDeleteCommandTree deleteCommand) { var predicate = deleteCommand.Predicate.Accept(new DeleteVisitor()); return(new DbDeleteCommandTree(deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, predicate)); }
private void GetAffectedEntitySets(DbCommandTree commandTree) { FindAffectedEntitySetsVisitor visitor = new FindAffectedEntitySetsVisitor(this.affectedEntitySets, this.functionsUsed); DbQueryCommandTree queryTree = commandTree as DbQueryCommandTree; if (queryTree != null) { queryTree.Query.Accept(visitor); return; } DbUpdateCommandTree updateTree = commandTree as DbUpdateCommandTree; if (updateTree != null) { this.IsModification = true; updateTree.Target.Expression.Accept(visitor); updateTree.Predicate.Accept(visitor); if (updateTree.Returning != null) { updateTree.Returning.Accept(visitor); } return; } DbInsertCommandTree insertTree = commandTree as DbInsertCommandTree; if (insertTree != null) { this.IsModification = true; insertTree.Target.Expression.Accept(visitor); if (insertTree.Returning != null) { insertTree.Returning.Accept(visitor); } return; } DbDeleteCommandTree deleteTree = commandTree as DbDeleteCommandTree; if (deleteTree != null) { this.IsModification = true; deleteTree.Target.Expression.Accept(visitor); if (deleteTree.Predicate != null) { deleteTree.Predicate.Accept(visitor); } return; } throw new NotSupportedException("Command tree type " + commandTree.GetType() + " is not supported."); }
internal UpdateCommand BuildDeleteCommand( PropagatorResult oldRow, TableChangeProcessor processor) { bool rowMustBeTouched = true; DbExpressionBinding target = UpdateCompiler.GetTarget(processor); DbExpression predicate = this.BuildPredicate(target, oldRow, (PropagatorResult)null, processor, ref rowMustBeTouched); DbDeleteCommandTree deleteCommandTree = new DbDeleteCommandTree(this.m_translator.MetadataWorkspace, DataSpace.SSpace, target, predicate); return((UpdateCommand) new DynamicUpdateCommand(processor, this.m_translator, ModificationOperator.Delete, oldRow, (PropagatorResult)null, (DbModificationCommandTree)deleteCommandTree, (Dictionary <int, string>)null)); }
/// <summary> /// General purpose static function that can be called from System.Data assembly /// </summary> /// <param name="tree">command tree</param> /// <param name="version">version</param> /// <param name="parameters">Parameters to add to the command tree corresponding /// to constants in the command tree. Used only in ModificationCommandTrees.</param> /// <returns>The string representing the SQL to be executed.</returns> internal static string GenerateSql(DbCommandTree tree, EFIngresStoreVersion version, out List <DbParameter> parameters, out CommandType commandType) { commandType = CommandType.Text; //Handle Query DbQueryCommandTree queryCommandTree = tree as DbQueryCommandTree; if (queryCommandTree != null) { SqlGenerator sqlGen = new SqlGenerator(version); parameters = null; return(sqlGen.GenerateSql((DbQueryCommandTree)tree)); } //Handle Function DbFunctionCommandTree DbFunctionCommandTree = tree as DbFunctionCommandTree; if (DbFunctionCommandTree != null) { SqlGenerator sqlGen = new SqlGenerator(version); parameters = null; string sql = sqlGen.GenerateFunctionSql(DbFunctionCommandTree, out commandType); return(sql); } //Handle Insert DbInsertCommandTree insertCommandTree = tree as DbInsertCommandTree; if (insertCommandTree != null) { return(DmlSqlGenerator.GenerateInsertSql(insertCommandTree, out parameters)); } //Handle Delete DbDeleteCommandTree deleteCommandTree = tree as DbDeleteCommandTree; if (deleteCommandTree != null) { return(DmlSqlGenerator.GenerateDeleteSql(deleteCommandTree, out parameters)); } //Handle Update DbUpdateCommandTree updateCommandTree = tree as DbUpdateCommandTree; if (updateCommandTree != null) { return(DmlSqlGenerator.GenerateUpdateSql(updateCommandTree, out parameters)); } throw new NotSupportedException("Unrecognized command tree type"); }
internal string Print(DbDeleteCommandTree tree) { TreeNode treeNode1 = tree.Target == null ? new TreeNode("Target", new TreeNode[0]) : this._visitor.VisitBinding("Target", tree.Target); TreeNode treeNode2 = tree.Predicate == null ? new TreeNode("Predicate", new TreeNode[0]) : this._visitor.VisitExpression("Predicate", tree.Predicate); return(this.Print(new TreeNode("DbDeleteCommandTree", new TreeNode[3] { ExpressionPrinter.CreateParametersNode((DbCommandTree)tree), treeNode1, treeNode2 }))); }
public override string GenerateSQL(DbCommandTree tree) { DbDeleteCommandTree commandTree = tree as DbDeleteCommandTree; DeleteStatement statement = new DeleteStatement(); //scope.Push(null); statement.Target = commandTree.Target.Expression.Accept(this); statement.Where = commandTree.Predicate.Accept(this); return(statement.ToString()); }
internal static string GenerateDeleteSql(DbDeleteCommandTree tree, EFOracleProviderManifest providerManifest, EFOracleVersion sqlVersion, out List <OracleParameter> parameters) { StringBuilder commandText = new StringBuilder(256); DmlSqlGenerator.ExpressionTranslator expressionTranslator = new DmlSqlGenerator.ExpressionTranslator(commandText, (DbModificationCommandTree)tree, false, sqlVersion); commandText.Append("delete "); tree.Target.Expression.Accept((DbExpressionVisitor)expressionTranslator); commandText.AppendLine(); commandText.Append("where "); tree.Predicate.Accept((DbExpressionVisitor)expressionTranslator); parameters = expressionTranslator.Parameters; return(commandText.ToString()); }
internal static string GenerateDeleteSql(DbDeleteCommandTree tree, out List <DbParameter> parameters, bool insertParametersValuesInSql = false) { StringBuilder commandText = new StringBuilder(COMMANDTEXT_STRINGBUILDER_INITIALCAPACITY); ExpressionTranslator translator = new ExpressionTranslator(commandText, tree, false, insertParametersValuesInSql); commandText.Append("delete from "); tree.Target.Expression.Accept(translator); commandText.AppendLine(); // where c1 = ... AND c2 = ... commandText.Append("where "); tree.Predicate.Accept(translator); parameters = translator.Parameters; return(commandText.ToString()); }
internal static string GenerateDeleteSql(DbDeleteCommandTree tree, out List<DbParameter> parameters) { StringBuilder commandText = new StringBuilder(CommandTextBuilderInitialCapacity); ExpressionTranslator translator = new ExpressionTranslator(commandText, tree, false); commandText.Append("DELETE FROM "); tree.Target.Expression.Accept(translator); commandText.AppendLine(); // where c1 = ... AND c2 = ... commandText.Append("WHERE "); tree.Predicate.Accept(translator); parameters = translator.Parameters; return commandText.ToString(); }
private DbModificationCommandTree ConvertInternal(DbDeleteCommandTree commandTree) { DebugCheck.NotNull(commandTree); _currentFunctionMapping = _entityTypeModificationFunctionMapping != null ? _entityTypeModificationFunctionMapping.DeleteFunctionMapping : _associationSetModificationFunctionMapping.DeleteFunctionMapping; return (new DbDeleteCommandTree( commandTree.MetadataWorkspace, commandTree.DataSpace, commandTree.Target, commandTree.Predicate.Accept(this))); }
internal static string GenerateDeleteSql(DbDeleteCommandTree tree, out List <DbParameter> parameters, bool generateParameters = true) { StringBuilder commandText = new StringBuilder(CommandTextBuilderInitialCapacity); ExpressionTranslator translator = new ExpressionTranslator(commandText, tree, false, generateParameters); commandText.Append("DELETE FROM "); tree.Target.Expression.Accept(translator); commandText.AppendLine(); // where c1 = ... AND c2 = ... commandText.Append("WHERE "); tree.Predicate.Accept(translator); parameters = translator.Parameters; return(commandText.ToString()); }
internal static string GenerateDeleteSql(DbDeleteCommandTree tree, out List <DbParameter> parameters) { StringBuilder commandText = new StringBuilder(s_commandTextBuilderInitialCapacity); ExpressionTranslator translator = new ExpressionTranslator(commandText, tree, false); // delete [schemaName].[tableName] commandText.Append("delete "); tree.Target.Expression.Accept(translator); commandText.AppendLine(); // where c1 = ... AND c2 = ... commandText.Append("where "); tree.Predicate.Accept(translator); parameters = translator.Parameters; return(commandText.ToString()); }
/// <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 bool rowMustBeTouched = true; // Initialize DML command tree DbExpressionBinding target = GetTarget(processor); // Create delete predicate DbExpression predicate = BuildPredicate(target, oldRow, null, processor, ref rowMustBeTouched); DbDeleteCommandTree 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); }
internal static string GenerateDeleteSql( DbDeleteCommandTree tree, SqlGenerator sqlGenerator, out List <SqlParameter> parameters, bool upperCaseKeywords = true, bool createParameters = true) { SqlStringBuilder commandText = new SqlStringBuilder(256) { UpperCaseKeywords = upperCaseKeywords }; DmlSqlGenerator.ExpressionTranslator expressionTranslator = new DmlSqlGenerator.ExpressionTranslator(commandText, (DbModificationCommandTree)tree, false, sqlGenerator, (ICollection <EdmProperty>)null, createParameters); commandText.AppendKeyword("delete "); tree.Target.Expression.Accept((DbExpressionVisitor)expressionTranslator); commandText.AppendLine(); commandText.AppendKeyword("where "); tree.Predicate.Accept((DbExpressionVisitor)expressionTranslator); parameters = expressionTranslator.Parameters; return(commandText.ToString()); }
internal static string[] GenerateDeleteSql(DbDeleteCommandTree tree, out List <DbParameter> parameters, bool isLocalProvider) { var commandTexts = new List <String>(); var commandText = new StringBuilder(s_commandTextBuilderInitialCapacity); var translator = new ExpressionTranslator(commandText, tree, false, isLocalProvider); // delete [schemaName].[tableName] commandText.Append("delete "); tree.Target.Expression.Accept(translator); commandText.AppendLine(); // where c1 = ... AND c2 = ... commandText.Append("where "); tree.Predicate.Accept(translator); commandTexts.Add(commandText.ToString()); commandText.Length = 0; parameters = translator.Parameters; return(commandTexts.ToArray()); }
private static DbCommandTree HandleDeleteCommand(DbDeleteCommandTree deleteCommand) { var setClauses = new List <DbModificationClause>(); var table = (EntityType)deleteCommand.Target.VariableType.EdmType; if (table.Properties.All(p => p.Name != IsDeletedColumnName)) { return(deleteCommand); } setClauses.Add(DbExpressionBuilder.SetClause( deleteCommand.Target.VariableType.Variable(deleteCommand.Target.VariableName).Property(IsDeletedColumnName), DbExpression.FromBoolean(true))); return(new DbUpdateCommandTree( deleteCommand.MetadataWorkspace, deleteCommand.DataSpace, deleteCommand.Target, deleteCommand.Predicate, setClauses.AsReadOnly(), null)); }
public SqlDeleteGenerator(DbDeleteCommandTree commandTree, NpgsqlProviderManifest providerManifest) : base(providerManifest) { _commandTree = commandTree; }
public SqlDeleteGenerator(DbDeleteCommandTree commandTree) { _commandTree = commandTree; }
public void TreeCreated(DbCommandTreeInterceptionContext interceptionContext) { var logSchemaName = "test"; var logTableName = "TransactionHistory"; if (!(interceptionContext.OriginalResult.CommandTreeKind.Equals(DbCommandTreeKind.Insert) || interceptionContext.OriginalResult.CommandTreeKind.Equals(DbCommandTreeKind.Update) || interceptionContext.OriginalResult.CommandTreeKind.Equals(DbCommandTreeKind.Delete))) { return; } DbInsertCommandTree insertCommand = null; DbUpdateCommandTree updateCommand = null; DbDeleteCommandTree deleteCommand = null; if (interceptionContext.OriginalResult.CommandTreeKind.Equals(DbCommandTreeKind.Insert) && interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { insertCommand = interceptionContext.Result as DbInsertCommandTree; } if (interceptionContext.OriginalResult.CommandTreeKind.Equals(DbCommandTreeKind.Update) && interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { updateCommand = interceptionContext.Result as DbUpdateCommandTree; } if (interceptionContext.OriginalResult.CommandTreeKind.Equals(DbCommandTreeKind.Delete) && interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { deleteCommand = interceptionContext.Result as DbDeleteCommandTree; } if (((insertCommand != null && insertCommand.Target.VariableName.Contains(logTableName)) || (updateCommand != null && updateCommand.Target.VariableName.Contains(logTableName)) || (deleteCommand != null && deleteCommand.Target.VariableName.Contains(logTableName)))) { return; } var command = interceptionContext.DbContexts.First().Database.Connection.CreateCommand(); command.CommandText = $"INSERT INTO [{logSchemaName}].[{logTableName}]([ChangedUser],[TransactionDetail],[ChangingUser],[TransactionDate]) SELECT @ChangedUser, @TransactionDetail_Created, @ChangingUser, @TransactionDate;" + command.CommandText; command.Parameters.AddRange(new[] { new SqlParameter("@ChangedUser", ""), new SqlParameter("@ChangingUser", ""), new SqlParameter("@TransactionDetail_Updated", ""), new SqlParameter("@TransactionDate", DateTime.Now) }); if (insertCommand != null) { var context = interceptionContext.WithDbContext(interceptionContext.DbContexts.First()); } if (interceptionContext.OriginalResult.DataSpace == DataSpace.SSpace) { var queryCommand = interceptionContext.Result as DbQueryCommandTree; if (queryCommand != null) { var newQuery = queryCommand.Query.Accept(new StringTrimmerQueryVisitor()); interceptionContext.Result = new DbQueryCommandTree( queryCommand.MetadataWorkspace, queryCommand.DataSpace, newQuery); } } }
public DeleteCommandAction(DbDeleteCommandTree commandTree) { this.commandTree = commandTree; }
private DbModificationCommandTree ConvertInternal( DbDeleteCommandTree commandTree) { this._currentFunctionMapping = this._entityTypeModificationFunctionMapping != null ? this._entityTypeModificationFunctionMapping.DeleteFunctionMapping : this._associationSetModificationFunctionMapping.DeleteFunctionMapping; return((DbModificationCommandTree) new DbDeleteCommandTree(commandTree.MetadataWorkspace, commandTree.DataSpace, commandTree.Target, commandTree.Predicate.Accept <DbExpression>((DbExpressionVisitor <DbExpression>) this))); }
public VfpExpression Visit(DbDeleteCommandTree commandTree) { return(new VfpDeleteCommandTree(CreateDbExpressionBinding(commandTree.Target), commandTree.Predicate.Accept(this), commandTree.Parameters)); }