Source code: javatools/db/DbJoinedTable.java
1 /*
2 * DbJoinedTable.java
3 *
4 * Created on 23 agosto 2002, 17.43
5 Javatools (modified version) - Some useful general classes.
6 Copyright (C) 2002-2003 Chris Bitmead (original) Antonio Petrelli (modified)
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21
22 Contact me at: brenmcguire@users.sourceforge.net
23 */
24
25 package javatools.db;
26
27 import java.util.Set;
28
29 /** This class represents a "joined table" in an SQL statement.
30 * @author Antonio Petrelli
31 * @version 0.2.0
32 */
33 public class DbJoinedTable extends DbAbstractTable {
34
35 /** Represents an inner join.
36 */
37 public static final int INNER_JOIN = 1;
38 /** Represents a left join.
39 */
40 public static final int NATURAL_JOIN = 2;
41 /** Represents a left join.
42 */
43 public static final int LEFT_JOIN = 3;
44 /** Represents a right join.
45 */
46 public static final int RIGHT_JOIN = 4;
47 /** Represents an outer join.
48 */
49 public static final int OUTER_JOIN = 5;
50
51 /** Creates a new instance of DbJoinedTable
52 * @param db The database to use.
53 * @param tabLeft The left table to be joined.
54 * @param tabRight The right table to be joined.
55 * @param pJoinType The join type to use.
56 * @param pJoinCondition The join condition expression.
57 * @throws DbException If something goes wrong.
58 */
59 public DbJoinedTable(DbDatabase db, DbAbstractTable tabLeft,
60 DbAbstractTable tabRight, int pJoinType, DbExpr pJoinCondition)
61 throws DbException {
62 super(db);
63 tableLeft = tabLeft;
64 tableRight = tabRight;
65 joinType = pJoinType;
66 joinCondition = pJoinCondition;
67 buildTableName();
68 }
69
70 /** Returns the default value for the field at the given index.
71 * @param index The field index.
72 * @throws DbException If something goes wrong.
73 * @return The default value for the requested field.
74 */
75 public Object getDefault(int index) throws DbException {
76 return null;
77 }
78
79 /** Check if an object is equal to the current one.
80 * @param o The object to be compared.
81 * @return <CODE>true</CODE>: if these object are equal;
82 * <CODE>false</CODE>: otherwise.
83 */
84 public boolean equals(Object o) {
85 return false;
86 }
87
88 /** Returns the default value for the field whose name is specified.
89 * @param name The name of the needed field.
90 * @throws DbException If something goes wrong.
91 * @return The default value for the requested field.
92 */
93 public Object getDefault(String name) throws DbException {
94 return null;
95 }
96
97 /** Get the DbColumn representing the column with this name.
98 *
99 * @param name The name of tjhe needed column
100 * @return The column value
101 * @exception DbException If something goes wrong.
102 */
103 public DbColumn getColumn(String name) throws DbException {
104 return null;
105 }
106
107 /** Returns the number of columns
108 * @return The number of columns.
109 */
110 public int getColumnCount() {
111 return -1;
112 }
113
114 /** Returns a deleter for deleting rows in the database.
115 * This deleter takes care of constraints.
116 *
117 * @return The DbDeleter object to perform delete operations.
118 * @exception DbException If something goes wrong.
119 */
120 public DbDeleter deleter() throws DbException {
121 return null;
122 }
123
124 /** Returns the DbConstraint object associated with this table.
125 * @return The requested DbConstraint.
126 */
127 public DbConstraint getConstraint() {
128 return null;
129 }
130
131 /** Get the column of the given index. Index is a zero based array.
132 *
133 * @param index The position of the needed column.
134 * @return The column value
135 * @exception DbException If something goes wrong.
136 */
137 public DbColumn getColumn(int index) throws DbException {
138 return null;
139 }
140
141 /** Return an inserter for inserting new data in the database. This method is
142 * for SQL of the form INSERT INTO table(...) VALUES(...)
143 * This inserter takes care of constraints.
144 *
145 * @return The DbInserter object to insert data into the table.
146 * @exception DbException If something goes wrong.
147 */
148 public DbInserter inserter() throws DbException {
149 return null;
150 }
151
152 /** Return an inserter for inserting new data in the database. This method is
153 * for SQL of the form INSERT INTO table(...) SELECT ...
154 * This inserter takes care of constraints.
155 *
156 * @param selector The selector to create the DbInserter object.
157 * @return The DbInserter object to insert data into the table.
158 * @exception DbException If something goes wrong.
159 */
160 public DbInserter inserter(DbSelector selector) throws DbException {
161 return null;
162 }
163
164 /** Sets the name of the referenced table.
165 * @param v The table name.
166 */
167 public void setTableName(String v) {
168 }
169
170 /** Returns a deleter for deleting rows in the database.
171 * This deleter takes care of constraints.
172 *
173 * @throws DbException If something goes wrong.
174 * @return The DbDeleter object to perform delete operations.
175 */
176 public DbDeleter simpleDeleter() throws DbException {
177 return null;
178 }
179
180 /** Return an inserter for inserting new data in the database. This method is
181 * for SQL of the form INSERT INTO table(...) SELECT ...
182 * This inserter DOES NOT take care of constraints.
183 *
184 * @throws DbException If something goes wrong.
185 * @return The DbInserter object to perform a simple insertion.
186 */
187 public DbInserter simpleInserter() throws DbException {
188 return null;
189 }
190
191 /** Return an inserter for inserting new data in the database. This method is
192 * for SQL of the form INSERT INTO table(...) SELECT ...
193 * This inserter DOES NOT take care of constraints.
194 *
195 * @param selector The selector to create the DbInserter object.
196 * @throws DbException If something goes wrong.
197 * @return The DbInserter object to perform a simple insertion.
198 */
199 public DbInserter simpleInserter(DbSelector selector) throws DbException {
200 return null;
201 }
202
203 /** Return an updater for updating rows in the database.
204 * This updater DOES NOT take care of constraints.
205 *
206 * @throws DbException If something goes wrong.
207 * @return The DbUpdater object to perform update operations.
208 */
209 public DbUpdater simpleUpdater() throws DbException {
210 return null;
211 }
212
213 /** Return an updater for updating rows in the database.
214 * This updater takes care of constraints.
215 *
216 * @return The DbUpdater object to perform update operations.
217 * @exception DbException If something goes wrong.
218 */
219 public DbUpdater updater() throws DbException {
220 return null;
221 }
222
223 /** Returns the base tables used in this joined table.
224 * @param c The set in which used tables will be put.
225 */
226 public void usesTables(Set c) {
227 tableLeft.usesTables(c);
228 tableRight.usesTables(c);
229 }
230
231 private DbAbstractTable tableLeft;
232 private DbAbstractTable tableRight;
233 private int joinType;
234 private DbExpr joinCondition;
235
236 private void buildTableName() throws DbException {
237 String joinTypeString;
238
239 switch (joinType) {
240 case INNER_JOIN:
241 joinTypeString = " JOIN ";
242 break;
243 case NATURAL_JOIN:
244 joinTypeString = " NATURAL JOIN ";
245 break;
246 case LEFT_JOIN:
247 joinTypeString = " LEFT JOIN ";
248 break;
249 case RIGHT_JOIN:
250 joinTypeString = " RIGHT JOIN ";
251 break;
252 case OUTER_JOIN:
253 joinTypeString = " OUTER JOIN ";
254 break;
255 default:
256 joinTypeString = " JOIN ";
257 }
258 if (joinCondition != null)
259 tableName = "(" + tableLeft.tableName + joinTypeString +
260 tableRight.tableName + " ON " +
261 joinCondition.getQueryString() + ")";
262 else
263 tableName = "(" + tableLeft.tableName + joinTypeString +
264 tableRight.tableName + ")";
265 }
266
267
268 }