Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
   *     http://www.apache.org/licenses/LICENSE-2.0
   *
   * Unless required by applicable law or agreed to in writing, software
   * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package com.facebook.presto.server;
 
 
 
 import java.net.URI;
 import java.util.List;
 
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_CATALOG;
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_LANGUAGE;
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_SCHEMA;
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_SOURCE;
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_TIME_ZONE;
 import static com.facebook.presto.client.PrestoHeaders.PRESTO_USER;
 import static com.facebook.presto.server.ResourceUtil.assertRequest;
 import static com.facebook.presto.server.ResourceUtil.createSessionForRequest;
 import static com.google.common.base.Preconditions.checkNotNull;
 import static com.google.common.base.Preconditions.checkState;
 import static com.google.common.base.Strings.isNullOrEmpty;
 import static com.google.common.collect.Iterators.concat;
 import static com.google.common.collect.Iterators.transform;
 import static java.lang.String.format;
 import static javax.ws.rs.core.Response.Status.INTERNAL_SERVER_ERROR;
 import static javax.ws.rs.core.Response.status;
 
 @Path("/v1/execute")
 public class ExecuteResource
 {
     private final HttpServerInfo serverInfo;
     private final HttpClient httpClient;
     private final JsonCodec<QueryResultsqueryResultsCodec;
 
     @Inject
     public ExecuteResource(
             HttpServerInfo serverInfo,
             @ForExecute HttpClient httpClient,
             JsonCodec<QueryResultsqueryResultsCodec)
     {
         this. = checkNotNull(serverInfo"serverInfo is null");
         this. = checkNotNull(httpClient"httpClient is null");
         this. = checkNotNull(queryResultsCodec"queryResultsCodec is null");
     }
 
     @POST
     public Response createQuery(
             String query,
             @HeaderParam(String user,
             @HeaderParam(String source,
             @HeaderParam(String catalog,
             @HeaderParam(String schema,
             @HeaderParam(String timeZoneId,
             @HeaderParam(String language,
             @Context HttpServletRequest servletRequest)
     {
         assertRequest(!isNullOrEmpty(query), "SQL query is empty");
 
         Session session = createSessionForRequest(servletRequest);
         ClientSession clientSession = session.toClientSession(serverUri(), false);
 
         StatementClient client = new StatementClient(clientSessionquery);
 
         List<Columncolumns = getColumns(client);
         Iterator<List<Object>> iterator = flatten(new ResultsPageIterator(client));
         SimpleQueryResults results = new SimpleQueryResults(columnsiterator);
 
        return Response.ok(results.).build();
    }
    private URI serverUri()
    {
        checkState(.getHttpUri() != null"No HTTP URI for this server (HTTP disabled?)");
        return .getHttpUri();
    }
    private static List<ColumngetColumns(StatementClient client)
    {
        while (client.isValid()) {
            List<Columncolumns = client.current().getColumns();
            if (columns != null) {
                return columns;
            }
            client.advance();
        }
        if (!client.isFailed()) {
            throw internalServerError("No columns");
        }
        throw internalServerError(failureMessage(client.finalResults()));
    }
    @SuppressWarnings("RedundantTypeArguments")
    private static <T> Iterator<T> flatten(Iterator<Iterable<T>> iterator)
    {
        // the explicit type argument is required by the Eclipse compiler
        return concat(transform(iteratorIterable<T>::iterator));
    }
    private static class ResultsPageIterator
            extends AbstractIterator<Iterable<List<Object>>>
    {
        private final StatementClient client;
        private ResultsPageIterator(StatementClient client)
        {
            this. = checkNotNull(client"client is null");
        }
        @Override
        protected Iterable<List<Object>> computeNext()
        {
            while (.isValid()) {
                Iterable<List<Object>> data = .current().getData();
                .advance();
                if (data != null) {
                    return data;
                }
            }
            if (.isFailed()) {
                throw internalServerError(failureMessage(.finalResults()));
            }
            return endOfData();
        }
    }
    private static WebApplicationException internalServerError(String message)
    {
        return new WebApplicationException(status().entity(message).build());
    }
    private static String failureMessage(QueryResults results)
    {
        return format("Query failed (#%s): %s"results.getId(), results.getError().getMessage());
    }
    public static class SimpleQueryResults
    {
        private final List<Columncolumns;
        private final Iterator<List<Object>> data;
        public SimpleQueryResults(List<ColumncolumnsIterator<List<Object>> data)
        {
            this. = checkNotNull(columns"columns is null");
            this. = checkNotNull(data"data is null");
        }
        @JsonProperty
        public List<ColumngetColumns()
        {
            return ;
        }
        @JsonProperty
        public Iterator<List<Object>> getData()
        {
            return ;
        }
    }
New to GrepCode? Check out our FAQ X