예제 #1
0
        public override GlyphDefinition GetDefinitions(string fragment, TSqlStatement statement, GlyphDefinition definition, List <TSqlStatement> queries)
        {
            var scriptTables = ScriptDom.GetTableList(statement).Where(p => p is NamedTableReference).Cast <NamedTableReference>().ToList();

            var dacTables = GetDacTables();

            var rewriter     = new TableReferenceRewriter(fragment, scriptTables);
            var replacements = rewriter.GetReplacements(dacTables);

            if (replacements.Count > 0)
            {
                definition.Menu.Add(new MenuDefinition()
                {
                    Caption = "Correct Case Table Identifiers",
                    Action  = () => { },
                    Type    = MenuItemType.Header
                    ,
                    Glyph = definition
                });

                var offsettedReplacments = new List <Replacements>();
                foreach (var replacement in replacements)
                {
                    var replacement1 = replacement;

                    replacement1.Original = fragment.Substring(replacement1.OriginalOffset - statement.StartOffset, replacement1.OriginalLength);

                    offsettedReplacments.Add(replacement1);
                }

                if (replacements.Count > 1)
                {
                    var menu = new MenuDefinition();
                    menu.Operation = new ClippyReplacementOperations(offsettedReplacments);
                    menu.Action    = () => PerformAction(menu.Operation, menu.Glyph);

                    menu.Glyph   = definition;
                    menu.Caption = GetCaptionForAll(statement);
                    menu.Type    = MenuItemType.MenuItem;
                    definition.Menu.Add(menu);
                }


                foreach (var replacement in offsettedReplacments)
                {
                    var menu = new MenuDefinition();
                    menu.Action    = () => PerformAction(menu.Operation, menu.Glyph);
                    menu.Glyph     = definition;
                    menu.Caption   = string.Format("\t\"{0}\" into \"{1}\"", replacement.Original, replacement.Replacement);
                    menu.Type      = MenuItemType.MenuItem;
                    menu.Operation = new ClippyReplacementOperation(replacement);
                    definition.Menu.Add(menu);
                }
            }

            return(definition);
        }
예제 #2
0
        public void CorrectCaseAllTableNames()
        {
            var statements = new StatementEnumerator().GetStatements();

            var dacTables = new List <TableDescriptor>();

            foreach (var project in new ProjectEnumerator().Get(ProjectType.SSDT))
            {
                try
                {
                    var path = DacpacPath.Get(project);
                    dacTables.AddRange(new TableRepository(path).Get());
                }
                catch (Exception ex)
                {
                    OutputPane.WriteMessage("Error getting list of tables in project: {0}, error: {1}", project.Name, ex.Message);
                }
            }

            var alreadyChanged = new Dictionary <string, object>();


            foreach (var statement in statements.OrderByDescending(p => p.Statement.StartOffset))
            {
                var scriptTables = ScriptDom.GetTableList(statement.Statement).Where(p => p is NamedTableReference).Cast <NamedTableReference>().ToList();

                var rewriter     = new TableReferenceRewriter(statement.Script, scriptTables);
                var replacements = rewriter.GetReplacements(dacTables);


                var approvedReplacements = new List <Replacements>();

                foreach (var replacement in replacements)
                {
                    var key = string.Format("{0}:{1}:{2}", statement.FileName, replacement.OriginalOffset, replacement.OriginalLength);

                    if (alreadyChanged.ContainsKey(key))
                    {
                        continue;
                    }

                    approvedReplacements.Add(replacement);
                    alreadyChanged[key] = null;
                }

                if (approvedReplacements.Count > 0)
                {
                    var script = File.ReadAllText(statement.FileName);
                    OutputPane.WriteMessage("File: {0}", statement.FileName);
                    foreach (var replacement in approvedReplacements.OrderByDescending(p => p.OriginalOffset))
                    {
                        var from = script.Substring(replacement.OriginalOffset, replacement.OriginalLength);

                        var to = replacement.Replacement;
                        OutputPane.WriteMessageWithLink(statement.FileName, statement.Line, "\tChanging case of {0} to {1}", from, to);
                        script = script.Substring(0, replacement.OriginalOffset) + to + script.Substring(replacement.OriginalOffset + replacement.OriginalLength);
                    }

                    File.WriteAllText(statement.FileName, script);
                }
            }
        }