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 import java.util.Map ; 23 24 import org.apache.ws.jaxme.sqls.BooleanConstraint; 25 import org.apache.ws.jaxme.sqls.Column; 26 import org.apache.ws.jaxme.sqls.ColumnReference; 27 import org.apache.ws.jaxme.sqls.ColumnSet; 28 import org.apache.ws.jaxme.sqls.CombinedConstraint; 29 import org.apache.ws.jaxme.sqls.ConstrainedStatement; 30 import org.apache.ws.jaxme.sqls.Constraint; 31 import org.apache.ws.jaxme.sqls.ForeignKey; 32 import org.apache.ws.jaxme.sqls.ObjectFactory; 33 import org.apache.ws.jaxme.sqls.SelectStatement; 34 import org.apache.ws.jaxme.sqls.TableReference; 35 import org.apache.ws.jaxme.sqls.Value; 36 37 38 41 public class CombinedConstraintImpl extends ConstraintImpl implements CombinedConstraint { 42 45 public static class TypeImpl extends SQLFactoryImpl.IdentImpl implements CombinedConstraint.Type { 46 48 public TypeImpl(String pName) { 49 super(pName); 50 } 51 } 52 53 private boolean not; 54 private List parts = new ArrayList (); 55 private CombinedConstraint.Type type; 56 57 protected CombinedConstraintImpl(ConstrainedStatement pConstrainedStatement, 58 CombinedConstraint.Type pType) { 59 super(pConstrainedStatement); 60 type = pType; 61 } 62 63 public CombinedConstraint.Type getType() { 64 return type; 65 } 66 67 public CombinedConstraint createAndConstraint() { 68 ObjectFactory f = getConstrainedStatement().getSQLFactory().getObjectFactory(); 69 CombinedConstraint result = f.newCombinedConstraint(getConstrainedStatement(), 70 CombinedConstraint.Type.AND); 71 parts.add(result); 72 return result; 73 } 74 75 public CombinedConstraint createOrConstraint() { 76 ObjectFactory f = getConstrainedStatement().getSQLFactory().getObjectFactory(); 77 CombinedConstraint result = f.newCombinedConstraint(getConstrainedStatement(), 78 CombinedConstraint.Type.OR); 79 parts.add(result); 80 return result; 81 } 82 83 public BooleanConstraint createEQ() { 84 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.EQ); 85 parts.add(result); 86 return result; 87 } 88 89 public BooleanConstraint createNE() { 90 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.NE); 91 parts.add(result); 92 return result; 93 } 94 95 public BooleanConstraint createLT() { 96 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.LT); 97 parts.add(result); 98 return result; 99 } 100 101 public BooleanConstraint createGT() { 102 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.GT); 103 parts.add(result); 104 return result; 105 } 106 107 public BooleanConstraint createLE() { 108 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.LE); 109 parts.add(result); 110 return result; 111 } 112 113 public BooleanConstraint createGE() { 114 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.GE); 115 parts.add(result); 116 return result; 117 } 118 119 public BooleanConstraint createLIKE() { 120 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.LIKE); 121 parts.add(result); 122 return result; 123 } 124 125 public BooleanConstraint createISNULL() { 126 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.ISNULL); 127 parts.add(result); 128 return result; 129 } 130 131 public BooleanConstraint createIN() { 132 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.IN); 133 parts.add(result); 134 return result; 135 } 136 137 public void createEXISTS(SelectStatement pStatement) { 138 BooleanConstraint bc = new BooleanConstraintImpl(this, BooleanConstraint.Type.EXISTS); 139 parts.add(bc); 140 bc.addPart(pStatement); 141 } 142 143 public BooleanConstraint createBETWEEN() { 144 BooleanConstraint result = new BooleanConstraintImpl(this, BooleanConstraint.Type.BETWEEN); 145 parts.add(result); 146 return result; 147 } 148 149 public void addColumnSetQuery(ColumnSet pSet, TableReference pTableReference) { 150 if (!pTableReference.getTable().equals(pSet.getTable())) { 151 throw new IllegalStateException ("The foreign keys referencing table is " + 152 pSet.getTable().getQName() + 153 ", but the arguments referencing table is " + 154 pTableReference.getTable().getQName()); 155 } 156 if (!pTableReference.getStatement().equals(getConstrainedStatement())) { 157 throw new IllegalStateException ("The statement of the table reference is not the same as this constraints statement."); 158 } 159 for (Iterator iter = pSet.getColumns(); iter.hasNext(); ) { 160 Column column = (Column) iter.next(); 161 BooleanConstraint eq = createEQ(); 162 eq.addPart(getConstrainedStatement().getSQLFactory().getObjectFactory().newColumnReference(pTableReference, column)); 163 eq.addPlaceholder(); 164 } 165 } 166 167 public void addJoin(ForeignKey pKey, TableReference pReferencingTable, 168 TableReference pReferencedTable) { 169 if (!pReferencingTable.getTable().equals(pKey.getTable())) { 170 throw new IllegalStateException ("The foreign keys referencing table is " + 171 pKey.getTable().getQName() + 172 ", but the arguments referencing table is " + 173 pReferencingTable.getTable().getQName()); 174 } 175 if (!pReferencedTable.getTable().equals(pKey.getReferencedTable())) { 176 throw new IllegalStateException ("The foreign keys referenced table is " + 177 pKey.getReferencedTable().getQName() + 178 ", but the arguments referenced table is " + 179 pReferencedTable.getTable().getQName()); 180 } 181 if (!pReferencingTable.getStatement().equals(getConstrainedStatement())) { 182 throw new IllegalStateException ("The statement of the referencing table is not the same as this constraints statement."); 183 } 184 if (!pReferencedTable.getStatement().equals(getConstrainedStatement())) { 185 throw new IllegalStateException ("The statement of the referenced table is not the same as this constraints statement."); 186 } 187 188 for (Iterator iter = pKey.getColumnLinks(); iter.hasNext(); ) { 189 ForeignKey.ColumnLink columnReference = (ForeignKey.ColumnLink) iter.next(); 190 BooleanConstraint eq = createEQ(); 191 ObjectFactory of = getConstrainedStatement().getSQLFactory().getObjectFactory(); 192 eq.addPart(of.newColumnReference(pReferencingTable, columnReference.getLocalColumn())); 193 eq.addPart(of.newColumnReference(pReferencedTable, columnReference.getReferencedColumn())); 194 } 195 } 196 197 public void addJoin(TableReference pReferencingTable, 198 ColumnSet pReferencingColumnSet, 199 TableReference pReferencedTable, 200 ColumnSet pReferencedColumnSet) { 201 if (pReferencingTable == null) { throw new NullPointerException ("The referencing table must not be null."); } 202 if (pReferencingColumnSet == null) { throw new NullPointerException ("The referencing column set must not be null."); } 203 if (pReferencedTable == null) { throw new NullPointerException ("The referenced table must not be null."); } 204 if (pReferencedColumnSet == null) { throw new NullPointerException ("The referenced column set must not be null."); } 205 if (!pReferencingTable.getTable().equals(pReferencingColumnSet.getTable())) { 206 throw new IllegalStateException ("The referencing column sets table " + 207 pReferencingColumnSet.getTable().getQName() + 208 " doesn't match the referencing table " + 209 pReferencingTable.getTable().getQName()); 210 } 211 if (!pReferencedTable.getTable().equals(pReferencedColumnSet.getTable())) { 212 throw new IllegalStateException ("The referenced column sets table " + 213 pReferencedColumnSet.getTable().getQName() + 214 " doesn't match the referenced table " + 215 pReferencedTable.getTable().getQName()); 216 } 217 218 Iterator referencedIter = pReferencedColumnSet.getColumns(); 219 for (Iterator iter = pReferencingColumnSet.getColumns(); iter.hasNext(); ) { 220 Column referencingColumn = (Column) iter.next(); 221 if (!referencedIter.hasNext()) { 222 throw new IllegalStateException ("The size of the referencing and referenced column sets doesn't match."); 223 } 224 Column referencedColumn = (Column) referencedIter.next(); 225 BooleanConstraint eq = createEQ(); 226 eq.addPart(pReferencingTable.newColumnReference(referencingColumn)); 227 eq.addPart(pReferencedTable.newColumnReference(referencedColumn)); 228 } 229 } 230 231 232 public int getNumParts() { 233 return parts.size(); 234 } 235 236 public Iterator getParts() { 237 return parts.iterator(); 238 } 239 240 public void addConstraint(Map pMap, Constraint pConstraint) { 241 if (pConstraint instanceof CombinedConstraint) { 242 CombinedConstraint source = (CombinedConstraint) pConstraint; 243 CombinedConstraint target; 244 if (source.getType().equals(getType())) { 245 target = this; 246 } else if (source.getType().equals(CombinedConstraint.Type.AND)) { 247 target = createAndConstraint(); 248 } else if (source.getType().equals(CombinedConstraint.Type.OR)) { 249 target = createOrConstraint(); 250 } else { 251 throw new IllegalStateException ("Unknown combined constraint type: " + 252 source.getType()); 253 } 254 for (Iterator iter = source.getParts(); iter.hasNext(); ) { 255 Object o = iter.next(); 256 if (o instanceof CombinedConstraint) { 257 target.addConstraint(pMap, (CombinedConstraint) o); 258 } else if (o instanceof BooleanConstraint) { 259 target.addConstraint(pMap, (BooleanConstraint) o); 260 } else { 261 throw new IllegalArgumentException ("Invalid part: " + o.getClass().getName()); 262 } 263 } 264 } else if (pConstraint instanceof BooleanConstraint) { 265 BooleanConstraint source = (BooleanConstraint) pConstraint; 266 BooleanConstraint target; 267 BooleanConstraint.Type sourceType = source.getType(); 268 if (BooleanConstraint.Type.EQ.equals(sourceType)) { 269 target = createEQ(); 270 } else if (BooleanConstraint.Type.NE.equals(sourceType)) { 271 target = createNE(); 272 } else if (BooleanConstraint.Type.GT.equals(sourceType)) { 273 target = createGT(); 274 } else if (BooleanConstraint.Type.LT.equals(sourceType)) { 275 target = createLT(); 276 } else if (BooleanConstraint.Type.GE.equals(sourceType)) { 277 target = createGE(); 278 } else if (BooleanConstraint.Type.LE.equals(sourceType)) { 279 target = createLE(); 280 } else if (BooleanConstraint.Type.LIKE.equals(sourceType)) { 281 target = createLIKE(); 282 } else if (BooleanConstraint.Type.ISNULL.equals(sourceType)) { 283 target = createISNULL(); 284 } else if (BooleanConstraint.Type.IN.equals(sourceType)) { 285 target = createIN(); 286 } else { 287 throw new IllegalArgumentException ("Invalid boolean constraint type: " + sourceType); 288 } 289 for (Iterator iter = source.getParts(); iter.hasNext(); ) { 290 Object o = iter.next(); 291 if (o instanceof Value) { 292 target.addPart((Value) o); 293 } else if (o instanceof ColumnReference) { 294 ColumnReference colRef = (ColumnReference) o; 295 TableReference tableRef = (TableReference) pMap.get(colRef.getTableReference()); 296 if (tableRef == null) { 297 throw new IllegalStateException ("Unknown reference to table " + colRef.getTableReference().getTable().getQName()); 298 } 299 target.addPart(tableRef.newColumnReference(colRef.getColumn())); 300 } else { 301 throw new IllegalStateException ("Unknown part type: " + o.getClass().getName()); 302 } 303 } 304 } else { 305 throw new IllegalStateException ("Unknown constraint type: " + pConstraint.getClass().getName()); 306 } 307 } 308 309 public void setNOT(boolean pNot) { 310 not = pNot; 311 } 312 313 public boolean isNOT() { 314 return not; 315 } 316 } 317 | Popular Tags |