Skip to content

Commit

Permalink
NugetUpdate
Browse files Browse the repository at this point in the history
Update of the nuget packages
  • Loading branch information
InvaderZim85 committed Oct 9, 2021
1 parent d189873 commit 769c409
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 47 deletions.
11 changes: 7 additions & 4 deletions Demo/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ static void Main(string[] args)
Console.WriteLine("Normal constructor");
var connector = new Connector("localhost", "testdatabase", "testuser", "testpassword");
LoadAndPrintPersons(connector);


// Step 2: Normal constructor with secure password
Console.WriteLine("Normal constructor with secure password");
Expand Down Expand Up @@ -45,9 +45,12 @@ static void Main(string[] args)
LoadAndPrintPersons(connector);

// Step 5: Print the connection info
var conStringInfo =
connector.ConnectionStringInfo(ConnectionInfoType.Server | ConnectionInfoType.Database);
Console.WriteLine($"Connection: {conStringInfo}");
Console.WriteLine("Connection info");
for (var i = 0; i <= 34; i++)
{
Console.WriteLine(connector.ConnectionStringInfo((ConnectionInfoType)i));
}


Console.WriteLine("Done");
Console.ReadLine();
Expand Down
7 changes: 6 additions & 1 deletion ZimLabs.Database.MySql/ConnectionInfoType.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,11 @@ public enum ConnectionInfoType
/// <summary>
/// Shows the port number
/// </summary>
Port = 8
Port = 8,

/// <summary>
/// Shows the connection timeout
/// </summary>
ConnectionTimeout = 16
}
}
53 changes: 16 additions & 37 deletions ZimLabs.Database.MySql/Connector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,9 @@ public Connector(DatabaseSettings settings)
/// <param name="userId">The user id</param>
/// <param name="password">The password</param>
/// <param name="port">The port (optional)</param>
public Connector(string server, string database, string userId, string password, uint port = 3306) : this(
new DatabaseSettings(server, database, userId, password.ToSecureString(), port))
{

}
/// <param name="connectionTimeout">The connection timeout (optional, 15 = default value)</param>
public Connector(string server, string database, string userId, string password, uint port = 3306, uint connectionTimeout = 15) : this(
new DatabaseSettings(server, database, userId, password.ToSecureString(), port, connectionTimeout)) { }

/// <summary>
/// Creates a new instance of the <see cref="Connector"/>
Expand All @@ -78,11 +76,9 @@ public Connector(string server, string database, string userId, string password,
/// <param name="userId">The user id</param>
/// <param name="password">The password</param>
/// <param name="port">The port (optional)</param>
public Connector(string server, string database, string userId, SecureString password, uint port = 3306) : this(
new DatabaseSettings(server, database, userId, password, port))
{

}
/// <param name="connectionTimeout">The connection timeout (optional, 15 = default value)</param>
public Connector(string server, string database, string userId, SecureString password, uint port = 3306, uint connectionTimeout = 15) : this(
new DatabaseSettings(server, database, userId, password, port, connectionTimeout)) { }

/// <summary>
/// Creates a new instance of the <see cref="Connector"/>
Expand All @@ -98,14 +94,21 @@ public Connector(SecureString connectionString)
/// </summary>
private void CreateConnectionString()
{
_connectionString = new MySqlConnectionStringBuilder
var conString = new MySqlConnectionStringBuilder
{
Server = _settings.Server,
Database = _settings.Database,
UserID = _settings.UserId,
Password = _settings.Password.ToInsecureString(),
Port = _settings.Port
}.ConnectionString.ToSecureString();
};

if (_settings.ConnectionTimeout > 0)
conString.ConnectionTimeout = _settings.ConnectionTimeout;
else
_settings.ConnectionTimeout = conString.ConnectionTimeout;

_connectionString = conString.ConnectionString.ToSecureString();
}

/// <summary>
Expand Down Expand Up @@ -145,31 +148,7 @@ public void SwitchDatabase(string database)
/// <returns>The connection string info</returns>
public string ConnectionStringInfo(ConnectionInfoType type)
{
var server = $"Server: {_settings.Server}";
var database = $"Database: {_settings.Database}";
var user = $"User: {_settings.UserId}";
var port = $"Port: {_settings.Port}";

return (int) type switch
{
0 => "",
1 => server,
2 => database,
3 => $"{server}; {database}",
4 => user,
5 => $"{server}; {user}",
6 => $"{database}; {user}",
7 => $"{server}; {database}; {user}",
8 => port,
9 => $"{server}; {port}",
10 => $"{database}; {port}",
11 => $"{server}; {database}; {port}",
12 => $"{user}; {port}",
13 => $"{server}; {user}; {port}",
14 => $"{database}; {user}; {port}",
15 => $"{server}; {database}; {user}; {port}",
_ => ""
};
return Helper.GetConnectionInfo(type, _settings);
}

