00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
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
00139
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) {
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
00187
00188
00189 public Constraints constraints() {
00190 return null;
00191 }
00192
00193
00194
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
00216
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
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
00251
00252
00253 public Query orderAscending() {
00254
00255 return null;
00256 }
00257
00258
00259
00260
00261 public Query orderDescending() {
00262
00263 return null;
00264 }
00265
00266
00267
00268
00269 Constraint or(Constraint with) {
00270
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 }