private static string SQLStripObjectName(string sql, string objectHeader)
{
var t = sql;
//Normalize line endings
t = t.Replace("\r\n", "\n");
t = t.Replace("\r", "\n");
//Find the database object type up to the name like CREATE PROC [schema].[name]
objectHeader = objectHeader.Replace(" ", @"[\s\n]");
var pattern = objectHeader + @"[\s\(\n]";
var regexObj = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
var matchResult = regexObj.Match(t);
var dropObjectName = string.Empty;
if (matchResult.Success)
{
t = t.Substring(matchResult.Index + matchResult.Length, t.Length - (matchResult.Index + matchResult.Length));
//Search for [schema].[name]
pattern = @"\[.*\].\[.*\]";
regexObj = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
matchResult = regexObj.Match(t);
if (matchResult.Success)
{
dropObjectName = matchResult.Value;
}
else
{
//Search for [name]
pattern = @"\[.*\][\s\(\n]";
regexObj = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
matchResult = regexObj.Match(t);
if (matchResult.Success)
{
dropObjectName = matchResult.Value;
}
else
{
//Search for "name" or "schema.name" with no braces
pattern = @".*[\s\(\n]";
regexObj = new System.Text.RegularExpressions.Regex(pattern, System.Text.RegularExpressions.RegexOptions.Multiline | System.Text.RegularExpressions.RegexOptions.IgnoreCase);
matchResult = regexObj.Match(t);
if (matchResult.Success)
{
dropObjectName = matchResult.Value;
}
}
}
}
return dropObjectName.Trim();
}