Skip to content

Commit

Permalink
Merge pull request #656 from stephenplusplus/spp--biquery-handle-inco…
Browse files Browse the repository at this point in the history
…nsistent-response

bigquery: handle case where API doesn't respond with jobReference
  • Loading branch information
stephenplusplus committed Jun 25, 2015
2 parents 3a337a5 + 3127153 commit 6e04e5e
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
26 changes: 15 additions & 11 deletions lib/bigquery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,8 @@ BigQuery.prototype.query = function(options, callback) {

options = options || {};

var job = options.job;

var requestQuery = extend({}, options);
delete requestQuery.job;

Expand All @@ -374,9 +376,9 @@ BigQuery.prototype.query = function(options, callback) {
}

function runQuery() {
if (options.job) {
if (job) {
// Get results of the query.
var path = '/queries/' + options.job.id;
var path = '/queries/' + job.id;
that.makeReq_('GET', path, requestQuery, null, responseHandler);
} else {
// Create a job.
Expand All @@ -389,23 +391,25 @@ BigQuery.prototype.query = function(options, callback) {
return;
}

var job = that.job(resp.jobReference.jobId);
var nextQuery = null;
var rows = Table.mergeSchemaWithRows_(resp.schema, resp.rows || []);
var rows = [];
if (resp.schema && resp.rows) {
rows = Table.mergeSchemaWithRows_(resp.schema, resp.rows);
}

var nextQuery = null;
if (resp.jobComplete === false) {
// Query is still running.
nextQuery = extend({
job: job
}, options);
nextQuery = extend({}, options);
} else if (resp.pageToken) {
// More results exist.
nextQuery = extend({
job: job
}, options, {
nextQuery = extend({}, options, {
pageToken: resp.pageToken
});
}
if (nextQuery && !nextQuery.job && resp.jobReference.jobId) {
// Create a prepared Job to continue the query.
nextQuery.job = that.job(resp.jobReference.jobId);
}

onComplete(null, rows, nextQuery, resp);
}
Expand Down
2 changes: 1 addition & 1 deletion test/bigquery/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ function fakeGsa() {
}

describe('BigQuery', function() {
var JOB_ID = JOB_ID;
var JOB_ID = 'JOB_ID';
var PROJECT_ID = 'test-project';

var BigQuery;
Expand Down

0 comments on commit 6e04e5e

Please sign in to comment.