Search in sources :

Example 11 with Trigger

use of org.jumpmind.db.model.Trigger in project symmetric-ds by JumpMind.

the class OracleDdlReader method getTriggers.

public List<Trigger> getTriggers(final String catalog, final String schema, final String tableName) throws SqlException {
    List<Trigger> triggers = new ArrayList<Trigger>();
    log.debug("Reading triggers for: " + tableName);
    JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
    String sql = "SELECT * FROM ALL_TRIGGERS " + "WHERE TABLE_NAME=? and OWNER=?";
    triggers = sqlTemplate.query(sql, new ISqlRowMapper<Trigger>() {

        public Trigger mapRow(Row row) {
            Trigger trigger = new Trigger();
            trigger.setName(row.getString("TRIGGER_NAME"));
            trigger.setSchemaName(row.getString("OWNER"));
            trigger.setTableName(row.getString("TABLE_NAME"));
            trigger.setEnabled(Boolean.valueOf(row.getString("STATUS")));
            trigger.setSource("create ");
            String triggerType = row.getString("TRIGGERING_EVENT");
            if (triggerType.equals("DELETE") || triggerType.equals("INSERT") || triggerType.equals("UPDATE")) {
                trigger.setTriggerType(TriggerType.valueOf(triggerType));
            }
            trigger.setMetaData(row);
            return trigger;
        }
    }, tableName, schema);
    for (final Trigger trigger : triggers) {
        String name = trigger.getName();
        String sourceSql = "select TEXT from all_source " + "where NAME=? order by LINE ";
        sqlTemplate.query(sourceSql, new ISqlRowMapper<Trigger>() {

            public Trigger mapRow(Row row) {
                trigger.setSource(trigger.getSource() + "\n" + row.getString("TEXT"));
                ;
                return trigger;
            }
        }, name);
    }
    return triggers;
}
Also used : JdbcSqlTemplate(org.jumpmind.db.sql.JdbcSqlTemplate) Trigger(org.jumpmind.db.model.Trigger) ArrayList(java.util.ArrayList) Row(org.jumpmind.db.sql.Row) ISqlRowMapper(org.jumpmind.db.sql.ISqlRowMapper)

Example 12 with Trigger

use of org.jumpmind.db.model.Trigger in project symmetric-ds by JumpMind.

the class MsSqlDdlReader method getTriggers.

@Override
public List<Trigger> getTriggers(final String catalog, final String schema, final String tableName) throws SqlException {
    log.debug("Reading triggers for: " + tableName);
    JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
    String sql = "select " + "TRIG.name, " + "TAB.name as table_name, " + "SC.name as table_schema, " + "TRIG.is_disabled, " + "TRIG.is_ms_shipped, " + "TRIG.is_not_for_replication, " + "TRIG.is_instead_of_trigger, " + "TRIG.create_date, " + "TRIG.modify_date, " + "OBJECTPROPERTY(TRIG.OBJECT_ID, 'ExecIsUpdateTrigger') AS isupdate, " + "OBJECTPROPERTY(TRIG.OBJECT_ID, 'ExecIsDeleteTrigger') AS isdelete, " + "OBJECTPROPERTY(TRIG.OBJECT_ID, 'ExecIsInsertTrigger') AS isinsert, " + "OBJECTPROPERTY(TRIG.OBJECT_ID, 'ExecIsAfterTrigger') AS isafter, " + "OBJECTPROPERTY(TRIG.OBJECT_ID, 'ExecIsInsteadOfTrigger') AS isinsteadof, " + "TRIG.object_id, " + "TRIG.parent_id, " + "TAB.schema_id, " + "OBJECT_DEFINITION(TRIG.OBJECT_ID) as trigger_source " + "from sys.triggers as TRIG " + "inner join sys.tables as TAB " + "on TRIG.parent_id = TAB.object_id " + "inner join sys.schemas as SC " + "on TAB.schema_id = SC.schema_id " + "where TAB.name=? and SC.name=? ";
    return sqlTemplate.query(sql, new ISqlRowMapper<Trigger>() {

        public Trigger mapRow(Row row) {
            Trigger trigger = new Trigger();
            trigger.setName(row.getString("name"));
            trigger.setSchemaName(row.getString("table_schema"));
            trigger.setTableName(row.getString("table_name"));
            trigger.setEnabled(!Boolean.valueOf(row.getString("is_disabled")));
            trigger.setSource(row.getString("trigger_source"));
            row.remove("trigger_source");
            //replace 0 and 1s with true and false
            for (String s : new String[] { "isupdate", "isdelete", "isinsert", "isafter", "isinsteadof" }) {
                if (row.getString(s).equals("0"))
                    row.put(s, false);
                else
                    row.put(s, true);
            }
            if (row.getBoolean("isupdate"))
                trigger.setTriggerType(TriggerType.UPDATE);
            else if (row.getBoolean("isdelete"))
                trigger.setTriggerType(TriggerType.DELETE);
            else if (row.getBoolean("isinsert"))
                trigger.setTriggerType(TriggerType.INSERT);
            trigger.setMetaData(row);
            return trigger;
        }
    }, tableName, schema);
}
Also used : JdbcSqlTemplate(org.jumpmind.db.sql.JdbcSqlTemplate) Trigger(org.jumpmind.db.model.Trigger) Row(org.jumpmind.db.sql.Row)

