Esempio n. 1
0
        public static bool Execute(OpenedFileManager openedFileManager)
        {
            var accessor = new ObjectAccessor();
            var objects  = accessor.GetObjects();

            var strToUrn = new Dictionary <string, Urn>();
            var items    = new List <Common.ChooseItem.Item>();

            foreach (var obj in objects)
            {
                strToUrn[obj.fullName] = obj.urn;
                var searchTerms = CreateSearchTerms(obj);
                items.Add(new Common.ChooseItem.Item(obj.fullName, obj.type, searchTerms));
            }

            string title  = "Choose object on " + accessor.ServerName();
            var    choose = new Common.ChooseItem(items.ToArray(), title);

            if (choose.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return(false);
            }

            string result    = choose.Result();
            Urn    resultUrn = strToUrn[result];
            string body      = accessor.GetObjectText(resultUrn);

            openedFileManager.Open(accessor.ServerName(), result, resultUrn, body);
            return(true);
        }
Esempio n. 2
0
        static public bool Execute(DTE2 application)
        {
            Document doc = application.ActiveDocument;

            if (doc == null)
            {
                return(false);
            }
            TextDocument textDoc = doc.Object("TextDocument") as TextDocument;

            if (textDoc == null)
            {
                return(false);
            }

            var    editPoint = textDoc.StartPoint.CreateEditPoint();
            string text      = editPoint.GetText(textDoc.EndPoint);

            text = text.Replace(Environment.NewLine, "\n");
            int cursorPos = textDoc.Selection.ActivePoint.AbsoluteCharOffset - 1;

            string      body    = text.Substring(0, cursorPos);
            ContextInfo context = JoinParser.ParseContext(body);

            if (context == null)
            {
                return(false);
            }

            string database      = Common.Connection.GetActiveDatabase(body);
            var    tableAccessor = new TableAccessor(database);

            var contextDatabases = new HashSet <string>();

            foreach (TableInfo t in context.AllTables())
            {
                tableAccessor.ResolveTable(t);
                contextDatabases.Add(t.Database());
            }

            var rules = JoinParser.ParseCustomRules(Properties.Settings.Default.CustomRules);

            foreach (var rule in rules)
            {
                foreach (var t in rule.AllTables())
                {
                    tableAccessor.ResolveTable(t);
                }
            }
            foreach (var contextDatabase in contextDatabases)
            {
                rules.AddRange(tableAccessor.GetForeignKeyRules(contextDatabase));
            }
            foreach (var t1 in context.AllTables())
            {
                foreach (var t2 in context.AllTables())
                {
                    if (t1 != t2)
                    {
                        bool hasRule = false;
                        foreach (var rule in rules)
                        {
                            if (rule.Match(t1, t2))
                            {
                                hasRule = true;
                                break;
                            }
                        }
                        if (hasRule)
                        {
                            continue;
                        }
                        var generated = RuleGenerator.Create(t1, t2);
                        if (generated != null)
                        {
                            rules.AddRange(generated);
                        }
                    }
                }
            }

            var options = new List <Tuple <string, string, int> >();

            if (context.newTable != null)
            {
                foreach (var rule in rules)
                {
                    foreach (var table in context.joinedTables)
                    {
                        var applied = rule.Apply(table, context.newTable);
                        if (applied != null)
                        {
                            if (!context.hasGlue)
                            {
                                applied = "\n  on " + applied;
                            }
                            options.Add(Tuple.Create(applied, rule.name, rule.priority));
                        }
                    }
                }
            }
            else
            {
                var usedAliases = new List <string>();
                foreach (var table in context.joinedTables)
                {
                    usedAliases.Add(table.Alias());
                }
                foreach (var rule in rules)
                {
                    foreach (var table in context.joinedTables)
                    {
                        var matched = rule.Match(table);
                        if (matched != null)
                        {
                            matched = matched.NewWithUniqueAlias(usedAliases);
                            string applied = context.hasGlue ? "" : "\njoin ";
                            applied += matched.Def(database) + "\n  on ";
                            applied += rule.Apply(table, matched);
                            options.Add(Tuple.Create(applied, rule.name, rule.priority));
                        }
                    }
                }
            }

            if (options.Count == 0)
            {
                return(false);
            }

            var items = new List <Common.ChooseItem.Item>();

            foreach (var option in options)
            {
                var searchTerms = new List <Common.ChooseItem.SearchTerm>();
                searchTerms.Add(new Common.ChooseItem.SearchTerm(option.Item1, option.Item3 + 100));
                searchTerms.Add(new Common.ChooseItem.SearchTerm(option.Item2, option.Item3));
                items.Add(new Common.ChooseItem.Item(option.Item1, option.Item2, searchTerms.ToArray()));
            }

            Common.ChooseItem dialog = new Common.ChooseItem(items.ToArray(), null);
            PlaceFormAtCaret(application, dialog);
            if (dialog.ShowDialog() != System.Windows.Forms.DialogResult.OK)
            {
                return(false);
            }
            string result = dialog.Result();

            if (result.StartsWith("\n") && Regex.IsMatch(body, @"\n\s*$"))
            {
                result = result.Substring(1);
            }
            if (!Regex.IsMatch(result, @"^\s") && !Regex.IsMatch(body, @"\s$"))
            {
                result = " " + result;
            }
            result = result.Replace("\n", "\n" + context.fromIndent);
            result = result.Replace("\n", Environment.NewLine); // back to crlf
            textDoc.Selection.Insert(result);
            return(true);
        }