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

Quick Search    Search Deep

Source code: javatools/db/DbReferencedTable.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  
23  import java.util.*;
24  import java.sql.*;
25  import javatools.util.SubstituteVariable;
26  import javatools.sql.SQL2Java;
27  
28  /** It is currently a hybrid. It should be rewritten. At present time, it has been
29   * surclassed by DbAbstractTable.
30   */
31  public class DbReferencedTable extends DbTable {
32      
33          public static final int DO_NOTHING      = 0,
34                                  SET_NULL        = 1,
35                                  SET_DEFAULT     = 2,
36                                  CASCADE         = 3;
37  
38          public DbReferencedTable (DbDatabase db) throws DbException {
39              super(db);
40              createReferences();
41          }
42          
43    public void setResultSet(PreparedStatement stmt, ResultSet rs) throws DbException {
44              super.setResultSet(stmt, rs);
45              defaultValues = null;
46              catchDefaults();
47          }
48          
49    public void setTableName(String v) {
50      tableName = v;
51                  try {
52                      catchDefaults();
53                  }
54                  catch (DbException e) {
55                  }
56    }
57  
58    /**
59     *  Return an inserter for inserting new data in the database. This method is
60     *  for SQL of the form INSERT INTO table(...) SELECT ...
61     *
62     * @param  selector         Description of Parameter
63     * @return                  Description of the Returned Value
64     * @exception  DbException  Description of Exception
65     */
66    public DbInserter inserter(DbSelector selector) throws DbException {
67      return new DbReferencedInserter(this, selector);
68    }
69  
70    /**
71     *  Return an inserter for inserting new data in the database. This method is
72     *  for SQL of the form INSERT INTO table(...) VALUES(...)
73     *
74     * @return                  Description of the Returned Value
75     * @exception  DbException  Description of Exception
76     */
77    public DbInserter inserter() throws DbException {
78      return new DbReferencedInserter(this);
79    }
80          
81    /*public DbReferencedInserter referencedInserter() throws DbException {
82      return new DbReferencedInserter(this);
83    }
84  
85    public DbReferencedInserter referencedInserter(DbSelector selector) throws DbException {
86      return new DbReferencedInserter(this, selector);
87    }
88  
89          /**
90     *  Return an updater for updating rows in the database
91     *
92     * @return                  Description of the Returned Value
93     * @exception  DbException  Description of Exception
94     */
95    public DbUpdater updater() throws DbException {
96      return new DbReferencedUpdater(this);
97    }
98  
99    /*public DbReferencedUpdater referencedUpdater() throws DbException {
100     return new DbReferencedUpdater(this);
101   }
102 
103         /**
104    *  Return a deleter for deleting rows in the database
105    *
106    * @return                  Description of the Returned Value
107    * @exception  DbException  Description of Exception
108    */
109   public DbDeleter deleter() throws DbException {
110     return new DbReferencedDeleter(this);
111   }
112 
113   /*public DbReferencedDeleter referencedDeleter() throws DbException {
114     return new DbReferencedDeleter(this);
115   }*/
116 
117         public void clearReferences() {
118             constraint.clear();
119         }
120         
121         public void addFatherTable(DbReferencedTable fatherTable, DbColumn[] fatherColumns, DbColumn[] pRefColumns,
122                 int updateOperation, int deleteOperation) {
123             constraint.addFatherTable(fatherTable, fatherColumns, pRefColumns, updateOperation, deleteOperation);
124         }
125         
126         public void addSonTable(DbReferencedTable sonTable, int fatherIndex) {
127             constraint.addSonTable(sonTable, fatherIndex);
128         }
129         
130         public int getFatherTablesCount() {
131             return constraint.getFatherTablesCount();
132         }
133         
134         public int getSonTablesCount() {
135             return constraint.getSonTablesCount();
136         }
137         
138         public DbAbstractTable getFatherTable(int index) throws DbException {
139             return constraint.getFatherTable(index);
140         }
141         
142         public DbAbstractTable getSonTable(int index) throws DbException {
143             return constraint.getSonTable(index);
144         }
145         
146         public int getSonFatherIndex(int index) throws DbException {
147             return constraint.getSonFatherIndex(index);
148         }
149         
150         public int getUpdateOperation(int index) throws DbException {
151             return constraint.getUpdateOperation(index);
152         }
153 
154         public int getDeleteOperation(int index) throws DbException {
155             return constraint.getDeleteOperation(index);
156         }
157         
158         public DbColumn[] getFatherColumns(int index) throws DbException {
159             return constraint.getFatherColumns(index);
160         }
161         
162         public DbColumn[] getRefColumns(int index) throws DbException {
163             return constraint.getRefColumns(index);
164         }
165         
166         public DbConstraint getConstraint() {
167             return constraint;
168         }
169         
170         private DbDynamicConstraint constraint;
171         
172         private void createReferences() {
173             constraint = new DbDynamicConstraint(this);
174         }
175         
176         private void catchDefaults() throws DbException {
177             int i;
178             Statement stmt;
179             ResultSet rs;
180             String descExpr, defaultField;
181             Connection con;
182             DbTable tempTable;
183             DbIterator tempIterator;
184             
185             if (defaultValues == null && tableName != null && columnCount > 0) {
186                 descExpr = db.getProperty("descCommand");
187                 defaultField = db.getProperty("defaultField");
188                 descExpr = SubstituteVariable.substitute(descExpr, "${tableName}", tableName);
189                 try {
190                     stmt = db.getThreadConnection().getSqlConnection().createStatement();
191                     rs = stmt.executeQuery(descExpr);
192                 }
193                 catch (SQLException e) {
194                     throw new DbException(e);
195                 }
196                 tempTable = new DbTable(db);
197                 tempTable.setResultSet(null, rs);
198                 tempIterator = tempTable.iterator();
199                 defaultValues = new Object[columnCount];
200                 i=0;
201                 while (tempIterator.hasNextRow()) {
202                     defaultValues[i] = SQL2Java.convert((String) tempIterator.nextRow().getValue(defaultField), types[i]);
203                     i++;
204                 }
205             }
206         }
207 }