/*
* Copyright (c) "Neo4j"
* Neo4j Sweden AB [http://neo4j.com]
*
* This file is part of Neo4j.
*
* Neo4j is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
package org.neo4j.graphdb;
import java.io.PrintWriter;
import java.util.List;
import java.util.Map;
import org.neo4j.annotations.api.PublicApi;
/**
* Represents the result of {@link Transaction#execute(String, java.util.Map) executing} a query.
*
* The result is comprised of a number of rows, potentially computed lazily, with this result object being an iterator
* over those rows. Each row is represented as a {@link Map}<{@link String}, {@link Object}>
, the
* keys in this map are the names of the columns in the row, as specified by the {@code return} clause of the query,
* and the values of the map is the corresponding computed value of the expression in the {@code return} clause. Each
* row will thus have the same set of keys, and these keys can be retrieved using the
* {@linkplain #columns() columns-method}.
*
* To ensure that any resource, including transactions bound to the query, are properly freed, the result must either
* be fully exhausted, by means of the {@linkplain java.util.Iterator iterator protocol}, or the result has to be
* explicitly closed, by invoking the {@linkplain #close() close-method}.
*
* Idiomatic use of the Result object would look like this:
*
* try ( Result result = graphDatabase.execute( query, parameters ) )
* {
* while ( result.hasNext() )
* {
* Map<String, Object> row = result.next();
* for ( String key : result.columns() )
* {
* System.out.printf( "%s = %s%n", key, row.get( key ) );
* }
* }
* }
*
* If the result consists of only a single column, or if only one of the columns is of interest, a projection can be
* extracted using {@link #columnAs(String)}. This produces a new iterator over the values of the named column. It
* should be noted that this iterator consumes the rows of the result in the same way as invoking {@link #next()} on
* this object would, and that the {@link #close() close-method} on either iterator has the same effect. It is thus
* safe to either close the projected column iterator, or this iterator, or both if all rows have not been consumed.
*
* In addition to the {@link #next() iteration methods} on this interface, {@link #close()}, and the
* {@link #columnAs(String) column projection method}, there are two methods for getting a string representation of the
* result that also consumes the entire result if invoked. {@link #resultAsString()} returns a single string
* representation of all (remaining) rows in the result, and {@link #writeAsStringTo(PrintWriter)} does the same, but
* streams the result to the provided {@link PrintWriter} instead, without allocating large string objects.
*
* The methods that do not consume any rows from the result, or in other ways alter the state of the result are safe to
* invoke at any time, even after the result has been {@linkplain #close() closed} or fully exhausted. These methods
* are:
*
* - {@link #columns()}
* - {@link #getQueryStatistics()}
* - {@link #getQueryExecutionType()}
* - {@link #getExecutionPlanDescription()}
*
*
* Not all queries produce an actual result, and some queries that do might yield an empty result set. In order to
* distinguish between these cases the {@link QueryExecutionType} {@linkplain #getQueryExecutionType() of this result}
* can be queried.
*/
@PublicApi
public interface Result extends ResourceIterator