Search in sources :

Example 86 with ResultSet

use of com.google.api.ads.admanager.axis.v202205.ResultSet in project googleads-java-lib by googleads.

the class GetRecentChanges method runExample.

/**
 * Runs the example.
 *
 * @param adManagerServices the services factory.
 * @param session the session.
 * @param filePath file path where changes will be saved.
 * @throws ApiException if the API request failed with one or more service errors.
 * @throws RemoteException if the API request failed due to other errors.
 * @throws IOException if unable to write the response to a file.
 */
public static void runExample(AdManagerServices adManagerServices, AdManagerSession session, String filePath) throws IOException {
    PublisherQueryLanguageServiceInterface pqlService = adManagerServices.get(session, PublisherQueryLanguageServiceInterface.class);
    // Create statement to select recent changes. Change_History only supports ordering by
    // descending ChangeDateTime. Offset is not supported. To page, use the change ID of
    // the earliest change as a pagination token. A date time range is required
    // when querying this table.
    DateTime endDateTime = DateTime.now();
    DateTime startDateTime = endDateTime.minusDays(1);
    StatementBuilder statementBuilder = new StatementBuilder().select("Id, ChangeDateTime, EntityId, EntityType, Operation, UserId").from("Change_History").where("ChangeDateTime < :endDateTime AND ChangeDateTime > :startDateTime").orderBy("ChangeDateTime DESC").withBindVariableValue("startDateTime", DateTimes.toDateTime(startDateTime)).withBindVariableValue("endDateTime", DateTimes.toDateTime(endDateTime)).limit(StatementBuilder.SUGGESTED_PAGE_LIMIT);
    // Retrieve a small amount of changes at a time, paging through
    // until all changes have been retrieved.
    ResultSet combinedResultSet = null;
    ResultSet resultSet;
    int i = 0;
    do {
        resultSet = pqlService.select(statementBuilder.toStatement());
        // Combine result sets with previous ones.
        combinedResultSet = combinedResultSet == null ? resultSet : Pql.combineResultSets(combinedResultSet, resultSet);
        if (resultSet.getRows() != null && resultSet.getRows().length > 0) {
            // Get the earliest change ID in the result set.
            int numRows = resultSet.getRows().length;
            Row lastRow = resultSet.getRows(numRows - 1);
            String id = (String) Pql.getNativeValue(lastRow.getValues(0));
            System.out.printf("%d) %d changes prior to ID %s were found.%n", i++, numRows, id);
            // Use the earliest change ID in the result set to page.
            statementBuilder.where("Id < :id AND ChangeDateTime < :endDateTime AND ChangeDateTime > :startDateTime").withBindVariableValue("id", id);
        }
    } while (resultSet.getRows() != null && resultSet.getRows().length > 0);
    // Write the result set to a CSV.
    CsvFiles.writeCsv(Pql.resultSetToStringArrayList(combinedResultSet), filePath);
    System.out.printf("Recent changes saved to: %s%n", filePath);
}
Also used : PublisherQueryLanguageServiceInterface(com.google.api.ads.admanager.axis.v202205.PublisherQueryLanguageServiceInterface) StatementBuilder(com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder) ResultSet(com.google.api.ads.admanager.axis.v202205.ResultSet) Row(com.google.api.ads.admanager.axis.v202205.Row) DateTime(org.joda.time.DateTime)

Example 87 with ResultSet

use of com.google.api.ads.admanager.axis.v202205.ResultSet in project java-spanner by googleapis.

the class RandomResultSetGenerator method generate.

