1 24 25 package org.objectweb.cjdbc.common.sql.schema; 26 27 import java.io.Serializable ; 28 import java.sql.SQLException ; 29 import java.util.ArrayList ; 30 import java.util.Iterator ; 31 32 import org.objectweb.cjdbc.common.xml.DatabasesXmlTags; 33 34 45 public class DatabaseTable implements Serializable 46 { 47 private static final long serialVersionUID = 7138810420058450235L; 48 49 50 private String name; 51 52 53 private ArrayList columns; 54 55 60 public DatabaseTable(String name) 61 { 62 this(name, new ArrayList ()); 63 } 64 65 71 public DatabaseTable(String name, int nbOfColumns) 72 { 73 this(name, new ArrayList (nbOfColumns)); 74 } 75 76 82 private DatabaseTable(String name, ArrayList columns) 83 { 84 if (name == null) 85 throw new IllegalArgumentException ( 86 "Illegal null database table name in DatabaseTable constructor"); 87 88 this.name = name; 89 this.columns = columns; 90 } 91 92 97 public String getName() 98 { 99 return name; 100 } 101 102 109 public void addColumn(DatabaseColumn column) 110 { 111 columns.add(column); 112 } 113 114 122 public void remove(String columnName) 123 { 124 columns.remove(getColumn(columnName)); 125 } 126 127 134 public void remove(DatabaseColumn column) 135 { 136 columns.remove(column); 137 } 138 139 147 public void mergeColumns(DatabaseTable table) throws SQLException 148 { 149 if (table == null) 150 return; 151 152 ArrayList otherColumns = table.getColumns(); 153 if (otherColumns == null) 154 return; 155 156 DatabaseColumn c, original; 157 int size = otherColumns.size(); 158 for (int i = 0; i < size; i++) 159 { 160 c = (DatabaseColumn) otherColumns.get(i); 161 original = getColumn(c.getName()); 162 if (original == null) 163 addColumn(c); 164 else 165 { 166 if (!original.equalsIgnoreType(c)) 167 throw new SQLException ("Unable to merge table [" + table.getName() 168 + "]: column '" + c.getName() + "' definition mismatch"); 169 } 170 } 171 } 172 173 181 public ArrayList getColumns() 182 { 183 return columns; 184 } 185 186 195 public ArrayList getUniqueColumns() 196 { 197 ArrayList cols = new ArrayList (); 198 Iterator i; 199 DatabaseColumn c; 200 201 for (i = columns.iterator(); i.hasNext();) 202 { 203 c = (DatabaseColumn) i.next(); 204 if (c.isUnique()) 205 cols.add(c); 206 } 207 return cols; 208 } 209 210 217 public DatabaseColumn getColumn(String columnName) 218 { 219 DatabaseColumn c; 220 for (Iterator i = columns.iterator(); i.hasNext();) 221 { 222 c = (DatabaseColumn) i.next(); 223 if (columnName.equalsIgnoreCase(c.getName())) 224 return c; 225 226 } 227 return null; 228 } 229 230 238 public DatabaseColumn getColumn(String columnName, boolean isCaseSensitive) 239 { 240 if (!isCaseSensitive) 241 return getColumn(columnName); 242 243 DatabaseColumn c; 244 for (Iterator i = columns.iterator(); i.hasNext();) 245 { 246 c = (DatabaseColumn) i.next(); 247 if (columnName.equals(c.getName())) 248 return c; 249 250 } 251 return null; 252 } 253 254 261 public boolean equals(Object other) 262 { 263 if ((other == null) || !(other instanceof DatabaseTable)) 264 return false; 265 266 DatabaseTable t = (DatabaseTable) other; 267 return (t.getName().equals(name)) && (t.getColumns().equals(columns)); 268 } 269 270 278 public boolean equalsIgnoreType(Object other) 279 { 280 if ((other == null) || !(other instanceof DatabaseTable)) 281 return false; 282 283 DatabaseTable t = (DatabaseTable) other; 284 if (!t.getName().equals(name)) 285 return false; 286 287 DatabaseColumn c1, c2; 288 Iterator iter = columns.iterator(); 289 while (iter.hasNext()) 290 { 291 c1 = (DatabaseColumn) iter.next(); 292 c2 = t.getColumn(c1.getName()); 293 294 if (!c1.equalsIgnoreType(c2)) 295 return false; } 297 return true; 298 } 299 300 305 public String getXml() 306 { 307 StringBuffer info = new StringBuffer (); 308 info.append("<" + DatabasesXmlTags.ELT_DatabaseTable + " " 309 + DatabasesXmlTags.ATT_tableName + "=\"" + name + "\" " 310 + DatabasesXmlTags.ATT_nbOfColumns + "=\"" + columns.size() + "\">"); 311 for (int i = 0; i < columns.size(); i++) 312 info.append(((DatabaseColumn) columns.get(i)).getXml()); 313 info.append("</" + DatabasesXmlTags.ELT_DatabaseTable + ">"); 314 return info.toString(); 315 } 316 } 317 | Popular Tags |