How to get the sql query result synchronously with Console through database API

Answered

I'm using  com.intellij.database.util.DbImplUtil#executeAndGetResult()   api .

How can I get the DatabaseConnectionCore arg
 

public static <T> T consoleExecuteSql(JdbcConsole console, String sql, Function<RemoteResultSet, T> resultSetTFunction) {
    try {
        return DbImplUtil.executeAndGetResult(null , sql, remoteResultSet -> {
            try {
                return resultSetTFunction.apply(remoteResultSet);
            } catch (Exception e) {
                throw new Exception(e);
            }
        });
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

I tried this way to get result with JdbcConsole, but it can't return  the result synchronously
 

public static <T> List<T> consoleExecuteSql(JdbcConsole console, String sql, Function<RemoteResultSet, T> resultSetTFunction) {
    List<T> result = new ArrayList<>();
    Function1<ResultsProducer, List<T>> function = resultsProducer -> {
        // Fix that "Please, call advance before processCurrent() and make sure that results exits"
        resultsProducer.advanceSafe();
        resultsProducer.processCurrent(new ResultsProducer.Processor<List<T>>() {
            @Override
            public List<T> parameterResults(@NotNull List<? extends OutParameter<?>> list, @NotNull Function1<? super OutParameter<?>, ?> function1, int i) throws Exception {
                return null;
            }
            @Override
            public List<T> results(@NotNull RemoteResultSet remoteResultSet, int i) throws Exception {
                while (remoteResultSet.next()) {
                    result.add(resultSetTFunction.apply(remoteResultSet));
                }
                return result;
            }
            @Override
            public List<T> updateCount(int i, int i1) throws Exception {
                return null;
            }
        });
        return result;
    };
    // Build the request
    DataRequest.RawRequest rawRequest = new DataRequest.RawRequest(console) {
        @Override
        public void processRaw(Context context, DatabaseConnectionCore databaseConnectionCore) throws Exception {
            SmartStatementFactoryService.getInstance().poweredBy(databaseConnectionCore).simple().execute(sql, function);
        }
    };
    // ProcessRequest using console
    console.getMessageBus().getDataProducer().processRequest(rawRequest);
    return result;
}

Thanks
 

0
1 comment

Finally I found the way to get the result  synchronously .

Here is the code, hoping help someone need it.

 

public static <T> T consoleExecuteSql(JdbcConsole console,String sql, Function<RemoteResultSet, T> resultSetTFunction) {
    List<DatabaseConnection> activeConnections = DatabaseConnectionManager.getInstance().getActiveConnections();
    SimpleDatabaseConnection simpleDatabaseConnection = null;
    for (DatabaseConnection activeConnection : activeConnections) {
        if (Objects.equals(console.getDataSource().getUrl(), activeConnection.getConnectionPoint().getUrl())) {
            simpleDatabaseConnection = new SimpleDatabaseConnection(
                    activeConnection.getConnectionPoint(),
                    activeConnection.getRemoteConnection(),
                    activeConnection.getConfiguration(),
                    activeConnection.getRequestor(),
                    activeConnection.getConfiguration().getProject());
            break;
        }
    }
    if (simpleDatabaseConnection == null) {
        throw new RuntimeException("unable to found the connection");
    }
    try {
        return DbImplUtil.executeAndGetResult(simpleDatabaseConnection, sql, remoteResultSet -> {
            try {
                return resultSetTFunction.apply(remoteResultSet);
            } catch (Exception e) {
                throw new Exception(e);
            }finally {
            	// close the resultSet otherwise u will get 'Streaming result set com.mysql.jdbc.RowDataDynamic is still active' error
                remoteResultSet.close();
            }
        });
    } catch (SQLException e) {
        throw new RuntimeException(e);
    } finally {
        simpleDatabaseConnection.release();
    }
}
0

Please sign in to leave a comment.