private string formatSQLTop(string sqlQuery)
{
string formattedQuery = sqlQuery;
string selectTop = "select top ";
int begintop = sqlQuery.ToLower().IndexOf(selectTop);
if (begintop >= 0)
{
int beginN = begintop + selectTop.Length;
int endN = sqlQuery.ToLower().IndexOf(" ",beginN) +1;
if (endN > beginN)
{
string N = sqlQuery.ToLower().Substring(beginN, endN - beginN);
string selectTopN = sqlQuery.Substring(begintop, endN);
switch ( this.repositoryType)
{
case RepositoryType.ORACLE :
// remove "top N" clause
formattedQuery = formattedQuery.Replace(selectTopN, "select ");
// find where clause
string whereString = "where ";
int beginWhere = formattedQuery.ToLower().IndexOf(whereString);
string rowcountCondition = "rownum <= " + N + " and ";
// add the rowcount condition
formattedQuery = formattedQuery.Insert(beginWhere + whereString.Length,rowcountCondition);
break;
case RepositoryType.MYSQL :
case RepositoryType.POSTGRES :
// remove "top N" clause
formattedQuery = formattedQuery.Replace(selectTopN, "select ");
string limitString = " limit " + N ;
// add limit clause
formattedQuery = formattedQuery + limitString;
break;
case RepositoryType.FIREBIRD:
// in firebird top becomes first
formattedQuery = formattedQuery.Replace(selectTopN,selectTopN.Replace("top","first"));
break;
}
}
}
return formattedQuery;
}