EntitySQLHelper.java 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. package com.hcks.cmfds.commons.util;
  2. import java.util.Set;
  3. import com.hcks.cmfds.core.exception.JdbcDaoException;
  4. /**
  5. *
  6. * @Author 刘厦 (liusha.information@gmail.com)
  7. * @Date 创建时间:2011-5-22,下午06:12:59
  8. * @Version 0.0.0
  9. *
  10. * 类说明:
  11. *
  12. */
  13. public class EntitySQLHelper {
  14. private static String SELECT_BY_PK_SQL_TEMP = "select #FIELDS from #TABLENAME where #PK";
  15. // private static String SELECT_ALL_SQL_TEMP = "select #FIELDS from #TABLENAME ";
  16. private static String INSERT_SQL_TEMP = "insert into #TABLENAME (#FIELDS) values (#VALUES)";
  17. // private static String UPDATE_BY_PK_SQL_TEMP = "update #TABLENAME set #FIELDS where #PK";
  18. private static String DELETE_BY_PK_SQL_TEMP = "delete from #TABLENAME where #PK";
  19. /**
  20. * 自动生成主键查询字符串
  21. * @param pkFieldNames
  22. * @return
  23. */
  24. private static String generatePrimaryKeyFormatStr(Set<String> pkFieldNames) {
  25. StringBuilder pks = new StringBuilder();
  26. for (String pkName : pkFieldNames) {
  27. if (pks.length() > 0) {
  28. pks.append(" and ");
  29. }
  30. pks.append(pkName).append("=?");
  31. }
  32. return pks.toString();
  33. }
  34. /**
  35. * 所有字段
  36. * @param fields
  37. * @return
  38. */
  39. private static String generateFieldsFormatStr(Set<String> fields) {
  40. String _returnValue = fields.toString();
  41. return _returnValue.substring(1, _returnValue.length() - 1);
  42. }
  43. /**
  44. * 获取按主键查询sql语句
  45. * @param tableName
  46. * @param pkFieldNames
  47. * @return
  48. */
  49. public static String generateSelectByPKSql(String tableName, Set<String> pkFieldNames) {
  50. String sql = SELECT_BY_PK_SQL_TEMP;
  51. sql = sql.replaceAll("#FIELDS", "*");
  52. sql = sql.replaceAll("#TABLENAME", tableName);
  53. sql = sql.replaceAll("#PK", generatePrimaryKeyFormatStr(pkFieldNames));
  54. return sql;
  55. }
  56. public static String generateSelectByPKSql(String tableName,Set<String> pkFieldNames,Set<String> fields){
  57. String sql = SELECT_BY_PK_SQL_TEMP;
  58. sql = sql.replaceAll("#FIELDS", generateFieldsFormatStr(fields));
  59. sql = sql.replaceAll("#TABLENAME", tableName);
  60. sql = sql.replaceAll("#PK", generatePrimaryKeyFormatStr(pkFieldNames));
  61. return sql;
  62. }
  63. private static String replaceX(String sql, String key) {
  64. int i = sql.indexOf(key);
  65. int j = sql.lastIndexOf(key);
  66. boolean lf = false;
  67. int k = j - 1;
  68. if (k >= 0) {
  69. char f = sql.charAt(k);
  70. if (f == ' ') {
  71. lf = true;
  72. }
  73. }
  74. if (i == 0) {
  75. sql = sql.replaceFirst(key + ", ", "");
  76. } else if (lf) {
  77. sql = sql.replaceAll(", " + key, "");
  78. } else if (i > 0) {
  79. sql = sql.replaceAll(" " + key + ", ", "");
  80. }
  81. return sql;
  82. }
  83. private static String generateFieldsValueFormatStr(Set<String> fields) {
  84. StringBuffer _returnValue = new StringBuffer();
  85. for (int i = 0; i < fields.size(); i++) {
  86. _returnValue.append("?");
  87. if (i < fields.size() - 1) {
  88. _returnValue.append(",");
  89. }
  90. }
  91. return _returnValue.toString();
  92. }
  93. /**
  94. *
  95. * @param fields
  96. * @param tableName
  97. * @param autoKeyFlag
  98. * @param pkFieldNames
  99. * @return
  100. */
  101. public static String generateInsertSql(Set<String> fields, String tableName, boolean autoKeyFlag, Set<String> pkFieldNames) {
  102. String sql = INSERT_SQL_TEMP;
  103. String[] _pks = pkFieldNames.toArray(new String[pkFieldNames.size()]);
  104. if (autoKeyFlag && pkFieldNames.size() ==1) {
  105. String _fieldStr = generateFieldsFormatStr(fields);
  106. if ( _fieldStr.indexOf(_pks[0]) < 0) {
  107. throw new JdbcDaoException("表不存在此[" + _pks[0] + "]主键字段,请检查字段名称是否正确,区分大小写");
  108. } else {
  109. _fieldStr = replaceX(_fieldStr, _pks[0]);
  110. String _fieldValueStr = generateFieldsFormatStr(fields);
  111. _fieldValueStr = _fieldValueStr.substring(2);
  112. sql = sql.replaceAll("#TABLENAME", tableName);
  113. sql = sql.replaceAll("#FIELDS", _fieldStr);
  114. sql = sql.replaceAll("#VALUES", _fieldValueStr);
  115. }
  116. } else {
  117. sql = sql.replaceAll("#TABLENAME", tableName);
  118. sql = sql.replaceAll("#FIELDS", generateFieldsFormatStr(fields));
  119. sql = sql.replaceAll("#VALUES",generateFieldsValueFormatStr(fields));
  120. }
  121. return sql;
  122. }
  123. /**
  124. * 拼装按主键删除sql语句
  125. * @param tableName
  126. * @param pkFieldNames
  127. * @return
  128. */
  129. public static String generateDeleteByPKSql(String tableName, Set<String> pkFieldNames) {
  130. String sql = DELETE_BY_PK_SQL_TEMP;
  131. sql = sql.replaceAll("#TABLENAME", tableName);
  132. StringBuilder _pkStr = new StringBuilder();
  133. for (String pkName : pkFieldNames) {
  134. if (_pkStr.length() > 0) {
  135. _pkStr.append(" and ");
  136. }
  137. _pkStr.append(pkName).append("=?");
  138. }
  139. sql = sql.replaceAll("#PK", _pkStr.toString());
  140. return sql;
  141. }
  142. }