Search in sources :

Example 1 with QueryMode

use of com.google.spanner.v1.ExecuteSqlRequest.QueryMode in project java-spanner by googleapis.

the class AbstractReadContext method executeQueryInternalWithOptions.

ResultSet executeQueryInternalWithOptions(final Statement statement, final com.google.spanner.v1.ExecuteSqlRequest.QueryMode queryMode, final Options options, final ByteString partitionToken) {
    beforeReadOrQuery();
    final int prefetchChunks = options.hasPrefetchChunks() ? options.prefetchChunks() : defaultPrefetchChunks;
    final ExecuteSqlRequest.Builder request = getExecuteSqlRequestBuilder(statement, queryMode, options, /* withTransactionSelector = */
    false);
    ResumableStreamIterator stream = new ResumableStreamIterator(MAX_BUFFERED_CHUNKS, SpannerImpl.QUERY, span) {

        @Override
        CloseableIterator<PartialResultSet> startStream(@Nullable ByteString resumeToken) {
            GrpcStreamIterator stream = new GrpcStreamIterator(statement, prefetchChunks);
            if (partitionToken != null) {
                request.setPartitionToken(partitionToken);
            }
            TransactionSelector selector = null;
            if (resumeToken != null) {
                request.setResumeToken(resumeToken);
                selector = getTransactionSelector();
            } else if (!request.hasTransaction()) {
                selector = getTransactionSelector();
            }
            if (selector != null) {
                request.setTransaction(selector);
            }
            SpannerRpc.StreamingCall call = rpc.executeQuery(request.build(), stream.consumer(), session.getOptions());
            call.request(prefetchChunks);
            stream.setCall(call, request.getTransaction().hasBegin());
            return stream;
        }
    };
    return new GrpcResultSet(stream, this);
}
Also used : ResumableStreamIterator(com.google.cloud.spanner.AbstractResultSet.ResumableStreamIterator) GrpcResultSet(com.google.cloud.spanner.AbstractResultSet.GrpcResultSet) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) ByteString(com.google.protobuf.ByteString) TransactionSelector(com.google.spanner.v1.TransactionSelector) GrpcStreamIterator(com.google.cloud.spanner.AbstractResultSet.GrpcStreamIterator) PartialResultSet(com.google.spanner.v1.PartialResultSet) SpannerRpc(com.google.cloud.spanner.spi.v1.SpannerRpc) Nullable(javax.annotation.Nullable)

Example 2 with QueryMode

use of com.google.spanner.v1.ExecuteSqlRequest.QueryMode in project java-spanner by googleapis.

the class AbstractReadContext method getExecuteSqlRequestBuilder.

ExecuteSqlRequest.Builder getExecuteSqlRequestBuilder(Statement statement, QueryMode queryMode, Options options, boolean withTransactionSelector) {
    ExecuteSqlRequest.Builder builder = ExecuteSqlRequest.newBuilder().setSql(statement.getSql()).setQueryMode(queryMode).setSession(session.getName());
    Map<String, Value> stmtParameters = statement.getParameters();
    if (!stmtParameters.isEmpty()) {
        com.google.protobuf.Struct.Builder paramsBuilder = builder.getParamsBuilder();
        for (Map.Entry<String, Value> param : stmtParameters.entrySet()) {
            paramsBuilder.putFields(param.getKey(), Value.toProto(param.getValue()));
            if (param.getValue() != null) {
                builder.putParamTypes(param.getKey(), param.getValue().getType().toProto());
            }
        }
    }
    if (withTransactionSelector) {
        TransactionSelector selector = getTransactionSelector();
        if (selector != null) {
            builder.setTransaction(selector);
        }
    }
    builder.setSeqno(getSeqNo());
    builder.setQueryOptions(buildQueryOptions(statement.getQueryOptions()));
    builder.setRequestOptions(buildRequestOptions(options));
    return builder;
}
Also used : ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) TransactionSelector(com.google.spanner.v1.TransactionSelector) ByteString(com.google.protobuf.ByteString) Map(java.util.Map)

Aggregations

ByteString (com.google.protobuf.ByteString)2 ExecuteSqlRequest (com.google.spanner.v1.ExecuteSqlRequest)2 TransactionSelector (com.google.spanner.v1.TransactionSelector)2 GrpcResultSet (com.google.cloud.spanner.AbstractResultSet.GrpcResultSet)1 GrpcStreamIterator (com.google.cloud.spanner.AbstractResultSet.GrpcStreamIterator)1 ResumableStreamIterator (com.google.cloud.spanner.AbstractResultSet.ResumableStreamIterator)1 SpannerRpc (com.google.cloud.spanner.spi.v1.SpannerRpc)1 PartialResultSet (com.google.spanner.v1.PartialResultSet)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1