Search in sources :

Example 11 with DatabaseInfo

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

the class MySqlBulkDatabaseWriter method flush.

protected void flush() {
    if (loadedRows > 0) {
        this.stagedInputFile.close();
        statistics.get(batch).startTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        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 sql = String.format("LOAD DATA " + (isLocal ? "LOCAL " : "") + "INFILE '" + stagedInputFile.getFile().getAbsolutePath()).replace('\\', '/') + "' " + (isReplace ? "REPLACE " : "IGNORE ") + "INTO TABLE " + this.getTargetTable().getQualifiedTableName(quote, catalogSeparator, schemaSeparator) + " FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' ESCAPED BY '\\\\' LINES TERMINATED BY '\\n' STARTING BY '' " + getCommaDeliminatedColumns(table.getColumns());
            Statement stmt = c.createStatement();
            //TODO:  clean this up, deal with errors, etc.?
            log.debug(sql);
            stmt.execute(sql);
            stmt.close();
            transaction.commit();
        } catch (SQLException ex) {
            throw platform.getSqlTemplate().translate(ex);
        } finally {
            statistics.get(batch).stopTimer(DataWriterStatisticConstants.DATABASEMILLIS);
        }
        this.stagedInputFile.delete();
        createStagingFile();
        loadedRows = 0;
        loadedBytes = 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)

Example 12 with DatabaseInfo

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

the class DbCompare method buildOrderBy.

protected String buildOrderBy(Table table, Column[] sortByColumns, IDatabasePlatform platform) {
    DatabaseInfo databaseInfo = platform.getDatabaseInfo();
    String quote = databaseInfo.getDelimiterToken() == null ? "" : databaseInfo.getDelimiterToken();
    StringBuilder orderByClause = new StringBuilder("ORDER BY ");
    for (Column sortByColumn : sortByColumns) {
        String columnName = new StringBuilder(quote).append(sortByColumn.getName()).append(quote).toString();
        orderByClause.append(columnName);
        orderByClause.append(",");
    }
    orderByClause.setLength(orderByClause.length() - 1);
    return orderByClause.toString();
}
Also used : DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) Column(org.jumpmind.db.model.Column)

Example 13 with DatabaseInfo

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

the class AuditTableDataRouter method routeToNodes.

public Set<String> routeToNodes(SimpleRouterContext context, DataMetaData dataMetaData, Set<Node> nodes, boolean initialLoad, boolean initialLoadSelectUsed, TriggerRouter triggerRouter) {
    DataEventType eventType = dataMetaData.getData().getDataEventType();
    if (eventType == DataEventType.INSERT || eventType == DataEventType.UPDATE || eventType == DataEventType.DELETE) {
        IParameterService parameterService = engine.getParameterService();
        IDatabasePlatform platform = engine.getDatabasePlatform();
        TriggerHistory triggerHistory = dataMetaData.getTriggerHistory();
        Table table = dataMetaData.getTable().copyAndFilterColumns(triggerHistory.getParsedColumnNames(), triggerHistory.getParsedPkColumnNames(), true);
        String tableName = table.getFullyQualifiedTableName();
        Table auditTable = auditTables.get(tableName);
        if (auditTable == null) {
            auditTable = toAuditTable(table);
            if (parameterService.is(ParameterConstants.AUTO_CONFIGURE_DATABASE)) {
                platform.alterTables(true, auditTable);
            }
            auditTable = platform.getTableFromCache(auditTable.getCatalog(), auditTable.getSchema(), auditTable.getName(), false);
            auditTables.put(tableName, auditTable);
        }
        DatabaseInfo dbInfo = platform.getDatabaseInfo();
        String auditTableName = auditTable.getQualifiedTableName(dbInfo.getDelimiterToken(), dbInfo.getCatalogSeparator(), dbInfo.getSchemaSeparator());
        ISqlTemplate template = platform.getSqlTemplate();
        Map<String, Object> values = null;
        if (eventType != DataEventType.DELETE) {
            values = new HashMap<String, Object>(getNewDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
        } else {
            values = new HashMap<String, Object>(getOldDataAsObject(null, dataMetaData, engine.getSymmetricDialect(), false));
        }
        Long sequence = (Long) context.get(auditTableName);
        if (sequence == null) {
            sequence = 1l + template.queryForLong(String.format("select max(%s) from %s", auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), auditTableName));
        } else {
            sequence = 1l + sequence;
        }
        context.put(auditTable.getName(), sequence);
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_ID).getName(), sequence);
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_TIME).getName(), new Date());
        values.put(auditTable.getColumnWithName(COLUMN_AUDIT_EVENT).getName(), eventType.getCode());
        DmlStatement statement = platform.createDmlStatement(DmlType.INSERT, auditTable, null);
        int[] types = statement.getTypes();
        Object[] args = statement.getValueArray(values);
        String sql = statement.getSql();
        template.update(sql, args, types);
    }
    return null;
}
Also used : IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) Table(org.jumpmind.db.model.Table) DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) DataEventType(org.jumpmind.symmetric.io.data.DataEventType) IParameterService(org.jumpmind.symmetric.service.IParameterService) Date(java.util.Date) ISqlTemplate(org.jumpmind.db.sql.ISqlTemplate) TriggerHistory(org.jumpmind.symmetric.model.TriggerHistory) DmlStatement(org.jumpmind.db.sql.DmlStatement)

Example 14 with DatabaseInfo

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

the class DataService method getCsvDataFor.

