Exemplo n.º 1
0
        public override void Emit(TextWriter outputWriter)
        {
            if (outputWriter != null)
            {
                string        identityColumnName   = _tableHelper.KeyColumn.Name;
                StringBuilder spParametersBuilder  = new StringBuilder();
                StringBuilder execArgumentsBuilder = new StringBuilder();
                StringBuilder uniqueColumnsBuilder = new StringBuilder();

                outputWriter.Write("\n");
                foreach (XPathNavigator nav in _tableNavigator.Select("rc:Columns/rc:Column", VulcanPackage.VulcanConfig.NamespaceManager))
                {
                    /* Build Argument List */
                    string columnName = nav.SelectSingleNode("@Name", VulcanPackage.VulcanConfig.NamespaceManager).Value;
                    string columnType = nav.SelectSingleNode("@Type", VulcanPackage.VulcanConfig.NamespaceManager).Value;
                    bool   isKey      = false;

                    if (
                        String.Compare(_tableHelper.KeyColumn.Name, columnName, StringComparison.InvariantCultureIgnoreCase) == 0
                        )
                    {
                        isKey = true;
                    }

                    spParametersBuilder.AppendFormat(
                        "\t@{0} {1} {2},\n",
                        columnName,
                        columnType,
                        isKey ? " OUTPUT" : ""
                        );

                    /* Re-Use this loop to build the INSERT statement */

                    execArgumentsBuilder.AppendFormat(
                        "\t\t@{0}{1},\n",
                        columnName,
                        isKey ? " OUTPUT" : ""
                        );
                }


                foreach (XPathNavigator nav in _tableNavigator.Select("rc:CheckAndInsertUniqueColumn", VulcanPackage.VulcanConfig.NamespaceManager))
                {
                    uniqueColumnsBuilder.AppendFormat(
                        "{0} = @{0} AND ",
                        nav.Value
                        );
                }

                // If its not > 0 then we have a problem, this stored proc should never get created.
                if (uniqueColumnsBuilder.Length <= 0)
                {
                    outputWriter.Flush();
                    return;
                }
                //remove trailing commas or newlines or ANDS
                spParametersBuilder.Replace(",", "", spParametersBuilder.Length - 2, 1);
                execArgumentsBuilder.Replace(",", "", execArgumentsBuilder.Length - 2, 1);
                uniqueColumnsBuilder.Replace("AND", "", uniqueColumnsBuilder.Length - 4, 3);

                outputWriter.Write("\n");
                TemplateEmitter te =
                    new TemplateEmitter(
                        "CheckAndInsertSP",
                        VulcanPackage,
                        _tableName,
                        CheckAndInsertSPEmitter.GetCheckAndInsertProcedureName(_tableName),
                        spParametersBuilder.ToString(),
                        execArgumentsBuilder.ToString(),
                        identityColumnName,
                        uniqueColumnsBuilder.ToString(),
                        InsertSPEmitter.GetInsertProcedureName(_tableName),
                        _tableHelper.KeyColumn.Properties["Type"]
                        );

                te.Emit(outputWriter);
                outputWriter.Write("\n");
                outputWriter.Flush();
            } // end if outputWriter != null
        }
