/// <summary> /// Use an arbitrary template to generate an SQL script. /// </summary> /// <param name="configFile"></param> public static void GenerateScript(Configs.AllTables[] config, string templateFileName, string paramRunOn) { // load config data Configs.AllTables sharedConfig = new Configs.AllTables(); string templateContents = Generators.GetTemplateContents(templateFileName); string paramFileNameTemplate = GetOutputFileNameMask(paramRunOn); // generate output one file at a time for (int i = 0; i < config.Length; i++) { // merge with the previous full version of the config sharedConfig = (Configs.AllTables)config[i].Merge(sharedConfig); // get the column list if there is {3} group in the template string tableCols = null; if (templateContents.Contains("{3}")) { tableCols = DbAccess.GetTableColumns(config[i].masterCS, config[i].masterTableOrSP); if (string.IsNullOrEmpty(tableCols)) { Program.WriteLine(); Program.WriteLine($"Missing table definition for {config[i].masterDB}..{config[i].masterTableOrSP}", ConsoleColor.Red); Program.ExitApp(); } } // get SP param list if needed var spParams = new DbAccess.ProcedureParts(); if (templateContents.Contains("{4}") || templateContents.Contains("{5}") || templateContents.Contains("{6}")) { spParams = DbAccess.GetProcedureParams(config[i].masterCS, config[i].masterTableOrSP); } // get a list of non-identity columns for insert statements, if needed string insertableColumnNames = ""; if (templateContents.Contains("{7}")) { insertableColumnNames = DbAccess.GetInsertableTableColumnNames(config[i].masterCS, config[i].masterTableOrSP); } // interpolate string outputContents = string.Format(templateContents, config[i].mirrorDB, config[i].masterDB, config[i].masterTableOrSP, tableCols, spParams.fullDef, spParams.listOfNames, spParams.selfAssignment, insertableColumnNames); string fileSuffix = string.Format(paramFileNameTemplate, config[i].mirrorDB, config[i].masterDB, config[i].masterTableOrSP); string outputFileName = $"{Path.GetFileNameWithoutExtension(templateFileName)}__{fileSuffix}{fileExtSQL}"; Generators.SaveGeneratedScript(outputContents, outputFileName, i); } }
/// <summary> /// Generates *CREATE MASTER KEY* statements /// </summary> /// <param name="configFile"></param> public static void CreateMasterKey(Configs.CreateMasterKey[] config) { // load data Configs.CreateMasterKey sharedConfig = new Configs.CreateMasterKey(); string templateContents = Generators.GetTemplateContents("CreateMasterKey.txt"); // generate output one file at a time for (int i = 0; i < config.Length; i++) { // merge with the previous full version of the config sharedConfig = (Configs.CreateMasterKey)config[i].Merge(sharedConfig); // interpolate string outputContents = string.Format(templateContents, config[i].localDB, config[i].password, config[i].credential, config[i].identity, config[i].secret); string outputFileName = $"CreateMasterKey__{config[i].localDB}__x__x{fileExtSQL}"; Generators.SaveGeneratedScript(outputContents, outputFileName, i); } }
/// <summary> /// Use an arbitrary template to generate an SQL script. /// </summary> /// <param name="configFile"></param> public static void GenerateScript(Configs.InitialConfig config, string templateFileName) { string templateContents = Generators.GetTemplateContents(templateFileName); // get the list of all placeholders in the template var matches = Regex.Matches(templateContents, @"{{\s*([^}]+)\s*}}"); var matchPlaceholders = new List <string>(); // contains replacement placeholders like {{loggingAzStorageContainerUrl}} // process placeholders one by one foreach (Match match in matches) { string placeholder = match.Value; if (matchPlaceholders.Contains(placeholder)) { continue; // ignore repeating placeholders } matchPlaceholders.Add(placeholder); // add it to the list so we don't process it multiple times try { // replace all instances with the value from config string matchValue = (string)config.GetType().GetField(match.Groups[1].Value).GetValue(config); templateContents = templateContents.Replace(placeholder, matchValue, StringComparison.Ordinal); } catch (Exception ex) { Program.WriteLine(); Program.WriteLine($"Variable has no matching config entry: {placeholder}.", ConsoleColor.Red); Program.ExitApp(); } } string fileSuffix = string.Format(Program.FileNames.OutputFileNameMaskRunOnMirror, config.mirrorDB, "x", "x"); string outputFileName = $"{Path.GetFileNameWithoutExtension(templateFileName)}__{fileSuffix}{fileExtSQL}"; Generators.SaveGeneratedScript(templateContents, outputFileName, 0); }
/// <summary> /// Generates *CREATE MASTER KEY* statements /// </summary> /// <param name="configFile"></param> public static void CreateExternalDataSource(Configs.CreateExternalDataSource[] config) { // load data Configs.CreateExternalDataSource sharedConfig = new Configs.CreateExternalDataSource(); string templateContents = Generators.GetTemplateContents("CreateExternalDataSource.txt"); // generate output one file at a time for (int i = 0; i < config.Length; i++) { // merge with the previous full version of the config sharedConfig = (Configs.CreateExternalDataSource)config[i].Merge(sharedConfig).Clone(); // there may be 2 loops if it generates scripts for mutual access for (int j = 0; j < 2; j++) { // interpolate string outputContents = string.Format(templateContents, config[i].localDB, config[i].externalDB, config[i].serverName, config[i].credential); string outputFileName = $"CreateExternalDataSource__{config[i].localDB}__{config[i].externalDB}__x{fileExtSQL}"; Generators.SaveGeneratedScript(outputContents, outputFileName, i); // exit the loop now if the data source is not reciprocal - one way only if (config[i].twoway != "1" || j == 1) { break; } // or swap the DBs if it's two-way generation string ldb = config[i].localDB; config[i].localDB = config[i].externalDB; config[i].externalDB = ldb; } } }