protected String getCsvDataFor(ISqlTransaction transaction, Trigger trigger, TriggerHistory triggerHistory, String whereClause, boolean pkOnly) {
    String data = null;
    String sql = null;
    try {
        if (pkOnly) {
            sql = symmetricDialect.createCsvPrimaryKeySql(trigger, triggerHistory, engine.getConfigurationService().getChannel(trigger.getChannelId()), whereClause);
        } else {
            sql = symmetricDialect.createCsvDataSql(trigger, triggerHistory, engine.getConfigurationService().getChannel(trigger.getChannelId()), whereClause);
        }
    } catch (NotImplementedException e) {
    }
    if (isNotBlank(sql)) {
        data = transaction.queryForObject(sql, String.class);
    } else {
        DatabaseInfo databaseInfo = platform.getDatabaseInfo();
        String quote = databaseInfo.getDelimiterToken() == null || !parameterService.is(ParameterConstants.DB_DELIMITED_IDENTIFIER_MODE) ? "" : databaseInfo.getDelimiterToken();
        sql = "select " + triggerHistory.getColumnNames() + " from " + Table.getFullyQualifiedTableName(triggerHistory.getSourceCatalogName(), triggerHistory.getSourceSchemaName(), triggerHistory.getSourceTableName(), quote, databaseInfo.getCatalogSeparator(), databaseInfo.getSchemaSeparator()) + " t where " + whereClause;
        Row row = transaction.queryForRow(sql);
        if (row != null) {
            data = row.csvValue();
        }
    }
    if (data != null) {
        data = data.trim();
    }
    return data;
}
Also used : DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) NotImplementedException(org.apache.commons.lang.NotImplementedException) Row(org.jumpmind.db.sql.Row)

Example 15 with DatabaseInfo

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

the class DataGapRouteReaderTest method buildReader.

protected DataGapRouteReader buildReader(int peekAheadMemoryThreshold, List<DataGap> dataGaps) throws Exception {
    when(parameterService.getEngineName()).thenReturn(ENGINE_NAME);
    when(parameterService.is(ParameterConstants.SYNCHRONIZE_ALL_JOBS)).thenReturn(true);
    when(parameterService.getInt(ParameterConstants.ROUTING_WAIT_FOR_DATA_TIMEOUT_SECONDS)).thenReturn(330);
    when(parameterService.getInt(ParameterConstants.ROUTING_PEEK_AHEAD_MEMORY_THRESHOLD)).thenReturn(peekAheadMemoryThreshold);
    when(parameterService.getInt(ParameterConstants.ROUTING_MAX_GAPS_TO_QUALIFY_IN_SQL)).thenReturn(100);
    when(parameterService.getInt(ParameterConstants.ROUTING_DATA_READER_THRESHOLD_GAPS_TO_USE_GREATER_QUERY)).thenReturn(100);
    when(parameterService.is(ParameterConstants.ROUTING_DATA_READER_ORDER_BY_DATA_ID_ENABLED)).thenReturn(true);
    IStatisticManager statisticManager = mock(StatisticManager.class);
    when(statisticManager.newProcessInfo((ProcessInfoKey) any())).thenReturn(new ProcessInfo());
    INodeService nodeService = mock(NodeService.class);
    when(nodeService.findIdentity()).thenReturn(new Node(NODE_ID, NODE_GROUP_ID));
    IDatabasePlatform platform = mock(IDatabasePlatform.class);
    when(platform.getSqlTemplate()).thenReturn(sqlTemplate);
    when(platform.getDatabaseInfo()).thenReturn(new DatabaseInfo());
    ISymmetricDialect symmetricDialect = mock(AbstractSymmetricDialect.class);
    when(symmetricDialect.supportsTransactionId()).thenReturn(true);
    when(symmetricDialect.getPlatform()).thenReturn(platform);
    IExtensionService extensionService = mock(ExtensionService.class);
    ISymmetricEngine engine = mock(AbstractSymmetricEngine.class);
    when(engine.getParameterService()).thenReturn(parameterService);
    when(engine.getStatisticManager()).thenReturn(statisticManager);
    when(engine.getNodeService()).thenReturn(nodeService);
    when(engine.getDataService()).thenReturn(dataService);
    when(engine.getSymmetricDialect()).thenReturn(symmetricDialect);
    when(engine.getExtensionService()).thenReturn(extensionService);
    IRouterService routerService = new RouterService(engine);
    when(engine.getRouterService()).thenReturn(routerService);
    ChannelRouterContext context = new ChannelRouterContext(NODE_ID, nodeChannel, mock(ISqlTransaction.class));
    context.setDataGaps(dataGaps);
    return new DataGapRouteReader(context, engine);
}
Also used : ISymmetricDialect(org.jumpmind.symmetric.db.ISymmetricDialect) RouterService(org.jumpmind.symmetric.service.impl.RouterService) IRouterService(org.jumpmind.symmetric.service.IRouterService) IDatabasePlatform(org.jumpmind.db.platform.IDatabasePlatform) DatabaseInfo(org.jumpmind.db.platform.DatabaseInfo) Node(org.jumpmind.symmetric.model.Node) ISymmetricEngine(org.jumpmind.symmetric.ISymmetricEngine) ProcessInfo(org.jumpmind.symmetric.model.ProcessInfo) IRouterService(org.jumpmind.symmetric.service.IRouterService) IStatisticManager(org.jumpmind.symmetric.statistic.IStatisticManager) ISqlTransaction(org.jumpmind.db.sql.ISqlTransaction) INodeService(org.jumpmind.symmetric.service.INodeService) IExtensionService(org.jumpmind.symmetric.service.IExtensionService)

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