00001 /* 00002 * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM) 00003 * Copyright (C) 2004-2006 Philippe Delrieu 00004 * All rights reserved. 00005 * Contact: pdelrieu@openmobileis.org 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.odbms.Constraints; 00033 import org.odbms.ObjectSet; 00034 import org.odbms.Query; 00035 import org.openmobileis.common.util.collection.LongArray; 00036 import org.openmobileis.common.util.log.LogManager; 00037 import org.openmobileis.database.fastobjectdb.FastObjectDB; 00038 import org.openmobileis.database.fastobjectdb.db.CollectionManager; 00039 import org.openmobileis.database.fastobjectdb.db.FODBCollection; 00040 import org.openmobileis.database.fastobjectdb.db.index.FODBIndex; 00041 00117 public final class FODBSodaQuery implements Query { 00118 private CollectionManager colManager; 00119 private boolean queryNoError = true; 00120 private FODBSodaConstraint firstConstraint; 00121 private int constraintId = 0; 00122 private FODBCollection collection; 00123 private FastObjectDB db; 00124 00128 public FODBSodaQuery(CollectionManager colManager, FastObjectDB db) { 00129 super(); 00130 this.colManager = colManager; 00131 this.db = db; 00132 } 00133 00134 /* (non-Javadoc) 00135 * @see org.odbms.Query#constrain(java.lang.Object) 00136 */ 00137 public Constraint constrain(Object constraint) { 00138 if (constraint instanceof Class) { 00139 FODBCollection col = colManager.getCollectionByType((Class) constraint); 00140 if (col != null) { 00141 FODBSodaConstraint constr = new FODBSodaConstraint(this, constraintId++); 00142 firstConstraint = constr; 00143 this.setCollection(col); 00144 } 00145 } else { 00146 if (constraint instanceof String) { 00147 FODBCollection col = colManager.getCollectionByName((String) constraint); 00148 if ((col != null) && (this.collection == null)) { 00149 this.setCollection(col); 00150 if (firstConstraint == null) { 00151 FODBSodaConstraint constr = new FODBSodaConstraint(this, constraintId++); 00152 firstConstraint = constr; 00153 } 00154 return firstConstraint; 00155 } 00156 } 00157 FODBSodaConstraint path = (FODBSodaConstraint)firstConstraint; 00158 if (path.hasOperande) { //if already declared create a new constraint. One constraint per operation. 00159 path = new FODBSodaConstraint(this, constraintId++); 00160 FODBIndex index = collection.getIndexByMember(firstConstraint.getIndexMember()); 00161 if (index != null) { 00162 FODBSodaIndexPath ipath = new FODBSodaIndexPath(index); 00163 path.setIndex(ipath); 00164 } 00165 } 00166 this.setError(path.setOperand(constraint)); 00167 return path; 00168 } 00169 return firstConstraint; 00170 } 00171 00172 /* (non-Javadoc) 00173 * @see org.odbms.Query#constraints() 00174 */ 00175 public Constraints constraints() { 00176 return null; 00177 } 00178 00179 /* (non-Javadoc) 00180 * @see org.odbms.Query#execute() 00181 */ 00182 public ObjectSet execute() { 00183 FODBSodaObjectSet set = new FODBSodaObjectSet(); 00184 if ((queryNoError) && (firstConstraint != null)) { 00185 try { 00186 db.getTransactionManager().begin(); 00187 try { 00188 db.getTransactionManager().enterTransaction(this.collection.getName()); 00189 LongArray arrayLong = new LongArray(); 00190 firstConstraint.execute(arrayLong); 00191 set.addCollectionPtrs(arrayLong.toArray(), this.collection); 00192 } finally { 00193 db.getTransactionManager().commit(); 00194 } 00195 } catch (Throwable ex) { 00196 LogManager.traceError(0, ex); 00197 } 00198 } 00199 return set; 00200 } 00201 00202 /* (non-Javadoc) 00203 * @see org.odbms.Query#descend(java.lang.String) 00204 */ 00205 public Query descend(String fieldName) { 00206 if (firstConstraint != null) { 00207 FODBSodaQuery retQuery = this; 00208 FODBSodaConstraint constr = firstConstraint; 00209 if (firstConstraint.hasIndex()) { 00210 retQuery = new FODBSodaQuery(colManager, db); 00211 retQuery.setCollection(this.collection); 00212 constr = new FODBSodaConstraint(this, constraintId++); 00213 retQuery.setFirstConstraint(constr); 00214 // this.firstConstraint.setNextConstraint(constr, BaseConstraint.CONSTRAINT_AND); 00215 } 00216 FODBIndex index = collection.getIndexByMember(fieldName); 00217 if (index != null) { 00218 FODBSodaIndexPath ipath = new FODBSodaIndexPath(index); 00219 retQuery.getFirstConstraint().setIndex(ipath); 00220 return retQuery; 00221 } else { 00222 this.setError(false); 00223 } 00224 } else { 00225 this.setError(false); 00226 } 00227 return null; 00228 } 00229 00230 private void setError(boolean error) { 00231 if (!error) { 00232 queryNoError = error; 00233 } 00234 00235 } 00236 00237 /* (non-Javadoc) 00238 * @see org.odbms.Query#orderAscending() 00239 */ 00240 public Query orderAscending() { 00241 // TODO Auto-generated method stub 00242 return null; 00243 } 00244 00245 /* (non-Javadoc) 00246 * @see org.odbms.Query#orderDescending() 00247 */ 00248 public Query orderDescending() { 00249 // TODO Auto-generated method stub 00250 return null; 00251 } 00252 00253 /* (non-Javadoc) 00254 * @see org.odbms.Constraint#or(org.odbms.Constraint) 00255 */ 00256 Constraint or(Constraint with) { 00257 // TODO Auto-generated method stub 00258 return null; 00259 } 00260 00264 void setCollection(FODBCollection collection) { 00265 this.collection = collection; 00266 } 00267 00271 void setFirstConstraint(FODBSodaConstraint constraint) { 00272 firstConstraint = constraint; 00273 } 00274 00275 FODBSodaConstraint getFirstConstraint() { 00276 return firstConstraint; 00277 } 00278 00279 }