001: package net.sourceforge.squirrel_sql.plugins.mysql.tab;
002:
003: /*
004: * Copyright (C) 2002-2003 Colin Bell
005: * colbell@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.Component;
022: import java.sql.ResultSet;
023: import java.sql.SQLException;
024: import java.sql.Statement;
025: import java.util.HashMap;
026: import java.util.Map;
027:
028: import net.sourceforge.squirrel_sql.client.session.ISession;
029: import net.sourceforge.squirrel_sql.client.session.mainpanel.objecttree.tabs.BaseObjectTab;
030: import net.sourceforge.squirrel_sql.client.session.properties.SessionProperties;
031: import net.sourceforge.squirrel_sql.fw.datasetviewer.ColumnDisplayDefinition;
032: import net.sourceforge.squirrel_sql.fw.datasetviewer.DataSetException;
033: import net.sourceforge.squirrel_sql.fw.datasetviewer.DataSetScrollingPanel;
034: import net.sourceforge.squirrel_sql.fw.datasetviewer.IDataSet;
035: import net.sourceforge.squirrel_sql.fw.datasetviewer.MapDataSet;
036: import net.sourceforge.squirrel_sql.fw.datasetviewer.ResultSetDataSet;
037: import net.sourceforge.squirrel_sql.fw.util.log.ILogger;
038: import net.sourceforge.squirrel_sql.fw.util.log.LoggerController;
039:
040: abstract class BaseSQLTab extends BaseObjectTab {
041: /** Title to display for tab. */
042: private final String _title;
043:
044: /** Hint to display for tab. */
045: private final String _hint;
046:
047: private boolean _firstRowOnly;
048:
049: /** Component to display in tab. */
050: private DataSetScrollingPanel _comp;
051:
052: /** Logger for this class. */
053: private final static ILogger s_log = LoggerController
054: .createLogger(BaseSQLTab.class);
055:
056: public BaseSQLTab(String title, String hint) {
057: this (title, hint, false);
058: }
059:
060: public BaseSQLTab(String title, String hint, boolean firstRowOnly) {
061: super ();
062: if (title == null) {
063: throw new IllegalArgumentException("Title == null");
064: }
065: _title = title;
066: _hint = hint != null ? hint : title;
067: _firstRowOnly = firstRowOnly;
068: }
069:
070: /**
071: * Return the title for the tab.
072: *
073: * @return The title for the tab.
074: */
075: public String getTitle() {
076: return _title;
077: }
078:
079: /**
080: * Return the hint for the tab.
081: *
082: * @return The hint for the tab.
083: */
084: public String getHint() {
085: return _hint;
086: }
087:
088: public void clear() {
089: }
090:
091: public Component getComponent() {
092: if (_comp == null) {
093: ISession session = getSession();
094: SessionProperties props = session.getProperties();
095: String destClassName = props.getMetaDataOutputClassName();
096: try {
097: _comp = new DataSetScrollingPanel(destClassName, null);
098: } catch (Exception e) {
099: s_log.error(
100: "Unexpected exception from call to getComponent: "
101: + e.getMessage(), e);
102: }
103: }
104: return _comp;
105: }
106:
107: protected void refreshComponent() throws DataSetException {
108: final ISession session = getSession();
109: if (session == null) {
110: throw new IllegalStateException("Null ISession");
111: }
112:
113: try {
114: Statement stmt = session.getSQLConnection()
115: .createStatement();
116: try {
117: ResultSet rs = stmt.executeQuery(getSQL());
118: try {
119: _comp.load(createDataSetFromResultSet(rs));
120: } finally {
121: rs.close();
122: }
123: } finally {
124: stmt.close();
125: }
126: } catch (SQLException ex) {
127: throw new DataSetException(ex);
128: }
129: }
130:
131: protected abstract String getSQL() throws SQLException;
132:
133: protected IDataSet createDataSetFromResultSet(ResultSet rs)
134: throws DataSetException {
135: final ResultSetDataSet rsds = new ResultSetDataSet();
136: rsds.setResultSet(rs);
137: if (!_firstRowOnly) {
138: return rsds;
139: }
140:
141: final int columnCount = rsds.getColumnCount();
142: final ColumnDisplayDefinition[] colDefs = rsds
143: .getDataSetDefinition().getColumnDefinitions();
144: final Map<String, Object> data = new HashMap<String, Object>();
145: if (rsds.next(null)) {
146: for (int i = 0; i < columnCount; ++i) {
147: data.put(colDefs[i].getLabel(), rsds.get(i));
148: }
149: }
150: return new MapDataSet(data);
151: }
152: }
|