Skip to content

Commit

Permalink
Adding better toString() output for Query.
Browse files Browse the repository at this point in the history
  • Loading branch information
niclash committed Feb 13, 2012
1 parent 1c4f423 commit 6a96841
Show file tree
Hide file tree
Showing 4 changed files with 279 additions and 138 deletions.
Expand Up @@ -128,6 +128,12 @@ private <T> List<T> filter( Class<T> resultType, Specification whereClause )
}
}

@Override
public String toString()
{
return "IterableQuerySource{" + iterable + '}';
}

private class OrderByComparator<T extends Composite>
implements Comparator<T>
{
Expand Down
55 changes: 40 additions & 15 deletions core/runtime/src/main/java/org/qi4j/runtime/query/QueryImpl.java
Expand Up @@ -18,6 +18,9 @@
*/
package org.qi4j.runtime.query;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import org.qi4j.api.composite.Composite;
import org.qi4j.api.property.Property;
import org.qi4j.api.query.Query;
Expand All @@ -28,10 +31,6 @@
import org.qi4j.functional.Specification;
import org.qi4j.spi.query.QuerySource;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
* Default implementation of {@link org.qi4j.api.query.Query}
*/
Expand Down Expand Up @@ -87,22 +86,26 @@ class QueryImpl<T>
*/
public Query<T> orderBy( final OrderBy... segments )
{
orderBySegments = Iterables.iterable(segments);
orderBySegments = Iterables.iterable( segments );
return this;
}

@Override
public Query<T> orderBy( Property<?> property, OrderBy.Order order )
{
if (orderBySegments == null)
if( orderBySegments == null )
{
orderBySegments = Iterables.iterable( new OrderBy( QueryExpressions.property( property ), order ) );
}
else
{
orderBySegments = Iterables.append( new OrderBy( QueryExpressions.property( property ), order ), orderBySegments );
}
return this;
}

@Override
public Query<T> orderBy( Property<?> property)
public Query<T> orderBy( Property<?> property )
{
orderBy( property, OrderBy.Order.ASCENDING );
return this;
Expand Down Expand Up @@ -132,8 +135,10 @@ public Query<T> maxResults( int maxResults )
@SuppressWarnings( "unchecked" )
public Query<T> setVariable( final String name, final Object value )
{
if (variables == null)
variables = new HashMap<String, Object>( );
if( variables == null )
{
variables = new HashMap<String, Object>();
}
variables.put( name, value );

return this;
Expand All @@ -145,10 +150,14 @@ public Query<T> setVariable( final String name, final Object value )
@SuppressWarnings( "unchecked" )
public <V> V getVariable( final String name )
{
if (variables == null)
if( variables == null )
{
return null;
}
else
{
return (V) variables.get( name );
}
}

public Class<T> resultType()
Expand All @@ -157,20 +166,36 @@ public Class<T> resultType()
}

@Override
public T find() throws QueryExecutionException
public T find()
throws QueryExecutionException
{
return querySource.find(resultType, whereClause, orderBySegments, firstResult, maxResults, variables);
return querySource.find( resultType, whereClause, orderBySegments, firstResult, maxResults, variables );
}

@Override
public long count() throws QueryExecutionException
public long count()
throws QueryExecutionException
{
return querySource.count(resultType, whereClause, orderBySegments, firstResult, maxResults, variables);
return querySource.count( resultType, whereClause, orderBySegments, firstResult, maxResults, variables );
}

@Override
public Iterator<T> iterator()
{
return querySource.iterator(resultType, whereClause, orderBySegments, firstResult, maxResults, variables);
return querySource.iterator( resultType, whereClause, orderBySegments, firstResult, maxResults, variables );
}

@Override
public String toString()
{
return "Query{" +
" FROM " + querySource +
" WHERE " + whereClause +
( orderBySegments != null ? " ORDER BY " + orderBySegments : "" ) +
( firstResult != null ? " FIRST " + firstResult : "" ) +
( maxResults != null ? " MAX " + maxResults : "" ) +
" EXPECT " + resultType +
( variables != null ? " WITH VARIABLES " + variables : "" ) +
'}';
}
}

0 comments on commit 6a96841

Please sign in to comment.