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