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 2.1KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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.io.IOException;
  10. import java.nio.charset.StandardCharsets;
  11. import java.nio.file.Files;
  12. import java.nio.file.Paths;
  13. import java.util.Arrays;
  14. import java.util.Collection;
  15. import java.util.List;
  16. import java.util.Optional;
  17. import java.util.stream.Collectors;
  18. /**
  19. * Created by robin on 11/15/16.
  20. */
  21. public abstract class Helper {
  22. public static List<DbDataSource> getDataSources()
  23. {
  24. ProjectManager pm = ProjectManager.getInstance();
  25. Project[] projects = pm.getOpenProjects();
  26. return Arrays.stream(projects).map(project -> DbPsiFacade.getInstance(project).getDataSources())
  27. .flatMap(Collection::stream).collect(Collectors.toList());
  28. }
  29. public static DbDataSource findDataSource(String name)
  30. {
  31. Optional<DbDataSource> opt = getDataSources().stream().filter(d -> d.getName().equals(name)).findFirst();
  32. if (opt.isPresent()) {
  33. return opt.get();
  34. }
  35. return null;
  36. }
  37. public static JSONObject findTableInJson(JSONArray tables, String name)
  38. {
  39. for (int i = 0; i < tables.length(); ++i) {
  40. if (tables.getJSONObject(i).getString("table").equals(name)) {
  41. return tables.getJSONObject(i);
  42. }
  43. }
  44. return null;
  45. }
  46. public static JSONObject findColumnInJson(JSONArray tables, String name)
  47. {
  48. for (int i = 0; i < tables.length(); ++i) {
  49. if (tables.getJSONObject(i).getString("column").equals(name)) {
  50. return tables.getJSONObject(i);
  51. }
  52. }
  53. return null;
  54. }
  55. public static String readFile(String path) throws IOException {
  56. return Files.readAllLines(Paths.get(path), StandardCharsets.UTF_8)
  57. .stream().reduce("", (s1, s2) -> s1 + s2 + "\n");
  58. }
  59. }