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.

DataSourcesDataAccess.java 7.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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.setType(column.getDataType().typeName);
  47. type.setVagueArg(column.getDataType().vagueArg);
  48. columnDataSourceDbo.data.setSqlType(type);
  49. columnDataSourceDbo.data.setDefaultValue(column.getDefault());
  50. columnDataSourceDbo.data.setPrimary(DasUtil.isPrimary(column));
  51. tableDataSourceDbo.data.addColumn(columnDataSourceDbo.data);
  52. }
  53. });
  54. dataSource.getModel().traverser().forEach(dasObject ->
  55. {
  56. if (dasObject instanceof DbTable) {
  57. DbTable table = (DbTable) dasObject;
  58. tableDataSourceDbo.data = dataSourceDbo.findTable(table.getName());
  59. DasUtil.getForeignKeys(table).forEach(fk ->
  60. {
  61. if (fk.getRefTable() == null) {
  62. return;
  63. }
  64. TableDataSourceDbo srcTable = dataSourceDbo.findTable(fk.getTable().getName());
  65. TableDataSourceDbo targetTable = dataSourceDbo.findTable(fk.getRefTable().getName());
  66. ForeignKeyDbo fkDbo = new ForeignKeyDbo();
  67. fkDbo.setName(fk.getName());
  68. fkDbo.setSourceTable(srcTable);
  69. fkDbo.setSourceForeignKeyName(fkDbo.getName());//TODO find if it already exists
  70. fkDbo.setTargetTable(targetTable);
  71. fkDbo.setTargetForeignKeyName(fkDbo.getName());//TODO find if it already exists
  72. fk.getColumnsRef().names().forEach(s -> fkDbo.addSourceColumn(srcTable.findColumn(s)));
  73. fk.getRefColumns().names().forEach(s -> fkDbo.addTargetColumn(targetTable.findColumn(s)));
  74. srcTable.addSourceForeignKey(fkDbo);
  75. targetTable.addTargetForeignKey(fkDbo);
  76. });
  77. }
  78. else if (dasObject instanceof DbColumn) {
  79. DbColumn column = (DbColumn) dasObject;
  80. columnDataSourceDbo.data = tableDataSourceDbo.data.findColumn(column.getName());
  81. }
  82. else if (dasObject instanceof DbIndex) {
  83. DbIndex index = (DbIndex) dasObject;
  84. }
  85. else if (dasObject instanceof DbConstraint) {
  86. DbConstraint constraint = (DbConstraint) dasObject;
  87. }
  88. else if (dasObject instanceof DbRoutine) {
  89. DbRoutine sp = (DbRoutine) dasObject;
  90. StoredProcedureDbo spDbo = new StoredProcedureDbo();
  91. spDbo.setSelected(false);
  92. spDbo.setName(sp.getName());
  93. spDbo.setFullName(sp.getText());
  94. dataSourceDbo.addStoredProcedure(spDbo);
  95. DataHolder<Integer> position = new DataHolder<>();
  96. position.data = 0;
  97. TableDataSourceDbo returnType = new TableDataSourceDbo();
  98. returnType.setName(spDbo.getName());
  99. sp.getArguments().forEach(o ->
  100. {
  101. SqlTypeDbo sqlTypeDbo = new SqlTypeDbo();
  102. sqlTypeDbo.setType(o.getDataType().typeName);
  103. sqlTypeDbo.setVagueArg(o.getDataType().vagueArg);
  104. DasArgument.Direction dir = o.getArgumentDirection();
  105. if (dir.isReturnOrResult()) {
  106. spDbo.setSqlType(sqlTypeDbo);
  107. }
  108. if (dir.isIn()) {
  109. StoredProcedureArgDbo argDbo = new StoredProcedureArgDbo();
  110. argDbo.setName(o.getName().isEmpty() ? "arg" + position.data : o.getName());
  111. argDbo.setSqlType(sqlTypeDbo);
  112. spDbo.addArgument(argDbo);
  113. ++position.data;
  114. }
  115. if (dir.isOut()) {
  116. ColumnDataSourceDbo field = new ColumnDataSourceDbo();
  117. field.setSelected(true);
  118. field.setPrimary(false);
  119. field.setDefaultValue(null);
  120. field.setNotNull(false);
  121. field.setName(o.getName());
  122. field.setSqlType(sqlTypeDbo);
  123. returnType.addColumn(field);
  124. }
  125. });
  126. if (returnType.getColumns().size() > 1) {
  127. boolean isSetOf = sp.getReturnArgument().getDataType().typeName.startsWith("setof ");
  128. SqlTypeDbo sqlTypeDbo = new SqlTypeDbo();
  129. sqlTypeDbo.setType((isSetOf ? "setof " : "" ) + returnType.getName());
  130. sqlTypeDbo.setVagueArg(null);
  131. spDbo.setSqlType(sqlTypeDbo);
  132. dataSourceDbo.addType(returnType);
  133. }
  134. else {
  135. DasArgument returnArg = sp.getReturnArgument();
  136. if (returnArg != null) {
  137. }
  138. }
  139. }
  140. });
  141. return dataSourceDbo;
  142. }
  143. }