Accessing database through API
Answered
Hi,
I'm developing a plugin that requires interaction with DB's defined in Pycharm.
I need to be able to see the list of defined DB's and to run a query on them.
I managed to import the database navigator plugin into my project but there's no documentation on how to use it.
I want to have control on DB browser like I have as a user of Pycharm
Please sign in to leave a comment.
Sorry, but the Database Navigator plugin isn't open source – thus, we can't provide documentation nor its code.
Thanks for your answer Jakub.
Is it something that changed recently?
I see the answer in this ticket
Why? The Database plugin is available for use, but it's not OS – nothing has changed in that matter.
Then how do I use it?
I need to ask the database plugin to run a query (it's pycharm professional).
I can see all the classes (DatabaseConnectionManager, EvaluationRequestorFactory, etc).
I Just don't know how to use them. It would be extremely beneficial for us
There's no API to perform a query in the Database plugin, so if you figure out how to reuse some parts of the Database plugin, things may eventually be broken in the future.
Apart from that, you may try to use com.intellij.database.datagrid.DataRequest.RawRequest and com.intellij.database.dataSource.DatabaseConnectionCore#getRemoteConnection on the second argument – that would be almost jdbc Connection.
You need to
1. Create session by using
com.intellij.database.console.session.DatabaseSessionManager#getSession(project: Project, connectionPoint: DatabaseConnectionPoint).2. Create request
com.intellij.database.datagrid.DataRequest.RawRequestand overrideprocessRawmethod. Here you can get the connection from the second argument -com.intellij.database.dataSource.DatabaseConnectionCore#getRemoteConnection.3. Send the request for execution
session.getMessageBus().getDataProducer().processRequest(request).I think it will be helpful for you to look at a simple example in
com.intellij.database.session.SessionsTest#ping.Thank you for the detailed reply!
I spent all day trying but came out empty handed. The DatabaseSession.isConnected() is always false, and there is no output on screen to see a query is running or anything. It might be too complicated and may be broken in the future as you say.
It might indeed be a better idea to have a workaround, I will open a separate question on it
Opened:
https://intellij-support.jetbrains.com/hc/en-us/community/posts/4408928764306-Change-database-and-schema-with-code
Ok I got things up and running but it might worth asking if connecting to AWS and not a regular Postgres might be an issue here?
Can I get just a reference to the Ping example? I can't find it anywhere
Hi Jakub Chrzanowski,
I also cannot find the example which you mentioned
Could you please give a link or advise where it could be?