Search in sources :

Example 1 with PostgreGenericTrigger

use of org.jkiss.dbeaver.ext.postgresql.model.PostgreGenericTrigger in project dbeaver by serge-rider.

the class PostgreMetaModel method loadTriggers.

@Override
public List<PostgreGenericTrigger> loadTriggers(DBRProgressMonitor monitor, @NotNull GenericStructContainer container, @Nullable GenericTable table) throws DBException {
    try (JDBCSession session = DBUtils.openMetaSession(monitor, container.getDataSource(), "Read triggers")) {
        StringBuilder sql = new StringBuilder();
        sql.append("SELECT trigger_name,event_manipulation,action_order,action_condition,action_statement,action_orientation,action_timing\n" + "FROM INFORMATION_SCHEMA.TRIGGERS\n" + "WHERE ");
        if (table == null) {
            sql.append("trigger_schema=? AND event_object_table IS NULL");
        } else {
            sql.append("event_object_schema=? AND event_object_table=?");
        }
        try (JDBCPreparedStatement dbStat = session.prepareStatement(sql.toString())) {
            if (table == null) {
                dbStat.setString(1, container.getSchema().getName());
            } else {
                dbStat.setString(1, table.getSchema().getName());
                dbStat.setString(2, table.getName());
            }
            Map<String, PostgreGenericTrigger> result = new LinkedHashMap<>();
            try (JDBCResultSet dbResult = dbStat.executeQuery()) {
                while (dbResult.next()) {
                    String name = JDBCUtils.safeGetString(dbResult, "trigger_name");
                    if (name == null) {
                        continue;
                    }
                    String manipulation = JDBCUtils.safeGetString(dbResult, "event_manipulation");
                    PostgreGenericTrigger trigger = result.get(name);
                    if (trigger != null) {
                        trigger.addManipulation(manipulation);
                        continue;
                    }
                    String description = "";
                    trigger = new PostgreGenericTrigger(container, table, name, description, manipulation, JDBCUtils.safeGetString(dbResult, "action_orientation"), JDBCUtils.safeGetString(dbResult, "action_timing"), JDBCUtils.safeGetString(dbResult, "action_statement"));
                    result.put(name, trigger);
                }
            }
            return new ArrayList<>(result.values());
        }
    } catch (SQLException e) {
        throw new DBException(e, container.getDataSource());
    }
}
Also used : JDBCPreparedStatement(org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement) JDBCSession(org.jkiss.dbeaver.model.exec.jdbc.JDBCSession) DBException(org.jkiss.dbeaver.DBException) SQLException(java.sql.SQLException) JDBCResultSet(org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet) ArrayList(java.util.ArrayList) PostgreGenericTrigger(org.jkiss.dbeaver.ext.postgresql.model.PostgreGenericTrigger) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

SQLException (java.sql.SQLException)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 DBException (org.jkiss.dbeaver.DBException)1 PostgreGenericTrigger (org.jkiss.dbeaver.ext.postgresql.model.PostgreGenericTrigger)1 JDBCPreparedStatement (org.jkiss.dbeaver.model.exec.jdbc.JDBCPreparedStatement)1 JDBCResultSet (org.jkiss.dbeaver.model.exec.jdbc.JDBCResultSet)1 JDBCSession (org.jkiss.dbeaver.model.exec.jdbc.JDBCSession)1