All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.jinq.orm.stream.QueryJinqStream Maven / Gradle / Ivy

Go to download

Jinq public API for extending Java 8 streams with database functionality

There is a newer version: 2.0.2
Show newest version
package org.jinq.orm.stream;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import org.jinq.orm.internal.QueryComposer;
import org.jinq.tuples.Pair;
import org.jinq.tuples.Tuple;

public class QueryJinqStream extends NonQueryJinqStream implements JinqStream
{
   QueryComposer queryComposer;
   public QueryJinqStream(QueryComposer query)
   {
      this(query, null);
   }
   
   public QueryJinqStream(QueryComposer query, InQueryStreamSource inQueryStreamSource)
   {
      super(inQueryStreamSource);
      this.queryComposer = query;
   }
   
   protected  JinqStream makeQueryStream(QueryComposer query, InQueryStreamSource inQueryStreamSource)
   {
      return new QueryJinqStream<>(query, inQueryStreamSource);
   }
   
   protected Stream createWrappedStream() 
   {
      return StreamSupport.stream(
            Spliterators.spliteratorUnknownSize(
                  queryComposer.executeAndReturnResultIterator( err -> propagateException(this, err) ), 
                  Spliterator.CONCURRENT), 
            false);
   }
   
   @Override
   public  JinqStream where(final Where test)
   {
      QueryComposer newComposer = queryComposer.where(test);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.where(test);
   }

   @Override
   public  JinqStream where(
         org.jinq.orm.stream.JinqStream.WhereWithSource test)
   {
      QueryComposer newComposer = queryComposer.whereWithSource(test);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.where(test);
   }
   
   @Override
   public  JinqStream select(Select select)
   {
      QueryComposer newComposer = queryComposer.select(select);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.select(select);
   }

   @Override
   public  JinqStream select(SelectWithSource select)
   {
      QueryComposer newComposer = queryComposer.selectWithSource(select);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.select(select);
   }

   @Override
   public  JinqStream selectAll(Join select)
   {
      QueryComposer newComposer = queryComposer.selectAll(select);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.selectAll(select);
   }

   @Override
   public  JinqStream selectAll(JoinWithSource select)
   {
      QueryComposer newComposer = queryComposer.selectAllWithSource(select);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.selectAll(select);
   }

   @Override
   public  JinqStream selectAllList(JoinToIterable select)
   {
      QueryComposer newComposer = queryComposer.selectAllIterable(select);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.selectAllList(select);
   }
   
   @Override
   public  JinqStream> join(Join join)
   {
      QueryComposer> newComposer = queryComposer.join(join);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.join(join);
   }
   
   @Override
   public  JinqStream> join(JoinWithSource join)
   {
      QueryComposer> newComposer = queryComposer.joinWithSource(join);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.join(join);
   }

   @Override
   public  JinqStream> joinList(JoinToIterable join)
   {
      QueryComposer> newComposer = queryComposer.joinIterable(join);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.joinList(join);
   }
   
   @Override
   public  JinqStream> leftOuterJoin(Join join)
   {
      QueryComposer> newComposer = queryComposer.leftOuterJoin(join);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.leftOuterJoin(join);
   }

   @Override
   public  JinqStream> leftOuterJoinList(JoinToIterable join)
   {
      QueryComposer> newComposer = queryComposer.leftOuterJoinIterable(join);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.leftOuterJoinList(join);
   }

   @Override
   public  JinqStream> leftOuterJoin(JoinWithSource join, WhereForOn on)
   {
      QueryComposer> newComposer = queryComposer.leftOuterJoinWithSource(join, on);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.leftOuterJoin(join, on);
   }
   
   @Override
   public  JinqStream> crossJoin(JinqStream join)
   {
      QueryComposer> newComposer = queryComposer.crossJoin(join);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.crossJoin(join);
   }
   
