Search in sources :

Example 6 with TriggerExecutionContext

use of org.apache.derby.iapi.db.TriggerExecutionContext in project symmetric-ds by JumpMind.

the class DerbyFunctions method insertData.

public static void insertData(int enabled, String schemaName, String prefixName, String tableName, String channelName, String dmlType, int triggerHistId, String transactionId, String externalData, String columnNames, String pkColumnNames) throws SQLException {
    if (enabled == 1) {
        TriggerExecutionContext context = Factory.getTriggerExecutionContext();
        String rowData = null;
        String pkData = null;
        String oldData = null;
        String[] parsedColumnNames = StringUtils.splitPreserveAllTokens(columnNames, ',');
        String[] parsedPkColumnNames = StringUtils.splitPreserveAllTokens(pkColumnNames, ',');
        if (dmlType.equals("I") || dmlType.equals("U")) {
            StringBuilder dataBuilder = new StringBuilder();
            appendCsvString(tableName, parsedColumnNames, parsedPkColumnNames, context.getNewRow(), dataBuilder);
            rowData = dataBuilder.substring(0, dataBuilder.length() - 1);
        }
        if (dmlType.equals("U") || dmlType.equals("D")) {
            StringBuilder dataBuilder = new StringBuilder();
            appendCsvString(tableName, parsedColumnNames, parsedPkColumnNames, context.getOldRow(), dataBuilder);
            oldData = dataBuilder.substring(0, dataBuilder.length() - 1);
            dataBuilder = new StringBuilder();
            appendCsvString(tableName, parsedPkColumnNames, parsedPkColumnNames, context.getOldRow(), dataBuilder);
            pkData = dataBuilder.substring(0, dataBuilder.length() - 1);
        }
        Connection conn = DriverManager.getConnection(CURRENT_CONNECTION_URL);
        StringBuilder sql = new StringBuilder("insert into ");
        sql.append(schemaName);
        sql.append(prefixName);
        sql.append("_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) ");
        sql.append(" values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, current_timestamp)");
        PreparedStatement ps = conn.prepareStatement(sql.toString());
        ps.setString(1, tableName);
        ps.setString(2, dmlType);
        ps.setLong(3, triggerHistId);
        ps.setString(4, pkData);
        ps.setString(5, rowData);
        ps.setString(6, oldData);
        ps.setString(7, channelName);
        ps.setString(8, transactionId);
        ps.setString(9, getSyncNodeDisabled());
        ps.setString(10, externalData);
        ps.executeUpdate();
        ps.close();
        conn.close();
    }
}
Also used : Connection(java.sql.Connection) EmbedConnection(org.apache.derby.impl.jdbc.EmbedConnection) PreparedStatement(java.sql.PreparedStatement) TriggerExecutionContext(org.apache.derby.iapi.db.TriggerExecutionContext)

Aggregations

TriggerExecutionContext (org.apache.derby.iapi.db.TriggerExecutionContext)6 SQLException (java.sql.SQLException)2 Connection (java.sql.Connection)1 PreparedStatement (java.sql.PreparedStatement)1 EmbedConnection (org.apache.derby.impl.jdbc.EmbedConnection)1