001: package net.sourceforge.squirrel_sql.plugins.informix.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.awt.BorderLayout;
022: import java.sql.PreparedStatement;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025:
026: import javax.swing.JTextArea;
027:
028: import net.sourceforge.squirrel_sql.client.session.ISession;
029: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseSourcePanel;
030: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseSourceTab;
031: import net.sourceforge.squirrel_sql.fw.codereformat.CodeReformator;
032: import net.sourceforge.squirrel_sql.fw.codereformat.CommentSpec;
033: import net.sourceforge.squirrel_sql.fw.util.StringManager;
034: import net.sourceforge.squirrel_sql.fw.util.StringManagerFactory;
035: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
036: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
037:
038: /**
039: *
040: * @author manningr
041: *
042: */
043: public abstract class InformixSourceTab extends BaseSourceTab {
044:
045: @SuppressWarnings("unused")
046: private static final StringManager s_stringMgr = StringManagerFactory
047: .getStringManager(InformixSourceTab.class);
048:
049: public static final int VIEW_TYPE = 0;
050: public static final int STORED_PROC_TYPE = 1;
051: public static final int TRIGGER_TYPE = 2;
052:
053: protected int sourceType = VIEW_TYPE;
054:
055: /** Logger for this class. */
056: private final static ILogger s_log = LoggerController
057: .createLogger(InformixSourceTab.class);
058:
059: private static CommentSpec[] commentSpecs = new CommentSpec[] {
060: new CommentSpec("/*", "*/"), new CommentSpec("--", "\n") };
061:
062: private static CodeReformator formatter = new CodeReformator(";",
063: commentSpecs);
064:
065: public InformixSourceTab(String hint) {
066: super (hint);
067: super .setSourcePanel(new InformixSourcePanel());
068: }
069:
070: private final class InformixSourcePanel extends BaseSourcePanel {
071: private static final long serialVersionUID = 1L;
072:
073: private JTextArea _ta;
074:
075: InformixSourcePanel() {
076: super (new BorderLayout());
077: createUserInterface();
078: }
079:
080: public void load(ISession session, PreparedStatement stmt) {
081: _ta.setText("");
082: _ta.setWrapStyleWord(true);
083: try {
084: ResultSet rs = stmt.executeQuery();
085: StringBuffer buf = new StringBuffer(4096);
086: int lastProcId = -1;
087: while (rs.next()) {
088: if (sourceType == STORED_PROC_TYPE) {
089: int tmpProcId = rs.getInt(1);
090: String tmpProcData = rs.getString(2);
091: if (lastProcId != tmpProcId) {
092: // First time through, skip the double spacing
093: if (lastProcId != -1) {
094: // double space since this is a new version of
095: // the stored procedure (overloading name with
096: // different parameters)
097: buf.append("\n\n");
098: }
099: lastProcId = tmpProcId;
100: }
101: buf.append(tmpProcData);
102: }
103: if (sourceType == TRIGGER_TYPE) {
104: String data = rs.getString(1);
105: buf.append(data);
106: }
107: if (sourceType == VIEW_TYPE) {
108: String line = rs.getString(1);
109: buf.append(line);
110: }
111: }
112: String trimmedSource = buf.toString().trim();
113: if (sourceType == VIEW_TYPE) {
114: if (s_log.isDebugEnabled()) {
115: s_log.debug("View source before formatting: "
116: + trimmedSource);
117: }
118: _ta.setText(formatter.reformat(trimmedSource));
119: } else if (sourceType == TRIGGER_TYPE) {
120: if (s_log.isDebugEnabled()) {
121: s_log
122: .debug("Trigger source before formatting: "
123: + trimmedSource);
124: }
125: _ta.setText(formatter.reformat(trimmedSource));
126: } else {
127: // Skip formatting for Stored Procedures - They can have
128: // comments embedded in them, and I'm presently not sure
129: // how the formatter handles this.
130: _ta.setText(trimmedSource);
131: }
132: _ta.setCaretPosition(0);
133: } catch (SQLException ex) {
134: session.showErrorMessage(ex);
135: }
136:
137: }
138:
139: private void createUserInterface() {
140: _ta = new JTextArea();
141: _ta.setEditable(false);
142: add(_ta, BorderLayout.CENTER);
143: }
144: }
145:
146: }
|