JDBCCore { public void executeQuery(ExtensionServiceMessage message) { Map<String, ?> request = (Map<String, ?>) message.getObject(); String replyAddress = ExtensionServiceMessage.extractReplyAddress(message); JDBC localJDBC = null; synchronized (SYNCH_LOCK) { localJDBC = jdbc; } if (localJDBC == null) { if (client != null) { client.sendQueryError(replyAddress, this.getClass().getName() + ".closed", "JDBC connection closed before operation could complete.", null); } } try { if (request.get("query") instanceof String) { String queryString = (String) request.get("query"); if (queryString.trim().toLowerCase().startsWith(SELECT_STATEMENT_IDENTIFIER)) { HashMap[] queryArray = localJDBC.processQuery(queryString); sendDataFromQuery(queryArray, message); } else { int data = localJDBC.processPublish(queryString); log.trace("The returned integer value from Publish Query is the following: ", data); client.sendQueryResponse(204, replyAddress, new LinkedHashMap<>()); } } else if (request.get("query") instanceof List) { List queryArray = (List) request.get("query"); for (int i = 0; i < queryArray.size(); i++) { if (queryArray.get(i).toString().trim().toLowerCase().startsWith(SELECT_STATEMENT_IDENTIFIER)) { client.sendQueryError(replyAddress, this.getClass().getName() + ".invalidBatchElement", "The Query Request could not be executed because at least one batch element " + "was not a string representation of a SQL Update Statement.", null); return; } } int[] data = localJDBC.processBatchPublish(queryArray); log.trace("The returned integer array from Publish Query is the following: ", data); client.sendQueryResponse(204, replyAddress, new LinkedHashMap<>()); } else { log.error("Query could not be executed because query was not a String."); client.sendQueryError(replyAddress, this.getClass().getName() + ".queryNotString", "The Query Request could not be executed because the query property is " + "not a string.", null); } } catch (VantiqSQLException e) { log.error("Could not execute requested query.", e); log.error("Request was: {}", request); client.sendQueryError(replyAddress, VantiqSQLException.class.getCanonicalName(), "Failed to execute query for reason: " + e.getMessage() + ". Exception was: " + e.getClass().getName() + ". Request was: " + request.get("query"), null); } catch (Exception e) { log.error("An unexpected error occurred when executing the requested query.", e); log.error("Request was: {}", request); client.sendQueryError(replyAddress, Exception.class.getCanonicalName(), "Failed to execute query for reason: " + e.getMessage() + ". Exception was: " + e.getClass().getName() + ". Request was: " + request.get("query"), null); } } JDBCCore(String sourceName, String authToken, String targetVantiqServer); String getSourceName(); boolean start(int timeout); void executeQuery(ExtensionServiceMessage message); void executePublish(ExtensionServiceMessage message); void executePolling(String pollQuery); void sendDataFromQuery(HashMap[] queryArray, ExtensionServiceMessage message); void close(); void stop(); boolean exitIfConnectionFails(ExtensionWebSocketClient client, int timeout); public HashMap[] lastRowBundle; final Handler<ExtensionServiceMessage> reconnectHandler; final Handler<ExtensionWebSocketClient> closeHandler; }
@Test public void testExecuteQuery() throws VantiqSQLException { assumeTrue(testDBUsername != null && testDBPassword != null && testDBURL != null && jdbcDriverLoc != null); jdbc.setupJDBC(testDBURL, testDBUsername, testDBPassword, false, 0); Map<String, Object> request; ExtensionServiceMessage msg = new ExtensionServiceMessage(""); Map<String, String> header = new LinkedHashMap<>(); header.put(ExtensionServiceMessage.ORIGIN_ADDRESS_HEADER, "queryAddress"); msg.messageHeaders = header; request = new LinkedHashMap<>(); msg.object = request; core.executeQuery(msg); assertFalse("Core should not be closed", core.isClosed()); request = new LinkedHashMap<>(); request.put("query", "jibberish"); msg.object = request; core.executeQuery(msg); assertFalse("Core should not be closed", core.isClosed()); }