Exemple #1
0
        public override void Emit(XPathNavigator patternNavigator)
        {
            // Reloads invalidate the ParentContainer, so we should do it much later.
            string etlName =
                patternNavigator.SelectSingleNode("@Name", VulcanPackage.VulcanConfig.NamespaceManager).Value;

            Message.Trace(Severity.Notification, "{0}", etlName);
            bool delayValidation = patternNavigator.SelectSingleNode("@DelayValidation").ValueAsBoolean;

            string sourceName = patternNavigator.SelectSingleNode("rc:SourceConnection/@Name", VulcanPackage.VulcanConfig.NamespaceManager).Value;

            Connection sourceConnection =
                Connection.GetExistingConnection(
                    VulcanPackage,
                    patternNavigator.SelectSingleNode("rc:SourceConnection/@Name", VulcanPackage.VulcanConfig.NamespaceManager).Value
                    );

            if (sourceConnection != null)
            {
                string query = patternNavigator.SelectSingleNode("rc:Query", VulcanPackage.VulcanConfig.NamespaceManager).Value.Trim();

                // Add the Data Flow Task to the Package.
                DTS.TaskHost pipeHost = (DTS.TaskHost)ParentContainer.Executables.Add("STOCK:PipelineTask");
                pipeHost.Properties["DelayValidation"].SetValue(pipeHost, delayValidation);
                MainPipe dataFlowTask = (MainPipe)pipeHost.InnerObject;
                pipeHost.Name = etlName;

                // Add the Source (this is temporary and will be replaced with a new style of Source element)
                IDTSComponentMetaData90 sourceDataComponent = dataFlowTask.ComponentMetaDataCollection.New();
                sourceDataComponent.ComponentClassID = "DTSAdapter.OleDbSource.1";

                // IMPORTANT! If you do not Instantiate() and ProvideComponentProperties first,
                // the component names do not get set... this is bad.
                CManagedComponentWrapper oleInstance = sourceDataComponent.Instantiate();
                oleInstance.ProvideComponentProperties();

                sourceDataComponent.Name = etlName + " Source";

                if (sourceDataComponent.RuntimeConnectionCollection.Count > 0)
                {
                    sourceDataComponent.RuntimeConnectionCollection[0].ConnectionManager =
                        DTS.DtsConvert.ToConnectionManager90(
                            sourceConnection.ConnectionManager
                            );
                    sourceDataComponent.RuntimeConnectionCollection[0].ConnectionManagerID =
                        sourceConnection.ConnectionManager.ID;
                }

                oleInstance.SetComponentProperty("AccessMode", 2);
                oleInstance.SetComponentProperty("SqlCommand", query);

                try
                {
                    oleInstance.AcquireConnections(null);
                    oleInstance.ReinitializeMetaData();
                    oleInstance.ReleaseConnections();
                }
                catch (System.Runtime.InteropServices.COMException ce)
                {
                    Message.Trace(Severity.Error, ce, "OLEDBSource:{0}: {1}: Source {2}: Query {3}", sourceDataComponent.GetErrorDescription(ce.ErrorCode), ce.Message, sourceConnection.ConnectionManager.Name, query);
                }
                catch (Exception e)
                {
                    Message.Trace(Severity.Error, e, "OLEDBSource:{0}: Source {1}: Query {2}", e.Message, sourceConnection.ConnectionManager.Name, query);
                }

                //Map parameter variables:

                StringBuilder parameterBuilder = new StringBuilder();
                foreach (XPathNavigator paramNav in patternNavigator.Select("rc:Parameter", VulcanPackage.VulcanConfig.NamespaceManager))
                {
                    string name    = paramNav.SelectSingleNode("@Name").Value;
                    string varName = paramNav.SelectSingleNode("@VariableName").Value;

                    if (VulcanPackage.DTSPackage.Variables.Contains(varName))
                    {
                        DTS.Variable variable = VulcanPackage.DTSPackage.Variables[varName];
                        parameterBuilder.AppendFormat("\"{0}\",{1};", name, variable.ID);
                    }
                    else
                    {
                        Message.Trace(Severity.Error, "DTS Variable {0} does not exist", varName);
                    }
                }

                oleInstance.SetComponentProperty("ParameterMapping", parameterBuilder.ToString());

                ///Transformation Factory
                IDTSComponentMetaData90 parentComponent = sourceDataComponent;
                XPathNavigator          transNav        = patternNavigator.SelectSingleNode("rc:Transformations", VulcanPackage.VulcanConfig.NamespaceManager);
                if (transNav != null)
                {
                    foreach (XPathNavigator nav in transNav.SelectChildren(XPathNodeType.Element))
                    {
                        // this is naughty but can be fixed later :)
                        Transformation t = TransformationFactory.ProcessTransformation(VulcanPackage, parentComponent, dataFlowTask, nav);
                        if (t != null)
                        {
                            parentComponent = t.Component;
                        }
                    }
                }

                XPathNavigator destNav = patternNavigator.SelectSingleNode("rc:Destination", VulcanPackage.VulcanConfig.NamespaceManager);
                if (destNav != null)
                {
                    string           name             = destNav.SelectSingleNode("@Name").Value;
                    Connection       destConnection   = Connection.GetExistingConnection(VulcanPackage, destNav.SelectSingleNode("@ConnectionName").Value);
                    string           tableName        = String.Format(System.Globalization.CultureInfo.InvariantCulture, "{0}", destNav.SelectSingleNode("@Table").Value.Trim());
                    OLEDBDestination oledbDestination = new OLEDBDestination(VulcanPackage, dataFlowTask, parentComponent, name, name, destConnection, tableName);

                    string accessMode       = destNav.SelectSingleNode("@AccessMode").Value;
                    bool   tableLock        = destNav.SelectSingleNode("@TableLock").ValueAsBoolean;
                    bool   checkConstraints = destNav.SelectSingleNode("@CheckConstraints").ValueAsBoolean;
                    bool   keepIdentity     = destNav.SelectSingleNode("@KeepIdentity").ValueAsBoolean;
                    bool   keepNulls        = destNav.SelectSingleNode("@KeepNulls").ValueAsBoolean;

                    int rowsPerBatch        = destNav.SelectSingleNode("@RowsPerBatch").ValueAsInt;
                    int maxInsertCommitSize = destNav.SelectSingleNode("@MaximumInsertCommitSize").ValueAsInt;

                    switch (accessMode.ToUpperInvariant())
                    {
                    case "TABLE":
                        oledbDestination.ComponentInstance.SetComponentProperty("AccessMode", 0);
                        oledbDestination.ComponentInstance.SetComponentProperty("OpenRowset", tableName);
                        break;

                    case "TABLEFASTLOAD":
                        oledbDestination.ComponentInstance.SetComponentProperty("AccessMode", 3);
                        oledbDestination.ComponentInstance.SetComponentProperty("OpenRowset", tableName);

                        oledbDestination.ComponentInstance.SetComponentProperty("FastLoadKeepIdentity", keepIdentity);
                        oledbDestination.ComponentInstance.SetComponentProperty("FastLoadKeepNulls", keepNulls);
                        oledbDestination.ComponentInstance.SetComponentProperty("FastLoadMaxInsertCommitSize", maxInsertCommitSize);

                        StringBuilder fastLoadOptions = new StringBuilder();
                        if (tableLock)
                        {
                            fastLoadOptions.AppendFormat("TABLOCK,");
                        }
                        if (checkConstraints)
                        {
                            fastLoadOptions.AppendFormat("CHECK_CONSTRAINTS,");
                        }
                        if (rowsPerBatch > 0)
                        {
                            fastLoadOptions.AppendFormat("ROWS_PER_BATCH = {0}", rowsPerBatch);
                        }
                        fastLoadOptions = fastLoadOptions.Replace(",", "", fastLoadOptions.Length - 5, 5);

                        oledbDestination.ComponentInstance.SetComponentProperty("FastLoadOptions", fastLoadOptions.ToString());
                        break;

                    default:
                        Message.Trace(Severity.Error, "Unknown Destination Load Type of {0}", accessMode);
                        break;
                    }

                    try
                    {
                        oledbDestination.InitializeAndMapDestination();
                    }
                    catch (Exception)
                    {
                    }

                    // Map any overrides
                    foreach (XPathNavigator nav in destNav.Select("rc:Map", VulcanPackage.VulcanConfig.NamespaceManager))
                    {
                        string source = nav.SelectSingleNode("@Source").Value;
                        string destination;
                        bool   unMap = false;

                        if (nav.SelectSingleNode("@Destination") == null)
                        {
                            unMap       = true;
                            destination = source;
                        }
                        else
                        {
                            destination = nav.SelectSingleNode("@Destination").Value;
                        }

                        oledbDestination.Map(source, destination, unMap);
                    }
                } // end DestNav != null
                this.FirstExecutableGeneratedByPattern = pipeHost;
                this.LastExecutableGeneratedByPattern  = pipeHost;
            } //END sourceConnection != null
            else
            {
                Message.Trace(Severity.Error, "Source Connection {0} does not exist in {1}", sourceName, etlName);
            }
        } //END function Emit