00001 /* 00002 * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM) 00003 * Copyright (C) 2004-2005 Philippe Delrieu 00004 * All rights reserved. 00005 * Contact: openmobileis@e-care.fr 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00020 * USA 00021 * 00022 * Author : Philippe Delrieu 00023 * 00024 * Modifications : 00025 * 2004 Creation P.Delrieu 00026 * 2004 Modified by Romain Beaugrand 00027 * 00028 */ 00029 package org.openmobileis.database.fastobjectdb.db.query.soda; 00030 00031 import org.odbms.Constraint; 00032 import org.openmobileis.common.util.collection.LongArray; 00033 import org.openmobileis.database.fastobjectdb.db.exception.FODBException; 00034 00041 public class FODBSodaConstraint extends BaseConstraint { 00042 00043 00044 private FODBSodaIndexPath indexpath; 00045 private boolean error; 00046 private int constraintId; 00047 boolean hasOperande = false; 00048 00049 public FODBSodaConstraint(FODBSodaQuery query, int constraintId) { 00050 super(); 00051 this.constraintId = constraintId; 00052 } 00053 00054 void setIndex(FODBSodaIndexPath index) { 00055 indexpath = index; 00056 } 00057 00058 String getIndexMember() { 00059 return indexpath.getIndexMember(); 00060 } 00061 00062 private void setError(boolean err) { 00063 if (!err) this.error = false; 00064 } 00065 00066 boolean setOperand(Object ope) { 00067 if (indexpath != null) { 00068 this.setHasOperande(true); 00069 return indexpath.setOperand(ope); 00070 } 00071 return false; 00072 } 00073 00074 boolean addComparator(int ope) { 00075 if (indexpath != null) { 00076 return indexpath.addComparator(ope); 00077 } 00078 return false; 00079 } 00080 00081 protected void execute(LongArray longArray) throws FODBException { 00082 if (error) throw new FODBException("Bad Query construction."); 00083 BaseConstraint nextConst = this.getNextConstraint(); 00084 if (nextConst != null) { 00085 nextConst.execute(longArray); 00086 } 00087 // execute current request 00088 long[] newPtrList = indexpath.execute(); 00089 if (this.getNextConstraint() == null) { 00090 longArray.add(newPtrList); 00091 } else if (this.nextContraintLink == BaseConstraint.CONSTRAINT_OR) { 00092 longArray.addUnion(newPtrList); 00093 } else { 00094 longArray.addIntersect(newPtrList); 00095 } 00096 } 00097 00098 00099 /* (non-Javadoc) 00100 * @see org.odbms.Constraint#and(org.odbms.Constraint) 00101 */ 00102 public Constraint and(Constraint with) { 00103 FODBSodaConstraint newConst = (FODBSodaConstraint)with; 00104 if (newConst.getIndexMember().equals(this.getIndexMember())) { //AND are done in the same tree search if possible 00105 indexpath.mergeIndexComporator(newConst.indexpath.getIndexComparator()); 00106 if (newConst.getNextConstraint() != null) { 00107 this.setNextConstraint(newConst.getNextConstraint(), newConst.nextContraintLink); 00108 } 00109 } else { 00110 this.setNextConstraint((BaseConstraint)with, BaseConstraint.CONSTRAINT_AND); 00111 } 00112 return this; 00113 } 00114 00115 /* (non-Javadoc) 00116 * @see org.odbms.Constraint#or(org.odbms.Constraint) 00117 */ 00118 public Constraint or(Constraint with) { 00119 this.setNextConstraint((BaseConstraint)with, BaseConstraint.CONSTRAINT_OR); 00120 return this; 00121 } 00122 /* (non-Javadoc) 00123 * @see org.odbms.Constraint#equal() 00124 */ 00125 public Constraint equal() { 00126 this.setError(this.addComparator(BaseConstraint.EQUALS)); 00127 return this; 00128 } 00129 00130 /* (non-Javadoc) 00131 * @see org.odbms.Constraint#greater() 00132 */ 00133 public Constraint greater() { 00134 this.setError(this.addComparator(BaseConstraint.GREATER)); 00135 return this; 00136 } 00137 00138 /* (non-Javadoc) 00139 * @see org.odbms.Constraint#smaller() 00140 */ 00141 public Constraint smaller() { 00142 this.setError(this.addComparator(BaseConstraint.SMALLER)); 00143 return this; 00144 } 00145 00146 /* (non-Javadoc) 00147 * @see org.odbms.Constraint#identity() 00148 */ 00149 public Constraint identity() { 00150 this.setError(this.addComparator(BaseConstraint.IDENTITY)); 00151 return this; 00152 } 00153 00154 /* (non-Javadoc) 00155 * @see org.odbms.Constraint#like() 00156 */ 00157 public Constraint like() { 00158 this.setError(this.addComparator(BaseConstraint.LIKE)); 00159 return this; 00160 } 00161 00167 public Constraint distinct () { 00168 this.setError(this.addComparator(BaseConstraint.DISTINCT)); 00169 return this; 00170 } 00171 00172 /* (non-Javadoc) 00173 * @see org.odbms.Constraint#contains() 00174 */ 00175 public Constraint contains() { 00176 this.setError(this.addComparator(BaseConstraint.CONTAINS)); 00177 return this; 00178 } 00179 00180 /* (non-Javadoc) 00181 * @see org.odbms.Constraint#not() 00182 */ 00183 public Constraint not() { 00184 this.setError(this.addComparator(BaseConstraint.NOT)); 00185 return this; 00186 } 00187 00188 /* (non-Javadoc) 00189 * @see org.odbms.Constraint#getObject() 00190 */ 00191 public Object getObject() { 00192 // TODO Auto-generated method stub 00193 return null; 00194 } 00195 00196 boolean hasIndex() { 00197 return (indexpath!=null); 00198 } 00202 public int getConstraintId() { 00203 return constraintId; 00204 } 00205 00206 public boolean equals(Object con) { 00207 if (con instanceof FODBSodaConstraint) { 00208 if (((FODBSodaConstraint)con).constraintId == this.constraintId) { 00209 return true; 00210 } 00211 } 00212 return false; 00213 } 00214 00218 boolean isHasOperande() { 00219 return hasOperande; 00220 } 00221 00225 void setHasOperande(boolean b) { 00226 hasOperande = b; 00227 } 00228 00229 }