-
Notifications
You must be signed in to change notification settings - Fork 24
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
SqlLine integration #17
Open
snuyanzin
wants to merge
1
commit into
vlsi:main
Choose a base branch
from
snuyanzin:SQLLINE_INTEGRATION
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+209
−2
Open
Changes from all commits
Commits
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
This file contains 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 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
19 changes: 19 additions & 0 deletions
19
MatCalcitePlugin/src/com/github/vlsi/mat/calcite/cli/DumpFileNameCompleter.java
This file contains 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,19 @@ | ||
package com.github.vlsi.mat.calcite.cli; | ||
|
||
import org.jline.builtins.Completers; | ||
|
||
import java.nio.file.FileSystems; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.nio.file.PathMatcher; | ||
|
||
public class DumpFileNameCompleter extends Completers.FileNameCompleter { | ||
private static final PathMatcher DUMP_FILE_MATCHER = | ||
FileSystems.getDefault().getPathMatcher("glob:**.{bin,hprof}"); | ||
|
||
@Override | ||
protected boolean accept(Path path) { | ||
return super.accept(path) | ||
&& (Files.isDirectory(path) || DUMP_FILE_MATCHER.matches(path)); | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
MatCalcitePlugin/src/com/github/vlsi/mat/calcite/cli/MCPApplication.java
This file contains 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,48 @@ | ||
package com.github.vlsi.mat.calcite.cli; | ||
|
||
import sqlline.Application; | ||
import sqlline.CommandHandler; | ||
import sqlline.MCPSqlLine; | ||
import sqlline.SqlLine; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public class MCPApplication extends Application { | ||
private static final Set<String> ALLOWED_SQLLINE_COMMANDS = new HashSet<String>() {{ | ||
add("quit"); | ||
add("history"); | ||
add("verbose"); | ||
add("run"); | ||
add("list"); | ||
add("all"); | ||
add("go"); | ||
add("script"); | ||
add("record"); | ||
add("brief"); | ||
add("close"); | ||
add("closeall"); | ||
add("outputformat"); | ||
add("set"); | ||
add("help"); | ||
add("reset"); | ||
add("save"); | ||
add("rerun"); | ||
add("prompthandler"); | ||
}}; | ||
|
||
@Override | ||
public Collection<CommandHandler> getCommandHandlers(SqlLine sqlLine) { | ||
Collection<CommandHandler> handlers = new ArrayList<>(); | ||
for (CommandHandler commandHandler : super.getCommandHandlers(sqlLine)) { | ||
if (ALLOWED_SQLLINE_COMMANDS.contains(commandHandler.getName())) { | ||
handlers.add(commandHandler); | ||
} | ||
} | ||
handlers.add(new OpenDumpCommandHandler((MCPSqlLine) sqlLine, new DumpFileNameCompleter(), "Open heap dump", | ||
"open_dump")); | ||
return handlers; | ||
} | ||
} |
68 changes: 68 additions & 0 deletions
68
MatCalcitePlugin/src/com/github/vlsi/mat/calcite/cli/OpenDumpCommandHandler.java
This file contains 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,68 @@ | ||
package com.github.vlsi.mat.calcite.cli; | ||
|
||
import org.eclipse.mat.SnapshotException; | ||
import org.eclipse.mat.snapshot.ISnapshot; | ||
import org.eclipse.mat.snapshot.SnapshotFactory; | ||
import org.eclipse.mat.util.VoidProgressListener; | ||
import org.jline.reader.Completer; | ||
import sqlline.AbstractCommandHandler; | ||
import sqlline.Commands; | ||
import sqlline.DispatchCallback; | ||
import sqlline.MCPDatabaseConnection; | ||
import sqlline.MCPSqlLine; | ||
|
||
import java.io.File; | ||
import java.util.Collections; | ||
import java.util.List; | ||
|
||
public class OpenDumpCommandHandler extends AbstractCommandHandler { | ||
public OpenDumpCommandHandler(MCPSqlLine sqlLine, List<Completer> completers, | ||
String helpText, String... cmds) { | ||
super(sqlLine, cmds, helpText, completers); | ||
} | ||
|
||
public OpenDumpCommandHandler(MCPSqlLine sqlLine, Completer completer, | ||
String helpText, String... cmds) { | ||
this(sqlLine, Collections.singletonList(completer), helpText, cmds); | ||
} | ||
|
||
public void execute(String line, DispatchCallback callback) { | ||
final String[] parts = sqlLine.split(line, " ", 0); | ||
if (parts.length != 2) { | ||
sqlLine.error("Usage: open_dump <path to file>"); | ||
callback.setToFailure(); | ||
return; | ||
} | ||
// replace ~/ with user directory | ||
final String filename = Commands.expand(parts[1]); | ||
if (!new File(filename).isFile()) { | ||
sqlLine.error("Dump file " + filename + " does not exists!"); | ||
callback.setToFailure(); | ||
return; | ||
} | ||
|
||
final MCPSqlLine mcpSqlLine = (MCPSqlLine) sqlLine; | ||
final MCPDatabaseConnection mcpDatabaseConnection; | ||
try { | ||
mcpDatabaseConnection = new MCPDatabaseConnection(mcpSqlLine, filename, openSnapshot(new File(filename))); | ||
} catch (SnapshotException e) { | ||
callback.setToFailure(); | ||
mcpSqlLine.error(e); | ||
return; | ||
} | ||
try { | ||
mcpSqlLine.setUpConnection(mcpDatabaseConnection); | ||
callback.setToSuccess(); | ||
} catch (Exception e) { | ||
mcpDatabaseConnection.close(); | ||
mcpSqlLine.removeConnection(mcpDatabaseConnection); | ||
callback.setToFailure(); | ||
mcpSqlLine.error(e); | ||
} | ||
} | ||
|
||
private static ISnapshot openSnapshot(File heapDump) throws SnapshotException { | ||
System.out.println("exists = " + heapDump.exists() + ", file = " + heapDump.getAbsolutePath()); | ||
return SnapshotFactory.openSnapshot(heapDump, new VoidProgressListener()); | ||
} | ||
} |
This file contains 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,22 @@ | ||
package sqlline; | ||
|
||
import com.github.vlsi.mat.calcite.CalciteDataSource; | ||
|
||
import org.eclipse.mat.snapshot.ISnapshot; | ||
|
||
import java.sql.SQLException; | ||
|
||
public class MCPDatabaseConnection extends DatabaseConnection { | ||
private final ISnapshot snapshot; | ||
|
||
public MCPDatabaseConnection(MCPSqlLine sqlLine, String filename, ISnapshot snapshot) { | ||
super(sqlLine, null, filename, "username", "password", null); | ||
this.snapshot = snapshot; | ||
} | ||
|
||
@Override | ||
boolean connect() throws SQLException { | ||
connection = CalciteDataSource.getConnection(snapshot); | ||
return true; | ||
} | ||
} |
This file contains 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,27 @@ | ||
package sqlline; | ||
|
||
import com.github.vlsi.mat.calcite.cli.MCPApplication; | ||
|
||
import java.lang.reflect.Proxy; | ||
import java.sql.Connection; | ||
import java.sql.DatabaseMetaData; | ||
import java.sql.SQLException; | ||
|
||
public class MCPSqlLine extends SqlLine { | ||
void setAppConfig(Application application) { | ||
super.setAppConfig(new MCPApplication()); | ||
} | ||
|
||
public void setUpConnection(final MCPDatabaseConnection mcpDatabaseConnection) throws SQLException { | ||
getDatabaseConnections().setConnection(mcpDatabaseConnection); | ||
Connection connection = getDatabaseConnection().getConnection(); | ||
mcpDatabaseConnection.meta = (DatabaseMetaData) Proxy.newProxyInstance( | ||
DatabaseMetaData.class.getClassLoader(), | ||
new Class[]{DatabaseMetaData.class}, | ||
new DatabaseMetaDataHandler(connection.getMetaData())); | ||
} | ||
|
||
public void removeConnection(final MCPDatabaseConnection mcpDatabaseConnection) { | ||
getDatabaseConnections().removeConnection(mcpDatabaseConnection); | ||
} | ||
} |
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.
@snuyanzin , I might be late here :) Do you remember by chance why do you limit the set of commands?
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.
As far as I remember there are database specific commands e.g.
!isolation
,!drop
and etc.The idea was to limit to set of commands which makes sense for mat-calcite-plugin.
Agree that the list of commands itself is the subject for discussion.