Skip to content

Commit

Permalink
Merge pull request #43 from mataqvazadeh/fix-connection-string
Browse files Browse the repository at this point in the history
fix problem with using old DataSourceConfig.
  • Loading branch information
Steveiwonder authored Feb 17, 2024
2 parents e3c6aa7 + 3bfe19e commit 41f60a3
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 18 deletions.
9 changes: 6 additions & 3 deletions src/DataMasker.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
using System.Threading.Tasks;
using DataMasker.Interfaces;
using DataMasker.Models;
using DataMasker.Utils;
using Newtonsoft.Json;
using Newtonsoft.Json.Schema;
using Newtonsoft.Json.Schema.Generation;
Expand All @@ -32,9 +33,11 @@ public static void RunExample()
Config config = LoadConfig(3);


var dataProviders = new List<IDataProvider>();
dataProviders.Add(new BogusDataProvider(config.DataGeneration));
dataProviders.Add(new SqlDataProvider(new System.Data.SqlClient.SqlConnection(config.DataSource.Config.connectionString.ToString())));
var dataProviders = new List<IDataProvider>
{
new BogusDataProvider(config.DataGeneration),
new SqlDataProvider(new System.Data.SqlClient.SqlConnection(config.DataSource.GetConnectionString()))
};
//create a data masker
IDataMasker dataMasker = new DataMasker(dataProviders);

Expand Down
9 changes: 6 additions & 3 deletions src/DataMasker.Runner/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
using DataMasker.Models;
using Konsole;
using Newtonsoft.Json;
using DataMasker.Utils;

namespace DataMasker.Runner
{
Expand Down Expand Up @@ -128,9 +129,11 @@ private static void Execute(
WriteLine("Masking Data");
UpdateProgress(ProgressType.Overall, 0, config.Tables.Count, "Overall Progress");

var dataProviders = new List<IDataProvider>();
dataProviders.Add(new BogusDataProvider(config.DataGeneration));
dataProviders.Add(new SqlDataProvider(new System.Data.SqlClient.SqlConnection(config.DataSource.Config.connectionString.ToString())));
var dataProviders = new List<IDataProvider>
{
new BogusDataProvider(config.DataGeneration),
new SqlDataProvider(new System.Data.SqlClient.SqlConnection(config.DataSource.GetConnectionString()))
};

//create a data masker
IDataMasker dataMasker = new DataMasker(dataProviders);
Expand Down
12 changes: 1 addition & 11 deletions src/DataMasker/DataSources/SqlDataSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,19 +28,9 @@ public SqlDataSource(
DataSourceConfig sourceConfig)
{
_sourceConfig = sourceConfig;
if (sourceConfig.Config.connectionString != null && !string.IsNullOrWhiteSpace(sourceConfig.Config.connectionString.ToString()))
{
_connectionString = sourceConfig.Config.connectionString;
}
else
{
_connectionString =
$"User ID={sourceConfig.Config.userName};Password={sourceConfig.Config.password};Data Source={sourceConfig.Config.server};Initial Catalog={sourceConfig.Config.name};Persist Security Info=False;";
}

_connectionString = sourceConfig.GetConnectionString();
}


/// <summary>
/// Gets the data.
/// </summary>
Expand Down
19 changes: 18 additions & 1 deletion src/DataMasker/Utils/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Bogus.DataSets;
using DataMasker.Models;
using System.Collections.Generic;
using System.Linq;

Expand Down Expand Up @@ -59,6 +60,22 @@ internal static string MakeColumnNameSafe(string paramName)
}

return paramName;
}
}

public static string GetConnectionString(this DataSourceConfig sourceConfig)
{
string connectionString;
if (sourceConfig.Config.connectionString != null && !string.IsNullOrWhiteSpace(sourceConfig.Config.connectionString.ToString()))
{
connectionString = sourceConfig.Config.connectionString;
}
else
{
connectionString =
$"User ID={sourceConfig.Config.userName};Password={sourceConfig.Config.password};Data Source={sourceConfig.Config.server};Initial Catalog={sourceConfig.Config.name};Persist Security Info=False;";
}

return connectionString;
}
}
}

0 comments on commit 41f60a3

Please sign in to comment.