Exemplo n.º 2
0
        public override void Emit(XPathNavigator patternNavigator)
        {
            if (patternNavigator != null)
            {

                string createTableFile = VulcanPackage.AddFileToProject(
                                                Resources.Create + Resources.Schema + Resources.Seperator +
                                                VulcanPackage.Name +
                                                Resources.ExtensionSQLFile
                                            );

                File.Delete(createTableFile);
                string insertSPFile = null;
                string checkAndinsertSPFile = null;
                string insertAndUpdateSPFile = null;
                bool hasCheckAndInsert = false;
                bool hasInsertAndUpdate = false;

                foreach (XPathNavigator nav in patternNavigator.Select("rc:Dimension", VulcanPackage.VulcanConfig.NamespaceManager))
                {
                    string dimensionName = nav.SelectSingleNode("@Name", VulcanPackage.VulcanConfig.NamespaceManager).Value;

                    Message.Trace(Severity.Debug, "Emitting Create Statement for " + dimensionName);

                    TableHelper th = new TableHelper(dimensionName, VulcanPackage.VulcanConfig, nav.SelectSingleNode("rc:Table", VulcanPackage.VulcanConfig.NamespaceManager));
                    TableEmitterEx te = new TableEmitterEx(th, VulcanPackage);
                    te.Emit(createTableFile, true);

                    if (String.IsNullOrEmpty(insertSPFile))
                    {
                        insertSPFile = VulcanPackage.AddFileToProject(
                                                Resources.SPPrefix + Resources.Seperator +
                                                Resources.Insert +
                                                VulcanPackage.Name +
                                                Resources.ExtensionSQLFile
                                                );
                        File.Delete(insertSPFile);
                    }

                    InsertSPEmitter spe = new InsertSPEmitter(dimensionName, nav.SelectSingleNode("rc:Table", VulcanPackage.VulcanConfig.NamespaceManager), th, VulcanPackage);
                    spe.Emit(insertSPFile, true);

                    if (nav.Select("rc:Table/rc:CheckAndInsertUniqueColumn", VulcanPackage.VulcanConfig.NamespaceManager).Count > 0)
                    {
                        hasCheckAndInsert = true;

                        if (String.IsNullOrEmpty(checkAndinsertSPFile))
                        {
                            checkAndinsertSPFile = VulcanPackage.AddFileToProject(
                                                           Resources.SPPrefix + Resources.Seperator +
                                                           Resources.CheckAndInsert +
                                                           VulcanPackage.Name +
                                                           Resources.ExtensionSQLFile
                                                           );

                            File.Delete(checkAndinsertSPFile);
                        }

                        CheckAndInsertSPEmitter cispe = new CheckAndInsertSPEmitter(dimensionName, nav.SelectSingleNode("rc:Table", VulcanPackage.VulcanConfig.NamespaceManager), th, VulcanPackage);
                        cispe.Emit(checkAndinsertSPFile, true);
                    }

                    if (nav.Select("rc:Table/rc:InsertOrUpdateUniqueColumn", VulcanPackage.VulcanConfig.NamespaceManager).Count > 0)
                    {
                        hasInsertAndUpdate = true;

                        if (String.IsNullOrEmpty(insertAndUpdateSPFile))
                        {
                            insertAndUpdateSPFile = VulcanPackage.AddFileToProject(
                                                           Resources.SPPrefix + Resources.Seperator +
                                                           Resources.CheckAndInsert +
                                                           Resources.Update +
                                                           VulcanPackage.Name +
                                                           Resources.ExtensionSQLFile
                                                           );

                            File.Delete(insertAndUpdateSPFile);
                        }
                        InsertAndUpdateSPEmitter iuspe = new InsertAndUpdateSPEmitter(dimensionName, nav.SelectSingleNode("rc:Table", VulcanPackage.VulcanConfig.NamespaceManager), th, VulcanPackage);
                        iuspe.Emit(insertAndUpdateSPFile, true);
                    }
                }

                Dictionary<string, object> properties = new Dictionary<string, object>();
                properties["Name"] = Resources.Create + Resources.Schema + Resources.Seperator + VulcanPackage.Name;

                //TODO: This is a hardcoded bug, should be removed when Dimensions are rewritten!
                Connection dimensionConnection = Connection.GetExistingConnection(VulcanPackage, patternNavigator);

                SQLTask createTableTask =
                    new SQLTask(
                                VulcanPackage,
                                properties["Name"].ToString(),
                                properties["Name"].ToString(),
                                ParentContainer,
                                dimensionConnection,
                                properties
                                );

                createTableTask.TransmuteToFileTask(properties["Name"] + Resources.ExtensionSQLFile);

                properties["Name"] = Resources.SPPrefix + Resources.Seperator +
                                        Resources.Insert +
                                        VulcanPackage.Name;

                this.FirstExecutableGeneratedByPattern = createTableTask.SQLTaskHost;
                this.LastExecutableGeneratedByPattern = this.FirstExecutableGeneratedByPattern;

                SQLTask createInsertSPTask =
                    new SQLTask(
                                VulcanPackage,
                                properties["Name"].ToString(),
                                properties["Name"].ToString(),
                                ParentContainer,
                                dimensionConnection,
                                properties
                                );
                createInsertSPTask.TransmuteToFileTask(properties["Name"] + Resources.ExtensionSQLFile);

                properties["Name"] = Resources.SPPrefix + Resources.Seperator +
                                        Resources.CheckAndInsert +
                                        VulcanPackage.Name;

                properties["Description"] = properties["Name"];

                VulcanPackage.AddPrecedenceConstraint(LastExecutableGeneratedByPattern, createInsertSPTask.SQLTaskHost, ParentContainer);
                this.LastExecutableGeneratedByPattern = createInsertSPTask.SQLTaskHost;

                if (hasCheckAndInsert)
                {
                    SQLTask createCheckInsertSPTask =
                        new SQLTask(
                                    VulcanPackage,
                                    properties["Name"].ToString(),
                                    properties["Name"].ToString(),
                                    ParentContainer,
                                    dimensionConnection,
                                    properties
                                    );
                    createCheckInsertSPTask.TransmuteToFileTask(properties["Name"] + Resources.ExtensionSQLFile);

                    properties["Name"] = Resources.SPPrefix + Resources.Seperator +
                                            Resources.Insert + Resources.Defaults +
                                            VulcanPackage.Name;

                    properties["Description"] = properties["Name"];

                    VulcanPackage.AddPrecedenceConstraint(createInsertSPTask.SQLTaskHost, createCheckInsertSPTask.SQLTaskHost, ParentContainer);
                    this.LastExecutableGeneratedByPattern = createCheckInsertSPTask.SQLTaskHost;
                } // hasCheckAndInsert

                if (hasInsertAndUpdate)
                {
                    properties["Name"] = properties["Name"] = Resources.SPPrefix + Resources.Seperator +
                                            Resources.CheckAndInsert + Resources.Update +
                                            VulcanPackage.Name;
                    properties["Description"] = properties["Name"];

                    SQLTask createInsertUpdateTask =
            new SQLTask(
                VulcanPackage,
                properties["Name"].ToString(),
                properties["Name"].ToString(),
                ParentContainer,
                dimensionConnection,
                properties
                );
                    createInsertUpdateTask.TransmuteToFileTask(properties["Name"] + Resources.ExtensionSQLFile);

                    VulcanPackage.AddPrecedenceConstraint(LastExecutableGeneratedByPattern, createInsertUpdateTask.SQLTaskHost, ParentContainer);
                    this.LastExecutableGeneratedByPattern = createInsertUpdateTask.SQLTaskHost;

                } // hasInsertAndUpdate
            }
        }