~ubuntu-branches/ubuntu/precise/weka/precise

« back to all changes in this revision

Viewing changes to weka/gui/sql/ResultSetHelper.java

  • Committer: Bazaar Package Importer
  • Author(s): Soeren Sonnenburg
  • Date: 2008-02-24 09:18:45 UTC
  • Revision ID: james.westby@ubuntu.com-20080224091845-1l8zy6fm6xipbzsr
Tags: upstream-3.5.7+tut1
ImportĀ upstreamĀ versionĀ 3.5.7+tut1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *    This program is free software; you can redistribute it and/or modify
 
3
 *    it under the terms of the GNU General Public License as published by
 
4
 *    the Free Software Foundation; either version 2 of the License, or
 
5
 *    (at your option) any later version.
 
6
 *
 
7
 *    This program is distributed in the hope that it will be useful,
 
8
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
 *    GNU General Public License for more details.
 
11
 *
 
12
 *    You should have received a copy of the GNU General Public License
 
13
 *    along with this program; if not, write to the Free Software
 
14
 *    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 
15
 */
 
16
 
 
17
/*
 
18
 * ResultSetHelper.java
 
19
 * Copyright (C) 2005 University of Waikato, Hamilton, New Zealand
 
20
 *
 
21
 */
 
22
 
 
23
 
 
24
package weka.gui.sql;
 
25
 
 
26
import java.sql.ResultSet;
 
27
import java.sql.ResultSetMetaData;
 
28
import java.sql.Types;
 
29
 
 
30
/**
 
31
 * Represents an extended JTable, containing a table model based on a ResultSet
 
32
 * and the corresponding query.
 
33
 *
 
34
 *
 
35
 * @author      FracPete (fracpete at waikato dot ac dot nz)
 
36
 * @version     $Revision: 1.2 $
 
37
 */
 