public ResultSet generate() {
    ResultSet.Builder builder = ResultSet.newBuilder();
    for (int row = 0; row < rowCount; row++) {
        ListValue.Builder rowBuilder = ListValue.newBuilder();
        for (Type type : TYPES) {
            Value.Builder valueBuilder = Value.newBuilder();
            setRandomValue(valueBuilder, type);
            rowBuilder.addValues(valueBuilder.build());
        }
        builder.addRows(rowBuilder.build());
    }
    builder.setMetadata(METADATA);
    return builder.build();
}
Also used : StructType(com.google.spanner.v1.StructType) Type(com.google.spanner.v1.Type) ListValue(com.google.protobuf.ListValue) ResultSet(com.google.spanner.v1.ResultSet) ListValue(com.google.protobuf.ListValue) NullValue(com.google.protobuf.NullValue) Value(com.google.protobuf.Value)

Example 88 with ResultSet

use of com.google.api.ads.admanager.axis.v202205.ResultSet in project java-spanner by googleapis.

the class RandomResultSetGenerator method generate.

public ResultSet generate() {
    ResultSet.Builder builder = ResultSet.newBuilder();
    for (int row = 0; row < rowCount; row++) {
        ListValue.Builder rowBuilder = ListValue.newBuilder();
        for (Type type : TYPES) {
            Value.Builder valueBuilder = Value.newBuilder();
            setRandomValue(valueBuilder, type);
            rowBuilder.addValues(valueBuilder.build());
        }
        builder.addRows(rowBuilder.build());
    }
    builder.setMetadata(METADATA);
    return builder.build();
}
Also used : StructType(com.google.spanner.v1.StructType) Type(com.google.spanner.v1.Type) ListValue(com.google.protobuf.ListValue) ResultSet(com.google.spanner.v1.ResultSet) ListValue(com.google.protobuf.ListValue) NullValue(com.google.protobuf.NullValue) Value(com.google.protobuf.Value)

Example 89 with ResultSet

use of com.google.api.ads.admanager.axis.v202205.ResultSet in project java-spanner by googleapis.

the class SpannerClientTest method executeSqlTest.

@Test
public void executeSqlTest() throws Exception {
    ResultSet expectedResponse = ResultSet.newBuilder().setMetadata(ResultSetMetadata.newBuilder().build()).addAllRows(new ArrayList<ListValue>()).setStats(ResultSetStats.newBuilder().build()).build();
    mockSpanner.addResponse(expectedResponse);
    ExecuteSqlRequest request = ExecuteSqlRequest.newBuilder().setSession(SessionName.of("[PROJECT]", "[INSTANCE]", "[DATABASE]", "[SESSION]").toString()).setTransaction(TransactionSelector.newBuilder().build()).setSql("sql114126").setParams(Struct.newBuilder().build()).putAllParamTypes(new HashMap<String, Type>()).setResumeToken(ByteString.EMPTY).setPartitionToken(ByteString.EMPTY).setSeqno(109325920).setQueryOptions(ExecuteSqlRequest.QueryOptions.newBuilder().build()).setRequestOptions(RequestOptions.newBuilder().build()).build();
    ResultSet actualResponse = client.executeSql(request);
    Assert.assertEquals(expectedResponse, actualResponse);
    List<AbstractMessage> actualRequests = mockSpanner.getRequests();
    Assert.assertEquals(1, actualRequests.size());
    ExecuteSqlRequest actualRequest = ((ExecuteSqlRequest) actualRequests.get(0));
    Assert.assertEquals(request.getSession(), actualRequest.getSession());
    Assert.assertEquals(request.getTransaction(), actualRequest.getTransaction());
    Assert.assertEquals(request.getSql(), actualRequest.getSql());
    Assert.assertEquals(request.getParams(), actualRequest.getParams());
    Assert.assertEquals(request.getParamTypesMap(), actualRequest.getParamTypesMap());
    Assert.assertEquals(request.getResumeToken(), actualRequest.getResumeToken());
    Assert.assertEquals(request.getQueryMode(), actualRequest.getQueryMode());
    Assert.assertEquals(request.getPartitionToken(), actualRequest.getPartitionToken());
    Assert.assertEquals(request.getSeqno(), actualRequest.getSeqno());
    Assert.assertEquals(request.getQueryOptions(), actualRequest.getQueryOptions());
    Assert.assertEquals(request.getRequestOptions(), actualRequest.getRequestOptions());
    Assert.assertTrue(channelProvider.isHeaderSent(ApiClientHeaderProvider.getDefaultApiClientHeaderKey(), GaxGrpcProperties.getDefaultApiClientHeaderPattern()));
}
Also used : Type(com.google.spanner.v1.Type) ExecuteSqlRequest(com.google.spanner.v1.ExecuteSqlRequest) AbstractMessage(com.google.protobuf.AbstractMessage) ListValue(com.google.protobuf.ListValue) PartialResultSet(com.google.spanner.v1.PartialResultSet) ResultSet(com.google.spanner.v1.ResultSet) ByteString(com.google.protobuf.ByteString) Test(org.junit.Test)

