List of usage examples for java.sql Clob getSubString
String getSubString(long pos, int length) throws SQLException;
From source file:org.zaproxy.zap.extension.websocket.db.TableWebSocket.java
/** * Filter out and count messages according to payloadFilter * * @param criteria//from ww w .jav a 2 s . com * @param opcodes Null when all opcodes should be retrieved. * @param inScopeChannelIds * @param payloadFilter Null when all payloads should be retrieved. * @param payloadLength * @return number of message that fulfill given template * @throws DatabaseException */ private int countMessageWithPayloadFilter(WebSocketMessageDTO criteria, List<Integer> opcodes, List<Integer> inScopeChannelIds, WebSocketMessagesPayloadFilter payloadFilter, int payloadLength) throws DatabaseException { String query = "SELECT m.opcode, m.payload_utf8 FROM websocket_message AS m " + "LEFT OUTER JOIN websocket_message_fuzz f " + "ON m.message_id = f.message_id AND m.channel_id = f.channel_id " + "<where> "; int count = 0; try { PreparedStatement stmt = buildMessageCriteriaStatement(query, criteria, opcodes, inScopeChannelIds); stmt.execute(); ResultSet resultSet = stmt.getResultSet(); try { while (resultSet.next()) { String payload; // read payload if (resultSet.getInt("opcode") != WebSocketMessage.OPCODE_BINARY) { if (payloadLength == -1) { // load all characters payload = resultSet.getString("payload_utf8"); } else { Clob clob = resultSet.getClob("payload_utf8"); int length = Math.min(payloadLength, (int) clob.length()); payload = clob.getSubString(1, length); clob.free(); } if (payloadFilter.isStringValidWithPattern(payload)) { count++; } } } } finally { resultSet.close(); stmt.close(); } } catch (SQLException e) { throw new DatabaseException(e); } return count; }
From source file:org.zaproxy.zap.extension.websocket.db.TableWebSocket.java
/** * @param rs/*from ww w . j ava 2 s .c o m*/ * @param interpretLiteralBytes * @param payloadLength * @return * @throws HttpMalformedHeaderException * @throws SQLException * @throws DatabaseException */ private List<WebSocketMessageDTO> buildMessageDTOs(ResultSet rs, boolean interpretLiteralBytes, int payloadLength) throws SQLException, DatabaseException { ArrayList<WebSocketMessageDTO> messages = new ArrayList<>(); try { while (rs.next()) { WebSocketMessageDTO message; int channelId = rs.getInt("channel_id"); WebSocketChannelDTO channel = getChannel(channelId); if (rs.getInt("fuzz_id") != 0) { WebSocketFuzzMessageDTO fuzzMessage = new WebSocketFuzzMessageDTO(channel); fuzzMessage.fuzzId = rs.getInt("fuzz_id"); fuzzMessage.state = WebSocketFuzzMessageDTO.State.valueOf(rs.getString("state")); fuzzMessage.fuzz = rs.getString("fuzz"); message = fuzzMessage; } else { message = new WebSocketMessageDTO(channel); } message.id = rs.getInt("message_id"); message.setTime(rs.getTimestamp("timestamp")); message.opcode = rs.getInt("opcode"); message.readableOpcode = WebSocketMessage.opcode2string(message.opcode); // read payload if (message.opcode == WebSocketMessage.OPCODE_BINARY) { if (payloadLength == -1) { // load all bytes message.payload = rs.getBytes("payload_bytes"); } else { Blob blob = rs.getBlob("payload_bytes"); int length = Math.min(payloadLength, (int) blob.length()); message.payload = blob.getBytes(1, length); blob.free(); } if (message.payload == null) { message.payload = new byte[0]; } } else { if (payloadLength == -1) { // load all characters message.payload = rs.getString("payload_utf8"); } else { Clob clob = rs.getClob("payload_utf8"); int length = Math.min(payloadLength, (int) clob.length()); message.payload = clob.getSubString(1, length); clob.free(); } if (message.payload == null) { message.payload = ""; } } message.isOutgoing = rs.getBoolean("is_outgoing"); message.payloadLength = rs.getInt("payload_length"); messages.add(message); } } finally { rs.close(); } messages.trimToSize(); return messages; }
From source file:uk.ac.ebi.orchem.search.SimilaritySearch.java
/** * Java interface to PL/SQL for similarity searching * @param userQuery query structure in some chemical format * @param queryType MOL or SMILES/*from ww w . ja v a2 s . c o m*/ * @param cutOff break out when similarity goes under this cut off * @param topN only find first top N results * @param debugYN debug info back to user Y/N * @param idsOnlyYN only return IDs Y/N * @param extraWhereClause an option to add an extra where clause refering to your base compound table * @return array of compound data * @throws Exception */ public static oracle.sql.ARRAY search(Clob userQuery, String queryType, Float cutOff, Integer topN, String debugYN, String idsOnlyYN, String extraWhereClause) throws Exception { int clobLen = new Long(userQuery.length()).intValue(); String query = (userQuery.getSubString(1, clobLen)); if (queryType.equals(Utils.QUERY_TYPE_MOL)) return molSearch(query, cutOff, topN, debugYN, idsOnlyYN, extraWhereClause); else if (queryType.equals(Utils.QUERY_TYPE_SMILES)) return smilesSearch(query, cutOff, topN, debugYN, idsOnlyYN, extraWhereClause); else throw new RuntimeException("Query type not recognized"); }
From source file:uk.ac.ebi.orchem.search.SimilaritySearch.java
/** * Similarity score calculation between one database compound and a user's query compound. * @param userQuery query structure in SMILES or MOL * @param queryType MOL or SMILES/*from w ww . j av a2 s . c om*/ * @param compoundId ID of database compound to calculate similarity with * @return tanimoto similarity score * @throws Exception */ public static float singleCompoundSimilarity(Clob userQuery, String queryType, String compoundId) throws Exception { OracleConnection conn = null; PreparedStatement pstmtFp = null; ResultSet resFp = null; float tanimotoCoeff = 0; try { //User query int clobLen = new Long(userQuery.length()).intValue(); String query = (userQuery.getSubString(1, clobLen)); IAtomContainer molecule = null; if (queryType.equals(Utils.QUERY_TYPE_MOL)) { molecule = MoleculeCreator.getMoleculeFromMolfile(query); } else if (queryType.equals(Utils.QUERY_TYPE_SMILES)) { SmilesParser sp = new SmilesParser(DefaultChemObjectBuilder.getInstance()); molecule = sp.parseSmiles(query); } else throw new RuntimeException("Query type not recognized"); BitSet queryFp = FingerPrinterAgent.FP.getExtendedFingerPrinter().getFingerprint(molecule); float queryBitCount = queryFp.cardinality(); byte[] queryBytes = Utils.toByteArray(queryFp, extFpSize); //Database comound conn = (OracleConnection) new OracleDriver().defaultConnection(); String compoundQuery = "select bit_count, fp from orchem_fingprint_simsearch s where id=?"; pstmtFp = conn.prepareStatement(compoundQuery); pstmtFp.setFetchSize(1); pstmtFp.setString(1, compoundId); resFp = pstmtFp.executeQuery(); if (resFp.next()) { byte[] dbByteArray = resFp.getBytes("fp"); float compoundBitCount = resFp.getFloat("bit_count"); tanimotoCoeff = calcTanimoto(queryBytes, queryBytes.length, dbByteArray, queryBitCount, compoundBitCount); } else throw new RuntimeException("Compound " + compoundId + " not found in similarity table"); } catch (Exception e) { e.printStackTrace(); throw e; } finally { if (resFp != null) resFp.close(); if (pstmtFp != null) pstmtFp.close(); if (conn != null) conn.close(); } return tanimotoCoeff; }