July 19, 2012

SSIS Reverse Class Modifications

Tonight is the premier of the movie of the Dark Night the previous are crazy and the ticket sale will start in a few minutes and I will be heading to the movies in a few minutes, but, before that here is the latest code of the reverse ssis

  
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SqlServer.Dts.Runtime;
using Microsoft.SqlServer.Dts.Pipeline.Wrapper;

using System.IO;

namespace jnovo.ETL.ssisManagement.Prototype
{
    public class ssisReverse
    {
        ssisPackage _package;



        Boolean PackageExists = false;
        public const string PackageCreationName = "Package";
        public const string EventHandlerCreationName = "EventHandler";
        public const string ConnectionCreationName = "Connection";
        public const string SequenceCreationName = "Sequence";
        public const string ForLoopCreationName = "ForLoop";
        public const string ForEachLoopCreationName = "ForEachLoop";
        #region constructor

        #endregion
        #region public properties
        public ssisReverse(String PackageDirectoryPath, String PackageName)
        {
            String FullFilePath = PackageDirectoryPath + "\\" + PackageName + ".dtsx";
            if (File.Exists(FullFilePath))
            {
                _package = new ssisPackage(FullFilePath);
                PackageExists = true;

            }
            else
            {
                Console.WriteLine("Error: {0} Package Not found", FullFilePath);
                PackageExists = false;

            }
            if (PackageExists)
            {

            }
        }
        #endregion
        #region public sub modules

        public void DisplayResults()
        {
            if (PackageExists)
            {
                Console.WriteLine("Searching Package: {0}", _package.PackageName);
                GetConnections();
                SearchPackage(_package.getExecutables);

            }
            else
            {
                Console.WriteLine("Errors with the Package or Path please validate");
            }
        }

        #endregion

        #region private sub modules
        // Search Packge Module to find Components

        private void SearchPackage(Executables CollExec)
        {
            int count = 0;
            int count2 = (CollExec.Count);
            while (count != (count2))

            // foreach (Executable exc in CollExec)
            {

                Executable exc = CollExec[count++];
                switch (exc.GetType().Name)
                {
                    case "TaskHost":
                        TaskHost th = (TaskHost)exc;
                        String Desc = th.CreationName;
                        int idx = Desc.IndexOf(",");
                        if (idx > 0)
                        {

                            Desc = Desc.Substring(0, idx);
                            Desc = Desc.Substring(Desc.LastIndexOf(".") + 1);

                        }

                        Console.WriteLine("TaskHost - {0} TaskHostType - {1}", th.Name, Desc);
                        if (Desc.CompareTo("SSIS.Pipeline.2") == 0)
                        {
                            GetTaskHost(th);
                        }


                        // DtsContainer e = (DtsContainer)exc;
                        // GetContainerType(e);

                        GetEventHandlers(th.EventHandlers);


                        th = null;
                        continue;
                    case "Sequence":
                        Sequence seq = (Sequence)exc;
                        Console.WriteLine("Sequence Container - {0}", seq.Name);
                        //  GetPrecedence(seq.PrecedenceConstraints);
                        SearchPackage(seq.Executables);
                        GetEventHandlers(seq.EventHandlers);

                        // seq = null;
                        continue;
                    case "ForEachLoop":
                        ForEachLoop fel = (ForEachLoop)exc;
                        Console.WriteLine("ForEachLoop Container - {0}", fel.Name);
                        SearchPackage(fel.Executables);
                        GetEventHandlers(fel.EventHandlers);
                        //   GetPrecedence(fel.PrecedenceConstraints);
                        fel = null;
                        continue;
                    case "ForLoop":
                        ForLoop fl = (ForLoop)exc;
                        Console.WriteLine("ForLoop Container - {0}", fl.Name);
                        SearchPackage(fl.Executables);
                        GetEventHandlers(fl.EventHandlers);
                        //    GetPrecedence(fl.PrecedenceConstraints);
                        fl = null;
                        continue;

                }

            }

        }


        // Recursive to find evenths
        private void GetContainerType(DtsContainer e)
        {
            Console.WriteLine("TaskHostContainer: {0}", e.GetType().ToString());
        }
        private void GetEventHandlers(DtsEventHandlers colleventh)
        {
            foreach (DtsEventHandler eventh in colleventh)
            {
                SearchPackage(eventh.Executables);
            }
        }
        public void GetComponentInfos()
        {
            Application app = _package.getApplication;
            PipelineComponentInfos cinfos = app.PipelineComponentInfos;
            foreach (PipelineComponentInfo componentInfo in cinfos)
            {

                Console.WriteLine("\"" + componentInfo.Name + "\"" + "," + "\"" + componentInfo.CreationName + "\"");
            }
            Console.Read();
        }
        // Find dataflows
        private void GetTaskHost(TaskHost th)
        {
            if (th == null)
            {
                return;
            }


            Console.WriteLine("TaskHostType--> {0}", th.CreationName);
            MainPipe pipeline = th.InnerObject as MainPipe;
            if (pipeline != null)
            {

                GetPipeline(th, pipeline);
                getPathCollections(pipeline.PathCollection);
            }
        }
        //SQL 2005
        private void GetPipeline(TaskHost th, MainPipe pipe)
        {


            foreach (IDTSComponentMetaData100 componentMetadata in pipe.ComponentMetaDataCollection)
            {


                Console.WriteLine("DataFlow: - {0} Component :{1} ", th.Name.ToString(), componentMetadata.Name);



            }
        }
        private void GetPrecedence(PrecedenceConstraints precedenceconstraints)
        {
            foreach (PrecedenceConstraint pre in precedenceconstraints)
            {

                Console.WriteLine("PrecedenceConstrain: {0}", pre.Name);
                Console.WriteLine("From  {0}", pre.PrecedenceExecutable.ToString());
                Console.WriteLine("To {0}", pre.ConstrainedExecutable.ToString());
            }
        }
        private void getPathCollections(IDTSPathCollection100 path)
        {
            foreach (IDTSPath100 p in path)
            {
                Console.WriteLine("DataFlow OutPutPaths: {0} TO {1}", p.StartPoint.Component.Name.ToString(), p.EndPoint.Component.Name.ToString());
            }
        }
        private void GetConnections()
        {
            foreach (ConnectionManager con in _package.getConnections)
            {
                Console.WriteLine("Connections: {0}", con.Name);
            }
        }

        #endregion

    }
}

No comments:

Post a Comment

Contact Form

Name

Email *

Message *