June 07, 2012

SSIS Template Generator and Reverse Engineering Part II

After completing a new schema for my new ssis framework finally is done, now is time to start coding the classes here is the First class to traverse a package enjoy:

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 ETL.SSIS
{
    class ssisReverse 
    {
        ssisPackage _package;
        string DirectoryPath = null;
        String PackageName = null;

        #region constructor
       
        #endregion
        #region public properties
        public ssisReverse(String PackageDirectoryPath, String PackageName)
        {
            String FullFilePath = PackageDirectoryPath + "\\" + PackageName + ".dtsx";
            if (File.Exists(FullFilePath))
            {
                _package = new ssisPackage(PackageDirectoryPath, PackageName);
                SearchPackage(_package.getExecutables);
            }
            else
            {
                Console.WriteLine("Error: {0} Package Not found", FullFilePath);
            }
        }
        #endregion 
        #region private sub modules
        // Search Packge Module to find Components 
      
        private void SearchPackage (Executables CollExec)
        {
            foreach (Executable exc in CollExec)
            {
                Sequence sequence = (Sequence)exc;
                if (sequence != null)
                {
                    Console.WriteLine("Sequence Container - {0}", sequence.Name);
                    SearchPackage(sequence.Executables);
                    GetEventHandlers(sequence.EventHandlers);
                    continue; 
                }
                ForEachLoop forEachLoop = (ForEachLoop) exc;
                if (forEachLoop != null)
                {
                    Console.WriteLine("For Each Loop - {0}", forEachLoop.Name);
                    SearchPackage(forEachLoop.Executables);
                    GetEventHandlers(forEachLoop.EventHandlers);
                    continue;
                }
                ForLoop floop = (ForLoop)exc;
                if (floop != null)
                {
                    Console.WriteLine("Sequence Container - {0}", floop.Name);
                    SearchPackage(floop.Executables);
                    GetEventHandlers(floop.EventHandlers);
                    continue; 

                }

                TaskHost taskHost = (TaskHost)exc;
                if (taskHost != null)
                {
                    GetTaskHost(taskHost);
                    GetEventHandlers(taskHost.EventHandlers);
                    continue;
                }

                
            }

        }
        // Recursive to find evenths
        private void GetEventHandlers(DtsEventHandlers colleventh)
        {
            foreach (DtsEventHandler eventh in colleventh)
            {
                SearchPackage(eventh.Executables);
            }
        }
        // Find dataflows
        private static void GetTaskHost(TaskHost th)
        {
            if (th == null)
            {
                return;
            }

            Console.WriteLine("TaskHost - {0}", th.Name);

           //DataFlow
            MainPipe pipeline = th.InnerObject as MainPipe;
            if (pipeline != null)
            {
                GetPipeline(pipeline);
            }
        }
        //SQL 2005
        private static void GetPipeline(MainPipe pipe)
        {
            foreach (IDTSComponentMetaData90 componentMetadata in pipe.ComponentMetaDataCollection)
            {
                Console.WriteLine("DataFlow Components - {0}", componentMetadata.Name);

             }
        }

        #endregion

    }
}

No comments:

Post a Comment

Contact Form

Name

Email *

Message *