How to access data sources which are in the /.idea/dataSources.xml in my custom plugin?

Answered

Hello,

I would like to access and modify data sources which are defined manually in the Database Tool Window. My plugin is generated from JetBrains/intellij-platform-plugin-template. That means the development language is Kotlin. The `gradle.properties` is updated with;

pluginUntilBuild = 211.*
platformType = IU
platformPlugins = com.intellij.database

Since I couldn't find the correct documentation, I tried several things by my own to access data sources like;

DbPsiFacade.getInstance(project).dataSources
// Method threw 'java.lang.ClassNotFoundException' exception.
DataSourceManager.getManagers(project)
// Method threw 'java.lang.ClassNotFoundException' exception.

Before going to the ugly usual XML updating way, I would like to get your opinions.
That would be really nice if you could redirect me to the up to date correct documentation.

Thanks in advance.
Greetings!

0
4 comments

ClassNotFoundException is most probably caused by not adding `<depends>` declaration in plugin.xml, please see guide in https://plugins.jetbrains.com/docs/intellij/data-grip.html

 

Please see https://youtrack.jetbrains.com/issue/IJSDK-104 (last comment) as starting point

 

0

Many thanks for your answer Yann! I updated the plugin.xml, and now it works. 

I update the data source URL like below, it works but `dataSource.delegate as LocalDataSource` says `'getDelegate()' "is marked unstable with @ApiStatus.Internal"`. Is there any other way to update the URL?

DbPsiFacade.getInstance(project).dataSources.forEach { dataSource ->
val localDataSource = dataSource.delegate as LocalDataSource
if (..some condition..) {
localDataSource.url = "jdbc:postgresql://localhost/myNewJdbcUrl"
}
}

Thanks again 🙏🏼

0

from javadoc

/**
* The upcoming breaking change: return type will be changed to DasDataSource.
* for connection config use {@link DbDataSource#getConnectionConfig()}
* for LocalDataSource use {@link com.intellij.database.util.DbImplUtil#getMaybeLocalDataSource(DasDataSource)}
*/
0

That helped, thanks a lot 🙏🏼

0

Please sign in to leave a comment.