001: /*
002: * SQLeonardo :: java database frontend
003: * Copyright (C) 2004 nickyb@users.sourceforge.net
004: *
005: * This program is free software; you can redistribute it and/or
006: * modify it under the terms of the GNU General Public License
007: * as published by the Free Software Foundation; either version 2
008: * of the License, or (at your option) any later version.
009: *
010: * This program is distributed in the hope that it will be useful,
011: * but WITHOUT ANY WARRANTY; without even the implied warranty of
012: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: * GNU General Public License for more details.
014: *
015: * You should have received a copy of the GNU General Public License
016: * along with this program; if not, write to the Free Software
017: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
018: *
019: */
020:
021: package nickyb.sqleonardo.environment.ctrl.content;
022:
023: import java.sql.ResultSet;
024: import java.sql.ResultSetMetaData;
025: import java.sql.SQLException;
026: import java.sql.Statement;
027: import java.sql.Types;
028:
029: import nickyb.sqleonardo.common.jdbc.ConnectionAssistant;
030: import nickyb.sqleonardo.common.jdbc.ConnectionHandler;
031: import nickyb.sqleonardo.environment.Application;
032: import nickyb.sqleonardo.environment.ctrl.ContentPane;
033:
034: public class TaskRetrieve implements Runnable {
035: private int limit;
036: private ContentPane target = null;
037:
038: private Statement stmt = null;
039: private ResultSet rs = null;
040:
041: public TaskRetrieve(ContentPane target) {
042: this (target, 0);
043: }
044:
045: public TaskRetrieve(ContentPane target, int limit) {
046: this .target = target;
047: this .limit = limit;
048: }
049:
050: public void run() {
051: try {
052: String syntax = target.getQueryModel().toString(false);
053: if (target.getHandlerKey() != null) {
054: ConnectionHandler ch = ConnectionAssistant
055: .getHandler(target.getHandlerKey());
056: stmt = ch.get().createStatement();
057: stmt.setMaxRows(limit);
058: rs = stmt.executeQuery(syntax);
059:
060: for (int i = 1; i <= this .getColumnCount(); i++) {
061: String l = this .getColumnLabel(i);
062: target.getView()
063: .addColumn(l, this .getColumnType(i));
064: }
065: target.getView().onTableChanged(false);
066:
067: for (int i = 1; i <= this .getColumnCount(); i++) {
068: // String t = this.getColumnLabel(i) + " : " + this.getColumnTypeName(i) + this.getColumnSize(i) + " " + this.getColumnNullable(i);
069: String t = this .getColumnLabel(i) + " : "
070: + this .getColumnTypeName(i) + " "
071: + this .getColumnNullable(i);
072: target.getView().setToolTipText(i - 1, t);
073: }
074:
075: for (int row = 1; target.isBusy() && rs.next(); row++) {
076: Object[] rowdata = new Object[this .getColumnCount()];
077: for (int i = 1; i <= this .getColumnCount(); i++) {
078: rowdata[i - 1] = rs.getString(i);
079: }
080: target.getView().addRow(rowdata, false);
081:
082: if (row == ContentModel.MAX_BLOCK_RECORDS)
083: target.getView().onTableChanged(true);
084:
085: if (row % ContentModel.MAX_BLOCK_RECORDS == 0)
086: target.doRefreshStatus();
087: }
088: rs.close();
089: }
090: } catch (SQLException sqle) {
091: Application.println(sqle, true);
092: } finally {
093: target.getView().onTableChanged(true);
094:
095: target.doStop();
096: target.doRefreshStatus();
097: }
098: }
099:
100: private int getColumnCount() throws SQLException {
101: return rs.getMetaData().getColumnCount();
102: }
103:
104: private String getColumnLabel(int index) throws SQLException {
105: return rs.getMetaData().getColumnLabel(index);
106: }
107:
108: /*
109: private String getColumnSize(int index) throws SQLException
110: {
111: String size;
112: if(isNumberType(getColumnType(index)))
113: {
114: int p = rs.getMetaData().getPrecision(index);
115: int s = rs.getMetaData().getScale(index);
116:
117: size = s > 0 ? p + "," + s : Integer.toString(p);
118: }
119: else
120: size = Integer.toString(rs.getMetaData().getColumnDisplaySize(index));
121:
122: return "(" + size + ")";
123: }
124: */
125: private String getColumnNullable(int index) throws SQLException {
126: return rs.getMetaData().isNullable(index) == ResultSetMetaData.columnNullable ? "(null)"
127: : "(not null)";
128: }
129:
130: private int getColumnType(int index) throws SQLException {
131: return rs.getMetaData().getColumnType(index);
132: }
133:
134: private String getColumnTypeName(int index) throws SQLException {
135: return rs.getMetaData().getColumnTypeName(index).toLowerCase();
136: }
137:
138: public static boolean isNumberType(int type) {
139: return type == Types.BIGINT || type == Types.BIT
140: || type == Types.DECIMAL || type == Types.DOUBLE
141: || type == Types.FLOAT || type == Types.INTEGER
142: || type == Types.NUMERIC || type == Types.REAL
143: || type == Types.SMALLINT;
144: }
145: }
|