Search in sources :

Example 6 with CsvWriter

use of org.jumpmind.symmetric.csv.CsvWriter in project symmetric-ds by JumpMind.

the class CsvUtils method escapeCsvData.

public static String escapeCsvData(String[] data, char recordDelimiter, char textQualifier, int escapeMode, String nullString) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CsvWriter writer = new CsvWriter(new OutputStreamWriter(out), ',');
    writer.setEscapeMode(escapeMode);
    if (recordDelimiter != '\0') {
        writer.setRecordDelimiter(recordDelimiter);
    }
    if (textQualifier != '\0') {
        writer.setTextQualifier(textQualifier);
        writer.setUseTextQualifier(true);
        writer.setForceQualifier(true);
    }
    if (nullString != null) {
        writer.setNullString(nullString);
    }
    try {
        writer.writeRecord(data, true);
    } catch (IOException e) {
        throw new IoException();
    }
    writer.close();
    return out.toString();
}
Also used : CsvWriter(org.jumpmind.symmetric.csv.CsvWriter) IoException(org.jumpmind.exception.IoException) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 7 with CsvWriter

use of org.jumpmind.symmetric.csv.CsvWriter in project symmetric-ds by JumpMind.

the class CsvUtils method escapeCsvData.

public static String escapeCsvData(String[] data) {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CsvWriter writer = new CsvWriter(new OutputStreamWriter(out), ',');
    writer.setEscapeMode(CsvWriter.ESCAPE_MODE_BACKSLASH);
    writer.setTextQualifier('\"');
    writer.setUseTextQualifier(true);
    writer.setForceQualifier(true);
    for (String s : data) {
        try {
            writer.write(s, true);
        } catch (IOException e) {
            throw new IoException();
        }
    }
    writer.close();
    return out.toString();
}
Also used : CsvWriter(org.jumpmind.symmetric.csv.CsvWriter) IoException(org.jumpmind.exception.IoException) OutputStreamWriter(java.io.OutputStreamWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 8 with CsvWriter

use of org.jumpmind.symmetric.csv.CsvWriter in project symmetric-ds by JumpMind.

the class AbstractDataLoaderServiceTest method test04SqlStatistics.

@Test
public void test04SqlStatistics() throws Exception {
    Level old = setLoggingLevelForTest(Level.OFF);
    String[] insertValues = new String[TEST_COLUMNS.length];
    insertValues[2] = insertValues[4] = "sql stat test";
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CsvWriter writer = getWriter(out);
    writer.writeRecord(new String[] { CsvConstants.NODEID, TestConstants.TEST_CLIENT_EXTERNAL_ID });
    writer.writeRecord(new String[] { CsvConstants.CHANNEL, TestConstants.TEST_CHANNEL_ID });
    String nextBatchId = getNextBatchId();
    writer.writeRecord(new String[] { CsvConstants.BATCH, nextBatchId });
    writeTable(writer, TEST_TABLE, TEST_KEYS, TEST_COLUMNS);
    // Clean insert
    String firstId = getNextId();
    insertValues[0] = firstId;
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(insertValues, true);
    // Clean insert
    String secondId = getNextId();
    insertValues[0] = secondId;
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(insertValues, true);
    String thirdId = getNextId();
    insertValues[0] = thirdId;
    // date column ...
    insertValues[5] = "This is a very long string that will fail upon insert into the database.";
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(insertValues, true);
    writer.writeRecord(new String[] { CsvConstants.COMMIT, nextBatchId });
    writer.close();
    load(out);
    IncomingBatch batch = getIncomingBatchService().findIncomingBatch(batchId, TestConstants.TEST_CLIENT_EXTERNAL_ID);
    assertNotNull(batch);
    assertEquals(batch.getStatus(), IncomingBatch.Status.ER, "Wrong status. " + printDatabase());
    assertEquals(batch.getFailedRowNumber(), 3l, "Wrong failed row number. " + printDatabase());
    Assert.assertEquals("Wrong byte count: " + batch.getByteCount() + ". " + printDatabase(), 394l, batch.getByteCount());
    assertEquals(batch.getStatementCount(), 3l, "Wrong statement count. " + printDatabase());
    assertEquals(batch.getFallbackInsertCount(), 0l, "Wrong fallback insert count. " + printDatabase());
    assertEquals(batch.getFallbackUpdateCount(), 0l, "Wrong fallback update count. " + printDatabase());
    assertEquals(batch.getMissingDeleteCount(), 0l, "Wrong missing delete count. " + printDatabase());
    assertNull(batch.getSqlState(), "Sql state should be null. " + printDatabase());
    assertNotNull(batch.getSqlMessage(), "Sql message should not be null. " + printDatabase());
    setLoggingLevelForTest(old);
}
Also used : CsvWriter(org.jumpmind.symmetric.csv.CsvWriter) Level(org.apache.log4j.Level) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IncomingBatch(org.jumpmind.symmetric.model.IncomingBatch) Test(org.junit.Test)

Example 9 with CsvWriter

use of org.jumpmind.symmetric.csv.CsvWriter in project symmetric-ds by JumpMind.

the class AbstractDataLoaderServiceTest method test07DataIntregrityError.

@Test
public void test07DataIntregrityError() throws Exception {
    Level old = setLoggingLevelForTest(Level.OFF);
    String[] values = { getNextId(), "string3", "string not null3", "char3", "char not null3", "2007-01-02 00:00:00.000", "2007-02-03 04:05:06.000", "0", "47", "67.89", "0.474" };
    ConflictNodeGroupLink conflictSettings = new ConflictNodeGroupLink();
    conflictSettings.setNodeGroupLink(TestConstants.TEST_2_ROOT);
    conflictSettings.setConflictId("dont_fallback");
    conflictSettings.setResolveType(ResolveConflict.MANUAL);
    getSymmetricEngine().getDataLoaderService().save(conflictSettings);
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CsvWriter writer = getWriter(out);
    writer.writeRecord(new String[] { CsvConstants.NODEID, TestConstants.TEST_CLIENT_EXTERNAL_ID });
    writer.writeRecord(new String[] { CsvConstants.CHANNEL, TestConstants.TEST_CHANNEL_ID });
    writer.writeRecord(new String[] { CsvConstants.BATCH, getNextBatchId() });
    writeTable(writer, TEST_TABLE, TEST_KEYS, TEST_COLUMNS);
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(values, true);
    writer.write(CsvConstants.INSERT);
    writer.writeRecord(values, true);
    writer.writeRecord(new String[] { CsvConstants.COMMIT, getBatchId() });
    writer.close();
    load(out);
    assertEquals(findIncomingBatchStatus(batchId, TestConstants.TEST_CLIENT_EXTERNAL_ID), IncomingBatch.Status.ER, "Wrong status");
    IncomingBatch batch = getIncomingBatchService().findIncomingBatch(batchId, TestConstants.TEST_CLIENT_EXTERNAL_ID);
    assertNotNull(batch);
    assertEquals(batch.getStatus(), IncomingBatch.Status.ER, "Wrong status");
    assertEquals(batch.getFailedRowNumber(), 2l, "Wrong failed row number");
    assertEquals(batch.getStatementCount(), 2l, "Wrong statement count");
    load(out);
    assertEquals(findIncomingBatchStatus(batchId, TestConstants.TEST_CLIENT_EXTERNAL_ID), IncomingBatch.Status.ER, "Wrong status");
    batch = getIncomingBatchService().findIncomingBatch(batchId, TestConstants.TEST_CLIENT_EXTERNAL_ID);
    assertNotNull(batch);
    assertEquals(batch.getStatus(), IncomingBatch.Status.ER, "Wrong status");
    assertEquals(batch.getFailedRowNumber(), 2l, "Wrong failed row number");
    assertEquals(batch.getStatementCount(), 2l, "Wrong statement count");
    getSymmetricEngine().getDataLoaderService().delete(conflictSettings);
    setLoggingLevelForTest(old);
}
Also used : CsvWriter(org.jumpmind.symmetric.csv.CsvWriter) ConflictNodeGroupLink(org.jumpmind.symmetric.service.impl.DataLoaderService.ConflictNodeGroupLink) Level(org.apache.log4j.Level) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IncomingBatch(org.jumpmind.symmetric.model.IncomingBatch) Test(org.junit.Test)

Example 10 with CsvWriter

use of org.jumpmind.symmetric.csv.CsvWriter in project symmetric-ds by JumpMind.

the class AbstractDataLoaderServiceTest method testSimple.

protected void testSimple(String dmlType, String[] values, String[] expectedValues) throws Exception {
    ByteArrayOutputStream out = new ByteArrayOutputStream();
    CsvWriter writer = getWriter(out);
    writer.writeRecord(new String[] { CsvConstants.NODEID, TestConstants.TEST_CLIENT_EXTERNAL_ID });
    writer.writeRecord(new String[] { CsvConstants.CHANNEL, TestConstants.TEST_CHANNEL_ID });
    String nextBatchId = getNextBatchId();
    writer.writeRecord(new String[] { CsvConstants.BATCH, nextBatchId });
    writeTable(writer, TEST_TABLE, TEST_KEYS, TEST_COLUMNS);
    writer.write(dmlType);
    writer.writeRecord(values, true);
    writer.writeRecord(new String[] { CsvConstants.COMMIT, nextBatchId });
    writer.close();
    load(out);
    assertTestTableEquals(values[0], expectedValues);
}
Also used : CsvWriter(org.jumpmind.symmetric.csv.CsvWriter) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Aggregations

CsvWriter (org.jumpmind.symmetric.csv.CsvWriter)14 ByteArrayOutputStream (java.io.ByteArrayOutputStream)13 Test (org.junit.Test)8 Level (org.apache.log4j.Level)7 IncomingBatch (org.jumpmind.symmetric.model.IncomingBatch)7 OutputStreamWriter (java.io.OutputStreamWriter)5 IOException (java.io.IOException)3 IoException (org.jumpmind.exception.IoException)2 SQLException (java.sql.SQLException)1 Column (org.jumpmind.db.model.Column)1 DataEventType (org.jumpmind.symmetric.io.data.DataEventType)1 ConflictNodeGroupLink (org.jumpmind.symmetric.service.impl.DataLoaderService.ConflictNodeGroupLink)1