ParserMysql.java 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. package com.yihu.mysql.sqlparser;
  2. import com.yihu.mysql.enums.Logical;
  3. import com.yihu.mysql.enums.Operation;
  4. import com.yihu.mysql.model.QueryCondition;
  5. import com.yihu.mysql.model.QueryEntity;
  6. import java.text.SimpleDateFormat;
  7. import java.util.Date;
  8. import java.util.List;
  9. /**
  10. * Mysql的sql解析
  11. * Created by hzp on 2015/11/30.
  12. */
  13. public class ParserMysql extends ParserSql {
  14. /**
  15. * 获取keyword转sql
  16. * @param keyword
  17. * @return
  18. */
  19. @Override
  20. public String getKeywordSql(Object keyword){
  21. if (keyword instanceof Date) {
  22. String dateFormat = "yyyy-MM-dd HH:mm:ss";
  23. Date d = (Date) keyword;
  24. SimpleDateFormat format = new SimpleDateFormat(dateFormat);
  25. return "date_format('"+ format .format(d)+"','"+dateFormat+"')";
  26. }
  27. else
  28. {
  29. return "'"+ keyword +"'";
  30. }
  31. }
  32. /**
  33. * 条件转换
  34. * @return
  35. */
  36. @Override
  37. public String getConditionSql(List<QueryCondition> conditions){
  38. String conditionSql = "";
  39. for(QueryCondition qc : conditions){
  40. if(qc.getLogical().equals(Logical.OR))
  41. {
  42. conditionSql += " or ";
  43. }
  44. else{
  45. conditionSql += " and ";
  46. }
  47. String operation = qc.getOperation();
  48. if(operation.equals(Operation.IN) || operation.equals(Operation.NIN))
  49. {
  50. Object[] keys = qc.getKeywords();
  51. String keywords = "";
  52. if(keys!=null&&keys.length>0)
  53. {
  54. for (Object key :keys)
  55. {
  56. keywords += "'"+key+"',";
  57. }
  58. keywords = keywords.substring(0,keywords.length()-1);
  59. }
  60. conditionSql += qc.getField() + operation + keywords;
  61. }
  62. else if(operation.equals(Operation.LIKE))
  63. {
  64. conditionSql += qc.getField() + operation + "'%"+qc.getKeyword()+"%'";
  65. }
  66. else{
  67. conditionSql += qc.getField() + operation + getKeywordSql(qc.getKeyword());
  68. }
  69. }
  70. return conditionSql;
  71. }
  72. /**
  73. * 转换成sql语句
  74. */
  75. @Override
  76. public String getSql(QueryEntity qe){
  77. String fields = qe.getFields();
  78. String tableName = qe.getTableName();
  79. String sort = qe.getSort();
  80. //查询条件
  81. String conditionSql = "";
  82. List<QueryCondition> conditions = qe.getConditions();
  83. if(conditions!=null && conditions.size()>0)
  84. {
  85. conditionSql = getConditionSql(conditions);
  86. }
  87. String sql = "select "+fields+" from "+ tableName +" where 1=1" + conditionSql;
  88. //判断是否排序
  89. if(sort!=null &&sort.length()>0)
  90. {
  91. sql+= " ORDER BY " + sort;
  92. }
  93. //判断是否分页
  94. if(qe.isPage())
  95. {
  96. int page = qe.getPage();
  97. int rows = qe.getRows();
  98. sql = getPageSql(sql,page,rows);
  99. }
  100. return sql;
  101. }
  102. /**
  103. * 转换成获取count的sql语句
  104. */
  105. @Override
  106. public String getCountSql(QueryEntity qe){
  107. String tableName = qe.getTableName();
  108. //查询条件
  109. String conditionSql = "";
  110. List<QueryCondition> conditions = qe.getConditions();
  111. if(conditions!=null && conditions.size()>0)
  112. {
  113. conditionSql = getConditionSql(conditions);
  114. }
  115. return "select count(1) from "+ tableName +" where 1=1" + conditionSql;
  116. }
  117. /**
  118. * 通过sql语句再分页
  119. * @return
  120. */
  121. @Override
  122. public String getPageSql(String sql,int page,int rows)
  123. {
  124. if(rows < 0) rows = 10;
  125. if(rows >100) rows = 100;
  126. if(page <0) page = 1;
  127. int start= (page-1) * rows;
  128. return sql +" LIMIT "+start+","+rows;
  129. }
  130. }