/// <summary>
Expand Down
9 changes: 8 additions & 1 deletion ZimLabs.Database.MySql/DatabaseSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@ public class DatabaseSettings
/// </summary>
public uint Port { get; set; }

/// <summary>
/// Gets or sets the connection timeout
/// </summary>
public uint ConnectionTimeout { get; set; }

/// <summary>
/// Creates a new empty instance of the <see cref="DatabaseSettings"/>
/// </summary>
Expand All @@ -45,13 +50,15 @@ public DatabaseSettings(){ }
/// <param name="userId">The user id</param>
/// <param name="password">The password</param>
/// <param name="port">The port (optional)</param>
public DatabaseSettings(string server, string database, string userId, SecureString password, uint port = 3306)
/// <param name="connectionTimeout">The connection timeout (optional, 15 = default value)</param>
public DatabaseSettings(string server, string database, string userId, SecureString password, uint port = 3306, uint connectionTimeout = 15)
{
Server = server;
Database = database;
UserId = userId;
Password = password;
Port = port;
ConnectionTimeout = connectionTimeout;
}
}
}
53 changes: 53 additions & 0 deletions ZimLabs.Database.MySql/Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,5 +52,58 @@ public static SecureString ToSecureString(this string value)

return secureString;
}

/// <summary>
/// Converts the settings based on the connection type into an info string
/// </summary>
/// <param name="type">The info type</param>
/// <param name="settings">The settings</param>
/// <returns>The converted info</returns>
internal static string GetConnectionInfo(ConnectionInfoType type, DatabaseSettings settings)
{
var server = $"Server: {settings.Server}";
var database = $"Database: {settings.Database}";
var user = $"User: {settings.UserId}";
var port = $"Port: {settings.Port}";
var timeout = $"Timeout: {settings.ConnectionTimeout}";

return (int)type switch
{
0 => "",
1 => server,
2 => database,
3 => $"{server}; {database}",
4 => user,
5 => $"{server}; {user}",
6 => $"{database}; {user}",
7 => $"{server}; {database}; {user}",
8 => port,
9 => $"{server}; {port}",
10 => $"{database}; {port}",
11 => $"{server}; {database}; {port}",
12 => $"{user}; {port}",
13 => $"{server}; {user}; {port}",
14 => $"{database}; {user}; {port}",
15 => $"{server}; {database}; {user}; {port}",
16 => timeout,
17 => $"{server}; {timeout}",
18 => $"{database}; {timeout}",
19 => $"{server}; {database}; {timeout}",
20 => $"{user}; {timeout}",
21 => $"{server}; {user}; {timeout}",
22 => $"{database}; {user}; {timeout}",
23 => $"{server}; {database}; {user}; {timeout}",
24 => $"{port}; {timeout}",
25 => $"{server}; {port}; {timeout}",
26 => $"{database}; {port}; {timeout}",
27 => $"{server}; {database}; {port}; {timeout}",
28 => $"{user}; {port}; {timeout}",
29 => $"{server}; {user}; {port}; {timeout}",
30 => $"{database}; {user}; {port}; {timeout}",
31 => $"{server}; {database}; {user}; {port}; {timeout}",
32 => "",
_ => ""
};
}
}
}
8 changes: 4 additions & 4 deletions ZimLabs.Database.MySql/ZimLabs.Database.MySql.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,10 @@
<RepositoryUrl>https://github.com/InvaderZim85/ZimLabs.Database.MySql</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PackageTags>Helper, Database, MySql, Connector</PackageTags>
<Version>1.2.0</Version>
<Version>1.2.1</Version>
<PackageReleaseNotes>Update of the used packages</PackageReleaseNotes>
<AssemblyVersion>1.2.0.0</AssemblyVersion>
<FileVersion>1.2.0.0</FileVersion>
<AssemblyVersion>1.2.1.0</AssemblyVersion>
<FileVersion>1.2.1.0</FileVersion>
<LangVersion>latest</LangVersion>
</PropertyGroup>

Expand All @@ -33,7 +33,7 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MySql.Data" Version="8.0.24" />
<PackageReference Include="MySql.Data" Version="8.0.26" />
</ItemGroup>

<ItemGroup>
Expand Down

0 comments on commit 769c409

Please sign in to comment.