Exemplo n.º 1
0
        public string ExtractIntoFunction()
        {
            var browser = new SolutionBrowserForm("");

            browser.ShowDialog();

            var destination = browser.DestinationItem;

            if (destination == null)
            {
                return(null);
            }

            var name = browser.GetObjectName();

            var function     = new CreateFunctionStatement();
            var returnSelect = (function.ReturnType = new SelectFunctionReturnType()) as SelectFunctionReturnType;

            returnSelect.SelectStatement = GetSelectStatementForQuery();
            function.Name = name.ToSchemaObjectName();

            var classFolder = destination.ProjectItems.AddFromTemplate("Procedure", name.UnQuote() + ".sql");
            var filePath    = classFolder.GetStringProperty("FullPath");

            File.WriteAllText(filePath, ScriptDom.GenerateTSql(function));

            classFolder.Open().Visible = true;

            return(GetCallingCode(function));
        }
Exemplo n.º 2
0
        public void CreateSchemas()
        {
            IList <ParseError> errors;
            var fragment = new TSql120Parser(false).Parse(new StringReader(_scripts), out errors);

            if (fragment == null)
            {
                return;
            }

            var visitor = new ProcedureVisitor();

            fragment.Accept(visitor);

            foreach (var procedure in visitor.Procedures)
            {
                var browser = new SolutionBrowserForm(procedure.ProcedureReference.Name.BaseIdentifier.Value.Quote());
                browser.ShowDialog();

                var destination = browser.DestinationItem;
                if (destination == null)
                {
                    continue;
                }

                var parentProjectItem = destination;

                var name   = browser.GetObjectName();
                var script = GetScript(name);

                for (var i = 1; i <= parentProjectItem.ProjectItems.Count; i++)
                {
                    var item = parentProjectItem.ProjectItems.Item(i);
                    if (item.Name.UnQuote() == name.UnQuote())
                    {
                        CreateNewFile(item, name, script);
                        return;
                    }
                }

                var folder = parentProjectItem.ProjectItems.AddFolder(name.UnQuote());
                CreateNewFile(folder, name, script);
            }

            foreach (var procedure in visitor.Functions)
            {
                var browser = new SolutionBrowserForm(procedure
                                                      .Name.BaseIdentifier.Value.Quote());
                browser.ShowDialog();

                var destination = browser.DestinationItem;
                if (destination == null)
                {
                    continue;
                }

                var parentProjectItem = destination;

                var name   = browser.GetObjectName();
                var script = GetScript(name);

                for (var i = 1; i <= parentProjectItem.ProjectItems.Count; i++)
                {
                    var item = parentProjectItem.ProjectItems.Item(i);
                    if (item.Name.UnQuote() == name.UnQuote())
                    {
                        CreateNewFile(item, name, script);
                        return;
                    }
                }

                var folder = parentProjectItem.ProjectItems.AddFolder(name.UnQuote());
                CreateNewFile(folder, name, script);
            }
        }
Exemplo n.º 3
0
        public void Go()
        {
            IList <ParseError> errors;
            var fragment = new TSql120Parser(false).Parse(new StringReader(_scripts), out errors);

            if (fragment == null)
            {
                return;
            }

            var visitor = new ProcedureVisitor();

            fragment.Accept(visitor);

            using (var procedureRepository = new ProcedureRepository(DacpacPath.Get(_sourceProject)))
                using (var functionRepository = new FunctionRepository(DacpacPath.Get(_sourceProject)))
                {
                    foreach (var procedure in visitor.Procedures)
                    {
                        var browser = new SolutionBrowserForm("test " + procedure.ProcedureReference.Name.BaseIdentifier.Value.UnQuote() + " does something");
                        browser.ShowDialog();

                        var destination = browser.DestinationItem;
                        if (destination == null)
                        {
                            continue;
                        }

                        if (String.IsNullOrEmpty(DacpacPath.Get(_sourceProject)) && !File.Exists(DacpacPath.Get(_sourceProject)))
                        {
                            MessageBox.Show("Cannot find dacpac for project");
                            return;
                        }

                        var parentProjectItem = destination;

                        var name = browser.GetObjectName();

                        var proc = procedureRepository.FirstOrDefault(p => p.Name.EqualsName(procedure.ProcedureReference.Name));
                        if (proc == null)
                        {
                            MessageBox.Show(string.Format("Cannot find stored procedure {0} in project compiled dacpac", procedure.ProcedureReference.Name));
                            return;
                        }

                        var testBuilder = new ProcedureBuilder(procedure.ProcedureReference.Name.BaseIdentifier.Value.UnQuote(), name, proc);
                        var script      = testBuilder.GetScript();

                        CreateNewFile(parentProjectItem, name, script);
                    }

                    foreach (var procedure in visitor.Functions)
                    {
                        var browser = new SolutionBrowserForm("test " + procedure.Name.BaseIdentifier.Value.UnQuote() + " does something");
                        browser.ShowDialog();

                        var destination = browser.DestinationItem;
                        if (destination == null)
                        {
                            continue;
                        }

                        if (String.IsNullOrEmpty(DacpacPath.Get(_sourceProject)) && !File.Exists(DacpacPath.Get(_sourceProject)))
                        {
                            MessageBox.Show("Cannot find dacpac for project");
                            return;
                        }

                        var parentProjectItem = destination;

                        var name = browser.GetObjectName();

                        var proc = functionRepository.FirstOrDefault(p => p.Name.EqualsName(procedure.Name));
                        if (proc == null)
                        {
                            MessageBox.Show(string.Format("Cannot find stored procedure {0} in project compiled dacpac", procedure.Name));
                            return;
                        }

                        var testBuilder = new ProcedureBuilder(procedure.Name.BaseIdentifier.Value.UnQuote(), name, proc);
                        var script      = testBuilder.GetScript();

                        CreateNewFile(parentProjectItem, name, script);
                    }
                }
        }