Integrating with Database View for querying and Code Generation
Answered
Hi,
I'm working in a plugin that integrates a bit with the com.intellij.database plugin for database detection.
I'm trying to implement a quickfix that generates code, and I would like to paste this code in a console for an existing database connection. If there is no open console, it should open one. Something like this (pseudo code):
var codeToRunInDb = generateCode();
var editorForConnection = getEditorForConnection(connection);
editorForConnection.append(codeToRunInDb);
editorForConnection.show();
Is this something feasible without depending on internals of the database plugin? If it depends on the internals of the plugin, where can I find how to integrate with the plugin?
Thanks a lot,
Please sign in to leave a comment.
Hi!
First, I will tell you how you can achieve what you want, and then I will provide my recommendations on what I think you should do.
How to achieve what you want:
1. Connection
I want to point out that you need to use sessions instead of raw connections.
The session is our wrapper over the connection we are working with.
You can create or get an existing session by using:
com.intellij.database.console.session.DatabaseSessionManager#getSession(project: Project, connectionPoint: DatabaseConnectionPoint)
2. EditorForConnection
I want to note that one session can have multiple clients (including consoles).
You can get all session clients that have a VirtualFile using:
com.intellij.database.console.session.DatabaseSession#getClientsWithFile
Next, find the first client that is an instance of com.intellij.database.console.JdbcConsole, which represents the console.
In case you don't find the console, you can create a new one.
Unfortunately, it is only possible to create a console with a new session.
You can create a new console with a new session and then attach this console to the session you are currently using.
However, I believe this is an overly complicated approach.
My recommendations:
Perhaps it would be better to follow the same approach as in the "Select all rows from a table" action and similar actions.
Instead of trying to use a specific connection and searching for an editor for it, start by finding the console and then use its connection.
I will provide you with functions that will help you implement such a solution.
Hi Anna,
thanks a lot for the detailed response and all the code examples.
I decided to go ahead with the solution you proposed. The plugin node provides a Quick Fix that generates the code and opens the console using your suggested approach.
Thanks a lot for your time, really appreciated.