How to execute sql query by passing a string and outputting results to the console output?
Answered
I've created an action event, and I have a string value that I'd like to execute using the existing connection and output the results to the console output window, in the same way that ctrl-enter would execute some highlighted string and output the results to the window. I can't simply overwrite the existing keymap because I have manipulated the highlighted string to create the query. For example, if I highlight "tableName", this action would want to run "SELECT * FROM tableName LIMIT 10".
I'm not sure if this is close or even running the query but not outputting the results to console, but here is what I have so far:
object EmptyDatabaseDepartment : DatabaseDepartment {
override val commonName = ""
override val departmentName = ""
override val icon = null
}
class ExecuteQueryAction : Alt1() {
override fun actionPerformed(e: AnActionEvent) {
val project = DataManager.getInstance().dataContextFromFocusAsync.blockingGet(2000)?.getData(CommonDataKeys.PROJECT)
val connection = DatabaseSessionManager.getFacade(
project as Project,
(getConsole(e)?.dataSource as LocalDataSource),
null,
null,
false,
null,
EmptyDatabaseDepartment
).connect().get()
SmartStatements.poweredBy(connection).simple().execute("SELECT * FROM tableName")
}
}
Please sign in to leave a comment.
Hi Jrmalin!
You do not need to create a new session with connection using the DatabaseSessionManager - the console already has a session with which you need to execute your request.
And console also already has its own DatabaseDepartment.
You need to create a DataRequest.RawRequest, implement how it will be processed and send it for processing.
The query will be executed in the console connection and the result will be printed to the console output window.
Also data context you can get easier.
This way:
Instead of:
And you can get the project from the action like this:
Anna, thanks so much for the help! This was super useful, and I was able to get the console to successfully execute the intended query.
However, it is only running the query and logging it to the Output tab in GridImpl->GridCellTabs. Normally, when I execute a query through the normal execution method, a tab is created with a JbLayeredPane->TableScrollPane.
Is there a straightforward way to do that here? I'm happy to step through an example that exists elsewhere if possible. This is the last major step to getting my plugin working, so I'm eager to figure out a solution to this!
Hi Justin!
I'm very sorry I didn't notice your question earlier.
Each session and client has its own Output window (com.intellij.database.console.session.DatabaseLogView)
When a query is executed by a session, it is logged to the session's Output window (com.intellij.database.console.session.DatabaseSessionLogView),
query running example (where session is com.intellij.database.console.session.MessageBusSession):
When a query is executed by a session client (for example, a console or a data editor), it is logged in the client’s Output window (com.intellij.database.console.session.DatabaseClientLogView) + in the Output window of the session with which this client is associated,
query running example (where console is com.intellij.database.console.JdbcConsole):
If the result is not logged in the window you would like - you probably need to run the query using a different session or client. Find the session or client associated with the Output window in which you would like to see the result.
Please let me know if I misunderstood your problem
Hey, Anna! I know it's a long delay here, but I'm still having some of the same issues.
I tried pulling the `session` by calling `console.session`, but I still had the same issue. One thing to note - when I run a query through the normal "Execute" button, it shows an additional "row retrieved" line printed that is not displayed when I run the query via the plugin. For example, the bolded part is the part not displayed when the plugin is running the query:
I wonder if the lack of row retrieval is a clue as to why I'm not seeing the output table. From your screenshot, the piece wrapped in the red box is the part that my plugin query is missing.
How did the getConsole method come from?
@anonymous I believe we get that for free by inheriting from `RunQueryAction.Alt1()`. It looks like this:
@Justin Malin Thanks, How to get the execution result of "SmartStatementFactoryService.getInstance().poweredBy(connection).simple().execute(query)"?
Justin Malin I have the same issue, but now I did, hope to help you.
@zion Hi,I found the way to get the execution result of
RawRequest
, Here is the code ,It works fine on my plugin.Thanks for the tip on QueryRequest! That works well.
Has anyone worked out how to get it to not open a new tab per execution, everytime you run it it always opens a new data tab, so it'll be like Result 1, Result 2, etc.
For the console, this is a setting called "Open results in new tab" under "Query execution", which has the same behaviour if checked.
Josh Taylor ,Hi
Here is the code that not open a new tab for per execution
Thanks for the quick reply, I'll give it a go. This will be for an opensource plugin, so I'll share my final implementation (I want to execute a compiled SQL file and show the query results).
I see that `newTab` is being set in `ScriptingClientDataConsumer`:
```
@NotNull
private ScriptingClientDataConsumer.ContentInfo reuseOrCreateContent(@NotNull DataRequest.Context context, int subQueryIndex, int resultSetIndex) {
ConsoleDataRequest.ResultSetSubQuery resultSetSubQuery = getConsoleResultSetSubQuery(context, subQueryIndex);
ScriptingClientDataConsumer.ResultsPlace place = this.getResultsPlace(resultSetSubQuery);
boolean newTab = !(context.request instanceof ConsoleDataRequest) || ((ConsoleDataRequest)context.request).newTab;
```
Would be nice if it took into account the setting as well..
Got it to work, I think I was in the wrong EDT thread or something funky going on, anyway once I fixed that, it now works as expected.
You need to use `ConsoleDataRequest`, and if you modify the snippets above, you should be able to see what you need to send the request, I also don't need to check the output of the results (yet?).
Josh Taylor It's great, but
How to get Editor?