選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

DataSourcesDataAccess.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. package com.rthoni.intellij.codefromds.DataAccess;
  2. import com.intellij.database.model.DasArgument;
  3. import com.intellij.database.psi.*;
  4. import com.intellij.database.util.DasUtil;
  5. import com.rthoni.intellij.codefromds.business.Helper;
  6. import com.rthoni.intellij.codefromds.dbo.template.*;
  7. import java.util.List;
  8. import java.util.stream.Collectors;
  9. /**
  10. * Created by robin on 4/13/17.
  11. */
  12. public class DataSourcesDataAccess {
  13. public static List<DataSourceDbo> getDataSourcesDbo()
  14. {
  15. return Helper.getDataSources().stream()
  16. .map(DataSourcesDataAccess::dataSourceToDbo)
  17. .collect(Collectors.toList());
  18. }
  19. public static DataSourceDbo getDataSourceDbo(String name)
  20. {
  21. return Helper.getDataSources().stream()
  22. .filter(dataSource -> dataSource.getName().equals(name))
  23. .map(DataSourcesDataAccess::dataSourceToDbo)
  24. .findFirst().orElse(null);
  25. }
  26. private static DataSourceDbo dataSourceToDbo(DbDataSource dataSource)
  27. {
  28. DataSourceDbo dataSourceDbo = new DataSourceDbo();
  29. dataSourceDbo.setName(dataSource.getName());
  30. final DataHolder<TableDataSourceDbo> tableDataSourceDbo = new DataHolder<>();
  31. final DataHolder<ColumnDataSourceDbo> columnDataSourceDbo = new DataHolder<>();
  32. dataSource.getModel().traverser().forEach(dasObject ->
  33. {
  34. if (dasObject instanceof DbTable) {
  35. DbTable table = (DbTable) dasObject;
  36. tableDataSourceDbo.data = new TableDataSourceDbo();
  37. tableDataSourceDbo.data.setName(table.getName());
  38. dataSourceDbo.addTable(tableDataSourceDbo.data);
  39. }
  40. else if (dasObject instanceof DbColumn) {
  41. DbColumn column = (DbColumn) dasObject;
  42. columnDataSourceDbo.data = new ColumnDataSourceDbo();
  43. columnDataSourceDbo.data.setName(column.getName());
  44. columnDataSourceDbo.data.setNotNull(column.isNotNull());
  45. SqlTypeDbo type = new SqlTypeDbo();
  46. type.setName(column.getDataType().typeName);
  47. type.guessArrayAndSetOf();
  48. type.setVagueArg(column.getDataType().vagueArg);
  49. columnDataSourceDbo.data.setType(type);
  50. columnDataSourceDbo.data.setDefaultValue(column.getDefault());
  51. columnDataSourceDbo.data.setPrimary(DasUtil.isPrimary(column));
  52. tableDataSourceDbo.data.addColumn(columnDataSourceDbo.data);
  53. }
  54. });
  55. dataSource.getModel().traverser().forEach(dasObject ->
  56. {
  57. if (dasObject instanceof DbTable) {
  58. DbTable table = (DbTable) dasObject;
  59. tableDataSourceDbo.data = dataSourceDbo.findTable(table.getName());
  60. DasUtil.getForeignKeys(table).forEach(fk ->
  61. {
  62. if (fk.getRefTable() == null) {
  63. return;
  64. }
  65. TableDataSourceDbo srcTable = dataSourceDbo.findTable(fk.getTable().getName());
  66. TableDataSourceDbo targetTable = dataSourceDbo.findTable(fk.getRefTable().getName());
  67. ForeignKeyDbo fkDbo = new ForeignKeyDbo();
  68. fkDbo.setName(fk.getName());
  69. fkDbo.setSourceTable(srcTable);
  70. fkDbo.setSourceForeignKeyName(fkDbo.getName());//TODO find if it already exists
  71. fkDbo.setTargetTable(targetTable);
  72. fkDbo.setTargetForeignKeyName(fkDbo.getName());//TODO find if it already exists
  73. fk.getColumnsRef().names().forEach(s -> fkDbo.addSourceColumn(srcTable.findColumn(s)));
  74. fk.getRefColumns().names().forEach(s -> fkDbo.addTargetColumn(targetTable.findColumn(s)));
  75. srcTable.addSourceForeignKey(fkDbo);
  76. targetTable.addTargetForeignKey(fkDbo);
  77. });
  78. }
  79. else if (dasObject instanceof DbColumn) {
  80. DbColumn column = (DbColumn) dasObject;
  81. columnDataSourceDbo.data = tableDataSourceDbo.data.findColumn(column.getName());
  82. }
  83. else if (dasObject instanceof DbIndex) {
  84. DbIndex index = (DbIndex) dasObject;
  85. }
  86. else if (dasObject instanceof DbConstraint) {
  87. DbConstraint constraint = (DbConstraint) dasObject;
  88. }
  89. else if (dasObject instanceof DbRoutine) {
  90. DbRoutine sp = (DbRoutine) dasObject;
  91. StoredProcedureDbo spDbo = new StoredProcedureDbo();
  92. spDbo.setSelected(false);
  93. spDbo.setName(sp.getName());
  94. spDbo.setFullName(sp.getText());
  95. dataSourceDbo.addStoredProcedure(spDbo);
  96. DataHolder<Integer> position = new DataHolder<>();
  97. position.data = 0;
  98. TableDataSourceDbo returnType = new TableDataSourceDbo();
  99. returnType.setName(spDbo.getName());
  100. sp.getArguments().forEach(o ->
  101. {
  102. SqlTypeDbo sqlTypeDbo = new SqlTypeDbo();
  103. sqlTypeDbo.setName(o.getDataType().typeName);
  104. sqlTypeDbo.guessArrayAndSetOf();
  105. sqlTypeDbo.setVagueArg(o.getDataType().vagueArg);
  106. DasArgument.Direction dir = o.getArgumentDirection();
  107. if (dir.isReturnOrResult()) {
  108. spDbo.setType(sqlTypeDbo);
  109. }
  110. if (dir.isIn()) {
  111. StoredProcedureArgDbo argDbo = new StoredProcedureArgDbo();
  112. argDbo.setName(o.getName().isEmpty() ? "arg" + position.data : o.getName());
  113. argDbo.setType(sqlTypeDbo);
  114. spDbo.addArgument(argDbo);
  115. ++position.data;
  116. }
  117. if (dir.isOut()) {
  118. ColumnDataSourceDbo field = new ColumnDataSourceDbo();
  119. field.setSelected(true);
  120. field.setPrimary(false);
  121. field.setDefaultValue(null);
  122. field.setNotNull(false);
  123. field.setName(o.getName());
  124. field.setType(sqlTypeDbo);
  125. returnType.addColumn(field);
  126. }
  127. });
  128. if (returnType.getColumns().size() > 1) {
  129. boolean isSetOf = sp.getReturnArgument().getDataType().typeName.startsWith("setof ");
  130. SqlTypeDbo sqlTypeDbo = new SqlTypeDbo();
  131. sqlTypeDbo.setName((isSetOf ? "setof " : "" ) + returnType.getName());
  132. sqlTypeDbo.guessArrayAndSetOf();
  133. sqlTypeDbo.setVagueArg(null);
  134. spDbo.setType(sqlTypeDbo);
  135. dataSourceDbo.addType(returnType);
  136. }
  137. }
  138. });
  139. return dataSourceDbo;
  140. }
  141. }