00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025 package org.objectweb.cjdbc.common.sql.schema;
00026
00027 import java.io.Serializable;
00028 import java.sql.SQLException;
00029 import java.util.ArrayList;
00030 import java.util.Iterator;
00031
00032 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags;
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045 public class DatabaseTable implements Serializable
00046 {
00047
00048 private String name;
00049
00050
00051 private ArrayList columns;
00052
00053
00054
00055
00056
00057
00058 public DatabaseTable(String name)
00059 {
00060 this(name, new ArrayList());
00061 }
00062
00063
00064
00065
00066
00067
00068
00069 public DatabaseTable(String name, int nbOfColumns)
00070 {
00071 this(name, new ArrayList(nbOfColumns));
00072 }
00073
00074
00075
00076
00077
00078
00079
00080 private DatabaseTable(String name, ArrayList columns)
00081 {
00082 if (name == null)
00083 throw new IllegalArgumentException("Illegal null database table name in DatabaseTable constructor");
00084
00085 this.name = name;
00086 this.columns = columns;
00087 }
00088
00089
00090
00091
00092
00093
00094 public String getName()
00095 {
00096 return name;
00097 }
00098
00099
00100
00101
00102
00103
00104
00105
00106 public void addColumn(DatabaseColumn column)
00107 {
00108 columns.add(column);
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118 public void remove(String columnName)
00119 {
00120 columns.remove(getColumn(columnName));
00121 }
00122
00123
00124
00125
00126
00127
00128
00129
00130 public void remove(DatabaseColumn column)
00131 {
00132 columns.remove(column);
00133 }
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143 public void mergeColumns(DatabaseTable table) throws SQLException
00144 {
00145 if (table == null)
00146 return;
00147
00148 ArrayList otherColumns = table.getColumns();
00149 if (otherColumns == null)
00150 return;
00151
00152 DatabaseColumn c, original;
00153 int size = otherColumns.size();
00154 for (int i = 0; i < size; i++)
00155 {
00156 c = (DatabaseColumn) otherColumns.get(i);
00157 original = getColumn(c.getName());
00158 if (original == null)
00159 addColumn(c);
00160 else
00161 {
00162 if (!original.equalsIgnoreType(c))
00163 throw new SQLException(
00164 "Unable to merge table ["+table.getName()+"]: column '"
00165 + c.getName()
00166 + "' definition mismatch");
00167 }
00168 }
00169 }
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179 public ArrayList getColumns()
00180 {
00181 return columns;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 public ArrayList getUniqueColumns()
00194 {
00195 ArrayList cols = new ArrayList();
00196 Iterator i;
00197 DatabaseColumn c;
00198
00199 for (i = columns.iterator(); i.hasNext();)
00200 {
00201 c = (DatabaseColumn) i.next();
00202 if (c.isUnique())
00203 cols.add(c);
00204 }
00205 return cols;
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215 public DatabaseColumn getColumn(String columnName)
00216 {
00217 DatabaseColumn c;
00218 for (Iterator i = columns.iterator(); i.hasNext();)
00219 {
00220 c = (DatabaseColumn) i.next();
00221 if (columnName.equalsIgnoreCase(c.getName()))
00222 return c;
00223
00224 }
00225 return null;
00226 }
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236 public DatabaseColumn getColumn(String columnName, boolean isCaseSensitive)
00237 {
00238 if (!isCaseSensitive)
00239 return getColumn(columnName);
00240
00241 DatabaseColumn c;
00242 for (Iterator i = columns.iterator(); i.hasNext();)
00243 {
00244 c = (DatabaseColumn) i.next();
00245 if (columnName.equals(c.getName()))
00246 return c;
00247
00248 }
00249 return null;
00250 }
00251
00252
00253
00254
00255
00256
00257
00258
00259 public boolean equals(Object other)
00260 {
00261 if ((other == null) || !(other instanceof DatabaseTable))
00262 return false;
00263
00264 DatabaseTable t = (DatabaseTable) other;
00265 return (t.getName().equals(name)) && (t.getColumns().equals(columns));
00266 }
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276 public boolean equalsIgnoreType(Object other)
00277 {
00278 if ((other == null) || !(other instanceof DatabaseTable))
00279 return false;
00280
00281 DatabaseTable t = (DatabaseTable) other;
00282 if (!t.getName().equals(name))
00283 return false;
00284
00285 DatabaseColumn c1, c2;
00286 Iterator iter = columns.iterator();
00287 while (iter.hasNext())
00288 {
00289 c1 = (DatabaseColumn) iter.next();
00290 c2 = t.getColumn(c1.getName());
00291
00292 if (!c1.equalsIgnoreType(c2))
00293 return false;
00294 }
00295 return true;
00296 }
00297
00298
00299
00300
00301
00302
00303 public String getXml()
00304 {
00305 StringBuffer info = new StringBuffer();
00306 info.append(
00307 "<"
00308 + DatabasesXmlTags.ELT_DatabaseTable
00309 + " "
00310 + DatabasesXmlTags.ATT_tableName
00311 + "=\""
00312 + name
00313 + "\" "
00314 + DatabasesXmlTags.ATT_nbOfColumns
00315 + "=\""
00316 + columns.size()
00317 + "\">");
00318 for (int i = 0; i < columns.size(); i++)
00319 info.append(((DatabaseColumn) columns.get(i)).getXml());
00320 info.append("</" + DatabasesXmlTags.ELT_DatabaseTable + ">");
00321 return info.toString();
00322 }
00323 }