Example 13 with Trigger

use of org.jumpmind.db.model.Trigger in project symmetric-ds by JumpMind.

the class SqliteDdlReader method getTriggers.

public List<Trigger> getTriggers(final String catalog, final String schema, final String tableName) throws SqlException {
    List<Trigger> triggers = new ArrayList<Trigger>();
    String sql = "SELECT " + "name AS trigger_name, " + "tbl_name AS table_name, " + "rootpage, " + "sql, " + "type AS object_type " + "FROM sqlite_master " + "WHERE table_name=? AND object_type='trigger';";
    triggers = platform.getSqlTemplate().query(sql, new ISqlRowMapper<Trigger>() {

        public Trigger mapRow(Row row) {
            Trigger trigger = new Trigger();
            trigger.setName(row.getString("trigger_name"));
            trigger.setTableName(row.getString("table_name"));
            trigger.setEnabled(true);
            trigger.setSource(row.getString("sql"));
            row.remove("sql");
            trigger.setMetaData(row);
            return trigger;
        }
    }, tableName.toLowerCase());
    return triggers;
}
Also used : Trigger(org.jumpmind.db.model.Trigger) ArrayList(java.util.ArrayList) Row(org.jumpmind.db.sql.Row) ISqlRowMapper(org.jumpmind.db.sql.ISqlRowMapper)

Example 14 with Trigger

use of org.jumpmind.db.model.Trigger in project symmetric-ds by JumpMind.

the class Db2DdlReader method getTriggers.

