69 lines
2.3 KiB
C#
69 lines
2.3 KiB
C#
///
|
|
/// Copyright (c) 2017-2022 Sensus Slovensko a.s.
|
|
///
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace Config
|
|
{
|
|
public static class Utils
|
|
{
|
|
/// <summary>
|
|
/// Extract and return a DB server from a MySQL connection string.
|
|
/// </summary>
|
|
public static string GetDBServer(string connectionString)
|
|
{
|
|
return GetFromConnectionString(connectionString, new string[] { "SERVER=" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract and return a DB name from a MySQL connection string.
|
|
/// </summary>
|
|
public static string GetDBName(string connectionString)
|
|
{
|
|
return GetFromConnectionString(connectionString, new string[] { "DATABASE=" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract and return a username from a MySQL connection string
|
|
/// </summary>
|
|
public static string GetDBUser(string connectionString)
|
|
{
|
|
return GetFromConnectionString(connectionString, new string[] { "USER=", "UID=" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract and return a password from a MySQL connection string
|
|
/// </summary>
|
|
public static string GetDBPassword(string connectionString)
|
|
{
|
|
return GetFromConnectionString(connectionString, new string[] { "PASSWORD=", "PWD=" });
|
|
}
|
|
|
|
/// <summary>
|
|
/// Extract and return an element of a MySQL connection string
|
|
/// (a host, a database, a user name or a password).
|
|
/// Return an empty string on any error.
|
|
/// </summary>
|
|
public static string GetFromConnectionString(string connectionString, string[] patterns)
|
|
{
|
|
foreach (var pattern in patterns)
|
|
{
|
|
int startIx = connectionString.IndexOf(pattern);
|
|
if (startIx >= 0)
|
|
{
|
|
/// pattern found, extract the subsequent element
|
|
startIx += pattern.Length;
|
|
int endIx = connectionString.IndexOf(';', startIx);
|
|
return (endIx > 0) ? connectionString.Substring(startIx, endIx - startIx) : string.Empty;
|
|
}
|
|
}
|
|
|
|
/// pattern NOT found
|
|
return string.Empty;
|
|
}
|
|
}
|
|
}
|