AzurePoolCrossDbGenerator/GenerateScriptGeneric.cs
/// <summary>
/// Use an arbitrary template to generate an SQL script. 
/// </summary>
/// <param name="configFile"></param>
Use an arbitrary template to generate an SQL script.
Use an arbitrary template to generate an SQL script .
public static void GenerateScript(Configs.InitialConfig config, string templateFileName)
        {
            string templateContents = Generators.GetTemplateContents(templateFileName);
            var matches = Regex.Matches(templateContents, @"{{\s*([^}]+)\s*}}");
            var matchPlaceholders = new List<string>(); 
            foreach (Match match in matches)
            {
                string placeholder = match.Value;
                if (matchPlaceholders.Contains(placeholder)) continue; 
                matchPlaceholders.Add(placeholder); 
                try
                {
                    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);
        }
public static void GenerateScript ( Configs . InitialConfig config , string templateFileName ) { string templateContents = Generators . GetTemplateContents ( templateFileName ) ; var matches = Regex . Matches ( templateContents , @"{{\s*([^}]+)\s*}}" ) ; var matchPlaceholders = new List < string > ( ) ; foreach ( Match match in matches ) { string placeholder = match . Value ; if ( matchPlaceholders . Contains ( placeholder ) ) continue ; matchPlaceholders . Add ( placeholder ) ; try { 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 , " " , " " ) ; string outputFileName = $" { Path . GetFileNameWithoutExtension ( templateFileName ) } __ { fileSuffix } { fileExtSQL } " ; Generators . SaveGeneratedScript ( templateContents , outputFileName , 0 ) ; }
Use an arbitrary template to generate an SQL script.
Use an arbitrary template to generate an SQL script .
// get the list of all placeholders in the template // contains replacement placeholders like {{loggingAzStorageContainerUrl}} // process placeholders one by one // ignore repeating placeholders // add it to the list so we don't process it multiple times // replace all instances with the value from config
config
Configs.InitialConfig
templateFileName
string
configFile
None