public List<Trigger> getTriggers(final String catalog, final String schema, final String tableName) throws SqlException {
    List<Trigger> triggers = new ArrayList<Trigger>();
    log.debug("Reading triggers for: " + tableName);
    JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
    String sql = "SELECT " + "NAME as TRIGGER_NAME, " + "SCHEMA, " + "DEFINER, " + "TBNAME as TABLE_NAME, " + "TBCREATOR as TABLE_CREATOR, " + "TRIGEVENT as TRIGGER_TYPE, " + "TRIGTIME as TRIGGER_TIME, " + "GRANULARITY, " + "VALID, " + "TEXT, " + "ENABLED, " + "CREATE_TIME, " + "FUNC_PATH as FUNCTION_PATH, " + "ALTER_TIME as LAST_ALTERED " + "FROM SYSIBM.SYSTRIGGERS " + "WHERE TBNAME=? and SCHEMA=?";
    triggers = sqlTemplate.query(sql, new ISqlRowMapper<Trigger>() {

        public Trigger mapRow(Row row) {
            Trigger trigger = new Trigger();
            trigger.setName(row.getString("TRIGGER_NAME"));
            trigger.setSchemaName(row.getString("SCHEMA"));
            trigger.setTableName(row.getString("TABLE_NAME"));
            trigger.setEnabled(row.getString("ENABLED").equals("Y"));
            trigger.setSource(row.getString("TEXT"));
            row.remove("TEXT");
            String trigEvent = row.getString("TRIGGER_TYPE");
            switch(trigEvent.charAt(0)) {
                case ('I'):
                    trigEvent = "INSERT";
                    break;
                case ('U'):
                    trigEvent = "UPDATE";
                    break;
                case ('D'):
                    trigEvent = "DELETE";
            }
            trigger.setTriggerType(TriggerType.valueOf(trigEvent));
            row.put("TRIGGER_TYPE", trigEvent);
            switch(row.getString("TRIGGER_TIME").charAt(0)) {
                case ('A'):
                    row.put("TRIGGER_TIME", "AFTER");
                    break;
                case ('B'):
                    row.put("TRIGGER_TIME", "BEFORE");
                    break;
                case ('I'):
                    row.put("TRIGGER_TIME", "INSTEAD OF");
            }
            if (row.getString("GRANULARITY").equals("S"))
                row.put("GRANULARITY", "ONCE PER STATEMENT");
            else if (row.getString("GRANULARITY").equals("R"))
                row.put("GRANULARITY", "ONCE PER ROW");
            trigger.setMetaData(row);
            return trigger;
        }
    }, tableName, schema);
    return triggers;
}
Also used : JdbcSqlTemplate(org.jumpmind.db.sql.JdbcSqlTemplate) Trigger(org.jumpmind.db.model.Trigger) ArrayList(java.util.ArrayList) Row(org.jumpmind.db.sql.Row) ISqlRowMapper(org.jumpmind.db.sql.ISqlRowMapper)

Example 15 with Trigger

use of org.jumpmind.db.model.Trigger in project symmetric-ds by JumpMind.

the class Db2zOsDdlReader method getTriggers.

public List<Trigger> getTriggers(final String catalog, final String schema, final String tableName) throws SqlException {
    List<Trigger> triggers = new ArrayList<Trigger>();
    log.debug("Reading triggers for: " + tableName);
    JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
    String sql = "SELECT * FROM SYSCAT.TRIGGERS " + "WHERE TABNAME=? and TABSCHEMA=?";
    triggers = sqlTemplate.query(sql, new ISqlRowMapper<Trigger>() {

        public Trigger mapRow(Row row) {
            Trigger trigger = new Trigger();
            trigger.setName(row.getString("TRIGNAME"));
            trigger.setSchemaName(row.getString("TRIGSCHEMA"));
            trigger.setTableName(row.getString("TABNAME"));
            trigger.setEnabled(true);
            trigger.setSource(row.getString("TEXT"));
            row.remove("TEXT");
            switch(row.getString("TRIGEVENT").charAt(0)) {
                case ('I'):
                    row.put("TRIGEVENT", "INSERT");
                    break;
                case ('U'):
                    row.put("TRIGEVENT", "UPDATE");
                    break;
                case ('D'):
                    row.put("TRIGEVENT", "DELETE");
            }
            trigger.setTriggerType(TriggerType.valueOf(row.getString("TRIGEVENT")));
            switch(row.getString("TRIGTIME").charAt(0)) {
                case ('A'):
                    row.put("TRIGTIME", "AFTER");
                    break;
                case ('B'):
                    row.put("TRIGTIME", "BEFORE");
                    break;
                case ('I'):
                    row.put("TRIGTIME", "INSTEAD OF");
            }
            trigger.setMetaData(row);
            return trigger;
        }
    }, tableName, schema);
    return triggers;
}
Also used : JdbcSqlTemplate(org.jumpmind.db.sql.JdbcSqlTemplate) Trigger(org.jumpmind.db.model.Trigger) ArrayList(java.util.ArrayList) Row(org.jumpmind.db.sql.Row) ISqlRowMapper(org.jumpmind.db.sql.ISqlRowMapper)

Aggregations

Trigger (org.jumpmind.db.model.Trigger)21 Row (org.jumpmind.db.sql.Row)19 ArrayList (java.util.ArrayList)18 ISqlRowMapper (org.jumpmind.db.sql.ISqlRowMapper)18 JdbcSqlTemplate (org.jumpmind.db.sql.JdbcSqlTemplate)18