public void SyntaxHighlight()
 {
     TextHandling.SyntaxHighlightSql(localRichTextBoxGenerationOutput, localRichTextBoxGenerationOutput.Text);
 }
Exemplo n.º 2
0
        /// <summary>
        ///   Create output using Handlebars as templating engine
        /// </summary>
        private void GenerateFromPattern()
        {
            // Establish the current time at the start of generation, to display only messages related to the current generation run.
            var currentTime = DateTime.Now;

            localRichTextBoxGenerationOutput.Clear();
            RaiseOnClearMainText();
            localTabControl.SelectedIndex = 0;

            // Loop through the checked items, select the right mapping and generate the pattern
            if (_localCheckedListBox.CheckedItems.Count != 0)
            {
                for (int x = 0; x <= _localCheckedListBox.CheckedItems.Count - 1; x++)
                {
                    var targetTableName = _localCheckedListBox.CheckedItems[x].ToString();
                    _localRichTextBox.AppendText(@"Processing generation for " + targetTableName + ".\r\n");

                    // Only process the selected items in the total of available source-to-target mappings
                    ItemList.TryGetValue(targetTableName, out var dataObjectMappingList);

                    // Return the result to the user
                    try
                    {
                        // Compile the template, and merge it with the metadata
                        var template = Handlebars.Compile(localRichTextBoxGenerationPattern.Text);
                        var result   = template(dataObjectMappingList);

                        // Check if the metadata needs to be displayed
                        if (DisplayJsonFlag)
                        {
                            try
                            {
                                var json = JsonConvert.SerializeObject(dataObjectMappingList, Formatting.Indented);
                                localRichTextBoxGenerationOutput.AppendText(json + "\r\n\r\n");
                            }
                            catch (Exception ex)
                            {
                                RaiseOnChangeMainText("An error was encountered while generating the Json metadata. The error message is: " + ex);
                            }
                        }

                        // Display the output of the template to the user
                        localRichTextBoxGenerationOutput.AppendText(result);

                        // Spool the output to disk
                        if (SaveOutputFileFlag)
                        {
                            VdwUtility.SaveOutputToDisk(FormBase.VdwConfigurationSettings.VdwOutputPath + targetTableName + ".sql", result);
                        }

                        //Generate in database
                        if (GenerateInDatabaseFlag)
                        {
                            // Find the right connection for the pattern connection key
                            var localConnection = TeamConfiguration.GetTeamConnectionByInternalId(localLabelActiveConnectionKeyValue.Text, FormBase.TeamConfigurationSettings.ConnectionDictionary);

                            if (localConnection != null)
                            {
                                var conn = new SqlConnection {
                                    ConnectionString = localConnection.CreateSqlServerConnectionString(false)
                                };

                                VdwUtility.CreateVdwSchema(conn);
                                VdwUtility.ExecuteOutputInDatabase(conn, result);
                            }
                            else
                            {
                                FormBase.VdwConfigurationSettings.VdwEventLog.Add(Event.CreateNewEvent(EventTypes.Error, $"There was an issue establishing a connection to generate the output for {targetTableName}. Is there a TEAM connections file in the configuration directory?"));
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        FormBase.VdwConfigurationSettings.VdwEventLog.Add(Event.CreateNewEvent(EventTypes.Error, $"The template could not be compiled. The error message is: {ex.Message}"));
                    }
                }
            }
            else
            {
                _localRichTextBox.AppendText($"There was no metadata selected to generate {_inputNiceName} code. Please check the metadata schema - are there any {_inputNiceName} objects selected?");
            }

            // Report back to the user
            int errorCounter = 0;

            foreach (Event individualEvent in FormBase.VdwConfigurationSettings.VdwEventLog)
            {
                if (individualEvent.eventCode == 1 && individualEvent.eventTime > currentTime)
                {
                    errorCounter++;
                    RaiseOnChangeMainText(individualEvent.eventDescription + "\r\n");
                }
            }

            RaiseOnChangeMainText($"\r\n{errorCounter} error(s) have been found.\r\n");
            RaiseOnChangeMainText($"\r\nAssociated scripts have been saved in {FormBase.VdwConfigurationSettings.VdwOutputPath}.\r\n");

            // Apply syntax highlighting
            TextHandling.SyntaxHighlightSql(localRichTextBoxGenerationOutput, localRichTextBoxGenerationOutput.Text);
        }