Exemple #1
0
        private static void ProcessDeleteEntries(string args)
        {
            Regex          deleteRegex = new Regex(@"^(?<table>(?:(?:\[(?:\S| )+\])|\S|\.)+)(?: (?<defaultDeleteAction>" + string.Join("|", SUPPORTED_DELETE_ACTIONS) + @"))?(?: (?:-d (?<d>(?: ?(?<foreignKey>(?:(?:\[(?:\S| )+\])|\S|\.)+)\=(?<deleteAction>" + string.Join("|", SUPPORTED_DELETE_ACTIONS) + @"))+)))?(?: (?:-c (?<c>.+)))?$");
            Match          deleteMatch = deleteRegex.Match(args);
            DatabaseObject deleteTable;

            if (!deleteMatch.Success)
            {
                Console.WriteLine($"Error: Invalid syntax for command {DELETE_ENTRIES_COMMAND}, syntax information can be showed by typing: help {DELETE_ENTRIES_COMMAND}");
                return;
            }
            else if (!_tableByName.TryGetValue(deleteMatch.Groups["table"].Value.Replace("[", "").Replace("]", "").ToLower(), out deleteTable))
            {
                Console.WriteLine($"Error: Table {deleteMatch.Groups["table"].Value} not found");
                return;
            }
            string conditions = "";

            ForeignKey.DeleteActions?defaultDeleteAction = null;
            if (deleteMatch.Groups["c"].Success)
            {
                conditions = deleteMatch.Groups["c"].Value;
            }
            if (deleteMatch.Groups["defaultDeleteAction"].Success)
            {
                if (DELETE_ACTION_CONVERTOR.ContainsKey(deleteMatch.Groups["defaultDeleteAction"].Value.ToUpper()))
                {
                    defaultDeleteAction = DELETE_ACTION_CONVERTOR[deleteMatch.Groups["defaultDeleteAction"].Value.ToUpper()];
                }
                else
                {
                    Console.WriteLine($"Error: Unsupported default delete action, {SUPPORTED_DELETE_ACTION_VALUES_TEXT}");
                    return;
                }
            }
            IDictionary <string, ForeignKey.DeleteActions> overrideDeleteActions = new Dictionary <string, ForeignKey.DeleteActions>();

            if (deleteMatch.Groups["foreignKey"].Success)
            {
                for (int i = 0; i < deleteMatch.Groups["foreignKey"].Captures.Count; i++)
                {
                    ForeignKey.DeleteActions deleteAction;
                    if (DELETE_ACTION_CONVERTOR.TryGetValue(deleteMatch.Groups["deleteAction"].Captures[i].Value.ToUpper(), out deleteAction))
                    {
                        overrideDeleteActions.Add(deleteMatch.Groups["foreignKey"].Captures[i].Value.ToLower(), deleteAction);
                    }
                    else
                    {
                        Console.WriteLine($"Error: Unsupported default delete action {deleteMatch.Groups["defaultDeleteAction"].Value} at foreign key {deleteMatch.Groups["foreignKey"].Captures[i].Value}, {SUPPORTED_DELETE_ACTION_VALUES_TEXT}");
                        return;
                    }
                }
            }
            ISet <DependencyEdge> edges = _dependencies.GetDescendantEdges(deleteTable);
            IDictionary <ForeignKey, ForeignKey.DeleteActions> foreignKeyDeleteActions = new Dictionary <ForeignKey, ForeignKey.DeleteActions>(edges.Count);

            foreach (DependencyEdge edge in edges)
            {
                ForeignKey.DeleteActions?overrideDeleteAction;
                if (overrideDeleteActions.ContainsKey(edge.ForeignKey.Name.ToLower()))
                {
                    overrideDeleteAction = overrideDeleteActions[edge.ForeignKey.Name.ToLower()];
                }
                else
                {
                    overrideDeleteAction = defaultDeleteAction;
                }
                if (overrideDeleteAction == null)
                {
                    foreignKeyDeleteActions.Add(edge.ForeignKey, edge.ForeignKey.DeleteAction);
                }
                else if (overrideDeleteAction.Value == ForeignKey.DeleteActions.SetDefault && edge.ForeignKey.HasDefaultValue)
                {
                    foreignKeyDeleteActions.Add(edge.ForeignKey, ForeignKey.DeleteActions.SetDefault);
                }
                else if (overrideDeleteAction.Value == ForeignKey.DeleteActions.SetNull && edge.ForeignKey.CanBeNull)
                {
                    foreignKeyDeleteActions.Add(edge.ForeignKey, ForeignKey.DeleteActions.SetNull);
                }
                else
                {
                    foreignKeyDeleteActions.Add(edge.ForeignKey, overrideDeleteAction.Value);
                }
            }
            try
            {
                Console.WriteLine("Delete procedure is running");
                string deleteQuery = _optimizer.DeleteEntriesFromTable(deleteTable, conditions, foreignKeyDeleteActions);
                Console.WriteLine(deleteQuery);
                Database.Instance.ExecuteNonResultQuery(deleteQuery);
                Console.WriteLine("Delete procedure was completed");
            }
            catch (DatabaseException exc)
            {
                Debug.WriteLine(exc);
                Console.WriteLine($"Error: cannot get data - {exc.InnerException.Message}");
                return;
            }
            catch (DeleteDependencyException exc)
            {
                Debug.WriteLine(exc);
                Console.WriteLine($"Error: cannot get data - {exc.Message}");
                return;
            }
            catch (SqlException exc)
            {
                Debug.WriteLine(exc);
                Console.WriteLine("Error: delete query failed");
                return;
            }
        }