TSF.UmlToolingFramework.Wrappers.EA.Model.formatSQLTop C# (CSharp) Метод

formatSQLTop() приватный Метод

limiting the number of results in an sql query is different on different platforms. "SELECT TOP N" is used on SQLSVR ADOJET ASA OPENEDGE ACCESS2007 "WHERE rowcount <= N" is used on ORACLE "LIMIT N" is used on MYSQL POSTGRES This operation will replace the SELECT TOP N by the appropriate sql syntax depending on the repositorytype
private formatSQLTop ( string sqlQuery ) : string
sqlQuery string the sql query to format
Результат string
        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;
        }