-
Notifications
You must be signed in to change notification settings - Fork 56
Added inspection to show warnings from explain queries #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...n/java/com/neueda/jetbrains/plugin/graphdb/database/api/query/GraphQueryNotification.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package com.neueda.jetbrains.plugin.graphdb.database.api.query; | ||
|
||
public interface GraphQueryNotification { | ||
|
||
String getTitle(); | ||
|
||
String getDescription(); | ||
|
||
Integer getPositionOffset(); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
.../neueda/jetbrains/plugin/graphdb/database/neo4j/bolt/data/Neo4jBoltQueryNotification.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package com.neueda.jetbrains.plugin.graphdb.database.neo4j.bolt.data; | ||
|
||
import com.neueda.jetbrains.plugin.graphdb.database.api.query.GraphQueryNotification; | ||
|
||
public class Neo4jBoltQueryNotification implements GraphQueryNotification { | ||
|
||
private String title; | ||
private String description; | ||
private Integer positionOffset; | ||
|
||
public Neo4jBoltQueryNotification(String title, String description, Integer positionOffset) { | ||
this.title = title; | ||
this.description = description; | ||
this.positionOffset = positionOffset; | ||
} | ||
|
||
@Override | ||
public String getTitle() { | ||
return title; | ||
} | ||
|
||
@Override | ||
public String getDescription() { | ||
return description; | ||
} | ||
|
||
@Override | ||
public Integer getPositionOffset() { | ||
return positionOffset; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 6 additions & 0 deletions
6
...tabase-support-plugin/src/main/resources/inspectionDescriptions/CypherExplainWarning.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
<html> | ||
<body> | ||
<!-- tooltip end --> | ||
Executes EXPLAIN query against active data source. | ||
</body> | ||
</html> |
55 changes: 55 additions & 0 deletions
55
...db/test/integration/neo4j/tests/cypher/inspection/CypherExplainWarningInspectionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package com.neueda.jetbrains.plugin.graphdb.test.integration.neo4j.tests.cypher.inspection; | ||
|
||
import com.intellij.codeInspection.LocalInspectionTool; | ||
import com.neueda.jetbrains.plugin.graphdb.jetbrains.inspection.CypherExplainWarningInspection; | ||
import com.neueda.jetbrains.plugin.graphdb.platform.GraphConstants; | ||
import com.neueda.jetbrains.plugin.graphdb.test.integration.neo4j.tests.cypher.util.BaseInspectionTest; | ||
|
||
import java.util.Set; | ||
|
||
import static java.util.Collections.singleton; | ||
import static java.util.Collections.singletonList; | ||
|
||
public class CypherExplainWarningInspectionTest extends BaseInspectionTest { | ||
|
||
@Override | ||
protected Set<Class<? extends LocalInspectionTool>> provideInspectionClasses() { | ||
return singleton(CypherExplainWarningInspection.class); | ||
} | ||
|
||
public void testNonDataSourceFile_NoHighlight() { | ||
addFileAndCheck("a.cyp", "MATCH (a)-->(b) RETURN *"); | ||
} | ||
|
||
public void testDataSourceFile_NoHighlight() { | ||
addDataSourceFileAndCheck("MATCH (a)-->(b) RETURN *"); | ||
} | ||
|
||
public void testDataSourceFile_HighlightExplainWarning() { | ||
addDataSourceFileAndCheck("MATCH (a)-[r:" + | ||
"<warning descr=\"The provided relationship type is not in the database.\">" + | ||
"ART</warning>]-(b) RETURN *;"); | ||
} | ||
|
||
public void testDataSourceFile_NoHighlightQueryError() { | ||
addDataSourceFileAndCheck("MATCH (a)-->() RETURN b;"); | ||
} | ||
|
||
public void testDataSourceFile_NoHighlightParserError() { | ||
addDataSourceFileAndCheck("MATCH a<error>-</error>->() RETURN *;"); | ||
} | ||
|
||
public void testDataSourceFile_NoDataSource() { | ||
component().dataSources().getDataSourceContainer().removeDataSources(singletonList(dataSource().neo4j31())); | ||
addFileAndCheck(GraphConstants.BOUND_DATA_SOURCE_PREFIX + "imaginary-ds-uuid-with-36-symbols-in.cypher", | ||
"MATCH (a:Turbo)-->() RETURN *;"); | ||
} | ||
|
||
public void testDataSourceFile_UserCreatedDSLikeFile() { | ||
component().dataSources().getDataSourceContainer().removeDataSources(singletonList(dataSource().neo4j31())); | ||
// uuid should be 36 symbols long, let's assume user created a file with a name, starting like ds file | ||
// but does not match the expected format | ||
addFileAndCheck(GraphConstants.BOUND_DATA_SOURCE_PREFIX + "ds-uuid-with-23-symbols.cypher", | ||
"MATCH (a:Turbo)-->() RETURN *;"); | ||
} | ||
} |
42 changes: 42 additions & 0 deletions
42
...jetbrains/plugin/graphdb/test/integration/neo4j/tests/cypher/util/BaseInspectionTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package com.neueda.jetbrains.plugin.graphdb.test.integration.neo4j.tests.cypher.util; | ||
|
||
import com.intellij.codeInspection.LocalInspectionTool; | ||
import com.intellij.openapi.vfs.VirtualFile; | ||
import com.intellij.psi.PsiFile; | ||
import com.neueda.jetbrains.plugin.graphdb.jetbrains.util.NameUtil; | ||
import com.neueda.jetbrains.plugin.graphdb.test.integration.neo4j.util.base.BaseIntegrationTest; | ||
|
||
import java.util.Optional; | ||
import java.util.Set; | ||
|
||
public abstract class BaseInspectionTest extends BaseIntegrationTest { | ||
private String dsApiUUID; | ||
|
||
@Override | ||
public void setUp() throws Exception { | ||
super.setUp(); | ||
this.dsApiUUID = dataSource().neo4j31().getUUID(); | ||
myFixture.enableInspections(provideInspectionClasses()); | ||
} | ||
|
||
protected abstract Set<Class<? extends LocalInspectionTool>> provideInspectionClasses(); | ||
|
||
protected void addFileAndCheck(String filePath, String fileContent) { | ||
PsiFile psiFile = myFixture.addFileToProject(filePath, fileContent); | ||
configureAndCheck(psiFile.getVirtualFile()); | ||
} | ||
|
||
protected void addDataSourceFileAndCheck(String fileContent) { | ||
String fileName = Optional.of(dsApiUUID) | ||
.flatMap(uuid -> component().dataSources().getDataSourceContainer().findDataSource(uuid)) | ||
.map(NameUtil::createDataSourceFileName) | ||
.orElseThrow(IllegalStateException::new); | ||
|
||
addFileAndCheck(fileName, fileContent); | ||
} | ||
|
||
private void configureAndCheck(VirtualFile virtualFile) { | ||
myFixture.configureFromExistingVirtualFile(virtualFile); | ||
myFixture.checkHighlighting(); | ||
} | ||
} |
93 changes: 93 additions & 0 deletions
93
.../neueda/jetbrains/plugin/graphdb/jetbrains/inspection/CypherExplainWarningInspection.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package com.neueda.jetbrains.plugin.graphdb.jetbrains.inspection; | ||
|
||
import com.intellij.codeInspection.LocalInspectionTool; | ||
import com.intellij.codeInspection.LocalInspectionToolSession; | ||
import com.intellij.codeInspection.ProblemsHolder; | ||
import com.intellij.openapi.components.ServiceManager; | ||
import com.intellij.psi.PsiElement; | ||
import com.intellij.psi.PsiElementVisitor; | ||
import com.neueda.jetbrains.plugin.graphdb.database.api.GraphDatabaseApi; | ||
import com.neueda.jetbrains.plugin.graphdb.database.api.query.GraphQueryResult; | ||
import com.neueda.jetbrains.plugin.graphdb.jetbrains.component.datasource.DataSourcesComponent; | ||
import com.neueda.jetbrains.plugin.graphdb.jetbrains.database.DatabaseManagerService; | ||
import com.neueda.jetbrains.plugin.graphdb.jetbrains.util.NameUtil; | ||
import com.neueda.jetbrains.plugin.graphdb.language.cypher.psi.CypherTypes; | ||
import com.neueda.jetbrains.plugin.graphdb.platform.GraphConstants; | ||
import org.jetbrains.annotations.NotNull; | ||
import org.neo4j.driver.v1.exceptions.ClientException; | ||
|
||
import java.util.Objects; | ||
import java.util.Optional; | ||
|
||
public class CypherExplainWarningInspection extends LocalInspectionTool { | ||
|
||
private DatabaseManagerService service; | ||
|
||
public CypherExplainWarningInspection() { | ||
this.service = ServiceManager.getService(DatabaseManagerService.class); | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly, | ||
@NotNull LocalInspectionToolSession session) { | ||
return new PsiElementVisitor() { | ||
@Override | ||
public void visitElement(PsiElement element) { | ||
checkStatement(element, holder); | ||
} | ||
}; | ||
} | ||
|
||
@NotNull | ||
@Override | ||
public PsiElementVisitor buildVisitor(@NotNull ProblemsHolder holder, boolean isOnTheFly) { | ||
return new PsiElementVisitor() { | ||
@Override | ||
public void visitElement(PsiElement element) { | ||
checkStatement(element, holder); | ||
} | ||
}; | ||
} | ||
|
||
private void checkStatement(@NotNull PsiElement statement, @NotNull ProblemsHolder problemsHolder) { | ||
if (statement.getNode().getElementType() == CypherTypes.SINGLE_QUERY) { | ||
Optional.of(statement.getContainingFile().getName()) | ||
.filter(s -> s.startsWith(GraphConstants.BOUND_DATA_SOURCE_PREFIX)) | ||
.map(this::safeExtractDataSourceUUID) | ||
.flatMap(uuid -> statement.getProject() | ||
.getComponent(DataSourcesComponent.class) | ||
.getDataSourceContainer() | ||
.findDataSource(uuid)) | ||
.map(service::getDatabaseFor) | ||
.map(api -> this.executeExplainQuery(api, statement.getText())) | ||
.filter(Objects::nonNull) | ||
.map(GraphQueryResult::getNotifications) | ||
.filter(list -> !list.isEmpty()) | ||
.ifPresent(notifications -> notifications.forEach(notification -> { | ||
PsiElement elementAt = Optional.ofNullable(notification.getPositionOffset()) | ||
.filter(position -> position > 0) | ||
.map(statement::findElementAt) | ||
.orElse(statement); | ||
|
||
problemsHolder.registerProblem(elementAt, notification.getTitle()); | ||
})); | ||
} | ||
} | ||
|
||
private GraphQueryResult executeExplainQuery(GraphDatabaseApi api, String query) { | ||
try { | ||
return api.execute("EXPLAIN " + query); | ||
} catch (ClientException ex) { | ||
return null; | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. remove this empty line |
||
private String safeExtractDataSourceUUID(String fileName) { | ||
try { | ||
return NameUtil.extractDataSourceUUID(fileName); | ||
} catch (IndexOutOfBoundsException e) { | ||
return null; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be test that check behaviour if we are in data source bound file, but there are no actual data source,