Search in sources :

Example 26 with JdbcResultRow

use of com.adaptris.jdbc.JdbcResultRow in project interlok by adaptris.

the class IntegerColumnTranslatorTest method testFormattedDouble.

@Test
public void testFormattedDouble() throws Exception {
    translator.setFormat("%05d");
    Double doubleVal = Double.valueOf("123");
    JdbcResultRow row = new JdbcResultRow();
    row.setFieldValue("testField", doubleVal, Types.INTEGER);
    try {
        translator.translate(row, 0);
        fail();
    } catch (Exception ex) {
    // expected
    }
    try {
        translator.translate(row, "testField");
        fail();
    } catch (Exception ex) {
    // expected
    }
}
Also used : JdbcResultRow(com.adaptris.jdbc.JdbcResultRow) Test(org.junit.Test)

Example 27 with JdbcResultRow

use of com.adaptris.jdbc.JdbcResultRow in project interlok by adaptris.

the class XmlPayloadTranslatorImpl method createListFromResultSet.

protected List<Element> createListFromResultSet(DocumentWrapper wrapper, JdbcResultSet rs) throws SQLException {
    List<Element> results = new ArrayList<Element>();
    List<String> elementNames = new ArrayList<>();
    boolean firstRecord = true;
    Document doc = wrapper.document;
    for (JdbcResultRow row : rs.getRows()) {
        Element elementRow = doc.createElement(getColumnNameStyle().format(ELEMENT_NAME_ROW));
        // let's go through and build up the element names we need once.
        if (firstRecord) {
            firstRecord = false;
            elementNames = createElementNames(row);
        }
        for (int i = 0; i < row.getFieldCount(); i++) {
            String columnName = row.getFieldName(i);
            String value = toString(row, i);
            Element node = doc.createElement(elementNames.get(i));
            if (isXmlColumn(columnName)) {
                try {
                    Document xmlColumn = null;
                    xmlColumn = wrapper.builder.parse(createInputSource(value));
                    node.appendChild(doc.importNode(xmlColumn.getFirstChild(), true));
                } catch (Exception e) {
                    if (isDisplayColumnErrors()) {
                        log.warn("Failed to parse column {} as an XML Document, treating as text.", columnName);
                        log.trace("Failed to parse column {} as an XML Document", columnName, e);
                    }
                    node.appendChild(createTextNode(doc, value, isCdataColumn(columnName, wrapper)));
                }
            } else {
                node.appendChild(createTextNode(doc, value, isCdataColumn(columnName, wrapper)));
            }
            elementRow.appendChild(node);
        }
        results.add(elementRow);
    }
    return results;
}
Also used : Element(org.w3c.dom.Element) JdbcResultRow(com.adaptris.jdbc.JdbcResultRow) ArrayList(java.util.ArrayList) Document(org.w3c.dom.Document) CoreException(com.adaptris.core.CoreException) SQLException(java.sql.SQLException)

Example 28 with JdbcResultRow

use of com.adaptris.jdbc.JdbcResultRow in project interlok by adaptris.

the class AllRowsMetadataTranslator method translateResult.

@Override
public long translateResult(JdbcResult source, AdaptrisMessage target) throws SQLException, ServiceException {
    List<MetadataElement> added = new ArrayList<MetadataElement>();
    long resultSetCount = 0;
    for (JdbcResultSet resultSet : source.getResultSets()) {
        int counter = 0;
        String resultSetPrefix = source.countResultSets() > 1 ? Long.toString(resultSetCount) + getResultSetCounterPrefix() : "";
        for (JdbcResultRow row : resultSet.getRows()) {
            for (int i = 0; i < row.getFieldCount(); i++) {
                MetadataElement md = new MetadataElement(resultSetPrefix + getMetadataKeyPrefix() + getSeparator() + getColumnNameStyle().format(row.getFieldName(i)) + getSeparator() + counter, toString(row, i));
                if (log.isTraceEnabled()) {
                    added.add(md);
                }
                target.addMetadata(md);
            }
            counter++;
        }
        // In the event that we have multiple selects (unlikely from a JdbcDataQuery) then
        // row-total will have the total for each result set
        // resultSetCount will have the total for all resultSets.
        // if (!isEmpty(getRowTotalMetadataKey())) {
        // target.addMetadata(resultSetPrefix + getRowTotalMetadataKey(), String.valueOf(counter));
        // }
        resultSetCount += counter;
    }
    if (log.isTraceEnabled()) {
        log.debug("Added metadata : " + added);
    }
    return resultSetCount;
}
Also used : JdbcResultRow(com.adaptris.jdbc.JdbcResultRow) ArrayList(java.util.ArrayList) MetadataElement(com.adaptris.core.MetadataElement) JdbcResultSet(com.adaptris.jdbc.JdbcResultSet)

