001: /**
002: * Copyright (C) 2001-2004 France Telecom R&D
003: *
004: * This library is free software; you can redistribute it and/or
005: * modify it under the terms of the GNU Lesser General Public
006: * License as published by the Free Software Foundation; either
007: * version 2 of the License, or (at your option) any later version.
008: *
009: * This library is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012: * Lesser General Public License for more details.
013: *
014: * You should have received a copy of the GNU Lesser General Public
015: * License along with this library; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */package org.objectweb.speedo.query.jdo;
018:
019: import org.objectweb.medor.tuple.api.TupleCollection;
020: import org.objectweb.medor.tuple.api.Tuple;
021: import org.objectweb.medor.api.TupleStructure;
022: import org.objectweb.medor.expression.api.ParameterOperand;
023: import org.objectweb.medor.api.MedorException;
024: import org.objectweb.medor.api.EvaluationException;
025: import org.objectweb.speedo.pm.api.POManagerItf;
026: import org.objectweb.speedo.pm.jdo.api.JDOPOManagerItf;
027: import org.objectweb.speedo.query.api.QueryDefinition;
028:
029: /**
030: * Is in charge of making the union of Medor query. This union is hidden behind
031: * the TupleCollection interface, but the TupleStructure is variable.
032: *
033: * @author S.Chassande-Barrioz
034: */
035: public class JDOQueriesUnion implements TupleCollection {
036:
037: /**
038: * The common parameter operand of the queries
039: */
040: ParameterOperand[] pos;
041:
042: /**
043: * The PersistenceManager holding the query
044: */
045: JDOPOManagerItf pm;
046:
047: /**
048: * is the current TupleCollection over which the iteration is done
049: */
050: TupleCollection currentTC;
051:
052: /**
053: * Is the index of the current query which is evaluated or read
054: */
055: int queryIdx;
056:
057: /**
058: * Is the commong connection to access the persistent support
059: */
060: Object connection;
061:
062: JDOQueryEvalContext[] qecs;
063:
064: int row = 0;
065: boolean hasResult = false;
066:
067: public JDOQueriesUnion(ParameterOperand[] pos, JDOPOManagerItf pm,
068: Object connection, JDOQueryEvalContext[] qecs,
069: QueryDefinition userqd) throws MedorException {
070: queryIdx = -1;
071: this .pos = pos;
072: this .pm = pm;
073: this .connection = connection;
074: this .qecs = qecs;
075: calculateNext();
076: }
077:
078: private boolean calculateNext() throws EvaluationException,
079: MedorException {
080: queryIdx++;
081: row++;
082: currentTC = null;
083: if (qecs.length <= queryIdx) {
084: return false;
085: }
086: currentTC = qecs[queryIdx].eval(pm, pos, connection, null);
087: if (currentTC == null || currentTC.isEmpty()) {
088: calculateNext();
089: }
090: hasResult |= currentTC != null;
091: if (currentTC == null) {
092: row = 0;
093: }
094: return currentTC != null;
095: }
096:
097: //IMPLEMENTATION OF THE TupleCollection INTERFACE //
098: //------------------------------------------------//
099:
100: public boolean next() throws MedorException {
101: return currentTC.next() || calculateNext();
102: }
103:
104: public void first() throws MedorException {
105: if (row != 1) {
106: queryIdx = -1;
107: row = 0;
108: calculateNext();
109: }
110: }
111:
112: public Tuple getTuple() throws MedorException {
113: if (currentTC == null) {
114: throw new MedorException("No more result");
115: }
116: return currentTC.getTuple();
117: }
118:
119: public boolean isEmpty() throws MedorException {
120: return !hasResult;
121: }
122:
123: public void close() throws MedorException {
124: if (currentTC != null) {
125: currentTC.close();
126: }
127: }
128:
129: public TupleStructure getMetaData() throws MedorException {
130: return currentTC.getMetaData();
131: }
132:
133: public boolean isLast() throws MedorException {
134: throw new MedorException("IsLast not yet suuported");
135: }
136:
137: public Tuple getTuple(int row) throws MedorException {
138: throw new MedorException("getTuple(int) not yet suuported");
139: }
140:
141: public boolean row(int row) throws MedorException {
142: throw new MedorException("row(int) not yet suuported");
143: }
144:
145: public int getRow() throws MedorException {
146: throw new MedorException("getRow not yet suuported");
147: }
148: }
|