Skip to content

Commit 2c3b690

Browse files
authored
Added neo4j db schema info (#48)
1 parent 94532a1 commit 2c3b690

File tree

7 files changed

+97
-24
lines changed

7 files changed

+97
-24
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,5 @@ hs_err_pid*
9696
idea-flex.skeleton
9797
/book-dev
9898
/_book
99+
/.ideaDataSources
100+
/dataSources

platform/src/main/java/com/neueda/jetbrains/plugin/graphdb/platform/GraphIcons.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ public static final class Window {
2323
}
2424

2525
public static final class Nodes {
26+
public static final Icon INDEX = AllIcons.Nodes.ResourceBundle;
27+
public static final Icon CONSTRAINT = AllIcons.Nodes.C_protected;
2628
public static final Icon LABEL = AllIcons.Nodes.Class;
2729
public static final Icon RELATIONSHIP_TYPE = AllIcons.Vcs.Arrow_right;
2830
public static final Icon PROPERTY_KEY = AllIcons.Nodes.Property;

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/component/datasource/metadata/DataSourcesComponentMetadata.java

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
import java.util.Optional;
2020
import java.util.stream.Collectors;
2121

22+
import static java.util.stream.Collectors.toList;
23+
2224
public class DataSourcesComponentMetadata implements ProjectComponent {
2325

2426
private CypherMetadataProviderService cypherMetadataProviderService;
@@ -57,11 +59,16 @@ private DataSourceMetadata getNeo4jBoltMetadata(DataSourceApi dataSource) {
5759
GraphDatabaseApi db = databaseManager.getDatabaseFor(dataSource);
5860
Neo4jBoltCypherDataSourceMetadata metadata = new Neo4jBoltCypherDataSourceMetadata();
5961

62+
GraphQueryResult indexesResult = db.execute("CALL db.indexes()");
63+
GraphQueryResult constraintsResult = db.execute("CALL db.constraints()");
6064
GraphQueryResult labelsQueryResult = db.execute("CALL db.labels()");
6165
GraphQueryResult relationshipQueryResult = db.execute("CALL db.relationshipTypes()");
6266
GraphQueryResult propertyKeysResult = db.execute("CALL db.propertyKeys()");
6367
GraphQueryResult storedProceduresResult = db.execute("CALL dbms.procedures()");
6468

69+
metadata.addIndexes(indexesResult);
70+
metadata.addConstraints(constraintsResult);
71+
6572
List<String> listOfLabels = extractLabels(labelsQueryResult);
6673
if (!listOfLabels.isEmpty()) {
6774
GraphQueryResult labelCount = db.execute(queryLabelCount(listOfLabels));
@@ -78,8 +85,8 @@ private DataSourceMetadata getNeo4jBoltMetadata(DataSourceApi dataSource) {
7885
metadata.addStoredProcedures(storedProceduresResult);
7986

8087
boolean supportsUserFunctions = metadata.getMetadata(Neo4jBoltCypherDataSourceMetadata.STORED_PROCEDURES)
81-
.stream()
82-
.anyMatch((map) -> map.get("name").equals("dbms.functions"));
88+
.stream()
89+
.anyMatch((map) -> map.get("name").equals("dbms.functions"));
8390

8491
if (supportsUserFunctions) {
8592
GraphQueryResult userFunctionsResult = db.execute("CALL dbms.functions()");
@@ -92,31 +99,31 @@ private DataSourceMetadata getNeo4jBoltMetadata(DataSourceApi dataSource) {
9299
private List<String> extractRelationshipTypes(GraphQueryResult relationshipQueryResult) {
93100
GraphQueryResultColumn column = relationshipQueryResult.getColumns().get(0);
94101
return relationshipQueryResult.getRows()
95-
.stream()
96-
.map(row -> (String) row.getValue(column))
97-
.collect(Collectors.toList());
102+
.stream()
103+
.map(row -> (String) row.getValue(column))
104+
.collect(toList());
98105
}
99106

100107
private List<String> extractLabels(GraphQueryResult labelsQueryResult) {
101108
GraphQueryResultColumn column = labelsQueryResult.getColumns().get(0);
102109
return labelsQueryResult.getRows()
103-
.stream()
104-
.map(row -> (String) row.getValue(column))
105-
.collect(Collectors.toList());
110+
.stream()
111+
.map(row -> (String) row.getValue(column))
112+
.collect(toList());
106113
}
107114

108115
private String queryRelationshipTypeCount(List<String> relationshipTypes) {
109116
return relationshipTypes
110-
.stream()
111-
.map(relationshipType -> "MATCH ()-[r:" + relationshipType + "]->() RETURN count(r)")
112-
.collect(Collectors.joining(" UNION "));
117+
.stream()
118+
.map(relationshipType -> "MATCH ()-[r:" + relationshipType + "]->() RETURN count(r)")
119+
.collect(Collectors.joining(" UNION "));
113120
}
114121

115122
private String queryLabelCount(List<String> labels) {
116123
return labels
117-
.stream()
118-
.map(label -> "MATCH (n:" + label + ") RETURN count(n)")
119-
.collect(Collectors.joining(" UNION "));
124+
.stream()
125+
.map(label -> "MATCH (n:" + label + ") RETURN count(n)")
126+
.collect(Collectors.joining(" UNION "));
120127
}
121128

122129
private void updateNeo4jBoltMetadata(DataSourceApi dataSource, Neo4jBoltCypherDataSourceMetadata metadata) {
@@ -125,23 +132,23 @@ private void updateNeo4jBoltMetadata(DataSourceApi dataSource, Neo4jBoltCypherDa
125132
CypherMetadataContainer container = cypherMetadataProviderService.getContainer(dataSource.getName());
126133

127134
metadata.getLabels()
128-
.stream()
129-
.map(Neo4jLabelMetadata::getName)
130-
.forEach(container::addLabel);
135+
.stream()
136+
.map(Neo4jLabelMetadata::getName)
137+
.forEach(container::addLabel);
131138
metadata.getRelationshipTypes()
132-
.stream()
133-
.map(Neo4jRelationshipTypeMetadata::getName)
134-
.forEach(container::addRelationshipType);
139+
.stream()
140+
.map(Neo4jRelationshipTypeMetadata::getName)
141+
.forEach(container::addRelationshipType);
135142
metadata.getMetadata(Neo4jBoltCypherDataSourceMetadata.PROPERTY_KEYS).stream()
136-
.map((row) -> row.get("propertyKey"))
137-
.forEach(container::addPropertyKey);
143+
.map((row) -> row.get("propertyKey"))
144+
.forEach(container::addPropertyKey);
138145
metadata.getMetadata(Neo4jBoltCypherDataSourceMetadata.STORED_PROCEDURES)
139-
.forEach(row -> container.addProcedure(row.get("name"), row.get("signature"), row.get("description")));
146+
.forEach(row -> container.addProcedure(row.get("name"), row.get("signature"), row.get("description")));
140147

141148
List<Map<String, String>> userFunctionMetadata = metadata.getMetadata(Neo4jBoltCypherDataSourceMetadata.USER_FUNCTIONS);
142149
if (userFunctionMetadata != null) {
143150
userFunctionMetadata
144-
.forEach(row -> container.addUserFunction(row.get("name"), row.get("signature"), row.get("description")));
151+
.forEach(row -> container.addUserFunction(row.get("name"), row.get("signature"), row.get("description")));
145152
}
146153
}
147154

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/component/datasource/metadata/Neo4jBoltCypherDataSourceMetadata.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
public class Neo4jBoltCypherDataSourceMetadata implements DataSourceMetadata {
1313

14+
public static final String INDEXES = "indexes";
15+
public static final String CONSTRAINTS = "constraints";
1416
public static final String PROPERTY_KEYS = "propertyKeys";
1517
public static final String STORED_PROCEDURES = "procedures";
1618
public static final String USER_FUNCTIONS = "functions";
@@ -94,4 +96,12 @@ public void addRelationshipType(Neo4jRelationshipTypeMetadata relationshipTypeMe
9496
public List<Neo4jRelationshipTypeMetadata> getRelationshipTypes() {
9597
return relationshipTypes;
9698
}
99+
100+
public void addIndexes(GraphQueryResult indexesResult) {
101+
addDataSourceMetadata(INDEXES, indexesResult);
102+
}
103+
104+
public void addConstraints(GraphQueryResult constraintsResult) {
105+
addDataSourceMetadata(CONSTRAINTS, constraintsResult);
106+
}
97107
}

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/ui/datasource/metadata/DataSourceMetadataUi.java

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.metadata;
22

33
import com.intellij.ui.treeStructure.PatchedDefaultMutableTreeNode;
4+
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.DataSourceMetadata;
45
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.DataSourcesComponentMetadata;
56
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.metadata.Neo4jBoltCypherDataSourceMetadata;
67
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.state.DataSourceApi;
@@ -10,6 +11,10 @@
1011
import com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.tree.TreeNodeModelApi;
1112
import com.neueda.jetbrains.plugin.graphdb.platform.GraphIcons;
1213

14+
import javax.swing.tree.MutableTreeNode;
15+
import java.util.List;
16+
import java.util.Map;
17+
1318
import static com.neueda.jetbrains.plugin.graphdb.jetbrains.ui.datasource.tree.Neo4jTreeNodeType.*;
1419

1520
public class DataSourceMetadataUi {
@@ -19,6 +24,8 @@ public class DataSourceMetadataUi {
1924
private static final String LABELS_TITLE = "labels (%s)";
2025
private static final String STORED_PROCEDURES_TITLE = "stored procedures";
2126
private static final String USER_FUNCTIONS_TITLE = "user functions";
27+
private static final String INDEXES_TITLE = "indexes (%s)";
28+
private static final String CONSTRAINTS_TITLE = "constraints (%s)";
2229

2330
private final DataSourcesComponentMetadata dataSourcesComponent;
2431

@@ -46,6 +53,9 @@ boolean updateNeo4jBoltCypherMetadataUi(PatchedDefaultMutableTreeNode dataSource
4653
TreeNodeModelApi model = (TreeNodeModelApi) dataSourceRootTreeNode.getUserObject();
4754
DataSourceApi dataSourceApi = model.getDataSourceApi();
4855

56+
dataSourceRootTreeNode.add(createConstraintsNode(dataSourceMetadata, dataSourceApi));
57+
dataSourceRootTreeNode.add(createIndexesNode(dataSourceMetadata, dataSourceApi));
58+
4959
// Labels
5060
int labelCount = dataSourceMetadata.getLabels().size();
5161
PatchedDefaultMutableTreeNode labelsTreeNode = new PatchedDefaultMutableTreeNode(
@@ -108,6 +118,35 @@ boolean updateNeo4jBoltCypherMetadataUi(PatchedDefaultMutableTreeNode dataSource
108118
return true;
109119
}
110120

121+
private MutableTreeNode createIndexesNode(DataSourceMetadata dataSourceMetadata, DataSourceApi dataSourceApi) {
122+
List<Map<String, String>> indexesMetadata =
123+
dataSourceMetadata.getMetadata(Neo4jBoltCypherDataSourceMetadata.INDEXES);
124+
PatchedDefaultMutableTreeNode indexTreeNode = new PatchedDefaultMutableTreeNode(
125+
new MetadataTreeNodeModel(INDEXES,
126+
dataSourceApi,
127+
String.format(INDEXES_TITLE, indexesMetadata.size()),
128+
GraphIcons.Nodes.INDEX));
129+
indexesMetadata
130+
.forEach(row -> indexTreeNode.add(of(new MetadataTreeNodeModel(INDEX, dataSourceApi,
131+
row.get("description").substring(6) + " " + row.get("state")))));
132+
133+
return indexTreeNode;
134+
}
135+
136+
private MutableTreeNode createConstraintsNode(DataSourceMetadata dataSourceMetadata, DataSourceApi dataSourceApi) {
137+
List<Map<String, String>> constraintsMetadata =
138+
dataSourceMetadata.getMetadata(Neo4jBoltCypherDataSourceMetadata.CONSTRAINTS);
139+
PatchedDefaultMutableTreeNode indexTreeNode = new PatchedDefaultMutableTreeNode(
140+
new MetadataTreeNodeModel(CONSTRAINTS, dataSourceApi,
141+
String.format(CONSTRAINTS_TITLE, constraintsMetadata.size()), GraphIcons.Nodes.CONSTRAINT));
142+
constraintsMetadata
143+
.forEach(row ->
144+
indexTreeNode.add(of(new MetadataTreeNodeModel(CONSTRAINT, dataSourceApi,
145+
row.get("description").substring(11)))));
146+
147+
return indexTreeNode;
148+
}
149+
111150
private PatchedDefaultMutableTreeNode of(MetadataTreeNodeModel model) {
112151
return new PatchedDefaultMutableTreeNode(model);
113152
}

ui/jetbrains/src/main/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/ui/datasource/tree/Neo4jTreeNodeType.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,10 @@
33
public enum Neo4jTreeNodeType implements NodeType {
44
ROOT,
55
DATASOURCE,
6+
INDEXES,
7+
INDEX,
8+
CONSTRAINTS,
9+
CONSTRAINT,
610
LABELS,
711
LABEL,
812
RELATIONSHIPS,

ui/jetbrains/src/test/java/com/neueda/jetbrains/plugin/graphdb/jetbrains/ui/datasource/metadata/DataSourceMetadataUiTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,13 @@ public void setUp() {
5050
HashMap<String, String> propertyKeys = new HashMap<>();
5151
propertyKeys.put("propertyKey", PROPERTY);
5252

53+
HashMap<String, String> indexes = new HashMap<>();
54+
indexes.put("description", "index ON (:aaa)");
55+
indexes.put("state", "ONLINE");
56+
57+
HashMap<String, String> constraints = new HashMap<>();
58+
constraints.put("description", "constraint ON (:aaa) UNIQUE");
59+
5360
HashMap<String, String> procedures = new HashMap<>();
5461
procedures.put("signature", "db.labels() :: (label :: STRING?)");
5562
procedures.put("name", "db.labels");
@@ -60,6 +67,8 @@ public void setUp() {
6067
metadata.addDataSourceMetadata(PROPERTY_KEYS, singletonList(propertyKeys));
6168
metadata.addDataSourceMetadata(STORED_PROCEDURES, singletonList(procedures));
6269
metadata.addDataSourceMetadata(USER_FUNCTIONS, singletonList(new HashMap<>()));
70+
metadata.addDataSourceMetadata(INDEXES, singletonList(indexes));
71+
metadata.addDataSourceMetadata(CONSTRAINTS, singletonList(constraints));
6372

6473
ui.updateNeo4jBoltCypherMetadataUi(datasource, metadata);
6574
}

0 commit comments

Comments
 (0)