/// <summary>
        /// Resolve table-value by <paramref name="template"/> and <paramref name="tableName"/><br/>
        /// i.e: With <paramref name="template"/> equal to "tables:{<paramref name="tableName"/>}:globalId" and <paramref name="tableName"/> equal to "Person" (and ie. <paramref name="migrationConfig"/>.GetSchemaPrefixId() (<see cref="IDbConfigSchemaTargets.GetSchemaPrefixId()"/>) returns "EX" <br/>
        /// => Will search configuration:<br/>
        /// - "database:migration:tables:Person:globalId"<br/>
        /// - "database:migration:tables:EXPerson:globalId"<br/>
        /// - "database:tables:Person:globalId"<br/>
        /// - "database:tables:EXPerson:globalId"<br/>
        /// </summary>
        /// <param name="migrationConfig"></param>
        /// <param name="template">string template containing {tableName}</param>
        /// <param name="tableName"></param>
        /// <param name="fallbackTemplates"></param>
        /// <returns></returns>
        public static string GetTableConfigValue(this IDbMigrationConfig migrationConfig, string template, string tableName, params string[] fallbackTemplates)
        {
            if (migrationConfig == null)
            {
                return(null);
            }
            var defaultKey      = template.ReplaceIgnoreCase("{tableName}", tableName);
            var alternativeKey  = template.ReplaceIgnoreCase("{tableName}", tableName.TrimPrefixName(migrationConfig.GetSchemaPrefixId()));
            var alternativeKey2 = template.ReplaceIgnoreCase("{tableName}", tableName.GetPrefixedName(migrationConfig.GetSchemaPrefixId()));

            var list = new List <string>(new[] { defaultKey, alternativeKey, alternativeKey2 }.Distinct());

            if (fallbackTemplates.Any())
            {
                list.AddRange(fallbackTemplates.SelectMany(x => new[]
                {
                    x.ReplaceIgnoreCase("{tableName}", tableName),
                    x.ReplaceIgnoreCase("{tableName}", tableName.TrimPrefixName(migrationConfig.GetSchemaPrefixId())),
                    x.ReplaceIgnoreCase("{tableName}", tableName.GetPrefixedName(migrationConfig.GetSchemaPrefixId())),
                }.Distinct()));
            }

            var keys = list.Distinct().ToArray();

            return(migrationConfig.GetAllMigrationConfigValues()?.GetValue(keys) ??
                   migrationConfig.GetDbConfig().GetAllDatabaseConfigValues()?.GetValue(keys));
        }
        /// <summary>
        /// Replace elements by conventions<br/>
        /// Replace {SchemaName} with <paramref name="schemaName"/> or <see cref="IDbConfigSchemaTargets.Schema"/><br/>
        /// Replace {SchemaPrefixId} with <paramref name="schemaPrefixId"/> or  <see cref="IDbConfigSchemaTargets.GetSchemaPrefixId()"/><br/>
        /// Replace {SchemaPrefixUniqueId} with <paramref name="schemaPrefixUniqueId"/> or  <see cref="IDbMigrationConfig.GetSchemaPrefixUniqueId()"/><br/>
        /// Replace {MigrationName} with <paramref name="migrationName"/> or  <see cref="IDbMigrationConfig.GetMigrationName()"/><br/>
        /// Replace {SchemaPassword} with <paramref name="schemaPassword"/> or <see cref="IDbMigrationConfig.SchemaPassword"/> or <see cref="IDbConfigCredentials.Password"/> <br/>
        /// Replace {User} with <paramref name="user"/> or <see cref="IDbConfigCredentials.User"/><br/>
        /// Replace {Password} with <paramref name="password"/> or <see cref="IDbConfigCredentials.Password"/> or <see cref="IDbConfigCredentials.User"/> <br/>
        /// </summary>
        /// <param name="migrationConfig"></param>
        /// <param name="sql"></param>
        /// <param name="schemaName"></param>
        /// <param name="schemaPrefixId"></param>
        /// <param name="schemaPrefixUniqueId"></param>
        /// <param name="migrationName"></param>
        /// <param name="schemaPassword"></param>
        /// <param name="user"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static string PrepareSql(this IDbMigrationConfig migrationConfig, string sql, string schemaName = null, string schemaPrefixId = null, string schemaPrefixUniqueId = null,
                                        string migrationName = null, string schemaPassword = null, string user = null, string password = null)
        {
            if (sql == null)
            {
                return(null);
            }

            schemaName           = schemaName ?? migrationConfig.Schema;
            schemaPassword       = schemaPassword ?? migrationConfig.SchemaPassword.WithDefault(schemaName).WithDefault(migrationConfig.GetDbConfig()?.Password);
            schemaPrefixId       = schemaPrefixId ?? migrationConfig.GetSchemaPrefixId();
            schemaPrefixUniqueId = schemaPrefixUniqueId ?? migrationConfig.GetSchemaPrefixUniqueId();
            migrationName        = migrationName ?? migrationConfig.GetMigrationName();

            user     = user ?? migrationConfig?.GetDbConfig().User.WithDefault(migrationName);
            password = password ?? migrationConfig?.GetDbConfig().Password;

            return(sql
                   .ReplaceIgnoreCase("{MigrationName}", migrationName)
                   .ReplaceIgnoreCase("{User}", user)
                   .ReplaceIgnoreCase("{Password}", password)
                   .ReplaceIgnoreCase("{SchemaName}", schemaName?.ToUpper())
                   .ReplaceIgnoreCase("{SchemaPassword}", schemaPassword?.ToUpper())
                   .ReplaceIgnoreCase("{SchemaPrefixId}", schemaPrefixId)
                   .ReplaceIgnoreCase("{SchemaPrefixUniqueId}", schemaPrefixUniqueId));
        }
Esempio n. 3
0
 /// <summary>
 /// Constructor<br/>
 /// -------------<br/>
 /// AnonymousOperation is assigned with "NOOP"<br/>
 /// SchemaPrefixId is assigned with value from parameter <paramref name="migrationConfig"/> - property <see cref="IMigrationModel.SchemaPrefixId"/><br/>
 /// SchemaPrefixUniqueId is assigned with value from parameter <paramref name="migrationConfig"/> - property <see cref="IMigrationModel.SchemaPrefixUniqueId"/><br/>
 /// <br/>
 /// i.e:<br/>
 /// When configuration have database:migration:schemaPrefix:Id = "PR" and database:migration:schemaPrefix:UniqueId = "abode" <br/>
 /// SchemaPrefixId will be resolved to "PR"<br/>
 /// SchemaPrefixUniqueId will be resolved to "abode"<br/>
 /// </summary>
 /// <param name="migrationConfig"></param>
 public ChangeLogContext(IDbMigrationConfig migrationConfig)
     : this()
 {
     SchemaPrefixId       = migrationConfig?.GetSchemaPrefixId();
     SchemaPrefixUniqueId = migrationConfig?.GetSchemaPrefixUniqueId();
     InitMetadata(migrationConfig);
 }