12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758 |
- package com.rthoni.intellij.codefromds.business;
-
- import com.intellij.database.psi.DbDataSource;
- import com.intellij.database.psi.DbPsiFacade;
- import com.intellij.openapi.project.Project;
- import com.intellij.openapi.project.ProjectManager;
- import com.rthoni.intellij.codefromds.dbo.TableSelection;
- import org.json.JSONArray;
- import org.json.JSONObject;
-
- import java.util.Arrays;
- import java.util.Collection;
- import java.util.List;
- import java.util.Optional;
- import java.util.stream.Collectors;
-
- /**
- * Created by robin on 11/15/16.
- */
- public abstract class Helper {
-
- public static List<DbDataSource> getDataSources()
- {
- ProjectManager pm = ProjectManager.getInstance();
- Project[] projects = pm.getOpenProjects();
- return Arrays.stream(projects).map(project -> DbPsiFacade.getInstance(project).getDataSources())
- .flatMap(Collection::stream).collect(Collectors.toList());
- }
-
- public static DbDataSource findDataSource(String name)
- {
- Optional<DbDataSource> opt = getDataSources().stream().filter(d -> d.getName().equals(name)).findFirst();
- if (opt.isPresent()) {
- return opt.get();
- }
- return null;
- }
-
- public static JSONObject findTableInJson(JSONArray tables, String name)
- {
- for (int i = 0; i < tables.length(); ++i) {
- if (tables.getJSONObject(i).getString("table").equals(name)) {
- return tables.getJSONObject(i);
- }
- }
- return null;
- }
-
- public static JSONObject findColumnInJson(JSONArray tables, String name)
- {
- for (int i = 0; i < tables.length(); ++i) {
- if (tables.getJSONObject(i).getString("column").equals(name)) {
- return tables.getJSONObject(i);
- }
- }
- return null;
- }
- }
|