Submit query to Datagrip from plugin

I'm trying to create a plugin to help me explore data. I have a number of queries that I run one after the other when debugging and extracting information. It unfortunately gets messy to keep queries organized especially when individual queries are small and I can write them 

I'm trying to plug into the data grid and add some shortcuts to help me jump from data to data. 

In the screenshot, I have a certain data id. I am able to extract the selected value and print it as follows 

class FindUsersAction : AnAction() {
    override fun actionPerformed(e: AnActionEvent) {
        val grid = e.getData(DatabaseDataKeys.DATA_GRID_KEY) ?: return
        val selection = grid.selectionModel
        if (selection.selectedRowCount != 1) return

        val row = selection.selectedRows.first()
        val col = selection.selectedColumns.first()
        val accountId = grid.getDataModel(DataAccessType.DATA_WITH_MUTATIONS)
            .getValueAt(row, col)

        println("Action: AccountId -> $accountId finding Users")
    }
}

Now, based on the account Id, I would like to submit a query for datagrid. I would like it to be executed on the same data session and added as a result tab.

I'm lost on how to achieve this in a plugin? Is there a way to programmatically submit a query to the database plugin?

0
fun executeQuery(e: AnActionEvent, query: String) {
    val project = e.project ?: return
    val virtualFile = e.getData(CommonDataKeys.VIRTUAL_FILE) ?: return

    var editor: Editor;
    if (e.getData(CommonDataKeys.EDITOR) != null) {
        editor = e.getData(CommonDataKeys.EDITOR) ?: return
    } else {
        val fileEditor = e.getData(PlatformDataKeys.LAST_ACTIVE_FILE_EDITOR) ?: return;

        editor = EditorUtil.getEditorEx(fileEditor) ?: return;
    }

    val console: JdbcConsole = JdbcConsoleProvider.getConsole(project, virtualFile) ?: return

    val queryPsiFile = PsiFileFactory.getInstance(project)
        .createFileFromText("adhoc_query.sql", SqlLanguage.INSTANCE, query)

    val execOption = DatabaseSettings.ExecOption()
    execOption.newTab = true;

    val element = queryPsiFile.findElementAt(0) ?: return

    val scriptModel = JdbcConsoleProvider.findScriptModel(queryPsiFile, element, editor, execOption)

    val model = scriptModel?.model ?: return
    console.executeQueries(
        editor, model, execOption

I was able to do this and run the query, I'm not sure if this is the best way, but it worked. 

 

0

[2025-12-27 15:59:00] paga> SELECT FinancialTransactionItemId, FinancialAccountId, CreditAmount, DebitAmount, Description, *
                           FROM [FinancialTransactionItem]
                           WHERE FinancialTransactionId = 6519
                           ORDER BY TransactionLineNumber
[2025-12-27 15:59:00] 0 rows retrieved in 392 ms (execution: 31 ms, fetching: 361 ms)
[2025-12-27 15:59:00] paga> SELECT FinancialTransactionItemId, FinancialAccountId, CreditAmount, DebitAmount, Description, *
                           FROM
[2025-12-27 15:59:00] [S0001][102] Line 2: Incorrect syntax near 'FROM'.


The above code some how results in two queries being executed. It seems to be related to `[`.

0

请先登录再写评论。