public Mono<String> queryValidation(String connectionId, String query) {
return connectionConfigRepository.findByConnectionId(connectionId)
.switchIfEmpty(Mono.error(new RuntimeException("Connection not found: " + connectionId)))
.flatMap(config -> {
DatabaseConnectionDetails details = config.getDatabaseConnectionDetails();
return createConnectionFactory(details)
.flatMap(factory -> Mono.from(factory.create())
.flatMap(conn -> {
String escaped = query.replace("'", "''");
String validationQuery = "EXEC sp_describe_first_result_set N'" + escaped + "', NULL, 1;";
return Flux.from(conn.createStatement(validationQuery).execute())
.flatMap(result -> Flux.from(result.map((row, meta) -> "ok"))) // force row parsing
.then(Mono.just("Valid Query"))
.onErrorResume(e -> Mono.just("Invalid Query: " + e.getMessage()))
.doFinally(signal -> conn.close());
})
);
});
}
I tried this way where I am utilising the .execute() to get the errors and differentiate between a valid and an invalid query but unfortunately its giving me "positive response" in both cases. Its because even Invalid Queries are executed and they return errors along with a success token.
I need to find a way to validate mssql queries, manual Validation will take a lot of time and resources.
Also i tried JSQLParser, but for some reason it didn't work. Any suggestions or advices?