SqlPrintHandler.java 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. package jnpf.database.sql.model;
  2. import lombok.AccessLevel;
  3. import io.swagger.v3.oas.annotations.media.Schema;
  4. import lombok.Data;
  5. import lombok.Setter;
  6. import lombok.ToString;
  7. import org.springframework.stereotype.Component;
  8. import java.io.*;
  9. /**
  10. * 打印SQL语句功能类
  11. *
  12. * @author JNPF开发平台组 YanYu
  13. * @version v3.4.5
  14. * @copyrignt 引迈信息技术有限公司
  15. * @date 2022-12-06
  16. */
  17. @Component
  18. @ToString
  19. @Data
  20. public class SqlPrintHandler {
  21. /**
  22. * 打印使用的SQL
  23. */
  24. private StringBuffer sql = new StringBuffer();
  25. private StringBuffer sql2 = new StringBuffer();
  26. /**
  27. * 将要转换成的数据库类型
  28. */
  29. private String toDbType;
  30. /**
  31. * 创表开关
  32. */
  33. private Boolean creFlag = false;
  34. /**
  35. * 注释开关
  36. */
  37. private Boolean commentFlag = false;
  38. /**
  39. * 插入开关
  40. */
  41. private Boolean insertFlag = false;
  42. /**
  43. * 打印开关
  44. */
  45. private Boolean printFlag = false;
  46. /**
  47. * 执行开关
  48. */
  49. private Boolean executeFlag = true;
  50. /**
  51. * 文件
  52. */
  53. @Setter(value = AccessLevel.NONE)
  54. private File sqlFile;
  55. private File sqlFile2;
  56. /**
  57. * 开启打印状态
  58. */
  59. public void start(String outPath, Boolean creFlag, Boolean commentFlag, Boolean insertFlag, String toDbType) throws Exception {
  60. printFlag = true;
  61. this.executeFlag = false;
  62. this.sqlFile = formatOutPath(outPath);
  63. this.creFlag = creFlag;
  64. this.commentFlag = commentFlag;
  65. this.insertFlag = insertFlag;
  66. this.toDbType = toDbType;
  67. }
  68. /**
  69. * 格式化路径
  70. * @param outPath 输出路径
  71. */
  72. public static File formatOutPath(String outPath) throws Exception{
  73. File file = new File(outPath);
  74. if(file.exists()){
  75. if(file.isDirectory()) {
  76. return file;
  77. }else if(file.isFile()){
  78. return new File(file.getParent());
  79. }else {
  80. throw new Exception("路径异常");
  81. }
  82. }else {
  83. boolean flag = file.mkdir();
  84. if(!flag) throw new Exception("路径异常");
  85. return file;
  86. }
  87. }
  88. /**
  89. * 关闭打印状态
  90. */
  91. public void close(){
  92. this.printFlag = false;
  93. this.creFlag = false;
  94. this.commentFlag = false;
  95. this.insertFlag = false;
  96. this.executeFlag = true;
  97. this.sqlFile = null;
  98. this.sqlFile2 = null;
  99. clear();
  100. }
  101. private void clear(){
  102. this.sql = new StringBuffer();
  103. this.sql2 = new StringBuffer();
  104. }
  105. public void setToDbType(String toDbType){
  106. this.toDbType = toDbType;
  107. }
  108. public void setFileName(String fileName){
  109. if(sqlFile.isFile()){
  110. sqlFile = new File(sqlFile.getPath().replace(sqlFile.getName(), fileName));
  111. }else if(this.sqlFile.isDirectory()){
  112. sqlFile = new File(sqlFile.getPath() + "/" + fileName);
  113. }
  114. }
  115. public Boolean creTable(String sql){
  116. if(this.printFlag && this.creFlag) this.sql.append(sql).append(";\n");
  117. return this.printFlag;
  118. }
  119. public Boolean dropTable(String sql){
  120. if(this.printFlag && this.creFlag) this.sql.append(sql).append(";\n");
  121. return this.printFlag;
  122. }
  123. public Boolean oracleAutoIncrement(String sql){
  124. if(this.printFlag) this.sql.append(sql).append(";\n");
  125. return this.printFlag;
  126. }
  127. public Boolean comment(String sql){
  128. if(this.printFlag && this.commentFlag) this.sql.append(sql).append(";\n");
  129. return this.printFlag;
  130. }
  131. public void deleteAllInfo(String sql){
  132. if(this.insertFlag) this.sql.append(sql).append(";\n");
  133. }
  134. public Boolean insert(String sql){
  135. if(this.printFlag && this.insertFlag) this.sql.append(sql);
  136. return this.printFlag;
  137. }
  138. /**
  139. * Oracle使用
  140. */
  141. public void updateClob(String sql){
  142. if(this.insertFlag) this.sql2.append(";\n/\n").append(sql);
  143. }
  144. public void tableInfo(String table){
  145. table = "-- ----------------------------\n" +
  146. "-- 表名:" + table + "\n" +
  147. "-- ----------------------------\n";
  148. if(insertFlag) this.sql.append(table);
  149. }
  150. public SqlPrintHandler append(String sql){
  151. if(this.insertFlag) this.sql.append(sql);
  152. return this;
  153. }
  154. public void print() throws Exception {
  155. if(printFlag){
  156. createSqlFile(sql.toString(), this.sqlFile);
  157. // if(sqlFile2 != null) createSqlFile(sql2.toString(), this.sqlFile2);
  158. }
  159. // 打印完清理SQL语句
  160. clear();
  161. }
  162. private void createSqlFile(String outSql, File file) throws Exception {
  163. OutputStream outputStream = new FileOutputStream(file);
  164. String CHARSET_UTF8 = "UTF-8";
  165. OutputStreamWriter writer = new OutputStreamWriter(outputStream, CHARSET_UTF8);
  166. writer.append(outSql);
  167. writer.close();
  168. }
  169. /**
  170. * 打开文件夹
  171. * @param folder 文件路径
  172. */
  173. public static void openDirectory(String folder) {
  174. File file = new File(folder);
  175. if (!file.exists()) {
  176. return;
  177. }
  178. Runtime runtime = null;
  179. try {
  180. runtime = Runtime.getRuntime();
  181. // if (!SystemUtil.isWindows) {
  182. if (true) {
  183. runtime.exec("cmd /c start explorer " + folder);
  184. } else {
  185. // System.out.println("is linux");
  186. runtime.exec("nautilus " + folder);
  187. }
  188. } catch (IOException ex) {
  189. ex.printStackTrace();
  190. } finally {
  191. if (null != runtime) {
  192. runtime.runFinalization();
  193. }
  194. }
  195. }
  196. }