Example 90 with ResultSet

use of com.google.api.ads.admanager.axis.v202205.ResultSet in project stargate by stargate.

the class DefaultQueryInterceptor method interceptSystemLocalOrPeers.

private static Single<ResultMessage> interceptSystemLocalOrPeers(CQLStatement statement, QueryState state, QueryOptions options, long queryStartNanoTime) {
    SelectStatement selectStatement = ((SelectStatement) statement);
    // Re-parse so that we can intercept and replace the keyspace.
    SelectStatement.Raw rawStatement = (SelectStatement.Raw) QueryProcessor.parseStatement(selectStatement.queryString);
    rawStatement.setKeyspace(SYSTEM_KEYSPACE_NAME);
    SelectStatement interceptStatement = rawStatement.prepare(state.getClientState());
    Single<ResultMessage.Rows> rows = interceptStatement.execute(state, options, queryStartNanoTime);
    return rows.map(r -> {
        // see DseConnection
        assert state.getClientState() instanceof StargateClientState;
        StargateClientState clientState = (StargateClientState) state.getClientState();
        clientState.boundPort().ifPresent(port -> replaceNativeTransportPort(r.result, port));
        return new ResultMessage.Rows(new ResultSet(selectStatement.getResultMetadata(), r.result.rows));
    });
}
Also used : SelectStatement(org.apache.cassandra.cql3.statements.SelectStatement) ResultSet(org.apache.cassandra.cql3.ResultSet) StargateClientState(io.stargate.db.dse.impl.StargateClientState)

Aggregations

Test (org.junit.Test)37 ResultSet (com.google.spanner.v1.ResultSet)21 PartialResultSet (com.google.spanner.v1.PartialResultSet)14 ResultSet (com.google.api.ads.admanager.axis.v202108.ResultSet)12 ResultSet (com.google.api.ads.admanager.axis.v202111.ResultSet)12 ResultSet (com.google.api.ads.admanager.axis.v202202.ResultSet)12 ResultSet (com.google.api.ads.admanager.axis.v202205.ResultSet)12 Function (com.google.common.base.Function)10 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202108.StatementBuilder)8 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202111.StatementBuilder)8 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202202.StatementBuilder)8 StatementBuilder (com.google.api.ads.admanager.axis.utils.v202205.StatementBuilder)8 PublisherQueryLanguageServiceInterface (com.google.api.ads.admanager.axis.v202108.PublisherQueryLanguageServiceInterface)8 PublisherQueryLanguageServiceInterface (com.google.api.ads.admanager.axis.v202111.PublisherQueryLanguageServiceInterface)8 PublisherQueryLanguageServiceInterface (com.google.api.ads.admanager.axis.v202202.PublisherQueryLanguageServiceInterface)8 PublisherQueryLanguageServiceInterface (com.google.api.ads.admanager.axis.v202205.PublisherQueryLanguageServiceInterface)8 Session (com.google.spanner.v1.Session)8 DateTime (org.joda.time.DateTime)8 ListValue (com.google.protobuf.ListValue)5 ResultSet (org.apache.cassandra.cql3.ResultSet)5