/**
* Copyright (C) 2001-2004 France Telecom R&D
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package org.objectweb.speedo.query.jdo;
import org.objectweb.medor.tuple.api.TupleCollection;
import org.objectweb.medor.tuple.api.Tuple;
import org.objectweb.medor.api.TupleStructure;
import org.objectweb.medor.expression.api.ParameterOperand;
import org.objectweb.medor.api.MedorException;
import org.objectweb.medor.api.EvaluationException;
import org.objectweb.speedo.pm.api.POManagerItf;
import org.objectweb.speedo.pm.jdo.api.JDOPOManagerItf;
import org.objectweb.speedo.query.api.QueryDefinition;
/**
* Is in charge of making the union of Medor query. This union is hidden behind
* the TupleCollection interface, but the TupleStructure is variable.
*
* @author S.Chassande-Barrioz
*/
public class JDOQueriesUnion implements TupleCollection {
/**
* The common parameter operand of the queries
*/
ParameterOperand[] pos;
/**
* The PersistenceManager holding the query
*/
JDOPOManagerItf pm;
/**
* is the current TupleCollection over which the iteration is done
*/
TupleCollection currentTC;
/**
* Is the index of the current query which is evaluated or read
*/
int queryIdx;
/**
* Is the commong connection to access the persistent support
*/
Object connection;
JDOQueryEvalContext[] qecs;
int row = 0;
boolean hasResult = false;
public JDOQueriesUnion(ParameterOperand[] pos,
JDOPOManagerItf pm,
Object connection,
JDOQueryEvalContext[] qecs,
QueryDefinition userqd) throws MedorException {
queryIdx = -1;
this.pos = pos;
this.pm = pm;
this.connection = connection;
this.qecs = qecs;
calculateNext();
}
private boolean calculateNext() throws EvaluationException, MedorException {
queryIdx++;
row++;
currentTC = null;
if (qecs.length <= queryIdx) {
return false;
}
currentTC = qecs[queryIdx].eval(pm, pos, connection, null);
if (currentTC == null || currentTC.isEmpty()) {
calculateNext();
}
hasResult |= currentTC!= null;
if (currentTC == null) {
row = 0;
}
return currentTC != null;
}
//IMPLEMENTATION OF THE TupleCollection INTERFACE //
//------------------------------------------------//
public boolean next() throws MedorException {
return currentTC.next() || calculateNext();
}
public void first() throws MedorException {
if (row != 1) {
queryIdx = -1;
row = 0;
calculateNext();
}
}
public Tuple getTuple() throws MedorException {
if (currentTC == null) {
throw new MedorException("No more result");
}
return currentTC.getTuple();
}
public boolean isEmpty() throws MedorException {
return !hasResult;
}
public void close() throws MedorException {
if (currentTC != null) {
currentTC.close();
}
}
public TupleStructure getMetaData() throws MedorException {
return currentTC.getMetaData();
}
public boolean isLast() throws MedorException {
throw new MedorException("IsLast not yet suuported");
}
public Tuple getTuple(int row) throws MedorException {
throw new MedorException("getTuple(int) not yet suuported");
}
public boolean row(int row) throws MedorException {
throw new MedorException("row(int) not yet suuported");
}
public int getRow() throws MedorException {
throw new MedorException("getRow not yet suuported");
}
}