1 17 package org.apache.ws.jaxme.sqls.impl; 18 19 import java.util.ArrayList ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 23 import org.apache.ws.jaxme.sqls.Column; 24 import org.apache.ws.jaxme.sqls.ColumnSet; 25 import org.apache.ws.jaxme.sqls.DeleteStatement; 26 import org.apache.ws.jaxme.sqls.ForeignKey; 27 import org.apache.ws.jaxme.sqls.Index; 28 import org.apache.ws.jaxme.sqls.InsertStatement; 29 import org.apache.ws.jaxme.sqls.SQLFactory; 30 import org.apache.ws.jaxme.sqls.Schema; 31 import org.apache.ws.jaxme.sqls.SelectStatement; 32 import org.apache.ws.jaxme.sqls.Table; 33 import org.apache.ws.jaxme.sqls.TableReference; 34 import org.apache.ws.jaxme.sqls.UpdateStatement; 35 36 37 40 public class TableImpl implements Table { 41 public static class NameImpl extends SQLFactoryImpl.IdentImpl implements Table.Name { 42 public NameImpl(String pName) { 43 super(pName); 44 } 45 } 46 47 private Schema schema; 48 private Table.Name name; 49 private List columns = new ArrayList (); 50 private List indexes = new ArrayList (); 51 private List foreignKeys = new ArrayList (); 52 53 int indexNameCounter; 54 55 protected TableImpl(Schema pSchema, Table.Name pName) { 56 schema = pSchema; 57 name = pName; 58 } 59 60 public Schema getSchema() { 61 return schema; 62 } 63 64 public Table.Name getName() { 65 return name; 66 } 67 68 public String getQName() { 69 Schema mySchema = getSchema(); 70 if (mySchema.getName() == null) { 71 return getName().getName(); 72 } else { 73 return mySchema.getName().getName() + "." + getName().getName(); 74 } 75 } 76 77 public Iterator getColumns() { 78 return columns.iterator(); 79 } 80 81 public Column newColumn(String pName, Column.Type pType) { 82 return newColumn(new ColumnImpl.NameImpl(pName), pType); 83 } 84 85 public Column newColumn(Column.Name pName, Column.Type pType) { 86 if (pName == null) { 87 throw new NullPointerException ("The column name must not be null."); 88 } 89 Integer maxLength = getSchema().getSQLFactory().getMaxColumnNameLength(); 90 if (maxLength != null && pName.getName().length() > maxLength.intValue()) { 91 throw new IllegalArgumentException ("The column name " + pName + 92 " exceeds the maximum column length of " + maxLength); 93 } 94 95 if (pType == null) { 96 throw new NullPointerException ("The column type must not be null."); 97 } 98 Column column = getColumn(pName); 99 if (column != null) { 100 throw new IllegalStateException ("A column named " + column.getName() + 101 " already exists in the table " + getQName()); 102 } 103 column = ((SQLFactoryImpl) getSchema().getSQLFactory()).newColumnImpl(this, pName, pType); 104 columns.add(column); 105 return column; 106 } 107 108 public Column getColumn(Column.Name pName) { 109 if (pName == null) { 110 throw new NullPointerException ("Column names must not be null."); 111 } 112 for (Iterator iter = getColumns(); iter.hasNext(); ) { 113 Column column = (Column) iter.next(); 114 if (getSchema().getSQLFactory().isColumnNameCaseSensitive()) { 115 if (pName.getName().equalsIgnoreCase(column.getName().getName())) { 116 return column; 117 } 118 } else { 119 if (pName.equals(column.getName())) { 120 return column; 121 } 122 } 123 } 124 return null; 125 } 126 127 public Column getColumn(String pName) { 128 return getColumn(new ColumnImpl.NameImpl(pName)); 129 } 130 131 public Index newKey() { 132 Index result = new IndexImpl(this, true, false); 133 indexes.add(result); 134 return result; 135 } 136 137 public Index newIndex() { 138 Index result = new IndexImpl(this, false, false); 139 indexes.add(result); 140 return result; 141 } 142 143 public Index getPrimaryKey() { 144 for (Iterator iter = getIndexes(); iter.hasNext(); ) { 145 Index index = (Index) iter.next(); 146 if (index.isPrimaryKey()) { 147 return index; 148 } 149 } 150 return null; 151 } 152 153 public Iterator getIndexes() { 154 return indexes.iterator(); 155 } 156 157 public Index newPrimaryKey() { 158 Index pk = getPrimaryKey(); 159 if (pk != null) { 160 throw new IllegalStateException ("A primary key is already defined on table " + getName()); 161 } 162 Index result = new IndexImpl(this, true, true); 163 indexes.add(result); 164 return result; 165 } 166 167 public Iterator getForeignKeys() { 168 return foreignKeys.iterator(); 169 } 170 171 public ForeignKey newForeignKey(Table pTable) { 172 ForeignKey foreignKey = new ForeignKeyImpl(this, pTable); 173 foreignKeys.add(foreignKey); 174 return foreignKey; 175 } 176 177 public boolean equals(Object o) { 178 if (o == null || !(o instanceof Table)) { 179 return false; 180 } 181 Table other = (Table) o; 182 if (!getSchema().equals(other.getSchema())) { 183 return false; 184 } 185 return getName().equals(other.getName()); 186 } 187 188 public int hashCode() { 189 return getSchema().hashCode() + getName().hashCode(); 190 } 191 192 public InsertStatement getInsertStatement() { 193 InsertStatement result = getSchema().getSQLFactory().newInsertStatement(); 194 result.setTable(this); 195 for (Iterator iter = getColumns(); iter.hasNext(); ) { 196 result.addSet((Column) iter.next()); 197 } 198 return result; 199 } 200 201 public SelectStatement getSelectStatement() { 202 SQLFactory factory = getSchema().getSQLFactory(); 203 SelectStatement result = factory.newSelectStatement(); 204 result.setTable(this); 205 TableReference ref = result.getTableReference(); 206 for (Iterator iter = getColumns(); iter.hasNext(); ) { 207 Column column = (Column) iter.next(); 208 result.addResultColumn(factory.getObjectFactory().newColumnReference(ref, column)); 209 } 210 return result; 211 } 212 213 public UpdateStatement getUpdateStatement() { 214 UpdateStatement result = getSchema().getSQLFactory().newUpdateStatement(); 215 result.setTable(this); 216 TableReference ref = result.getTableReference(); 217 boolean hasPrimaryKey = false; 218 for (Iterator iter = getColumns(); iter.hasNext(); ) { 219 Column column = (Column) iter.next(); 220 if (column.isPrimaryKeyPart()) { 221 hasPrimaryKey = true; 222 } else { 223 result.addSet(column); 224 } 225 } 226 if (hasPrimaryKey) { 227 result.getWhere().addColumnSetQuery(getPrimaryKey(), ref); 228 } else { 229 throw new IllegalStateException ("Cannot create a default update statement without a primary key."); 230 } 231 return result; 232 } 233 234 public DeleteStatement getDeleteStatement() { 235 DeleteStatement result = getSchema().getSQLFactory().newDeleteStatement(); 236 result.setTable(this); 237 ColumnSet primaryKey = getPrimaryKey(); 238 if (primaryKey == null) { 239 throw new IllegalStateException ("Cannot create a default delete statement without a primary key."); 240 } 241 result.getWhere().addColumnSetQuery(primaryKey, result.getTableReference()); 242 return result; 243 } 244 } 245 | Popular Tags |