Example 29 with JdbcResultRow

use of com.adaptris.jdbc.JdbcResultRow in project interlok by adaptris.

the class SplittingXmlPayloadTranslator method translateResult.

/**
 * Split the JdbcResult into possibly multiple output messages. Each ResultSet will start in a new message if there are multiple.
 */
@Override
public long translateResult(JdbcResult source, AdaptrisMessage inputMessage) throws SQLException, ServiceException {
    long resultSetCount = 0;
    try {
        // Handle each ResultSet separately and create as many messages as required
        for (JdbcResultSet rs : source.getResultSets()) {
            // While we still have rows in this ResultSet
            final Iterator<JdbcResultRow> rows = rs.getRows().iterator();
            while (rows.hasNext()) {
                AdaptrisMessage outputMessage = newMessage(inputMessage);
                // is limit the number of iterations you make.
                try (LimitedResultSet lrs = new LimitedResultSet(rows, maxRowsPerMessage())) {
                    DocumentWrapper wrapper = toDocument(outputMessage, lrs);
                    XmlHelper.writeXmlDocument(wrapper.document, outputMessage, getOutputMessageEncoding(), createTransformer(wrapper));
                    // Use the configured producer to send the message on its way
                    getProducer().produce(outputMessage);
                    resultSetCount += wrapper.resultSetCount;
                }
            }
        }
    } catch (Exception e) {
        throw ExceptionHelper.wrapServiceException("Failed to process message", e);
    }
    return resultSetCount;
}
Also used : AdaptrisMessage(com.adaptris.core.AdaptrisMessage) JdbcResultRow(com.adaptris.jdbc.JdbcResultRow) ServiceException(com.adaptris.core.ServiceException) SQLException(java.sql.SQLException) NoSuchElementException(java.util.NoSuchElementException) CoreException(com.adaptris.core.CoreException) JdbcResultSet(com.adaptris.jdbc.JdbcResultSet)

Example 30 with JdbcResultRow

use of com.adaptris.jdbc.JdbcResultRow in project interlok by adaptris.

the class FirstRowMetadataTranslator method translateResult.

@Override
public long translateResult(JdbcResult source, AdaptrisMessage target) throws SQLException, ServiceException {
    List<MetadataElement> added = new ArrayList<MetadataElement>();
    long resultSetCount = 0;
    Iterator<JdbcResultRow> iter;
    for (JdbcResultSet resultSet : source.getResultSets()) {
        if (resultSet.getRows() != null && (iter = resultSet.getRows().iterator()) != null && iter.hasNext()) {
            JdbcResultRow storedProcedureResultRow = iter.next();
            if (storedProcedureResultRow != null) {
                for (int i = 0; i < storedProcedureResultRow.getFieldCount(); i++) {
                    String column = storedProcedureResultRow.getFieldName(i);
                    String resultSetPrefix = source.countResultSets() > 1 ? Long.toString(resultSetCount) + getResultSetCounterPrefix() : "";
                    MetadataElement md = new MetadataElement(resultSetPrefix + getMetadataKeyPrefix() + getSeparator() + getColumnNameStyle().format(column), toString(storedProcedureResultRow, i));
                    if (log.isTraceEnabled()) {
                        added.add(md);
                    }
                    target.addMetadata(md);
                }
                if (log.isTraceEnabled()) {
                    log.debug("Added metadata : " + added);
                }
            } else {
                log.debug("No Rows to process");
            }
            resultSetCount++;
        }
    }
    return resultSetCount;
}
Also used : JdbcResultRow(com.adaptris.jdbc.JdbcResultRow) ArrayList(java.util.ArrayList) MetadataElement(com.adaptris.core.MetadataElement) JdbcResultSet(com.adaptris.jdbc.JdbcResultSet)

Aggregations

JdbcResultRow (com.adaptris.jdbc.JdbcResultRow)69 Test (org.junit.Test)65 OutputStream (java.io.OutputStream)9 StringWriter (java.io.StringWriter)9 WriterOutputStream (org.apache.commons.io.output.WriterOutputStream)9 SQLException (java.sql.SQLException)5 Clob (java.sql.Clob)4 SerialClob (javax.sql.rowset.serial.SerialClob)4 JdbcResultSet (com.adaptris.jdbc.JdbcResultSet)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 ArrayList (java.util.ArrayList)3 CoreException (com.adaptris.core.CoreException)2 MetadataElement (com.adaptris.core.MetadataElement)2 AdaptrisMessage (com.adaptris.core.AdaptrisMessage)1 ServiceException (com.adaptris.core.ServiceException)1 NoSuchElementException (java.util.NoSuchElementException)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1