* @see net.sf.hajdbc.sync.SynchronizationSupport#synchronizeIdentityColumns()
*/
@Override
public void synchronizeIdentityColumns() throws SQLException
{
IdentityColumnSupport support = this.context.getDialect().getIdentityColumnSupport();
if (support != null)
{
Statement sourceStatement = this.context.getConnection(this.context.getSourceDatabase()).createStatement();
try
{
Statement targetStatement = this.context.getConnection(this.context.getTargetDatabase()).createStatement();
try
{
for (TableProperties table: this.context.getSourceDatabaseProperties().getTables())
{
Collection<String> columns = table.getIdentityColumns();
if (!columns.isEmpty())
{
String selectSQL = MessageFormat.format("SELECT max({0}) FROM {1}", Strings.join(columns, "), max("), table.getName()); //$NON-NLS-1$ //$NON-NLS-2$
this.logger.log(Level.DEBUG, selectSQL);
Map<String, Long> map = new HashMap<String, Long>();
ResultSet resultSet = sourceStatement.executeQuery(selectSQL);
try
{
if (resultSet.next())
{
int i = 0;
for (String column: columns)
{
map.put(column, resultSet.getLong(++i));
}
}
}
finally
{
Resources.close(resultSet);
}
if (!map.isEmpty())
{
for (Map.Entry<String, Long> mapEntry: map.entrySet())
{
String alterSQL = support.getAlterIdentityColumnSQL(table, table.getColumnProperties(mapEntry.getKey()), mapEntry.getValue() + 1);
if (alterSQL != null)
{
this.logger.log(Level.DEBUG, alterSQL);