001: package net.sourceforge.squirrel_sql.plugins.oracle.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.awt.BorderLayout;
022: import java.sql.PreparedStatement;
023: import java.sql.ResultSet;
024: import java.sql.SQLException;
025: import java.util.Arrays;
026: import java.util.List;
027:
028: import javax.swing.JTextArea;
029:
030: import net.sourceforge.squirrel_sql.client.session.ISession;
031: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseSourcePanel;
032: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseSourceTab;
033: import net.sourceforge.squirrel_sql.fw.codereformat.CodeReformator;
034: import net.sourceforge.squirrel_sql.fw.codereformat.CommentSpec;
035: import net.sourceforge.squirrel_sql.fw.dialects.CreateScriptPreferences;
036: import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
037: import net.sourceforge.squirrel_sql.fw.dialects.HibernateDialect;
038: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
039: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
040: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
041: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
042:
043: /**
044: * This class is only responsible for formatting source statements for Oracle
045: * views.
046: *
047: * @author manningr
048: */
049: public abstract class OracleSourceTab extends BaseSourceTab {
050:
051: public static final int VIEW_TYPE = 0;
052: public static final int STORED_PROC_TYPE = 1;
053: public static final int TRIGGER_TYPE = 2;
054: public static final int TABLE_TYPE = 3;
055:
056: protected int sourceType = VIEW_TYPE;
057:
058: /** Logger for this class. */
059: private final static ILogger s_log = LoggerController
060: .createLogger(OracleSourceTab.class);
061:
062: private static CommentSpec[] commentSpecs = new CommentSpec[] {
063: new CommentSpec("/*", "*/"), new CommentSpec("--", "\n") };
064:
065: private static CodeReformator formatter = new CodeReformator(";",
066: commentSpecs);
067:
068: public OracleSourceTab(String hint) {
069: super (hint);
070: super .setSourcePanel(new OracleSourcePanel());
071: }
072:
073: private final class OracleSourcePanel extends BaseSourcePanel {
074: private static final long serialVersionUID = 7855991042669454322L;
075:
076: private JTextArea _ta;
077:
078: OracleSourcePanel() {
079: super (new BorderLayout());
080: createUserInterface();
081: }
082:
083: public void load(ISession session, PreparedStatement stmt) {
084: _ta.setText("");
085: _ta.setWrapStyleWord(true);
086: try {
087: ResultSet rs = stmt.executeQuery();
088: StringBuffer buf = new StringBuffer(4096);
089: while (rs.next()) {
090: String line1 = rs.getString(1);
091: String line2 = rs.getString(2);
092: buf.append(line1.trim() + " ");
093: buf.append(line2.trim() + " ");
094: }
095: String source = "";
096: if (buf.length() == 0 && sourceType == TABLE_TYPE) {
097: ISQLDatabaseMetaData md = session.getMetaData();
098: // TODO: Need to define a better approach to getting dialects.
099: // That is, we don't really want to ever prompt the user in this
100: // case. It's always Oracle. Yet, we may have a new OracleDialect
101: // at some point.
102: HibernateDialect dialect = DialectFactory
103: .getDialect("Oracle");
104:
105: // TODO: How to let the user customize this??
106: CreateScriptPreferences prefs = new CreateScriptPreferences();
107:
108: ITableInfo[] tabs = new ITableInfo[] { (ITableInfo) getDatabaseObjectInfo() };
109: List<ITableInfo> tables = Arrays.asList(tabs);
110: // Handle table source
111: List<String> sqls = dialect.getCreateTableSQL(
112: tables, md, prefs, false);
113: String sep = session.getQueryTokenizer()
114: .getSQLStatementSeparator();
115: for (String sql : sqls) {
116: buf.append(sql);
117: buf.append(sep);
118: buf.append("\n");
119: }
120: source = buf.toString();
121: } else {
122: if (s_log.isDebugEnabled()) {
123: s_log.debug("View source before formatting: "
124: + buf.toString());
125: }
126: source = formatter.reformat(buf.toString());
127: }
128: _ta.setText(source);
129: _ta.setCaretPosition(0);
130: } catch (SQLException ex) {
131: s_log.error("Unexpected exception: " + ex.getMessage(),
132: ex);
133: session.showErrorMessage(ex);
134: }
135:
136: }
137:
138: private void createUserInterface() {
139: _ta = new JTextArea();
140: _ta.setEditable(false);
141: add(_ta, BorderLayout.CENTER);
142: }
143: }
144:
145: }
|