001: package net.sourceforge.squirrel_sql.plugins.derby.tab;
002:
003: /*
004: * Copyright (C) 2006 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.client.session.ISession;
025: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseSourceTab;
026: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
027: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
028: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
029: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
030:
031: /**
032: * This class will display the source for a Derby trigger.
033: *
034: * @author manningr
035: */
036: public class TriggerSourceTab extends BaseSourceTab {
037: /** SQL that retrieves the source of a stored procedure. */
038: private static String SQL = "select 'CREATE TRIGGER ' || t.TRIGGERNAME||' \n' "
039: + " ||(select "
040: + " CASE "
041: + " WHEN t3.FIRINGTIME='B' THEN 'BEFORE' "
042: + " WHEN t3.FIRINGTIME='A' THEN 'AFTER' "
043: + " END "
044: + " from SYS.SYSTRIGGERS t3 "
045: + " where t.TRIGGERID = t3.TRIGGERID) "
046: + " || ' ' "
047: + " ||(select CASE "
048: + " WHEN t2.EVENT='U' THEN 'UPDATE' "
049: + " WHEN t2.EVENT='D' THEN 'DELETE' "
050: + " WHEN t2.EVENT='I' THEN 'INSERT' "
051: + " END "
052: + " from SYS.SYSTRIGGERS t2 "
053: + " where t.TRIGGERID = t2.TRIGGERID) "
054: + " ||' ON ' "
055: + " || ta.TABLENAME || ' \n'"
056: + " ||(select "
057: + " CASE "
058: + " WHEN t4.REFERENCINGOLD = 0 THEN '' "
059: + " WHEN t4.REFERENCINGOLD = 1 "
060: + " THEN ' REFERENCING OLD AS ' || t4.OLDREFERENCINGNAME || ' \n'"
061: + " END "
062: + " from SYS.SYSTRIGGERS t4 "
063: + " where t.TRIGGERID = t4.TRIGGERID) "
064: + " ||(select "
065: + " CASE "
066: + " WHEN t5.REFERENCINGNEW = 0 THEN '' "
067: + " WHEN t5.REFERENCINGNEW = 1 "
068: + " THEN ' REFERENCING NEW AS ' || t5.NEWREFERENCINGNAME || ' \n'"
069: + " END "
070: + " from SYS.SYSTRIGGERS t5 "
071: + " where t.TRIGGERID = t5.TRIGGERID) "
072: + " ||' FOR EACH ROW MODE DB2SQL \n' "
073: + " || t.triggerdefinition "
074: + "from SYS.SYSTRIGGERS t, SYS.SYSTABLES ta, SYS.SYSSCHEMAS s "
075: + "where t.TABLEID = ta.TABLEID "
076: + "and s.SCHEMAID = t.SCHEMAID "
077: + "and t.TRIGGERNAME = ? "
078: + "and s.SCHEMANAME = ? ";
079:
080: /** Logger for this class. */
081: private final static ILogger s_log = LoggerController
082: .createLogger(TriggerSourceTab.class);
083:
084: public TriggerSourceTab(String hint) {
085: super (hint);
086: }
087:
088: protected PreparedStatement createStatement() throws SQLException {
089: final ISession session = getSession();
090: final IDatabaseObjectInfo doi = getDatabaseObjectInfo();
091:
092: if (s_log.isDebugEnabled()) {
093: s_log.debug("Running SQL: " + SQL);
094: s_log.debug("Trigger Name=" + doi.getSimpleName());
095: s_log.debug("Schema Name=" + doi.getSchemaName());
096: }
097: ISQLConnection conn = session.getSQLConnection();
098: PreparedStatement pstmt = conn.prepareStatement(SQL);
099: pstmt.setString(1, doi.getSimpleName());
100: pstmt.setString(2, doi.getSchemaName());
101: return pstmt;
102: }
103: }
|