38
 
 
39
public class ResultSetHelper {
 
40
  /** the resultset to work on */
 
41
  protected ResultSet m_ResultSet;
 
42
 
 
43
  /** whether we initialized */
 
44
  protected boolean m_Initialized = false;
 
45
 
 
46
  /** the maximum number of rows to retrieve */
 
47
  protected int m_MaxRows = 0;
 
48
 
 
49
  /** the number of columns */
 
50
  protected int m_ColumnCount = 0;
 
51
 
 
52
  /** the number of rows */
 
53
  protected int m_RowCount = 0;
 
54
 
 
55
  /** the column names */
 
56
  protected String[] m_ColumnNames = null;
 
57
 
 
58
  /** whether a column is numeric */
 
59
  protected boolean[] m_NumericColumns = null;
 
60
 
 
61
  /** the class for each column */
 
62
  protected Class[] m_ColumnClasses = null;
 
63
 
 
64
  /**
 
65
   * initializes the helper, with unlimited number of rows
 
66
   * @param rs        the resultset to work on
 
67
   */
 
68
  public ResultSetHelper(ResultSet rs) {
 
69
    this(rs, 0);
 
70
  }
 
71
 
 
72
  /**
 
73
   * initializes the helper, with the given maximum number of rows (less than
 
74
   * 1 means unlimited)
 
75
   * @param rs        the resultset to work on
 
76
   * @param max       the maximum number of rows to retrieve
 
77
   */
 
78
  public ResultSetHelper(ResultSet rs, int max) {
 
79
    super();
 
80
 
 
81
    m_ResultSet = rs;
 
82
    m_MaxRows   = max;
 
83
  }
 
84
 
 
85
  /**
 
86
   * initializes, i.e. reads the data, etc.
 
87
   */
 
88
  protected void initialize() {
 
89
    ResultSetMetaData     meta;
 
90
    int                   i;
 
91
    
 
92
    if (m_Initialized)
 
93
      return;
 
94
    
 
95
    try {
 
96
      meta = m_ResultSet.getMetaData();
 
97
 
 
98
      // columns names
 
99
      m_ColumnNames = new String[meta.getColumnCount()];
 
100
      for (i = 1; i <= meta.getColumnCount(); i++)
 
101
        m_ColumnNames[i - 1] = meta.getColumnName(i);
 
102
      
 
103
      // numeric columns
 
104
      m_NumericColumns = new boolean[meta.getColumnCount()];
 
105
      for (i = 1; i <= meta.getColumnCount(); i++)
 
106
        m_NumericColumns[i - 1] = typeIsNumeric(meta.getColumnType(i));
 
107
 
 
108
      // column classes
 
109
      m_ColumnClasses = new Class[meta.getColumnCount()];
 
110
      for (i = 1; i <= meta.getColumnCount(); i++) {
 
111
        try {
 
112
          m_ColumnClasses[i - 1] = Class.forName(meta.getColumnClassName(i));
 
113
        }
 
114
        catch (Exception e) {
 
115
          //e.printStackTrace();
 
116
          // JDBC does not support this function -> do it manually
 
117
          try {
 
118
            m_ColumnClasses[i - 1] = typeToClass(meta.getColumnType(i));
 
119
          }
 
120
          catch (Exception ex) {
 
121
            m_ColumnClasses[i - 1] = String.class;
 
122
          }
 
123
        }
 
124
      }
 
125
 
 
126
      // dimensions
 
127
      m_ColumnCount = meta.getColumnCount();
 
128
 
 
129
      m_RowCount = 0;
 
130
      m_ResultSet.first();
 
131
      if (m_MaxRows > 0) {
 
132
        try {
 
133
          m_ResultSet.absolute(m_MaxRows);
 
134
          m_RowCount = m_ResultSet.getRow();
 
135
        }
 
136
        catch (Exception ex) {
 
137
          // ignore it
 
138
        }
 
139
      }
 
140
      else {
 
141
        m_ResultSet.last();
 
142
        m_RowCount = m_ResultSet.getRow();
 
143
      }
 
144
 
 
145
      // sometimes, e.g. with a "desc <table>", we can't use absolute(int)
 
146
      // and getRow()???
 
147
      try {
 
148
        if ( (m_RowCount == 0) && (m_ResultSet.first()) ) {
 
149
          m_RowCount = 1;
 
150
          while (m_ResultSet.next()) {
 
151
            m_RowCount++;
 
152
            if (m_ResultSet.getRow() == m_MaxRows)
 
153
              break;
 
154
          };
 
155
        }
 
156
      }
 
157
      catch (Exception e) {
 
158
        // ignore it
 
159
      }
 
160
 
 
161
      m_Initialized = true;
 
162
    }
 
163
    catch (Exception ex) {
 
164
      // ignore it
 
165
    }
 
166
  }
 
167
 
 
168
  /**
 
169
   * the underlying resultset
 
170
   */
 
171
  public ResultSet getResultSet() {
 
172
    return m_ResultSet;
 
173
  }
 
174
 
 
175
  /**
 
176
   * returns the number of columns in the resultset
 
177
   */
 
178
  public int getColumnCount() {
 
179
    initialize();
 
180
 
 
181
    return m_ColumnCount;
 
182
  }
 
183
 
 
184
  /**
 
185
   * returns the number of rows in the resultset
 
186
   */
 
187
  public int getRowCount() {
 
188
    initialize();
 
189
 
 
190
    return m_RowCount;
 
191
  }
 
192
 
 
193
  /**
 
194
   * returns an array with the names of the columns in the resultset
 
195
   */
 
196
  public String[] getColumnNames() {
 
197
    initialize();
 
198
 
 
199
    return m_ColumnNames;
 
200
  }
 
201
 
 
202
  /**
 
203
   * returns an array that indicates whether a column is numeric or nor
 
204
   */
 
205
  public boolean[] getNumericColumns() {
 
206
    initialize();
 
207
 
 
208
    return m_NumericColumns;
 
209
  }
 
210
 
 
211
  /**
 
212
   * returns the classes for the columns
 
213
   */
 
214
  public Class[] getColumnClasses() {
 
215
    initialize();
 
216
 
 
217
    return m_ColumnClasses;
 
218
  }
 
219
 
 
220
  /**
 
221
   * whether a limit on the rows to retrieve was set
 
222
   */
 
223
  public boolean hasMaxRows() {
 
224
    return (m_MaxRows > 0);
 
225
  }
 
226
 
 
227
  /**
 
228
   * the maximum number of rows to retrieve, less than 1 means unlimited
 
229
   */
 
230
  public int getMaxRows() {
 
231
    return m_MaxRows;
 
232
  }
 
233
 
 
234
  /**
 
235
   * returns an 2-dimensional array with the content of the resultset, the first
 
236
   * dimension is the row, the second the column (i.e., getCells()[y][x]).
 
237
   * Note: the data is not cached! It is always retrieved anew.
 
238
   */
 
239
  public Object[][] getCells() {
 
240
    int           i;
 
241
    int           n;
 
242
    Object[][]    result;
 
243
    
 
244
    initialize();
 
245
 
 
246
    result = new Object[getRowCount()][getColumnCount()];
 
247
 
 
248
    try {
 
249
      m_ResultSet.first();
 
250
      
 
251
      for (i = 0; i < getRowCount(); i++) {
 
252
 
 
253
        for (n = 0; n < getColumnCount(); n++) {
 
254
          try {
 
255
            result[i][n] = m_ResultSet.getObject(n + 1);
 
256
          }
 
257
          catch (Exception e) {
 
258
            result[i][n] = null;
 
259
          }
 
260
        }
 
261
 
 
262
        // get next row
 
263
        if (i == getRowCount() - 1)
 
264
          break;
 
265
        else
 
266
          m_ResultSet.next();
 
267
      }
 
268
    }
 
269
    catch (Exception e) {
 
270
      e.printStackTrace();
 
271
    }
 
272
 
 
273
    return result;
 
274
  }
 
275
 
 
276
  /**
 
277
   * Returns the class associated with a SQL type.
 
278
   *
 
279
   * @param type the SQL type
 
280
   * @return the Java class corresponding with the type
 
281
   */
 
282
  public static Class typeToClass(int type) {
 
283
    Class     result;
 
284
    
 
285
    switch (type) {
 
286
      case Types.BIGINT :
 
287
        result = Long.class;
 
288
        break;
 
289
      case Types.BINARY:
 
290
        result = String.class;
 
291
      case Types.BIT:
 
292
        result = Boolean.class;
 
293
        break;
 
294
      case Types.CHAR:
 
295
        result = Character.class;
 
296
        break;
 
297
      case Types.DATE:
 
298
        result = java.sql.Date.class;
 
299
        break;
 
300
      case Types.DECIMAL:
 
301
        result = Double.class;
 
302
        break;
 
303
      case Types.DOUBLE:
 
304
        result = Double.class;
 
305
        break;
 
306
      case Types.FLOAT:
 
307
        result = Float.class;
 
308
        break;
 
309
      case Types.INTEGER:
 
310
        result = Integer.class;
 
311
        break;
 
312
      case Types.LONGVARBINARY:
 
313
        result = String.class;
 
314
        break;
 
315
      case Types.LONGVARCHAR:
 
316
        result = String.class;
 
317
        break;
 
318
      case Types.NULL:
 
319
        result = String.class;
 
320
        break;
 
321
      case Types.NUMERIC:
 
322
        result = Double.class;
 
323
        break;
 
324
      case Types.OTHER:
 
325
        result = String.class;
 
326
        break;
 
327
      case Types.REAL:
 
328
        result = Double.class;
 
329
        break;
 
330
      case Types.SMALLINT:
 
331
        result = Short.class;
 
332
        break;
 
333
      case Types.TIME:
 
334
        result = java.sql.Time.class;
 
335
        break;
 
336
      case Types.TIMESTAMP:
 
337
        result = java.sql.Timestamp.class;
 
338
        break;
 
339
      case Types.TINYINT:
 
340
        result = Short.class;
 
341
        break;
 
342
      case Types.VARBINARY:
 
343
        result = String.class;
 
344
        break;
 
345
      case Types.VARCHAR:
 
346
        result = String.class;
 
347
        break;
 
348
      default:
 
349
        result = null;
 
350
    }
 
351
 
 
352
    return result;
 
353
  }
 
354
 
 
355
  /**
 
356
   * returns whether the SQL type is numeric (and therefore the justification
 
357
   * should be right)
 
358
   * @param type      the SQL type
 
359
   * @return          whether the given type is numeric
 
360
   */
 
361
  public static boolean typeIsNumeric(int type) {
 
362
    boolean     result;
 
363
    
 
364
    switch (type) {
 
365
      case Types.BIGINT :
 
366
        result = true;
 
367
        break;
 
368
      case Types.BINARY:
 
369
        result = false;
 
370
      case Types.BIT:
 
371
        result = false;
 
372
        break;
 
373
      case Types.CHAR:
 
374
        result = false;
 
375
        break;
 
376
      case Types.DATE:
 
377
        result = false;
 
378
        break;
 
379
      case Types.DECIMAL:
 
380
        result = true;
 
381
        break;
 
382
      case Types.DOUBLE:
 
383
        result = true;
 
384
        break;
 
385
      case Types.FLOAT:
 
386
        result = true;
 
387
        break;
 
388
      case Types.INTEGER:
 
389
        result = true;
 
390
        break;
 
391
      case Types.LONGVARBINARY:
 
392
        result = false;
 
393
        break;
 
394
      case Types.LONGVARCHAR:
 
395
        result = false;
 
396
        break;
 
397
      case Types.NULL:
 
398
        result = false;
 
399
        break;
 
400
      case Types.NUMERIC:
 
401
        result = true;
 
402
        break;
 
403
      case Types.OTHER:
 
404
        result = false;
 
405
        break;
 
406
      case Types.REAL:
 
407
        result = true;
 
408
        break;
 
409
      case Types.SMALLINT:
 
410
        result = true;
 
411
        break;
 
412
      case Types.TIME:
 
413
        result = false;
 
414
        break;
 
415
      case Types.TIMESTAMP:
 
416
        result = true;
 
417
        break;
 
418
      case Types.TINYINT:
 
419
        result = true;
 
420
        break;
 
421
      case Types.VARBINARY:
 
422
        result = false;
 
423
        break;
 
424
      case Types.VARCHAR:
 
425
        result = false;
 
426
        break;
 
427
      default:
 
428
        result = false;
 
429
    }
 
430
 
 
431
    return result;
 
432
  }
 
433
}