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
Please sign in to leave a comment.
Finally I found the way to get the result synchronously .
Here is the code, hoping help someone need it.