Skip to content

Instantly share code, notes, and snippets.

@yemrekeskin
Created July 17, 2013 10:53
Show Gist options
  • Save yemrekeskin/6019550 to your computer and use it in GitHub Desktop.
Save yemrekeskin/6019550 to your computer and use it in GitHub Desktop.
Database Junk Finder for tables,stored procedure,view,functions and triger
public class DbJunkFinder
{
private List<string> _patterns;
private readonly IDbJunkFinderOperation _operation;
public DbJunkFinder()
{
this._operation = new DbJunkFinderOperation();
this._patterns = Patterns.GetCleanUpPatterns();
}
private const string outpathTable = @"C:\JunkTables.txt";
private const string outpathProcedure = @"C:\JunkProcedures.txt";
private const string outpathView = @"C:\JunkViews.";
private const string outpathFunction = @"C:\JunkFunctions.txt";
private const string outpathTrigger = @"C:\JunkTriggers.txt";
int total = 0;
public void OutAll()
{
OutJunkFunctions();
OutJunkStoredProcedures();
OutJunkTables();
OutJunkTrigers();
OutJunkViews();
}
public void OutJunkTables()
{
total = 0;
int count = 0;
File.Delete(outpathTable);
var records = this._operation.GetAllTable();
List<string> result_list = new List<string>();
foreach (var p in this._patterns)
{
Regex rx = new Regex(p);
IEnumerable<string> matchRecords = records.Cast<string>().Where(t => rx.IsMatch(t));
Console.Write(string.Format(@"Pattern: {0}, {1} match(es)", p, matchRecords.Count()));
count = 0;
foreach (var item in matchRecords)
{
if (!result_list.Contains(item))
{
result_list.Add(item);
File.AppendAllText(outpathTable, string.Format("{0}{1}", item, Environment.NewLine));
count++;
}
}
total += count;
Console.WriteLine(string.Format(@", {0} added", count));
}
Console.WriteLine(string.Format(@"Total: {0}", total));
Console.WriteLine(@"Junk Table search finished");
Console.ReadLine();
}
public void OutJunkStoredProcedures()
{
total = 0;
int count = 0;
File.Delete(outpathProcedure);
var records = this._operation.GetAllStoredProcedure();
List<string> result_list = new List<string>();
foreach (var p in this._patterns)
{
Regex rx = new Regex(p);
IEnumerable<string> matchRecords = records.Cast<string>().Where(t => rx.IsMatch(t));
Console.Write(string.Format(@"Pattern: {0}, {1} match(es)", p, matchRecords.Count()));
count = 0;
foreach (var item in matchRecords)
{
if (!result_list.Contains(item))
{
result_list.Add(item);
File.AppendAllText(outpathProcedure, string.Format("{0}{1}", item, Environment.NewLine));
}
}
total += count;
Console.WriteLine(string.Format(@", {0} added", count));
}
Console.WriteLine(string.Format(@"Total: {0}", total));
Console.WriteLine(@"Junk Stored Procedure search finished");
}
public void OutJunkViews()
{
total = 0;
int count = 0;
File.Delete(outpathView);
var records = this._operation.GetAllView();
List<string> result_list = new List<string>();
foreach (var p in this._patterns)
{
Regex rx = new Regex(p);
IEnumerable<string> matchRecords = records.Cast<string>().Where(t => rx.IsMatch(t));
Console.Write(string.Format(@"Pattern: {0}, {1} match(es)", p, matchRecords.Count()));
count = 0;
foreach (var item in matchRecords)
{
if (!result_list.Contains(item))
{
result_list.Add(item);
File.AppendAllText(outpathView, string.Format("{0}{1}", item, Environment.NewLine));
}
}
total += count;
Console.WriteLine(string.Format(@", {0} added", count));
}
Console.WriteLine(string.Format(@"Total: {0}", total));
Console.WriteLine(@"Junk View search finished");
}
public void OutJunkFunctions()
{
total = 0;
int count = 0;
File.Delete(outpathFunction);
var records = this._operation.GetAllFunction();
List<string> result_list = new List<string>();
foreach (var p in this._patterns)
{
Regex rx = new Regex(p);
IEnumerable<string> matchRecords = records.Cast<string>().Where(t => rx.IsMatch(t));
Console.Write(string.Format(@"Pattern: {0}, {1} match(es)", p, matchRecords.Count()));
count = 0;
foreach (var item in matchRecords)
{
if (!result_list.Contains(item))
{
result_list.Add(item);
File.AppendAllText(outpathFunction, string.Format("{0}{1}", item, Environment.NewLine));
}
}
total += count;
Console.WriteLine(string.Format(@", {0} added", count));
}
Console.WriteLine(string.Format(@"Total: {0}", total));
Console.WriteLine(@"Junk Function search finished");
}
public void OutJunkTrigers()
{
total = 0;
int count = 0;
File.Delete(outpathTrigger);
var records = this._operation.GetAllTrigger();
List<string> result_list = new List<string>();
foreach (var p in this._patterns)
{
Regex rx = new Regex(p);
IEnumerable<string> matchRecords = records.Cast<string>().Where(t => rx.IsMatch(t));
Console.Write(string.Format(@"Pattern: {0}, {1} match(es)", p, matchRecords.Count()));
count = 0;
foreach (var item in matchRecords)
{
if (!result_list.Contains(item))
{
result_list.Add(item);
File.AppendAllText(outpathTrigger, string.Format("{0}{1}", item, Environment.NewLine));
}
}
total += count;
Console.WriteLine(string.Format(@", {0} added", count));
}
Console.WriteLine(string.Format(@"Total: {0}", total));
Console.WriteLine(@"Junk Triger search finished");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment