diff --git a/lib/bigquery/index.js b/lib/bigquery/index.js index 2d2c2d5c010..ac4248971b1 100644 --- a/lib/bigquery/index.js +++ b/lib/bigquery/index.js @@ -361,6 +361,8 @@ BigQuery.prototype.query = function(options, callback) { options = options || {}; + var job = options.job; + var requestQuery = extend({}, options); delete requestQuery.job; @@ -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. @@ -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); } diff --git a/test/bigquery/index.js b/test/bigquery/index.js index 3758d60a335..ee178adc987 100644 --- a/test/bigquery/index.js +++ b/test/bigquery/index.js @@ -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;