Created
May 20, 2016 14:38
-
-
Save DhavalDalal/44ded973a24cf5478ee9be18083e99ff to your computer and use it in GitHub Desktop.
Refactoring in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
namespace BeforeRefactoring | |
{ | |
public class TopQuery | |
{ | |
public const int DB_ACCESS = 1; | |
public const int DB_ORACLE = 2; | |
public const int DB_SQL_SERVER = 3; | |
public const int DB_SYBASE = 4; | |
public const int DB_OTHER = 5; | |
private int dbType = -1; | |
public readonly static int NO_MAXIMUM_LIMIT = -1; | |
private int maxResults = NO_MAXIMUM_LIMIT; | |
private bool IsValidDBType(int db) | |
{ | |
switch (db) | |
{ | |
case DB_ACCESS: return true; | |
case DB_ORACLE: return true; | |
case DB_SQL_SERVER: return true; | |
case DB_SYBASE: return true; | |
case DB_OTHER: return true; | |
default: return false; | |
} | |
} | |
public int MaxResults | |
{ | |
set | |
{ | |
this.maxResults = (value < 0) ? NO_MAXIMUM_LIMIT : value; | |
} | |
get | |
{ | |
return maxResults; | |
} | |
} | |
public int GetDbType() | |
{ | |
return dbType; | |
} | |
public void SetDbType(int dbType) | |
{ | |
if (IsValidDBType(dbType)) | |
this.dbType = dbType; | |
} | |
public String Query(String query) | |
{ | |
switch (dbType) | |
{ | |
case DB_ACCESS: return (maxResults == NO_MAXIMUM_LIMIT) ? query | |
: "SELECT TOP " + maxResults + | |
" * FROM (" + query + ")"; | |
case DB_SYBASE: | |
case DB_SQL_SERVER: if (maxResults == NO_MAXIMUM_LIMIT) | |
return query; | |
else | |
{ | |
query = query.ToUpper(); | |
const String SELECT = "SELECT "; | |
int index = query.IndexOf(SELECT); | |
if (index != -1) | |
{ | |
String modifiedQuery = query.Substring(index + SELECT.Length); | |
return modifiedQuery.Insert(0, "SELECT TOP " + maxResults + " "); | |
} | |
else | |
return query; | |
} | |
case DB_ORACLE: return (maxResults == NO_MAXIMUM_LIMIT) ? query | |
: "SELECT * FROM (" + query + | |
") WHERE rownum <= " + maxResults; | |
case DB_OTHER: | |
default: return query; | |
} | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Text; | |
using System.Collections.Generic; | |
using System.Linq; | |
using Microsoft.VisualStudio.TestTools.UnitTesting; | |
using Specs = Microsoft.VisualStudio.TestTools.UnitTesting.TestClassAttribute; | |
using Specification = Microsoft.VisualStudio.TestTools.UnitTesting.TestMethodAttribute; | |
using Setup = Microsoft.VisualStudio.TestTools.UnitTesting.TestInitializeAttribute; | |
using Assert = NUnit.Framework.Assert; | |
using NUnit.Framework; | |
namespace BeforeRefactoring | |
{ | |
[Specs] | |
public class TopQueryTest | |
{ | |
private const String query = "select * from employee"; | |
private TopQuery topQuery; | |
private int maxResults = 50; | |
[Setup] | |
public void setUp() | |
{ | |
topQuery = new TopQuery(); | |
topQuery.MaxResults = maxResults; | |
} | |
[Specification] | |
public void SetMaximumResultsToNoLimitsWhenNegativeNumberIsPassed() | |
{ | |
topQuery.MaxResults = -100; | |
Assert.AreEqual(TopQuery.NO_MAXIMUM_LIMIT, topQuery.MaxResults); | |
} | |
[Specification] | |
public void SetsMaximumResultsToNumberOfResultsRequired() | |
{ | |
Assert.AreEqual(maxResults, topQuery.MaxResults); | |
} | |
[Specification] | |
public void CreatesAccessTopQuery() | |
{ | |
topQuery.SetDbType(TopQuery.DB_ACCESS); | |
String expectedQuery = "SELECT TOP " + maxResults + " * FROM (" + query + ")"; | |
String actualQuery = topQuery.Query(query); | |
Assert.AreEqual(expectedQuery, actualQuery); | |
} | |
[Specification] | |
public void CreatesSybaseTopQuery() | |
{ | |
topQuery.SetDbType(TopQuery.DB_SYBASE); | |
String expectedQuery = "SELECT TOP " + maxResults + " * FROM EMPLOYEE"; | |
String actualQuery = topQuery.Query(query); | |
Assert.AreEqual(expectedQuery, actualQuery); | |
} | |
[Specification] | |
public void CreatesSQLServerTopQuery() | |
{ | |
topQuery.SetDbType(TopQuery.DB_SQL_SERVER); | |
String expectedQuery = "SELECT TOP " + maxResults + " * FROM EMPLOYEE"; | |
String actualQuery = topQuery.Query(query); | |
Assert.AreEqual(expectedQuery, actualQuery); | |
} | |
[Specification] | |
public void CreatesOracleTopQuery() | |
{ | |
topQuery.SetDbType(TopQuery.DB_ORACLE); | |
String expectedQuery = "SELECT * FROM (" + query + ") WHERE rownum <= " + maxResults; | |
String actualQuery = topQuery.Query(query); | |
Assert.AreEqual(expectedQuery, actualQuery); | |
} | |
[Specification] | |
public void CreatesOtherTopQuery() | |
{ | |
topQuery.SetDbType(TopQuery.DB_OTHER); | |
String actualQuery = topQuery.Query(query); | |
Assert.AreEqual(query, actualQuery); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment