Search in sources :

Example 1 with DatabaseInfo

use of org.jumpmind.db.platform.DatabaseInfo in project symmetric-ds by JumpMind.

the class TableExtractDataReaderSource method startNewCursor.

protected void startNewCursor() {
    DatabaseInfo dbInfo = platform.getDatabaseInfo();
    String sql = String.format("select * from %s %s", table.getQualifiedTableName(dbInfo.getDelimiterToken(), dbInfo.getCatalogSeparator(), dbInfo.getSchemaSeparator()), StringUtils.isNotBlank(whereClause) ? " where " + whereClause : "");
    this.cursor = platform.getSqlTemplate().queryForCursor(sql, new ISqlRowMapper<CsvData>() {

        public CsvData mapRow(Row row) {
            return new CsvData(DataEventType.INSERT, toStringData(row, table.getPrimaryKeyColumns()), toStringData(row, table.getColumns()));
        }
    });
}
Also used : DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) Row(org.jumpmind.db.sql.Row) ISqlRowMapper(org.jumpmind.db.sql.ISqlRowMapper) CsvData(org.jumpmind.symmetric.io.data.CsvData)

Example 2 with DatabaseInfo

use of org.jumpmind.db.platform.DatabaseInfo in project symmetric-ds by JumpMind.

the class ExtractDataReader method buildSelect.

protected String buildSelect(Table table, Column lobColumn, Column[] pkColumns) {
    StringBuilder sql = new StringBuilder("select ");
    DatabaseInfo dbInfo = platform.getDatabaseInfo();
    String quote = platform.getDdlBuilder().isDelimitedIdentifierModeOn() ? dbInfo.getDelimiterToken() : "";
    sql.append(quote);
    sql.append(lobColumn.getName());
    sql.append(quote);
    sql.append(",");
    sql.delete(sql.length() - 1, sql.length());
    sql.append(" from ");
    sql.append(table.getQualifiedTableName(quote, dbInfo.getCatalogSeparator(), dbInfo.getSchemaSeparator()));
    sql.append(" where ");
    for (Column col : pkColumns) {
        sql.append(quote);
        sql.append(col.getName());
        sql.append(quote);
        sql.append("=? and ");
    }
    sql.delete(sql.length() - 5, sql.length());
    return sql.toString();
}
Also used : DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) Column(org.jumpmind.db.model.Column)

Example 3 with DatabaseInfo

use of org.jumpmind.db.platform.DatabaseInfo in project symmetric-ds by JumpMind.

the class RouterServiceTest method setup.

@Before
public void setup() {
    ISymmetricEngine engine = mock(ISymmetricEngine.class);
    IParameterService parameterService = mock(IParameterService.class);
    ISymmetricDialect symmetricDialect = mock(ISymmetricDialect.class);
    IDatabasePlatform databasePlatform = mock(IDatabasePlatform.class);
    IExtensionService extensionService = mock(IExtensionService.class);
    when(databasePlatform.getDatabaseInfo()).thenReturn(new DatabaseInfo());
    when(symmetricDialect.getPlatform()).thenReturn(databasePlatform);
    when(engine.getDatabasePlatform()).thenReturn(databasePlatform);
    when(engine.getParameterService()).thenReturn(parameterService);
    when(engine.getSymmetricDialect()).thenReturn(symmetricDialect);
    when(engine.getExtensionService()).thenReturn(extensionService);
    routerService = new RouterService(engine);
}
Also used : ISymmetricDialect(org.jumpmind.symmetric.db.ISymmetricDialect) IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) ISymmetricEngine(org.jumpmind.symmetric.ISymmetricEngine) IExtensionService(org.jumpmind.symmetric.service.IExtensionService) IParameterService(org.jumpmind.symmetric.service.IParameterService) Before(org.junit.Before)

Example 4 with DatabaseInfo

use of org.jumpmind.db.platform.DatabaseInfo in project symmetric-ds by JumpMind.

the class MySqlBulkDatabaseWriter method getCommaDeliminatedColumns.

protected String getCommaDeliminatedColumns(Column[] cols) {
    DatabaseInfo dbInfo = platform.getDatabaseInfo();
    String quote = dbInfo.getDelimiterToken();
    StringBuilder columns = new StringBuilder();
    columns.append("(");
    // The target syntax is (where BLOB is involved):
    // ('column1', 'column2', @hexColumn3) SET 'column3'=UNHEX(@hexColumn3);
    Map<String, String> blobColumns = new LinkedHashMap<String, String>();
    if (cols != null && cols.length > 0) {
        for (Column column : cols) {
            boolean blob = column.isOfBinaryType();
            if (blob) {
                String hexVariable = String.format("@%s_hex", column.getName());
                blobColumns.put(column.getName(), hexVariable);
                columns.append(hexVariable);
            } else {
                columns.append(quote);
                columns.append(column.getName());
                columns.append(quote);
            }
            columns.append(",");
        }
        columns.replace(columns.length() - 1, columns.length(), "");
        columns.append(")");
        // Build this (optional) clause:
        // SET 'column3'=UNHEX(@hexColumn3);
        StringBuilder setClause = new StringBuilder();
        for (String columnName : blobColumns.keySet()) {
            if (setClause.length() == 0) {
                setClause.append(" SET ");
            }
            setClause.append(quote);
            setClause.append(columnName);
            setClause.append(quote);
            setClause.append("=UNHEX(");
            setClause.append(blobColumns.get(columnName));
            setClause.append("),");
        }
        if (setClause.length() > 0) {
            setClause.setLength(setClause.length() - 1);
            columns.append(setClause);
        }
        return columns.toString();
    } else {
        return " ";
    }
}
Also used : DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) Column(org.jumpmind.db.model.Column) LinkedHashMap(java.util.LinkedHashMap)

Example 5 with DatabaseInfo

use of org.jumpmind.db.platform.DatabaseInfo in project symmetric-ds by JumpMind.

the class MsSqlBulkDatabaseWriter method flush.

protected void flush() {
    if (loadedRows > 0) {
        this.stagedInputFile.close();
        statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        String filename;
        if (StringUtils.isEmpty(uncPath)) {
            filename = stagedInputFile.getFile().getAbsolutePath();
        } else {
            filename = uncPath + "\\" + stagedInputFile.getFile().getName();
        }
        try {
            DatabaseInfo dbInfo = platform.getDatabaseInfo();
            String quote = dbInfo.getDelimiterToken();
            String catalogSeparator = dbInfo.getCatalogSeparator();
            String schemaSeparator = dbInfo.getSchemaSeparator();
            JdbcSqlTransaction jdbcTransaction = (JdbcSqlTransaction) transaction;
            Connection c = jdbcTransaction.getConnection();
            String rowTerminatorString = "";
            /*
                 * There seems to be a bug with the SQL server bulk insert when
                 * you have one row with binary data at the end using \n as the
                 * row terminator. It works when you leave the row terminator
                 * out of the bulk insert statement.
                 */
            if (!(rowTerminator.equals("\n") || rowTerminator.equals("\r\n"))) {
                rowTerminatorString = ", ROWTERMINATOR='" + StringEscapeUtils.escapeJava(rowTerminator) + "'";
            }
            String sql = String.format("BULK INSERT " + this.getTargetTable().getQualifiedTableName(quote, catalogSeparator, schemaSeparator) + " FROM '" + filename) + "'" + " WITH (DATAFILETYPE='widechar', FIELDTERMINATOR='" + StringEscapeUtils.escapeJava(fieldTerminator) + "', KEEPIDENTITY" + (fireTriggers ? ", FIRE_TRIGGERS" : "") + rowTerminatorString + ");";
            Statement stmt = c.createStatement();
            //TODO:  clean this up, deal with errors, etc.?
            stmt.execute(sql);
            stmt.close();
        } catch (SQLException ex) {
            throw platform.getSqlTemplate().translate(ex);
        } finally {
            statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        }
        this.stagedInputFile.delete();
        createStagingFile();
        loadedRows = 0;
    }
}
Also used : DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) SQLException(java.sql.SQLException) Statement(java.sql.Statement) Connection(java.sql.Connection) JdbcSqlTransaction(org.jumpmind.db.sql.JdbcSqlTransaction)

Aggregations

DatabaseInfo (org.jumpmind.db.platform.DatabaseInfo)18 IDatabasePlatform (org.jumpmind.db.platform.IDatabasePlatform)6 ISqlTransaction (org.jumpmind.db.sql.ISqlTransaction)5 ISymmetricEngine (org.jumpmind.symmetric.ISymmetricEngine)5 IExtensionService (org.jumpmind.symmetric.service.IExtensionService)5 IParameterService (org.jumpmind.symmetric.service.IParameterService)5 Column (org.jumpmind.db.model.Column)4 Before (org.junit.Before)4 ISqlTemplate (org.jumpmind.db.sql.ISqlTemplate)3 ISymmetricDialect (org.jumpmind.symmetric.db.ISymmetricDialect)3 Connection (java.sql.Connection)2 SQLException (java.sql.SQLException)2 Statement (java.sql.Statement)2 Table (org.jumpmind.db.model.Table)2 JdbcSqlTransaction (org.jumpmind.db.sql.JdbcSqlTransaction)2 Row (org.jumpmind.db.sql.Row)2 AbstractSymmetricDialect (org.jumpmind.symmetric.db.AbstractSymmetricDialect)2 Node (org.jumpmind.symmetric.model.Node)2 ProcessInfo (org.jumpmind.symmetric.model.ProcessInfo)2 IDataService (org.jumpmind.symmetric.service.IDataService)2