001: package net.sourceforge.squirrel_sql.plugins.postgres.tab;
002:
003: /*
004: * Copyright (C) 2007 Rob Manning
005: * manningr@users.sourceforge.net
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public
018: * License along with this library; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: */
021: import java.sql.PreparedStatement;
022: import java.sql.SQLException;
023:
024: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
025: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
026: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
027: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
028:
029: import net.sourceforge.squirrel_sql.client.session.ISession;
030:
031: /**
032: * This class will display the source for an PostgreSQL trigger.
033: *
034: * @author manningr
035: */
036: public class TriggerSourceTab extends PostgresSourceTab {
037: /** SQL that retrieves the source of a stored procedure. */
038: private static String SQL = "select 'create trigger ' || t.trigger_name || ' ' || condition_timing "
039: + "|| ' ' || v.manip || ' on ' || event_object_table "
040: + "|| ' for each ' || action_orientation || ' ' || action_statement as trigdef "
041: + "FROM information_schema.triggers t, "
042: + "(select trigger_schema, "
043: + " trigger_name, "
044: + " rtrim( "
045: + " max(case when pos=0 then manip else '' end)|| "
046: + " max(case when pos=1 then manip else '' end)|| "
047: + " max(case when pos=2 then manip else '' end), ' or ' "
048: + " ) as manip "
049: + "from ( "
050: + " select a.trigger_schema, "
051: + " a.trigger_name, "
052: + " a.event_manipulation||' or ' as manip, "
053: + " d.cnt, "
054: + " a.rnk as pos "
055: + " from ( select trigger_name, "
056: + " trigger_schema, "
057: + " event_manipulation, "
058: + " (select count(distinct is1.event_manipulation) "
059: + " from information_schema.triggers is1 "
060: + " where is2.event_manipulation < is1.event_manipulation) as rnk "
061: + " from information_schema.triggers is2 "
062: + " ) a, "
063: + " (select trigger_schema, trigger_name, count(event_manipulation) as cnt "
064: + " from ( "
065: + " select trigger_schema, "
066: + " trigger_name, "
067: + " event_manipulation, "
068: + " (select count(distinct is3.event_manipulation) "
069: + " from information_schema.triggers is3 "
070: + " where is4.event_manipulation < is3.event_manipulation) as rnk "
071: + " from information_schema.triggers is4 "
072: + " ) y "
073: + " group by trigger_schema, trigger_name) d "
074: + " where d.trigger_name = a.trigger_name "
075: + " and d.trigger_schema = a.trigger_schema "
076: + ") x "
077: + "group by trigger_schema, trigger_name "
078: + "order by 1) v "
079: + "where t.trigger_schema = v.trigger_schema "
080: + "and t.trigger_name = v.trigger_name "
081: + "and t.trigger_schema = ? "
082: + "and t.trigger_name = ? "
083: + "group by trigdef ";
084:
085: /** Logger for this class. */
086: private final static ILogger s_log = LoggerController
087: .createLogger(TriggerSourceTab.class);
088:
089: public TriggerSourceTab(String hint) {
090: super (hint);
091: sourceType = TRIGGER_TYPE;
092: }
093:
094: protected PreparedStatement createStatement() throws SQLException {
095: final ISession session = getSession();
096: final IDatabaseObjectInfo doi = getDatabaseObjectInfo();
097:
098: if (s_log.isDebugEnabled()) {
099: s_log.debug("Running SQL: " + SQL);
100: s_log.debug("schema=" + doi.getSchemaName());
101: s_log.debug("trigname=" + doi.getSimpleName());
102: }
103: ISQLConnection conn = session.getSQLConnection();
104: PreparedStatement pstmt = conn.prepareStatement(SQL);
105: pstmt.setString(1, doi.getSchemaName());
106: pstmt.setString(2, doi.getSimpleName());
107: return pstmt;
108: }
109: }
|