Integrating with Database View for querying and Code Generation



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);

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,



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:

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.

fun findOrCreateEditor(target: DbElement, preferExisting: Boolean): Editor? {
val project = target.project
val dataSource = target.dataSource

if (preferExisting) {
val activeEditor = EditorTracker.getInstance(target.project).activeEditors.firstOrNull {
it.getVirtualFile(project)?.let { DbVFSUtils.getDataSource(project, it) } == dataSource
if (activeEditor != null) {
val virtualFile = activeEditor.getVirtualFile(project)!!
FileEditorManager.getInstance(project).openFile(virtualFile, true)
return activeEditor

val vFile = DbUIUtil.openInConsole(dataSource, target, "", preferExisting) ?: return null
val psiFile = PsiManager.getInstance(project).findFile(vFile) ?: return null
val document = PsiDocumentManager.getInstance(project).getDocument(psiFile) ?: return null
return EditorFactory.getInstance().getEditors(document, project).firstOrNull()

fun moveCaretToEnd(editor: Editor) {
val document = editor.document
val textLength = document.textLength
if (textLength > 0 && document.charsSequence[textLength - 1] != '\n') {
WriteCommandAction.runWriteCommandAction(editor.project, null, null, Runnable { document.insertString(textLength, "\n") })

private fun Editor.getVirtualFile(project: Project): VirtualFile? {
val document = document
val psiFile = PsiDocumentManager.getInstance(project).getPsiFile(document)
return psiFile?.virtualFile




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.


Please sign in to leave a comment.