Search in sources :

Example 11 with JdbcSqlTemplate

use of org.jumpmind.db.sql.JdbcSqlTemplate in project symmetric-ds by JumpMind.

the class RedshiftDdlReader method getTriggers.

public List<Trigger> getTriggers(final String catalog, final String schema, final String tableName) {
    List<Trigger> triggers = new ArrayList<Trigger>();
    log.debug("Reading triggers for: " + tableName);
    JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
    String sql = "SELECT " + "trigger_name, " + "trigger_schema, " + "trigger_catalog, " + "event_manipulation AS trigger_type, " + "event_object_table AS table_name," + "trig.*, " + "pgproc.prosrc " + "FROM INFORMATION_SCHEMA.TRIGGERS AS trig " + "INNER JOIN pg_catalog.pg_trigger AS pgtrig " + "ON pgtrig.tgname=trig.trigger_name " + "INNER JOIN pg_catalog.pg_proc AS pgproc " + "ON pgproc.oid=pgtrig.tgfoid " + "WHERE event_object_table=? AND event_object_schema=?;";
    triggers = sqlTemplate.query(sql, new ISqlRowMapper<Trigger>() {

        public Trigger mapRow(Row row) {
            Trigger trigger = new Trigger();
            trigger.setName(row.getString("trigger_name"));
            trigger.setCatalogName(row.getString("trigger_catalog"));
            trigger.setSchemaName(row.getString("trigger_schema"));
            trigger.setTableName(row.getString("table_name"));
            trigger.setEnabled(true);
            trigger.setSource(row.getString("prosrc"));
            row.remove("prosrc");
            String triggerType = row.getString("trigger_type");
            if (triggerType.equals("DELETE") || triggerType.equals("INSERT") || triggerType.equals("UPDATE")) {
                trigger.setTriggerType(TriggerType.valueOf(triggerType));
            }
            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 12 with JdbcSqlTemplate

use of org.jumpmind.db.sql.JdbcSqlTemplate 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 13 with JdbcSqlTemplate

use of org.jumpmind.db.sql.JdbcSqlTemplate 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 14 with JdbcSqlTemplate

use of org.jumpmind.db.sql.JdbcSqlTemplate in project symmetric-ds by JumpMind.

the class AbstractJdbcDdlReader method readTables.

/*
     * Reads the database model from the given connection.
     * 
     * @param catalog The catalog to access in the database; use
     * <code>null</code> for the default value
     * 
     * @param schema The schema to access in the database; use <code>null</code>
     * for the default value
     * 
     * @param tableTypes The table types to process; use <code>null</code> or an
     * empty list for the default ones
     * 
     * @return The database model
     */
public Database readTables(final String catalog, final String schema, final String[] tableTypes) {
    JdbcSqlTemplate sqlTemplate = (JdbcSqlTemplate) platform.getSqlTemplate();
    return postprocessModelFromDatabase(sqlTemplate.execute(new IConnectionCallback<Database>() {

        public Database execute(Connection connection) throws SQLException {
            Database db = new Database();
            db.setName(Table.getFullyQualifiedTablePrefix(catalog, schema));
            db.setCatalog(catalog);
            db.setSchema(schema);
            db.addTables(readTables(connection, catalog, schema, tableTypes));
            db.initialize();
            return db;
        }
    }));
}
Also used : JdbcSqlTemplate(org.jumpmind.db.sql.JdbcSqlTemplate) IConnectionCallback(org.jumpmind.db.sql.IConnectionCallback) Connection(java.sql.Connection) Database(org.jumpmind.db.model.Database)

Example 15 with JdbcSqlTemplate

use of org.jumpmind.db.sql.JdbcSqlTemplate in project symmetric-ds by JumpMind.

the class AbstractJdbcDatabasePlatform method createSqlTemplateDirty.

protected ISqlTemplate createSqlTemplateDirty() {
    JdbcSqlTemplate sqlTemplateDirty = new JdbcSqlTemplate(dataSource, settings, null, getDatabaseInfo());
    sqlTemplateDirty.setIsolationLevel(Connection.TRANSACTION_READ_UNCOMMITTED);
    return sqlTemplateDirty;
}
Also used : JdbcSqlTemplate(org.jumpmind.db.sql.JdbcSqlTemplate)

Aggregations

JdbcSqlTemplate (org.jumpmind.db.sql.JdbcSqlTemplate)23 ArrayList (java.util.ArrayList)19 Row (org.jumpmind.db.sql.Row)19 Trigger (org.jumpmind.db.model.Trigger)18 ISqlRowMapper (org.jumpmind.db.sql.ISqlRowMapper)18 Connection (java.sql.Connection)3 ResultSet (java.sql.ResultSet)2 IConnectionCallback (org.jumpmind.db.sql.IConnectionCallback)2 DatabaseMetaData (java.sql.DatabaseMetaData)1 SQLException (java.sql.SQLException)1 List (java.util.List)1 Database (org.jumpmind.db.model.Database)1 SqlException (org.jumpmind.db.sql.SqlException)1