Skip to content

Commit

Permalink
Rename QueryResponse.job to jobId, better javadoc, no errors as empty…
Browse files Browse the repository at this point in the history
… list, add hasErrors
  • Loading branch information
mziccard committed Dec 14, 2015
1 parent e16c027 commit c6e53c8
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
* Google Cloud BigQuery Query Request. This class can be used to run a BigQuery SQL query and
* return results if the query completes within a specified timeout. The query results are saved to
* a temporary table that is deleted approximately 24 hours after the query is run. The query is run
* through a BigQuery Job whose identity can be accessed via {@link QueryResponse#job()}.
* through a BigQuery Job whose identity can be accessed via {@link QueryResponse#jobId()}.
*
* @see <a href="https://cloud.google.com/bigquery/docs/reference/v2/jobs/query">Query</a>
* @see <a href="https://cloud.google.com/bigquery/query-reference">Query Reference</a>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
package com.google.gcloud.bigquery;

import com.google.common.base.MoreObjects;
import com.google.common.collect.ImmutableList;

import java.io.Serializable;
import java.util.List;
Expand All @@ -30,10 +31,11 @@
* <pre> {@code
* QueryResponse response = bigquery.query(request);
* while (!response.jobComplete()) {
* response = bigquery.getQueryResults(response.job());
* response = bigquery.getQueryResults(response.jobId());
* Thread.sleep(1000);
* }
* List<BigQueryError> executionErrors = response.executionErrors();
* // look for errors in executionErrors
* QueryResult result = response.result();
* Iterator<List<FieldValue>> rowIterator = result.iterateAll();
* while(rowIterator.hasNext()) {
Expand All @@ -52,15 +54,15 @@ public class QueryResponse implements Serializable {

private final QueryResult result;
private final String etag;
private final JobId job;
private final JobId jobId;
private final boolean jobComplete;
private final List<BigQueryError> executionErrors;

static final class Builder {

private QueryResult result;
private String etag;
private JobId job;
private JobId jobId;
private boolean jobComplete;
private List<BigQueryError> executionErrors;

Expand All @@ -76,8 +78,8 @@ Builder etag(String etag) {
return this;
}

Builder job(JobId job) {
this.job = job;
Builder jobId(JobId jobId) {
this.jobId = jobId;
return this;
}

Expand All @@ -99,9 +101,10 @@ QueryResponse build() {
private QueryResponse(Builder builder) {
this.result = builder.result;
this.etag = builder.etag;
this.job = builder.job;
this.jobId = builder.jobId;
this.jobComplete = builder.jobComplete;
this.executionErrors = builder.executionErrors;
this.executionErrors = builder.executionErrors != null ? builder.executionErrors
: ImmutableList.<BigQueryError>of();
}

/**
Expand All @@ -123,8 +126,8 @@ public String etag() {
* Returns the identity of the BigQuery Job that was created to run the query. This field will be
* present even if the original request timed out.
*/
public JobId job() {
return job;
public JobId jobId() {
return jobId;
}

/**
Expand All @@ -137,6 +140,15 @@ public boolean jobComplete() {
return jobComplete;
}

/**
* Returns whether errors and warnings occurred during the execution of the job. If this method
* returns {@code true} it does not necessarily mean that the job has completed or was
* unsuccessful.
*/
public boolean hasErrors() {
return !executionErrors.isEmpty();
}

/**
* Returns errors and warnings encountered during the running of the job, if any. Errors here do
* not necessarily mean that the job has completed or was unsuccessful.
Expand All @@ -150,15 +162,15 @@ public String toString() {
return MoreObjects.toStringHelper(this)
.add("result", result)
.add("etag", etag)
.add("job", job)
.add("jobId", jobId)
.add("jobComplete", jobComplete)
.add("executionErrors", executionErrors)
.toString();
}

@Override
public int hashCode() {
return Objects.hash(job);
return Objects.hash(jobId);
}

@Override
Expand All @@ -173,7 +185,7 @@ public boolean equals(Object obj) {
return jobComplete == response.jobComplete
&& Objects.equals(etag, response.etag)
&& Objects.equals(result, response.result)
&& Objects.equals(job, response.job)
&& Objects.equals(jobId, response.jobId)
&& Objects.equals(executionErrors, response.executionErrors);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ Builder totalRows(long totalRows) {
Builder pageFetcher(QueryResultsPageFetcher pageFetcher) {
this.pageFetcher = pageFetcher;
return this;
};
}

Builder cursor(String cursor) {
this.cursor = cursor;
return this;
};
}

Builder results(Iterable<List<FieldValue>> results) {
this.results = results;
return this;
};
}

QueryResult build() {
return new QueryResult(this);
Expand All @@ -108,7 +108,7 @@ public boolean cacheHit() {
}

/**
* Returns the schema of the results.
* Returns the schema of the results. This is present only when the query completes successfully.
*/
public Schema schema() {
return schema;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableList;

Expand Down Expand Up @@ -62,7 +63,7 @@ public QueryResult nextPage() {
.build();
private static final QueryResponse QUERY_RESPONSE = QueryResponse.builder()
.etag(ETAG)
.job(JOB_ID)
.jobId(JOB_ID)
.jobComplete(JOB_COMPLETE)
.executionErrors(ERRORS)
.result(QUERY_RESULT)
Expand All @@ -72,19 +73,21 @@ public QueryResult nextPage() {
public void testBuilder() {
assertEquals(ETAG, QUERY_RESPONSE.etag());
assertEquals(QUERY_RESULT, QUERY_RESPONSE.result());
assertEquals(JOB_ID, QUERY_RESPONSE.job());
assertEquals(JOB_ID, QUERY_RESPONSE.jobId());
assertEquals(JOB_COMPLETE, QUERY_RESPONSE.jobComplete());
assertEquals(ERRORS, QUERY_RESPONSE.executionErrors());
assertTrue(QUERY_RESPONSE.hasErrors());
}

@Test
public void testBuilderIncomplete() {
QueryResponse queryResponse = QueryResponse.builder().jobComplete(false).build();
assertNull(queryResponse.etag());
assertNull(queryResponse.result());
assertNull(queryResponse.job());
assertNull(queryResponse.jobId());
assertFalse(queryResponse.jobComplete());
assertNull(queryResponse.executionErrors());
assertEquals(ImmutableList.<BigQueryError>of(), queryResponse.executionErrors());
assertFalse(queryResponse.hasErrors());
}

@Test
Expand All @@ -96,8 +99,9 @@ private void compareQueryResponse(QueryResponse expected, QueryResponse value) {
assertEquals(expected, value);
assertEquals(expected.etag(), value.etag());
assertEquals(expected.result(), value.result());
assertEquals(expected.job(), value.job());
assertEquals(expected.jobId(), value.jobId());
assertEquals(expected.jobComplete(), value.jobComplete());
assertEquals(expected.executionErrors(), value.executionErrors());
assertEquals(expected.hasErrors(), value.hasErrors());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ public class SerializationTest {
.build();
private static final QueryResponse QUERY_RESPONSE = QueryResponse.builder()
.etag(ETAG)
.job(JOB_ID)
.jobId(JOB_ID)
.jobComplete(true)
.result(QUERY_RESULT)
.build();
Expand Down

0 comments on commit c6e53c8

Please sign in to comment.