   @Override
   protected  JinqStream groupToTuple(Select select, AggregateGroup[] aggregates)
   {
      QueryComposer newComposer = queryComposer.groupToTuple(select, aggregates);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.groupToTuple(select, aggregates);
   }

//   @Override
//   public  JinqStream> group(Select select, AggregateGroup aggregate)
//   {
//      QueryComposer> newComposer = queryComposer.group(select, aggregate);
//      if (newComposer != null) return new QueryJinqStream>(newComposer, inQueryStreamSource);
//      return super.group(select, aggregate);
//   }
   
   @Override
   public long count()
   {
      Long count = queryComposer.count();
      if (count != null) return count;
      return super.count();
   }

   @Override
   public Long sumInteger(CollectInteger aggregate)
   {
      Long val = (Long)queryComposer.sum(aggregate, Integer.class);
      if (val != null) return val;
      return super.sumInteger(aggregate);
   }
   @Override
   public Long sumLong(CollectLong aggregate)
   {
      Long val = (Long)queryComposer.sum(aggregate, Long.class);
      if (val != null) return val;
      return super.sumLong(aggregate);
   }
   @Override
   public Double sumDouble(CollectDouble aggregate)
   {
      Double val = (Double)queryComposer.sum(aggregate, Double.class);
      if (val != null) return val;
      return super.sumDouble(aggregate);
   }
   @Override
   public BigDecimal sumBigDecimal(CollectBigDecimal aggregate)
   {
      BigDecimal val = (BigDecimal)queryComposer.sum(aggregate, BigDecimal.class);
      if (val != null) return val;
      return super.sumBigDecimal(aggregate);
   }
   @Override
   public BigInteger sumBigInteger(CollectBigInteger aggregate)
   {
      BigInteger val = (BigInteger)queryComposer.sum(aggregate, BigInteger.class);
      if (val != null) return val;
      return super.sumBigInteger(aggregate);
   }
   
   @Override
   public > V max(
         org.jinq.orm.stream.JinqStream.CollectComparable aggregate)
   {
      V val = (V)queryComposer.max(aggregate);
      if (val != null) return val;
      return super.max(aggregate);
   }

   @Override
   public > V min(
         org.jinq.orm.stream.JinqStream.CollectComparable aggregate)
   {
      V val = (V)queryComposer.min(aggregate);
      if (val != null) return val;
      return super.min(aggregate);
   }

   @Override
   public > Double avg(
         org.jinq.orm.stream.JinqStream.CollectNumber aggregate)
   {
      Double val = queryComposer.avg(aggregate);
      if (val != null) return val;
      return super.avg(aggregate);
   }
   
//   @Override
//   public  U selectAggregates(AggregateSelect aggregate)
//   {
//      U val = queryComposer.selectAggregates(aggregate);
//      if (val != null) return val;
//      return super.selectAggregates(aggregate);
//   }
//   
   @Override
   public > JinqStream sortedBy(
         JinqStream.CollectComparable sorter)
   {
      QueryComposer newComposer = queryComposer.sortedBy(sorter, true);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.sortedBy(sorter);
   }

   @Override
   public > JinqStream sortedDescendingBy(
         JinqStream.CollectComparable sorter)
   {
      QueryComposer newComposer = queryComposer.sortedBy(sorter, false);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.sortedDescendingBy(sorter);
   }

   @Override
   public JinqStream limit(long n)
   {
      QueryComposer newComposer = queryComposer.limit(n);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.limit(n);
   }

   @Override
   public JinqStream skip(long n)
   {
      QueryComposer newComposer = queryComposer.skip(n);
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.skip(n);
   }
   
   @Override 
   public JinqStream distinct()
   {
      QueryComposer newComposer = queryComposer.distinct();
      if (newComposer != null) return makeQueryStream(newComposer, inQueryStreamSource);
      return super.distinct();
   }

   @Override
   public String getDebugQueryString()
   {
      return queryComposer.getDebugQueryString();
   }
   
   @Override
    U multiaggregate(AggregateSelect[] aggregates)
   {
      U vals = queryComposer.multiaggregate(aggregates);
      if (vals != null) return vals;
      return super.multiaggregate(aggregates);
   }
   
   @Override
   public JinqStream setHint(String name, Object value)
   {
      queryComposer.setHint(name, value);
      return this;
   }
}