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.

Helper.java 1.8KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package com.rthoni.intellij.codefromds.business;
  2. import com.intellij.database.psi.DbDataSource;
  3. import com.intellij.database.psi.DbPsiFacade;
  4. import com.intellij.openapi.project.Project;
  5. import com.intellij.openapi.project.ProjectManager;
  6. import com.rthoni.intellij.codefromds.dbo.TableSelection;
  7. import org.json.JSONArray;
  8. import org.json.JSONObject;
  9. import java.util.Arrays;
  10. import java.util.Collection;
  11. import java.util.List;
  12. import java.util.Optional;
  13. import java.util.stream.Collectors;
  14. /**
  15. * Created by robin on 11/15/16.
  16. */
  17. public abstract class Helper {
  18. public static List<DbDataSource> getDataSources()
  19. {
  20. ProjectManager pm = ProjectManager.getInstance();
  21. Project[] projects = pm.getOpenProjects();
  22. return Arrays.stream(projects).map(project -> DbPsiFacade.getInstance(project).getDataSources())
  23. .flatMap(Collection::stream).collect(Collectors.toList());
  24. }
  25. public static DbDataSource findDataSource(String name)
  26. {
  27. Optional<DbDataSource> opt = getDataSources().stream().filter(d -> d.getName().equals(name)).findFirst();
  28. if (opt.isPresent()) {
  29. return opt.get();
  30. }
  31. return null;
  32. }
  33. public static JSONObject findTableInJson(JSONArray tables, String name)
  34. {
  35. for (int i = 0; i < tables.length(); ++i) {
  36. if (tables.getJSONObject(i).getString("table").equals(name)) {
  37. return tables.getJSONObject(i);
  38. }
  39. }
  40. return null;
  41. }
  42. public static JSONObject findColumnInJson(JSONArray tables, String name)
  43. {
  44. for (int i = 0; i < tables.length(); ++i) {
  45. if (tables.getJSONObject(i).getString("column").equals(name)) {
  46. return tables.getJSONObject(i);
  47. }
  48. }
  49. return null;
  50. }
  51. }