我们公司主要mysql存储数据,因此也封装了比较好用mysql通用方法,然后,我们做大量接口,在处理分页查询接口,没有很好分查询方法。sql查询 语句如何解析成“分页查询”和“总统计”两条语句。可能,很多人在处理“总统计”是这样:“select count(*) from (<sql原查询语句>) ”,而不是把原sql查询语句中columns替换成“count(*)”;相比前者统计查询效率高不高,大家心知肚明。“分页查询”很简单,对于mysql语句就是在原sql查询语句后面加上“limit 数字 offset 数字”

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks; namespace MySqlTest
{
class Program
{
static void Main(string[] args)
{
string sql = "select id,code,name,modifytime,storeid from retail_cashier where profileid=@profileid and storeId=@storeId order by id";
var parts = MysqlPageHelper.BuildPageQuery(sql, , ); Console.WriteLine("sql:{0};",parts.Sql);
Console.WriteLine("SqlCount:{0}", parts.SqlCount);
Console.WriteLine("SqlPage:{0}",parts.SqlPage);
Console.WriteLine("SqlOrderBy:{0}", parts.SqlOrderBy);
Console.ReadKey();
}
} public class MysqlPageHelper
{
public Regex RegexColumns = new Regex(@"\A\s*SELECT\s+((?:\((?>\((?<depth>)|\)(?<-depth>)|.?)*(?(depth)(?!))\)|.)*?)(?<!,\s+)\bFROM\b",
RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.Compiled); public Regex RegexDistinct = new Regex(@"\ADISTINCT\s",
RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.Compiled); public Regex RegexOrderBy =
new Regex(
@"\bORDER\s+BY\s+(?!.*?(?:\)|\s+)AS\s)(?:\((?>\((?<depth>)|\)(?<-depth>)|.?)*(?(depth)(?!))\)|[\[\]`""\w\(\)\.])+(?:\s+(?:ASC|DESC))?(?:\s*,\s*(?:\((?>\((?<depth>)|\)(?<-depth>)|.?)*(?(depth)(?!))\)|[\[\]`""\w\(\)\.])+(?:\s+(?:ASC|DESC))?)*",
RegexOptions.RightToLeft | RegexOptions.IgnoreCase | RegexOptions.Multiline | RegexOptions.Singleline | RegexOptions.Compiled); private string _sqlSelectRemoved = null;
public void SplitSql(string sql, long skip, long take)
{
this.Sql = sql;
this.SqlCount = null;
this.SqlOrderBy = null; // 从中提取列
var m = RegexColumns.Match(this.Sql);
if (!m.Success)
throw new Exception("无法解析分页查询的SQL语句"); // sql语句中columns替换成count(*)
var g = m.Groups[];
this._sqlSelectRemoved = this.Sql.Substring(g.Index); if (RegexDistinct.IsMatch(this._sqlSelectRemoved))
this.SqlCount = this.Sql.Substring(, g.Index) + "COUNT(" + m.Groups[].ToString().Trim() + ") " + sql.Substring(g.Index + g.Length);
else
this.SqlCount = this.Sql.Substring(, g.Index) + "COUNT(*) " + sql.Substring(g.Index + g.Length); // sql语句最后order by
m = RegexOrderBy.Match(this.SqlCount);
if (m.Success)
{
g = m.Groups[];
this.SqlOrderBy = g.ToString();
this.SqlCount = this.SqlCount.Substring(, g.Index) + this.SqlCount.Substring(g.Index + g.Length);
}
//分页读取数据
SqlPage = string.Format("{0}\nLIMIT {1} OFFSET {2}", this.Sql, take, skip);
} public static MysqlPageHelper BuildPageQuery(string sql, long skip, long take)
{
var page=new MysqlPageHelper();
page.SplitSql(sql,skip,take);
return page;
} public string Sql { get; private set; }
public string SqlPage { get;private set; }
public string SqlCount { get;private set; }
public string SqlOrderBy { get;private set; }
}
}

最新文章

  1. C++ 系列:C++ 对象模型
  2. MySQL 常用的sql语句小结(待续)
  3. backup mysql
  4. nodejs review-03
  5. as3自定义事件
  6. (ios实战)单个ViewControl适配不同ios版本xib文件实现
  7. HD1847 Good Luck in CET-4 Everybody!(巴什博弈)
  8. make -f dc_debug.mak 提示错误&quot;/usr/bin/ld:can not find -l***&quot;解决办法
  9. Spring4整合Hibernate4详细示例
  10. HDOJ-ACM1010(JAVA) 奇偶剪枝法 迷宫搜索
  11. JS 常用功能收集
  12. 【转载】Centos7 中使用Supervisor守护进程
  13. 解析ArcGis的字段计算器(二)——有玄机的要素Geometry属性,在属性表标记重复点线面
  14. mui列表系列
  15. npm install的时候出现unexpected end of file错误提示时的解决办法
  16. pip install pyinstaller
  17. hdu 2612
  18. jquery 给iframe里的元素添加事件
  19. Hive安装与应用过程
  20. url参数中有+、空格、=、%、&amp;、#等特殊符号的问题解决

热门文章

  1. Dr.com──加密方式(网页端)
  2. How to:如何让Installshield显示正确的软件所需空间--网友冰块先生贡献
  3. IntelliJ IDEA 工具常用快捷键
  4. new date() 在Linux下引起的时间差问题
  5. CSS应用心得
  6. Sqlserver 函数
  7. 如何处理json数据
  8. IIS 301 重定向 带参数链接
  9. Servlet页面注册用户的小程序(一)
  10. Python初学的易犯错误