Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: javatools/db/DbTable.java


1   /*
2       Javatools (modified version) - Some useful general classes.
3       Copyright (C) 2002-2003  Chris Bitmead (original) Antonio Petrelli (modified)
4   
5       This program is free software; you can redistribute it and/or modify
6       it under the terms of the GNU General Public License as published by
7       the Free Software Foundation; either version 2 of the License, or
8       (at your option) any later version.
9   
10      This program is distributed in the hope that it will be useful,
11      but WITHOUT ANY WARRANTY; without even the implied warranty of
12      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13      GNU General Public License for more details.
14  
15      You should have received a copy of the GNU General Public License
16      along with this program; if not, write to the Free Software
17      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  
19      Contact me at: brenmcguire@users.sourceforge.net
20   */
21  package javatools.db;
22  import java.util.*;
23  import java.sql.*;
24  import javatools.util.Compare;
25  import javatools.util.SubstituteVariable;
26  /**
27   * A class representing tabular data. Could be a real database table or the
28   * result of a SELECT.
29   *
30   * @author Chris Bitmead (original), Antonio Petrelli (modified)
31   * @created 5 September 2001
32   * @version 0.2.0
33   */
34  
35  public class DbTable extends DbAbstractTable implements DbResult {
36          int columnCount;
37          /** The real result set.
38           */ 
39    ResultSet rs;
40          /** The statement that generated the result set.
41           */        
42    PreparedStatement stmt;
43          /** Contains the list of columns.
44           */        
45    List columns = new ArrayList();
46          /** Contains the default values for this table.
47           */        
48          Object[] defaultValues;
49  
50          /** Creates a new DbTable.
51           * @param db The database to use.
52           * @throws DbException If something goes wrong.
53           */        
54    public DbTable(DbDatabase db) throws DbException {
55      super(db);
56                  defaultValues = null;
57    }
58  
59          /** Sets the result set to take rows from.
60           * @param stmt The statement that generated the result set. It can be null.
61           * @param rs The result set to use.
62           * @throws DbException If something goes wrong.
63           */        
64    public void setResultSet(PreparedStatement stmt, ResultSet rs) throws DbException {
65      this.stmt = stmt;
66      this.rs = rs;
67      try {
68        ResultSetMetaData md = rs.getMetaData();
69        columnCount = md.getColumnCount();
70        int cc = columnCount;
71        displaySize = new int[cc];
72        names = new String[cc];
73        types = new int[cc];
74                          columns.clear();
75        /*
76         *  autoIncrement = new boolean[cc];
77         *  caseSensitive = new boolean[cc];
78         *  searchable = new boolean[cc];
79         *  currency = new boolean[cc];
80         *  nullable = new int[cc];
81         *  signed = new boolean[cc];
82         *  labels = new String[cc];
83         *  schemaNames = new String[cc];
84         *  precision = new int[cc];
85         *  scale = new int[cc];
86         *  tableNames = new String[cc];
87         *  catalogNames = new String[cc];
88         *  typeNames = new String[cc];
89         *  readonly = new boolean[cc];
90         *  writable = new boolean[cc];
91         *  definitelyWritable = new boolean[cc];
92         *  classNames = new String[cc];
93         */
94        for (int i = 0; i < columnCount; i++) {
95          names[i] = md.getColumnName(i + 1).toUpperCase();
96          columns.add(new DbColumn(this, i));
97          colNameMap.put(names[i], new Integer(i));
98          displaySize[i] = md.getColumnDisplaySize(i + 1);
99          types[i] = md.getColumnType(i + 1);
100         /*
101          *  autoIncrement[i] = md.isAutoIncrement(i+1);
102          *  caseSensitive[i] = md.isCaseSensitive(i+1);
103          *  searchable[i] = md.isSearchable(i+1);
104          *  currency[i] = md.isCurrency(i+1);
105          *  nullable[i] = md.isNullable(i+1);
106          *  signed[i] = md.isSigned(i+1);
107          *  labels[i] = md.getColumnLabel(i+1);
108          *  schemaNames[i] = md.getSchemaName(i+1);
109          *  precision[i] = md.getPrecision(i+1);
110          *  scale[i] = md.getScale(i+1);
111          *  tableNames[i] = md.getTableName(i+1);
112          *  catalogNames[i] = md.getCatalogName(i+1);
113          *  typeNames[i] = md.getColumnTypeName(i+1);
114          *  readonly[i] = md.isReadOnly(i+1);
115          *  writable[i] = md.isWritable(i+1);
116          *  definitelyWritable[i] = md.isDefinitelyWritable(i+1);
117          *  classNames[i] = md.getColumnClassName(i+1);
118          */
119       }
120     } catch (SQLException e) {
121       throw new DbException(e);
122     }
123   }
124 
125         /** Sets the table name.
126          * @param v The new table name.
127          */        
128   public void setTableName(String v) {
129     tableName = v;
130   }
131 
132   /**
133          * Get the DbColumn representing the column with this name.
134          *
135          * @param name The name of the column.
136          * @return The column value
137          * @exception DbException If something goes wrong.
138          */
139   public DbColumn getColumn(String name) throws DbException {
140     Integer index = (Integer) colNameMap.get(name);
141     if (index == null) {
142       throw new DbException("Can't find column: " + name + " in table: " + tableName);
143     }
144     return (DbColumn) columns.get(index.intValue());
145   }
146 
147   /**
148          * Get the column of the given index. Index is a zero based array.
149          *
150          * @param index The index of the column.
151          * @return The column value
152          * @exception DbException If something goes wrong.
153          */
154   public DbColumn getColumn(int index) throws DbException {
155     if (colNameMap.size() <= index) {
156       throw new DbException("Can't find column #" + index + " in table: " + tableName);
157     }
158     return (DbColumn) columns.get(index);
159   }
160         
161         /** Returns the number of columns.
162          * @return The column count.
163          */        
164         public int getColumnCount() {
165             return columns.size();
166         }
167 
168   /**
169    *  Get the database that this table came fr
170    *
171    * @return    The database value
172    */
173   public DbDatabase getDatabase() {
174     return db;
175   }
176 
177         /** Returns the complete table name.
178          * @return The complete table name.
179          */        
180   public String getFullTableName() {
181     return tableName;
182   }
183 
184   /*
185    *  boolean[] autoIncrement;
186    *  boolean[] caseSensitive;
187    *  boolean[] searchable;
188    *  boolean[] currency;
189    *  int[] nullable;
190    *  boolean[] signed;
191    *  String[] labels;
192    *  String[] schemaNames;
193    *  int[] precision;
194    *  int[] scale;
195    *  String[] tableNames;
196    *  String[] catalogNames;
197    *  String[] typeNames;
198    *  boolean[] readonly;
199    *  boolean[] writable;
200    *  boolean[] definitelyWritable;
201    *  String[] classNames;
202    */
203 
204 
205   /**
206          * Return an iterator to iterate over the rows in this table. <P>
207          *
208          * BUGS: Currently you can only iterate over a table that was returned from a
209          * DbSelector.
210          *
211          * @return The requested iterator.
212          */
213   public DbIterator iterator() {
214     return new DbTableIterator(this);
215   }
216 
217         /** Closes this table.
218          * @throws DbException If something goes wrong.
219          */        
220   public void close() throws DbException {
221     try {
222       if (stmt != null) {
223         stmt.close();
224         stmt = null;
225       }
226     } catch (SQLException e) {
227       throw new DbException(e);
228     }
229   }
230 
231         /** Returns null (no constraint in a simple DbTable).
232          * @return null.
233          */        
234         public DbConstraint getConstraint() {
235             return null;
236         }
237         
238         /** Returns the default value for this table.
239          * @param index The index of the requested column.
240          * @throws DbException If something goes wrong.
241          * @return The needed default value.
242          */        
243         public Object getDefault(int index) throws DbException {
244             if (index >=0 && index < columnCount && defaultValues != null)
245                 return defaultValues[index];
246             else
247                 throw new DbException ("Index not valid in getting a default value");
248         }
249         
250         /** Returns the default value of a column.
251          * @param name The name of the column.
252          * @throws DbException If something goes wrong.
253          * @return The requested default value.
254          */        
255         public Object getDefault(String name) throws DbException {
256             int index;
257             
258             index = ((Integer) colNameMap.get(name)).intValue();
259             return getDefault(index);
260         }
261         
262         /** The database used for the connection.
263          */
264         protected DbDatabase db;
265 
266         /** Destroys this object.
267          * @throws Throwable If something goes wrong.
268          */        
269   protected void finalize() throws java.lang.Throwable {
270     close();
271     super.finalize();
272   }
273 
274         /** Adds a column and returns it to the caller.
275          * @throws DbException If something goes wrong.
276          * @return The added column.
277          */        
278   DbColumn getAddColumn() throws DbException {
279     DbColumn rtn = null;
280     rtn = new DbColumn(this, columns.size());
281     columns.add(rtn);
282     return rtn;
283   }
284         
285 }