Skip to content

Commit

Permalink
Added support for the transformer to insert into identity columns
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed Aug 19, 2011
1 parent d2fbb9e commit 108b010
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 18 deletions.
Expand Up @@ -186,10 +186,10 @@ public IColumnFilter newDatabaseColumnFilter() {
return null;
}

public void prepareTableForDataLoad(JdbcTemplate jdbcTemplate, Table table) {
public void allowIdentityInserts(JdbcTemplate jdbcTemplate, Table table) {
}

public void cleanupAfterDataLoad(JdbcTemplate jdbcTemplate, Table table) {
public void revertAllowIdentityInserts(JdbcTemplate jdbcTemplate, Table table) {
}

protected boolean allowsNullForIdentityColumn() {
Expand Down
Expand Up @@ -62,18 +62,17 @@ public void removeTrigger(StringBuilder sqlBuffer, String catalogName, String sc

/**
* This is called by the data loader each time the table context changes,
* giving the dialect an opportunity to do any pre loading work. Only one
* table is active at any one point.
* giving the dialect an opportunity to allow inserts into identity columns
*/
public void prepareTableForDataLoad(JdbcTemplate jdbcTemplate, Table table);
public void allowIdentityInserts(JdbcTemplate jdbcTemplate, Table table);

/**
* This is called by the data loader each time the table context changes
* away from a table or when the the data loader is closed, giving the
* dialect an opportunity to do any post loading work for the given table.
* @param jdbcTemplate TODO
* dialect an opportunity to reset the state of a table to allow identity
* inserts to work
*/
public void cleanupAfterDataLoad(JdbcTemplate jdbcTemplate, Table table);
public void revertAllowIdentityInserts(JdbcTemplate jdbcTemplate, Table table);

public Database readPlatformDatabase(boolean includeSymmetricTables);

Expand Down
Expand Up @@ -122,14 +122,14 @@ protected String switchCatalogForTriggerInstall(String catalog, Connection c) th
}

@Override
public void prepareTableForDataLoad(JdbcTemplate template, Table table) {
public void allowIdentityInserts(JdbcTemplate template, Table table) {
if (table != null && table.getAutoIncrementColumns().length > 0) {
template.execute("SET IDENTITY_INSERT " + table.getFullyQualifiedTableName() + " ON");
}
}

@Override
public void cleanupAfterDataLoad(JdbcTemplate template, Table table) {
public void revertAllowIdentityInserts(JdbcTemplate template, Table table) {
if (table != null && table.getAutoIncrementColumns().length > 0) {
template.execute("SET IDENTITY_INSERT " + table.getFullyQualifiedTableName() + " OFF");
}
Expand Down
Expand Up @@ -132,14 +132,14 @@ protected String switchCatalogForTriggerInstall(String catalog, Connection c) th
}

@Override
public void prepareTableForDataLoad(JdbcTemplate jdbcTemplate, Table table) {
public void allowIdentityInserts(JdbcTemplate jdbcTemplate, Table table) {
if (table != null && table.getAutoIncrementColumns().length > 0) {
jdbcTemplate.execute("SET IDENTITY_INSERT " + table.getName() + " ON");
}
}

@Override
public void cleanupAfterDataLoad(JdbcTemplate jdbcTemplate, Table table) {
public void revertAllowIdentityInserts(JdbcTemplate jdbcTemplate, Table table) {
if (table != null && table.getAutoIncrementColumns().length > 0) {
jdbcTemplate.execute("SET IDENTITY_INSERT " + table.getName() + " OFF");
}
Expand Down
Expand Up @@ -345,13 +345,13 @@ protected void resetTable() {

protected void prepareTableForDataLoad() {
if (context != null && context.getTableTemplate() != null) {
dbDialect.prepareTableForDataLoad(this.jdbcTemplate, context.getTableTemplate().getTable());
dbDialect.allowIdentityInserts(this.jdbcTemplate, context.getTableTemplate().getTable());
}
}

protected void cleanupAfterDataLoad() {
if (context != null && context.getTableTemplate() != null) {
dbDialect.cleanupAfterDataLoad(this.jdbcTemplate, context.getTableTemplate().getTable());
dbDialect.revertAllowIdentityInserts(this.jdbcTemplate, context.getTableTemplate().getTable());
}
}

Expand Down
Expand Up @@ -102,8 +102,10 @@ protected void apply(ICacheContext context, List<TransformedData> dataThatHasBee
case INSERT:
Table table = tableTemplate.getTable();
try {
if (table.hasAutoIncrementColumn()) {
dbDialect.prepareTableForDataLoad(context.getJdbcTemplate(), table);
if (Boolean.TRUE.equals(context.getContextCache().get(VariableColumnTransform.OPTION_IDENTITY))) {
dbDialect.revertAllowIdentityInserts(context.getJdbcTemplate(), table);
} else if (table.hasAutoIncrementColumn()) {
dbDialect.allowIdentityInserts(context.getJdbcTemplate(), table);
}
tableTemplate.insert((IDataLoaderContext) context, data.getColumnValues());
} catch (DataIntegrityViolationException ex) {
Expand All @@ -114,7 +116,7 @@ protected void apply(ICacheContext context, List<TransformedData> dataThatHasBee
data.getKeyValues());
} finally {
if (table.hasAutoIncrementColumn()) {
dbDialect.cleanupAfterDataLoad(context.getJdbcTemplate(), table);
dbDialect.revertAllowIdentityInserts(context.getJdbcTemplate(), table);
}
}
break;
Expand Down
Expand Up @@ -14,7 +14,9 @@ public class VariableColumnTransform implements ISingleValueColumnTransform, IBu

protected static final String OPTION_TIMESTAMP = "system_timestamp";

private static final String[] OPTIONS = new String[] {OPTION_TIMESTAMP};
protected static final String OPTION_IDENTITY = "use_identity";

private static final String[] OPTIONS = new String[] {OPTION_TIMESTAMP, OPTION_IDENTITY};

public boolean isAutoRegister() {
return true;
Expand All @@ -35,6 +37,9 @@ public String transform(ICacheContext context, TransformColumn column,
if (varName != null) {
if (varName.equalsIgnoreCase(OPTION_TIMESTAMP)) {
return DateFormatUtils.format(System.currentTimeMillis(), DATE_PATTERN);
} else if (varName.equalsIgnoreCase(OPTION_IDENTITY)) {
context.getContextCache().put(OPTION_IDENTITY, Boolean.TRUE);
throw new IgnoreColumnException();
}
}
return null;
Expand Down

0 comments on commit 108b010

Please sign in to comment.