You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

155 lines
5.5 KiB

2 years ago
  1. using EasyNet.DBUtility;
  2. using System;
  3. using System.Data;
  4. namespace EasyNet.Common
  5. {
  6. public class SQLBuilderHelper
  7. {
  8. private static readonly string mssqlPageTemplate = @"select * from (select ROW_NUMBER() OVER(order by {0}) AS RowIndex, {1}) as tmp_tbl where RowIndex BETWEEN @pageStart and @pageEnd ";
  9. private static readonly string mysqlOrderPageTemplate = @"{0} order by {1} limit ?offset,?limit";
  10. private static readonly string mysqlPageTemplate = @"{0} limit ?offset,?limit";
  11. private static readonly string accessPageTemplate = @"select * from (select top @limit * from (select top @offset {0} order by id desc) order by id) order by {1}";
  12. public static string FetchColumns(string strSQL)
  13. {
  14. strSQL = strSQL.Trim();
  15. var columns = strSQL.Substring(6, strSQL.IndexOf(" from ") - 6);
  16. return columns;
  17. }
  18. public static string FetchPageBody(string strSQL)
  19. {
  20. var body = strSQL.Trim().Substring(6);
  21. return body;
  22. }
  23. public static string FetchWhere(string strSQL)
  24. {
  25. var index = strSQL.LastIndexOf("where");
  26. if (index == -1) return "";
  27. var where = strSQL.Substring(index, strSQL.Length - index);
  28. return where;
  29. }
  30. public static bool IsPage(string strSQL)
  31. {
  32. var strSql = strSQL.ToLower();
  33. if (AdoHelper.DbType == DatabaseType.ACCESS && strSql.IndexOf("top") == -1)
  34. {
  35. return false;
  36. }
  37. if (AdoHelper.DbType == DatabaseType.SQLSERVER && strSql.IndexOf("row_number()") == -1)
  38. {
  39. return false;
  40. }
  41. if (AdoHelper.DbType == DatabaseType.MYSQL && strSql.IndexOf("limit") == -1)
  42. {
  43. return false;
  44. }
  45. if (AdoHelper.DbType == DatabaseType.ORACLE && strSql.IndexOf("rowid") == -1)
  46. {
  47. return false;
  48. }
  49. return true;
  50. }
  51. public static string BuilderPageSQL(string strSql, string orderField, string order)
  52. {
  53. var columns = FetchColumns(strSql);
  54. var orderBy = orderField + " " + order;
  55. if (AdoHelper.DbType == DatabaseType.SQLSERVER && strSql.IndexOf("row_number()") == -1)
  56. {
  57. if (string.IsNullOrEmpty(order))
  58. {
  59. throw new Exception(" SqlException: order field is null, you must support the order field for sqlserver page. ");
  60. }
  61. var pageBody = FetchPageBody(strSql);
  62. strSql = string.Format(mssqlPageTemplate, orderBy, pageBody);
  63. }
  64. if (AdoHelper.DbType == DatabaseType.ACCESS && strSql.IndexOf("top") == -1)
  65. {
  66. if (string.IsNullOrEmpty(order))
  67. {
  68. throw new Exception(" SqlException: order field is null, you must support the order field for sqlserver page. ");
  69. }
  70. //select {0} from (select top @pageSize {1} from (select top @pageSize*@pageIndex {2} from {3} order by {4}) order by id) order by {5}
  71. var pageBody = FetchPageBody(strSql);
  72. strSql = string.Format(accessPageTemplate, pageBody, orderBy);
  73. }
  74. if (AdoHelper.DbType == DatabaseType.MYSQL)
  75. {
  76. if (!string.IsNullOrEmpty(order))
  77. {
  78. strSql = string.Format(mysqlOrderPageTemplate, strSql, orderBy);
  79. }
  80. else
  81. {
  82. strSql = string.Format(mysqlPageTemplate, strSql);
  83. }
  84. }
  85. return strSql;
  86. }
  87. public static string BuilderCountSQL(string strSQL)
  88. {
  89. var index = strSQL.IndexOf(" from ");
  90. var strFooter = strSQL.Substring(index, strSQL.Length - index);
  91. var strText = "select count(*) " + strFooter;
  92. return strText;
  93. }
  94. public static string BuilderSQL(object entity, string strSql, IDbDataParameter[] parameters)
  95. {
  96. if (AdoHelper.DbType == DatabaseType.ACCESS || AdoHelper.DbType == DatabaseType.SQLSERVER)
  97. {
  98. foreach (IDbDataParameter param in parameters)
  99. {
  100. if (param.Value == null) continue;
  101. var paramName = param.ParameterName;
  102. var paramValue = param.Value.ToString();
  103. var type = ReflectionHelper.GetPropertyType(entity, paramName);
  104. if (type == "System.String" || type == "System.DateTime")
  105. {
  106. paramValue = "'" + paramValue + "'";
  107. }
  108. strSql = strSql.Replace("@" + paramName, paramValue);
  109. }
  110. }
  111. return strSql;
  112. }
  113. public static string BuilderSQL(string strSql, IDbDataParameter[] parameters)
  114. {
  115. if (AdoHelper.DbType == DatabaseType.ACCESS || AdoHelper.DbType == DatabaseType.SQLSERVER)
  116. {
  117. foreach (IDbDataParameter param in parameters)
  118. {
  119. if (param.Value == null) continue;
  120. var paramName = param.ParameterName;
  121. var paramValue = param.Value.ToString();
  122. strSql = strSql.Replace("@" + paramName, paramValue);
  123. }
  124. }
  125. return strSql;
  126. }
  127. }
  128. }