Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members

FODBSodaQuery.java

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.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.exception.FODBException;
00041 import org.openmobileis.database.fastobjectdb.db.index.FODBIndex;
00042 
00118 public final class FODBSodaQuery implements Query {
00119         private static int EQUALS=-1;
00120         private static int SUP=1;
00121         private static int INF=2;
00122         private CollectionManager colManager;
00123         private boolean queryNoError = true;
00124         private FODBSodaConstraint firstConstraint;
00125         private int constraintId = 0;
00126         private FODBCollection collection; 
00127         private FastObjectDB db;
00128         
00132   public FODBSodaQuery(CollectionManager colManager, FastObjectDB db) {
00133     super();
00134     this.colManager = colManager;
00135     this.db = db;
00136   }
00137 
00138   /* (non-Javadoc)
00139    * @see org.odbms.Query#constrain(java.lang.Object)
00140    */
00141   public Constraint constrain(Object constraint) {
00142         if (constraint instanceof Class)        {
00143                         FODBCollection col = colManager.getCollectionByType((Class) constraint);
00144                         if (col != null)        {
00145                                 FODBSodaConstraint constr = new FODBSodaConstraint(this, constraintId++);
00146                                 firstConstraint = constr;
00147                                 try     {
00148                                         db.getTransactionManager().enterTransaction(col.getName());
00149                                 } catch (FODBException ex)      {
00150                                         LogManager.traceError(0, ex);
00151                                 }
00152                                 this.setCollection(col);
00153                         }
00154                 } else  {
00155                         if (constraint instanceof String)       {
00156                                 FODBCollection col = colManager.getCollectionByName((String) constraint);
00157                                 if ((col != null) && (this.collection == null)) {
00158                                         this.setCollection(col);
00159                                         if (firstConstraint == null)    {
00160                                                 FODBSodaConstraint constr = new FODBSodaConstraint(this, constraintId++);
00161                                                 firstConstraint = constr;
00162                                         }
00163                                         try     {
00164                                                 db.getTransactionManager().enterTransaction(col.getName());
00165                                         } catch (FODBException ex)      {
00166                                                 LogManager.traceError(0, ex);
00167                                         }
00168                                         return firstConstraint;
00169                                 }
00170                         }
00171                         FODBSodaConstraint path = (FODBSodaConstraint)firstConstraint;
00172                         if (path.hasOperande)   { //if already declared create a new constraint. One constraint per operation.
00173                                 path = new FODBSodaConstraint(this, constraintId++);
00174                                 FODBIndex index = collection.getIndexByMember(firstConstraint.getIndexMember());
00175                                 if (index != null)      {
00176                                         FODBSodaIndexPath ipath = new FODBSodaIndexPath(index);
00177                                         path.setIndex(ipath);
00178                                 }
00179                         }
00180                         this.setError(path.setOperand(constraint));
00181                         return path;
00182         }
00183         return firstConstraint;
00184   }
00185 
00186   /* (non-Javadoc)
00187    * @see org.odbms.Query#constraints()
00188    */
00189   public Constraints constraints() {
00190                 return null;
00191   }
00192 
00193   /* (non-Javadoc)
00194    * @see org.odbms.Query#execute()
00195    */
00196   public ObjectSet execute() {
00197                 FODBSodaObjectSet set = new FODBSodaObjectSet();
00198         if ((queryNoError) && (firstConstraint != null))        {
00199                 try     {
00200                                 db.getTransactionManager().begin();
00201                         try     {
00202                                 LongArray arrayLong = new LongArray();
00203                         firstConstraint.execute(arrayLong);
00204                         set.addCollectionPtrs(arrayLong.toArray(), this.collection);
00205                         } finally       {
00206                                         db.getTransactionManager().commit();
00207                         }
00208                 } catch (Throwable ex)  {
00209                         LogManager.traceError(0, ex);
00210                 }
00211         }
00212     return set;
00213   }
00214 
00215   /* (non-Javadoc)
00216    * @see org.odbms.Query#descend(java.lang.String)
00217    */
00218   public Query descend(String fieldName) {
00219         if (firstConstraint != null)    {
00220                         FODBSodaQuery retQuery = this;
00221                         FODBSodaConstraint constr = firstConstraint;
00222                 if (firstConstraint.hasIndex()) {
00223                                 retQuery = new FODBSodaQuery(colManager, db);
00224                                 retQuery.setCollection(this.collection);
00225                                 constr = new FODBSodaConstraint(this, constraintId++);
00226                                 retQuery.setFirstConstraint(constr);
00227 //                              this.firstConstraint.setNextConstraint(constr, BaseConstraint.CONSTRAINT_AND);
00228                 }
00229                         FODBIndex index = collection.getIndexByMember(fieldName);
00230                         if (index != null)      {
00231                                 FODBSodaIndexPath ipath = new FODBSodaIndexPath(index);
00232                                 retQuery.getFirstConstraint().setIndex(ipath);
00233                                 return retQuery;
00234                         } else  {
00235                                 this.setError(false);
00236                         }
00237         } else  {
00238                 this.setError(false);
00239         }
00240                 return null;
00241   }
00242   
00243   private void setError(boolean error)  {
00244         if (!error)     {
00245                 queryNoError = error;
00246         }
00247         
00248   }
00249 
00250   /* (non-Javadoc)
00251    * @see org.odbms.Query#orderAscending()
00252    */
00253   public Query orderAscending() {
00254     // TODO Auto-generated method stub
00255     return null;
00256   }
00257 
00258   /* (non-Javadoc)
00259    * @see org.odbms.Query#orderDescending()
00260    */
00261   public Query orderDescending() {
00262     // TODO Auto-generated method stub
00263     return null;
00264   }
00265 
00266         /* (non-Javadoc)
00267          * @see org.odbms.Constraint#or(org.odbms.Constraint)
00268          */
00269         Constraint or(Constraint with) {
00270                 // TODO Auto-generated method stub
00271                 return null;
00272         }
00273 
00277   void setCollection(FODBCollection collection) {
00278     this.collection = collection;
00279   }
00280 
00284   void setFirstConstraint(FODBSodaConstraint constraint) {
00285     firstConstraint = constraint;
00286   }
00287 
00288   FODBSodaConstraint getFirstConstraint() {
00289     return firstConstraint;
00290   }
00291 
00292 }

Generated on Thu Oct 6 10:06:32 2005 for OpenMobileIS by  doxygen 1.4.3