Exemplo n.º 1
0
        public static List <TableConfigElement> CreateValidatedTableConfigCollection(DatabaseConfigElement dbConfig)
        {
            var validatedTableConfigs = new List <TableConfigElement>();

            var dbConfigValidationResult = ConfigurationValidator.IsDbConfigValid(dbConfig);

            if (!dbConfigValidationResult.IsValid)
            {
                logger.Error($"The anonymization of the database with the following connection string was skipped: {dbConfig.ConnectionString}. " +
                             $"Reason: {dbConfigValidationResult.ValidationMessage}");
                return(validatedTableConfigs);
            }


            for (int i = 0; i < dbConfig.Tables.Count; i++)
            {
                var tableConfig = dbConfig.Tables[i];

                var tableConfigValidationResult = ConfigurationValidator.IsTableConfigValid(dbConfig, tableConfig);
                if (!tableConfigValidationResult.IsValid)
                {
                    logger.Error($"The anonymization of the table {tableConfigValidationResult.TableNameWithSchema} " +
                                 $"with the following connection string was skipped: {tableConfigValidationResult.ConnectionString}. " +
                                 $"Reason: {tableConfigValidationResult.ValidationMessage}");
                }
                else
                {
                    validatedTableConfigs.Add(tableConfig);
                }
            }

            return(validatedTableConfigs);
        }
        public static List <DatabaseConfigElement> CreateValidatedDatabaseConfigCollection(DatabaseConfigCollection databases)
        {
            var validatedDatabaseConfigs = new List <DatabaseConfigElement>();

            for (int i = 0; i < databases.Count; i++)
            {
                var dbConfig = databases[i];

                var dbConfigValidationResult = ConfigurationValidator.IsDbConfigValid(dbConfig);

                if (!dbConfigValidationResult.IsValid)
                {
                    logger.Error($"The anonymization of the database with the following connection string was skipped: {dbConfig.ConnectionString}. " +
                                 $"Reason: {dbConfigValidationResult.ValidationMessage}");
                }
                else
                {
                    try
                    {
                        DbTypeInitializer.CreateDbTypes(dbConfig.ConnectionString);
                        validatedDatabaseConfigs.Add(databases[i]);
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex, $"The anonymization of the database with the following connection string was skipped: {dbConfig.ConnectionString}. " +
                                     $"Reason: {ex.Message}");
                    }
                }
            }

            return(validatedDatabaseConfigs);
        }
Exemplo n.º 3
0
        public ITableInfo Build()
        {
            var dbConfigValidationResult = ConfigurationValidator.IsDbConfigValid(DatabaseConfig);

            if (!dbConfigValidationResult.IsValid)
            {
                throw new TableInfoException(TableConfig.NameWithSchema, DatabaseConfig.ConnectionString,
                                             $"Error while creating the TableInfo object: {dbConfigValidationResult.ValidationMessage}");
            }

            var tableConfigValidationResult = ConfigurationValidator.IsTableConfigValid(DatabaseConfig, TableConfig);

            if (!tableConfigValidationResult.IsValid)
            {
                throw new TableInfoException(TableConfig.NameWithSchema, DatabaseConfig.ConnectionString,
                                             $"Error while creating the TableInfo object: {tableConfigValidationResult.ValidationMessage}");
            }

            var dbName = ParseDataSource(DatabaseConfig.ConnectionString);

            var tableInfo =
                new TableInfo
            {
                DbConnectionString = DatabaseConfig.ConnectionString,
                DbName             = RemoveParenthesis(dbName),
                SchemaName         = RemoveParenthesis(ParseSchemaAndTableName(TableConfig.NameWithSchema).schemaName),
                TableName          = RemoveParenthesis(ParseSchemaAndTableName(TableConfig.NameWithSchema).tableName),
                WhereClause        = ""
            };

            var constantColumnsAndValues = new Dictionary <string, string>();
            var constantColumns          = new List <string>();
            var scrambledColumns         = new List <string>();

            for (int i = 0; i < TableConfig.ScrambledColumns.Count; i++)
            {
                var column = TableConfig.ScrambledColumns[i];

                scrambledColumns.Add(RemoveParenthesis(column.Name));
            }

            for (int i = 0; i < TableConfig.ConstantColumns.Count; i++)
            {
                var column = TableConfig.ConstantColumns[i];

                constantColumns.Add(RemoveParenthesis(column.Name));
                constantColumnsAndValues.Add(RemoveParenthesis(column.Name), column.Value);
            }

            var inputParameterValidationResult = ParameterValidator.AreInputParamsValid(tableInfo, scrambledColumns, constantColumns);

            if (!inputParameterValidationResult.IsValid)
            {
                throw new TableInfoException(tableInfo.FullTableName, tableInfo.DbConnectionString, $"Error while creating the TableInfo object: " +
                                             $"{inputParameterValidationResult.ValidationMessage}");
            }

            var inputPrKeyParameterValidationResult = ParameterValidator.ArePrimaryKeysValid(tableInfo, scrambledColumns, constantColumns);

            if (!inputPrKeyParameterValidationResult.IsValid)
            {
                throw new TableInfoException(tableInfo.FullTableName, tableInfo.DbConnectionString, $"Error while creating the TableInfo object: " +
                                             $"{inputPrKeyParameterValidationResult.ValidationMessage}");
            }

            try
            {
                tableInfo.SqlToGetConstantColumnTypes = ColumnTypeManager.GenerateColumnTypeSqlQuery(tableInfo, constantColumns);
            }
            catch (Exception ex)
            {
                throw new TableInfoException(tableInfo.FullTableName, tableInfo.DbConnectionString,
                                             $"Error while generating the sql script for getting constant column types: {ex.Message}", ex);
            }

            try
            {
                tableInfo.SqlToGetScrambledColumnTypes = ColumnTypeManager.GenerateColumnTypeSqlQuery(tableInfo, scrambledColumns);
            }
            catch (Exception ex)
            {
                throw new TableInfoException(tableInfo.FullTableName, tableInfo.DbConnectionString,
                                             $"Error while generating the sql script for getting scrambled column types: {ex.Message}", ex);
            }

            tableInfo.ConstantColumnsAndValues = constantColumnsAndValues;
            tableInfo.ScrambledColumns         = scrambledColumns;

            return(tableInfo);
        }