Вы не можете выбрать более 25 тем Темы должны начинаться с буквы или цифры, могут содержать дефисы(-) и должны содержать не более 35 символов.

DataSourcesDataAccess.java 5.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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(true);
  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. sp.getArguments().forEach(o ->
  98. {
  99. SqlTypeDbo sqlTypeDbo = new SqlTypeDbo();
  100. sqlTypeDbo.setType(o.getDataType().typeName);
  101. sqlTypeDbo.setVagueArg(o.getDataType().vagueArg);
  102. DasArgument.Direction dir = o.getArgumentDirection();
  103. if (dir.isReturnOrResult()) {
  104. spDbo.setSqlType(sqlTypeDbo);
  105. }
  106. if (dir.isIn()) {
  107. StoredProcedureArgDbo argDbo = new StoredProcedureArgDbo();
  108. argDbo.setName(o.getName().isEmpty() ? "arg" + position.data : o.getName());
  109. argDbo.setSqlType(sqlTypeDbo);
  110. argDbo.setOut(o.getArgumentDirection().isOut());
  111. spDbo.addArgument(argDbo);
  112. ++position.data;
  113. }
  114. });
  115. // DasArgument arg = sp.getReturnArgument();
  116. // if (arg != null)
  117. // arg.toString();
  118. }
  119. });
  120. return dataSourceDbo;
  121. }
  122. }