You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
DS7/DSWeb/Areas/CommMng/DAL/PageHelperEIP.cs

148 lines
6.0 KiB
C#

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using System.Globalization;
using System.Data;
namespace DSWeb.Areas.CommMng.DAL
{
public class PagerHelperEIP
{
/// <summary>
/// 将原始SQL语句修改为分页SQL语句
/// ※只针对未进行SQL分页的SQL语句使用
/// ※原始语句中必须存在ORDER BY 排序!
/// ※ORDER BY字段不可使用别名
/// ※只适用于SQL语句中所有关键字为大写的情况下使用
/// ※只适用于where条件中不存在子查询的语句,例(WHERE a.id in (SELECT id FROM b))为不可用。
/// 若必须在where条件中添加子查询请将子查询的FROM写成小写,例(WHERE a.id in (SELECT id from b))为可用。
/// </summary>
/// <param name="OriginalSQLStr">原SQL语句</param>
/// <param name="start">开始位置</param>
/// <param name="limit">每页条数</param>
/// <param name="total">总页数</param>
/// <returns>新的分页SQL语句</returns>
public static StringBuilder PageSQL(string OriginalSQLStr, int start, int limit, out int total)
{
//最后一个orderby语句位置
CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
int orderByIndex = Compare.LastIndexOf(OriginalSQLStr, "order by", CompareOptions.IgnoreCase);
//最后一个orderby语句
string orderByStatments = "";
//主查询语句
string mainSql = OriginalSQLStr;
if (orderByIndex > 0)
{
orderByStatments = OriginalSQLStr.Substring(orderByIndex, OriginalSQLStr.Length - orderByIndex);
// 判断最后一个orderby语句是否在某个子查询里
// 如果是则取消新查询语句的orderby拼凑
// 如果否则删除原SQL语句中的orderby语句等待拼凑新orderby语句
if (orderByStatments.IndexOf(")") > 0)
{
orderByStatments = "";
}
else
{
mainSql = OriginalSQLStr.Substring(0, orderByIndex);
}
}
//更改主查询语句的select部分以及orderby语句
string numColumn = " row_number() over (" + orderByStatments + ") as num,";
mainSql = mainSql.Insert(7, numColumn);
//创建新SQL主体
StringBuilder sb = new StringBuilder();
sb.Append("select * from (");
sb.Append(mainSql);
sb.Append(") as t ");
int endindex = start + limit;
sb.Append("where t.num>" + start + " and t.num<=" + endindex + " order by t.num");
//总行数
total = getTotelForSqlNew(OriginalSQLStr);
return sb;
}
public static int getTotelForSql(string OriginalSQLStr)
{
//最后一个orderby语句位置
CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
int orderByIndex = Compare.LastIndexOf(OriginalSQLStr, "order by", CompareOptions.IgnoreCase);
//最后一个orderby语句
string orderByStatments = "";
//主查询语句
string mainSql = OriginalSQLStr;
if (orderByIndex > 0)
{
orderByStatments = OriginalSQLStr.Substring(orderByIndex, OriginalSQLStr.Length - orderByIndex);
// 判断最后一个orderby语句是否在某个子查询里
// 如果是则取消新查询语句的orderby拼凑
// 如果否则删除原SQL语句中的orderby语句等待拼凑新orderby语句
if (!(orderByStatments.IndexOf(")") > 0))
{
mainSql = OriginalSQLStr.Substring(0, orderByIndex);
}
}
//修改SELECT部分
//获取最后一个FROM部分
int fromIndex = Compare.LastIndexOf(mainSql, "FROM", CompareOptions.None);
string newSql = mainSql.Remove(7, fromIndex - 7);
newSql = newSql.Insert(7, " count(*) ");
Database db = DatabaseFactory.CreateDatabase("EIP");
int totel = 0;
using (IDataReader reader = db.ExecuteReader(CommandType.Text, newSql))
{
while (reader.Read())
{
totel = Convert.ToInt32(reader[0]);
}
}
return totel;
}
public static int getTotelForSqlNew(string OriginalSQLStr)
{
//最后一个orderby语句位置
CompareInfo Compare = CultureInfo.InvariantCulture.CompareInfo;
int orderByIndex = Compare.LastIndexOf(OriginalSQLStr, "order by", CompareOptions.IgnoreCase);
//最后一个orderby语句
string orderByStatments = "";
//主查询语句
string mainSql = OriginalSQLStr;
if (orderByIndex > 0)
{
orderByStatments = OriginalSQLStr.Substring(orderByIndex, OriginalSQLStr.Length - orderByIndex);
// 判断最后一个orderby语句是否在某个子查询里
// 如果是则取消新查询语句的orderby拼凑
// 如果否则删除原SQL语句中的orderby语句等待拼凑新orderby语句
if (!(orderByStatments.IndexOf(")") > 0))
{
mainSql = OriginalSQLStr.Substring(0, orderByIndex);
}
}
string newSql = mainSql.Insert(0, "select COUNT(*) from ( ");
newSql = newSql + " ) t1 ";
Database db = DatabaseFactory.CreateDatabase("EIP");
int totel = Convert.ToInt32(db.ExecuteScalar(CommandType.Text, newSql));
return totel;
}
}
}