001: package net.sourceforge.squirrel_sql.plugins.db2.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 static java.util.Arrays.asList;
022:
023: import java.sql.PreparedStatement;
024: import java.sql.SQLException;
025: import java.util.List;
026:
027: import net.sourceforge.squirrel_sql.client.session.ISession;
028: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.FormattedSourceTab;
029: import net.sourceforge.squirrel_sql.fw.dialects.CreateScriptPreferences;
030: import net.sourceforge.squirrel_sql.fw.dialects.DialectFactory;
031: import net.sourceforge.squirrel_sql.fw.dialects.HibernateDialect;
032: import net.sourceforge.squirrel_sql.fw.dialects.UnknownDialectException;
033: import net.sourceforge.squirrel_sql.fw.sql.DatabaseObjectType;
034: import net.sourceforge.squirrel_sql.fw.sql.IDatabaseObjectInfo;
035: import net.sourceforge.squirrel_sql.fw.sql.ISQLConnection;
036: import net.sourceforge.squirrel_sql.fw.sql.ISQLDatabaseMetaData;
037: import net.sourceforge.squirrel_sql.fw.sql.ITableInfo;
038: import net.sourceforge.squirrel_sql.fw.sql.TableInfo;
039: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
040: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
041:
042: /**
043: * This class will display the source for a DB2 table (including MQTs).
044: *
045: * @author manningr
046: */
047: public class TableSourceTab extends FormattedSourceTab {
048: /** SQL that retrieves the source of a MQT. */
049: private static final String MQT_SQL = "SELECT text "
050: + "FROM SYSCAT.VIEWS " + "WHERE viewschema = ? "
051: + "and viewname = ? ";
052:
053: /** SQL that retrieves the source of a MQT on OS/400 */
054: private static final String OS400_MQT_SQL = "select mqt_definition "
055: + "from qsys2.systables "
056: + "where table_schema = ? "
057: + "and table_name = ? ";
058:
059: /** Logger for this class. */
060: private final static ILogger s_log = LoggerController
061: .createLogger(ViewSourceTab.class);
062:
063: /** boolean to indicate whether or not this session is OS/400 */
064: private boolean isOS400 = false;
065:
066: /**
067: * Constructor
068: *
069: * @param isOS400 whether or not we are connected to an OS/400 system
070: */
071: public TableSourceTab(String hint, String stmtSep, boolean isOS400) {
072: super (hint);
073: super .setCompressWhitespace(true);
074: super .setupFormatter(stmtSep, null);
075: this .isOS400 = isOS400;
076: }
077:
078: /**
079: * @see net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseSourceTab#createStatement()
080: */
081: @Override
082: protected PreparedStatement createStatement() throws SQLException {
083: final ISession session = getSession();
084: final IDatabaseObjectInfo doi = getDatabaseObjectInfo();
085:
086: ISQLConnection conn = session.getSQLConnection();
087: String sql = MQT_SQL;
088: if (isOS400) {
089: sql = OS400_MQT_SQL;
090: }
091: if (!isMQT()) {
092: sql = getTableSelectSql((ITableInfo) doi);
093:
094: // we may have more than one statement in sql at this point
095: super .appendSeparator = false;
096: } else {
097: // MQTs only ever have one sql statement
098: super .appendSeparator = true;
099: }
100: if (s_log.isDebugEnabled()) {
101: s_log.debug("Running SQL for View source tab: " + sql);
102: s_log.debug("schema=" + doi.getSchemaName());
103: s_log.debug("view name=" + doi.getSimpleName());
104: }
105: PreparedStatement pstmt = conn.prepareStatement(sql);
106: if (isMQT()) {
107: pstmt.setString(1, doi.getSchemaName());
108: pstmt.setString(2, doi.getSimpleName());
109: }
110: return pstmt;
111: }
112:
113: private boolean isMQT() {
114: final IDatabaseObjectInfo doi = getDatabaseObjectInfo();
115:
116: boolean isMQT = false;
117:
118: if (doi.getDatabaseObjectType() == DatabaseObjectType.TABLE) {
119: TableInfo info = (TableInfo) doi;
120: if (info.getType().startsWith("MATERIALIZED")) {
121: isMQT = true;
122: if (s_log.isDebugEnabled()) {
123: s_log.debug("Table " + doi.getSimpleName()
124: + " appears to be an MQT");
125: }
126: } else {
127: if (s_log.isDebugEnabled()) {
128: s_log.debug("Table " + doi.getSimpleName()
129: + " appears to be a regular table");
130: }
131: }
132: }
133:
134: return isMQT;
135: }
136:
137: private String getTableSelectSql(ITableInfo ti) {
138: String sql = getRegularTableSelectSql(ti);
139: if (sql == null) {
140: sql = MQT_SQL;
141: }
142: return sql;
143: }
144:
145: /**
146: * This builds a create statement
147: * @param ti
148: * @return
149: */
150: private String getRegularTableSelectSql(ITableInfo ti) {
151: StringBuilder tmp = new StringBuilder();
152: tmp.append("select '");
153:
154: ISQLDatabaseMetaData md = getSession().getMetaData();
155: try {
156: HibernateDialect dialect = DialectFactory.getDialect(md);
157: List<ITableInfo> tableList = asList(new ITableInfo[] { ti });
158: CreateScriptPreferences prefs = new CreateScriptPreferences();
159: List<String> sqls = dialect.getCreateTableSQL(tableList,
160: md, prefs, false);
161: for (String sql : sqls) {
162: tmp.append(sql);
163: tmp.append(statementSeparator);
164: tmp.append("\n");
165: tmp.append("\n");
166: }
167: } catch (UnknownDialectException e) {
168: s_log
169: .error("createStatement: Unable to determine the dialect "
170: + "to use");
171: return null;
172: } catch (SQLException e) {
173: s_log.error("createStatement: Unexpected exception while "
174: + "constructing SQL for table("
175: + ti.getSimpleName() + "): " + e.getMessage(), e);
176: return null;
177: }
178: tmp.append("' from sysibm.sysdummy1");
179: return tmp.toString();
180: }
181: }
|