Query SQL in an IntelliJ Plugin

I’m trying to have my IDEA plugin run a simple SQL select query from a defined datasource (i.e. one configured in the project).

I’ve followed some of the instructions on other posts, and they seem to recommend this approach:

  1. Create session by using com.intellij.database.console.session.DatabaseSessionManager#getSession(project: Project, connectionPoint: DatabaseConnectionPoint).
  2. Create request com.intellij.database.datagrid.DataRequest.RawRequest and override processRaw method. 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).

Following this, I have the following:

    com.intellij.database.dataSource.LocalDataSource dataSource = LocalDataSourceManager
        .getInstance(project)
        .getDataSources().get(0); // just as example

    DatabaseSession session = com.intellij.database.console.session.DatabaseSessionManager.getSession(project, dataSource);
    com.intellij.database.datagrid.DataRequest.RawRequest req = new DataRequest.RawRequest(session)
    {
      @Override
      public void processRaw(Context context, DatabaseConnectionCore databaseConnectionCore) throws Exception
      {
        RemoteConnection conn = databaseConnectionCore.getRemoteConnection();
        RemotePreparedStatement stmt = conn.prepareStatement("select 1 from dual");
        stmt.execute();
        RemoteResultSet resultSet = stmt.getResultSet();
        while (resultSet.next())
        {
          String out = resultSet.getString(1);
          System.out.println(out);
        }
      }
    };
    session.getMessageBus().getDataProducer().processRequest(req);

Yet the processRaw method is never invoked; debugging shows it stepping over the call to processRequest, but it seems to be ignored/nixed.

Could you please point me in the right direction?

My actual scenario that I’m working towards is to run a prebuilt select query and retrieve all the records in it, then do some processing based on the results.

1 Like

Sigh… the problem was that my sandbox (debugging) IDE that I was running the plugin in didn’t have the drivers installed. An exception from a different thread was being thrown.

Now that is fixed, it appears to enter the processRaw method.

Is there a way to trap exceptions with the executing of the query and handle them, so that I can display this to the user?