Skip to content

Commit

Permalink
0000811: H2 trigger template doesn't resolve schema
Browse files Browse the repository at this point in the history
  • Loading branch information
chenson42 committed Sep 12, 2012
1 parent 5c3becb commit 88a41b2
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 15 deletions.
Expand Up @@ -55,6 +55,7 @@ abstract public class AbstractEmbeddedTrigger {
protected static final String KEY_INSERT_DATA_SQL = "INSERT_DATA_SQL";
protected static final String TEMPLATE_TABLE_SUFFIX = "_CONFIG";
protected String triggerName;
protected String schemaName;
protected Map<String, String> templates = null;

/**
Expand All @@ -65,11 +66,13 @@ abstract public class AbstractEmbeddedTrigger {
* a connection to the database
* @param triggerName
* the name of the trigger used in the CREATE TRIGGER statement
* @param schemaName TODO
* @param tableName
* the name of the table
*/
protected void init(Connection conn, String triggerName, String tableName) throws SQLException {
protected void init(Connection conn, String triggerName, String schemaName, String tableName) throws SQLException {
if (this.templates == null) {
this.schemaName = schemaName;
this.triggerName = triggerName;
this.templates = getTemplates(conn);
}
Expand Down Expand Up @@ -249,7 +252,8 @@ protected String escapeString(Object val) {
protected Map<String, String> getTemplates(Connection conn) throws SQLException {
Map<String, String> templates = new HashMap<String, String>();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(String.format("select * from %s%s", triggerName, TEMPLATE_TABLE_SUFFIX));
String schemaPrefix = schemaName != null && schemaName.length() > 0 ? schemaName+"." : "";
ResultSet rs = stmt.executeQuery(String.format("select * from %s%s%s", schemaPrefix, triggerName, TEMPLATE_TABLE_SUFFIX));
if (rs.next()) {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
Expand Down
Expand Up @@ -20,6 +20,7 @@

package org.jumpmind.symmetric.db.h2;

import org.jumpmind.db.model.Table;
import org.jumpmind.db.platform.IDatabasePlatform;
import org.jumpmind.db.sql.ISqlTransaction;
import org.jumpmind.db.util.BinaryEncoding;
Expand Down Expand Up @@ -58,10 +59,11 @@ protected boolean doesTriggerExistOnPlatform(String catalogName, String schemaNa
@Override
public void removeTrigger(StringBuilder sqlBuffer, String catalogName, String schemaName, String triggerName,
String tableName, TriggerHistory oldHistory) {
final String dropSql = String.format("DROP TRIGGER IF EXISTS %s", triggerName);
String prefix = Table.getQualifiedTablePrefix(catalogName, schemaName, getPlatform().getDatabaseInfo().getDelimiterToken());
final String dropSql = String.format("DROP TRIGGER IF EXISTS %s%s", prefix, triggerName);
logSql(dropSql, sqlBuffer);

final String dropTable = String.format("DROP TABLE IF EXISTS %s_CONFIG", triggerName);
final String dropTable = String.format("DROP TABLE IF EXISTS %s%s_CONFIG", prefix, triggerName);
logSql(dropTable, sqlBuffer);

if (parameterService.is(ParameterConstants.AUTO_SYNC_TRIGGERS)) {
Expand Down
Expand Up @@ -47,7 +47,7 @@ public class H2Trigger extends AbstractEmbeddedTrigger implements org.h2.api.Tri
*/
public void init(Connection conn, String schemaName, String triggerName, String tableName, boolean before, int type)
throws SQLException {
this.init(conn, triggerName, tableName);
this.init(conn, triggerName, schemaName, tableName);
}

public void close() throws SQLException {
Expand Down
Expand Up @@ -35,29 +35,29 @@ public H2TriggerTemplate(ISymmetricDialect symmetricDialect) {

sqlTemplates = new HashMap<String,String>();
sqlTemplates.put("insertTriggerTemplate" ,
"CREATE TABLE $(triggerName)_CONFIG (CONDITION_SQL CLOB, INSERT_DATA_SQL CLOB); " +
" INSERT INTO $(triggerName)_CONFIG values( " +
"CREATE TABLE $(schemaName)$(triggerName)_CONFIG (CONDITION_SQL CLOB, INSERT_DATA_SQL CLOB); " +
" INSERT INTO $(schemaName)$(triggerName)_CONFIG values( " +
" 'select count(*) from $(virtualOldNewTable) where $(syncOnInsertCondition) and $(syncOnIncomingBatchCondition)', " +
" 'insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, row_data, channel_id, transaction_id, source_node_id, external_data, create_time)" +
" (select ''$(targetTableName)'',''I'',$(triggerHistoryId),$(columns), ''$(channelName)'', $(txIdExpression), @node_value, $(externalSelect), CURRENT_TIMESTAMP from $(virtualOldNewTable))' " +
" ); " +
" CREATE TRIGGER $(triggerName) AFTER INSERT ON $(tableName) FOR EACH ROW CALL \"org.jumpmind.symmetric.db.h2.H2Trigger\"; " );
" CREATE TRIGGER $(schemaName)$(triggerName) AFTER INSERT ON $(schemaName)$(tableName) FOR EACH ROW CALL \"org.jumpmind.symmetric.db.h2.H2Trigger\"; " );
sqlTemplates.put("updateTriggerTemplate" ,
"CREATE TABLE $(triggerName)_CONFIG (CONDITION_SQL CLOB, INSERT_DATA_SQL CLOB); " +
" INSERT INTO $(triggerName)_CONFIG values( " +
"CREATE TABLE $(schemaName)$(triggerName)_CONFIG (CONDITION_SQL CLOB, INSERT_DATA_SQL CLOB); " +
" INSERT INTO $(schemaName)$(triggerName)_CONFIG values( " +
" 'select count(*) from $(virtualOldNewTable) where $(syncOnUpdateCondition) and $(syncOnIncomingBatchCondition)', " +
" 'insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, pk_data, row_data, old_data, channel_id, transaction_id, source_node_id, external_data, create_time)" +
" (select ''$(targetTableName)'',''U'',$(triggerHistoryId),$(oldKeys),$(columns),$(oldColumns), ''$(channelName)'', $(txIdExpression), @node_value, $(externalSelect), CURRENT_TIMESTAMP from $(virtualOldNewTable))'" +
" ); " +
" CREATE TRIGGER $(triggerName) AFTER UPDATE ON $(tableName) FOR EACH ROW CALL \"org.jumpmind.symmetric.db.h2.H2Trigger\"; " );
" CREATE TRIGGER $(schemaName)$(triggerName) AFTER UPDATE ON $(schemaName)$(tableName) FOR EACH ROW CALL \"org.jumpmind.symmetric.db.h2.H2Trigger\"; " );
sqlTemplates.put("deleteTriggerTemplate" ,
"CREATE TABLE $(triggerName)_CONFIG (CONDITION_SQL CLOB, INSERT_DATA_SQL CLOB); " +
" INSERT INTO $(triggerName)_CONFIG values( " +
"CREATE TABLE $(schemaName)$(triggerName)_CONFIG (CONDITION_SQL CLOB, INSERT_DATA_SQL CLOB); " +
" INSERT INTO $(schemaName)$(triggerName)_CONFIG values( " +
" 'select count(*) from $(virtualOldNewTable) where $(syncOnDeleteCondition) and $(syncOnIncomingBatchCondition)', " +
" 'insert into $(defaultSchema)$(prefixName)_data (table_name, event_type, trigger_hist_id, pk_data, old_data, channel_id, transaction_id, source_node_id, external_data, create_time)" +
" (select ''$(targetTableName)'',''D'',$(triggerHistoryId),$(oldKeys),$(oldColumns),''$(channelName)'', $(txIdExpression), @node_value, $(externalSelect), CURRENT_TIMESTAMP from $(virtualOldNewTable))'" +
" ); " +
" CREATE TRIGGER $(triggerName) AFTER DELETE ON $(tableName) FOR EACH ROW CALL \"org.jumpmind.symmetric.db.h2.H2Trigger\"; " );
" CREATE TRIGGER $(schemaName)$(triggerName) AFTER DELETE ON $(schemaName)$(tableName) FOR EACH ROW CALL \"org.jumpmind.symmetric.db.h2.H2Trigger\"; " );
sqlTemplates.put("initialLoadSqlTemplate" ,
"select $(columns) from $(schemaName)$(tableName) t where $(whereClause) " );
}
Expand Down
Expand Up @@ -48,7 +48,7 @@ public void fire(int type, String triggerName, String tableName, Object[] oldRow
Connection conn = findConnection(triggerName);
if (conn != null) {
try {
init(conn, triggerName, tableName);
init(conn, triggerName, null, tableName);
fire(conn, oldRow, newRow);
} catch (SQLException e) {
e.printStackTrace();
Expand Down

0 comments on commit 88a41b2

Please sign in to comment.