#if POSTGRES
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Sdaleo.Systems.Postgres.Internal
{
///
/// Central Location for Postgres Input Validation
///
internal class Validation
{
#region Private Character Validation Declaration
private const string CharSet_AllowedCharsAlphaNum = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
private const string CharSet_AllowedCharsServerName = CharSet_AllowedCharsAlphaNum + ":\\ _!@#$%^&()-+{}[],.;`~";
private const string CharSet_AllowedCharsInstanceName = CharSet_AllowedCharsAlphaNum + ":\\ _!@#$%^&()-+{}[],.;`~";
private const string CharSet_AllowedCharsPassword = CharSet_AllowedCharsAlphaNum + "_!@#$%^&()-+{}[],.;`~";
private const string CharSet_AllowedCharsTableName = CharSet_AllowedCharsAlphaNum + " -_";
///
/// Generic Function to use with Allowed Character Sets above
///
/// string to evaluate
/// Pass in one of the legal character consts declared above
/// true if valid, false otherwise
private static bool ContainsOnlyLegalChars(string TextToEvaluate, string TextToEvaluateWith)
{
foreach (char c in TextToEvaluate.ToCharArray())
{
bool bFound = (TextToEvaluateWith.IndexOf(c) >= 0);
if (!bFound)
return false;
}
return true;
}
#endregion
#region Name Validators
///
/// Main Validation Function to validate Server Names
///
/// Name of a Server
/// true if valid, false otherise
internal static bool IsValidServerName(string strServerName)
{
if (String.IsNullOrEmpty(strServerName))
{
return false;
}
else if (strServerName.Length < 1 || strServerName.Length > 248) // limit server Name length
{
return false;
}
else if (!ContainsOnlyLegalChars(strServerName, CharSet_AllowedCharsServerName))
{
return false;
}
else
{
return true;
}
}
///
/// Main Validation Function to validate Instance Names
///
/// Name of an Instance
/// true if valid, false otherise
internal static bool IsValidInstanceName(string strInstanceName)
{
if (string.IsNullOrEmpty(strInstanceName))
{
return false;
}
else if (strInstanceName.Length < 1 || strInstanceName.Length > 18) // limit Instance Name length
{
return false;
}
else if (!ContainsOnlyLegalChars(strInstanceName, CharSet_AllowedCharsInstanceName))
{
return false;
}
else
{
return true;
}
}
///
/// Main Validation Function to validate Passwords
///
/// Password
/// true if valid, false otherise
internal static bool IsValidPassword(string strPassword)
{
if (String.IsNullOrEmpty(strPassword))
{
return false;
}
else if (strPassword.Length < 1 || strPassword.Length > 40) // limit Password Length
{
return false;
}
else if (!ContainsOnlyLegalChars(strPassword, CharSet_AllowedCharsPassword))
{
return false;
}
else
{
return true;
}
}
///
/// Main Validation Function to validate TableName
///
/// Name of Table
/// true if valid, false otherwise
internal static bool IsValidTableName(string strTableName)
{
if (string.IsNullOrEmpty(strTableName))
{
return false;
}
else if (strTableName.Length < 1 || strTableName.Length > 52) // limit Table Name length
{
return false;
}
else if (!ContainsOnlyLegalChars(strTableName, CharSet_AllowedCharsTableName))
{
return false;
}
else
{
return true;
}
}
#endregion
#region Combination Validators
///
/// Validates that the passedin string has both a valid server and a valid Instance
///
/// a string to check for server and instance
/// true if valid, false otherwise
internal static bool IsValidServerNameAndInstanceName(string strServerNameNInstance)
{
if (!string.IsNullOrEmpty(strServerNameNInstance) && (strServerNameNInstance.IndexOf('\\') >= 0))
{
int nIndexLast = strServerNameNInstance.LastIndexOf('\\');
string[] values = new String[] { strServerNameNInstance.Substring(0, nIndexLast), strServerNameNInstance.Substring(nIndexLast + 1) };
if (values.Length == 2)
return IsValidServerName(values[0]) && IsValidInstanceName(values[1]);
}
return false;
}
#endregion
}
}
#endif