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

Quick Search    Search Deep

Source code: javatools/db/DbColumn.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.sql.*;
23  import java.util.*;
24  
25  /** A class that represents a particular column within a particular DbTable.
26   * Based upon class DbColumn by Chris Bitmead.
27   * @author Chris Bitmead (original), Antonio Petrelli (modified)
28   * @version 0.0.1
29   */
30  
31  public class DbColumn extends DbExpr {
32      /** The indexed table.
33       */    
34    DbAbstractTable table;
35          /** The position between the fields.
36           */        
37    int index;
38          /** Creates a new DbColumn.
39           * @param table The table to get the column from.
40           * @param index The position of table's field.
41           */        
42    public DbColumn(DbAbstractTable table, int index) {
43      super(table.getDatabase());
44      this.table = table;
45      this.index = index;
46    }
47  
48          /** Returns the query string related to this column.
49           * @return The needed string.
50           */        
51    public String getQueryString() {
52      return table.getFullTableName() + "." + table.names[index];
53    }
54  
55          /** Returns the column name.
56           * @return The name of this column.
57           */        
58    public String getName() {
59      return table.names[index];
60    }
61  
62          /** Sets, in a prepared statement, the value related to this column, that will be
63           * used, e.g., in an INSERT operation. Anyway it is obscure...
64           * @param ps The prepared statement.
65           * @param i The index.
66           * @throws DbException If something goes wrong.
67           * @return An index.
68           */        
69    public int setSqlValues(PreparedStatement ps, int i) throws DbException {
70      return i;
71    }
72          /** Returns the display size of this column.
73           * @return The needed display size of the column.
74           */        
75    public int getSize() {
76      return table.displaySize[index];
77    }
78          /** Returns the index of the column among the fields of the table.
79           * @return The index of the column.
80           */        
81    public int getIndex() {
82      return index;
83    }
84          /** Checks if a column equals another column.
85           * @param o The object to compare to.
86           * @return <CODE>true</CODE>: if it is equal;
87           * <CODE>false</CODE>: otherwise.
88           */        
89    public boolean equals(Object o) {
90      if (!(o instanceof DbColumn)) {
91        return false;
92      }
93      DbColumn c = (DbColumn)o;
94      return table.equals(c.table) && index == c.index;
95    }
96    /**
97           * The fully qualified name of this column.
98           * @return The name of the column.
99           */
100   public String toString() {
101     return getQueryString();
102   }
103         /** Adds to the set, the currently indexed table.
104          * @param c The set to add current column to.
105          */        
106   public void usesTables(Set c) {
107             String tableName;
108             
109             try {
110                 tableName = table.getFullTableName();
111                 if (tableName != null)
112                     c.add(table);
113                 else
114                     table.getColumn(index).usesTables(c);
115             }
116             catch (DbException e) {
117                 System.out.println("Acc");
118             }
119   }
120         
121         /** Currently unused. It should take a sub-expression containing the specified
122          * columns.
123          * @param cols An array of columns. Only specified columns are taken in the result.
124          * @throws DbException If something goes wrong.
125          * @return The column itself if it is in the list, <CODE>null</CODE> otherwise.
126          */        
127         public DbExpr getSubExpr(DbColumn[] cols) throws DbException {
128             int i, numCols;
129             boolean found;
130             
131             numCols = cols.length;
132             found = false;
133             for (i=0; i < numCols && !found; i++)
134                 if (this.equals(cols[i]))
135                     found = true;
136             if (found)
137                 return this;
138             else
139                 return null;
140         }
141         
142         /** Currently unused. It should replace <CODE>oldCol</CODE> with
143          * <CODE>newCol</CODE>.
144          * @param oldCol The column to be replaced.
145          * @param newCol The column to put.
146          * @throws DbException If something goes wrong.
147          * @return <CODE>newCol</CODE> if this column represents <CODE>oldCol</CODE>, this column
148          * otherwise.
149          */        
150         public DbExpr substituteColumn(DbColumn oldCol, DbColumn newCol) throws DbException {
151             if (oldCol.equals(this))
152                 return newCol;
153             else
154                 return this;
155         }
156         
157 }