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;
}
}
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();
}
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;
}
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;
}
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);
}
Aggregations