Skip to content

Commit a0f5f1b

Browse files
committed
Merge pull request #500 from mziccard/bigquery
Make BigQuery get/create/updated Table and Job methods generic
2 parents 75a0ece + 862905f commit a0f5f1b

5 files changed

Lines changed: 88 additions & 85 deletions

File tree

gcloud-java-bigquery/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ Field stringField = Field.of("StringField", Field.Type.string());
118118
// Table schema definition
119119
Schema schema = Schema.of(stringField);
120120
// Create a table
121-
BaseTableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema));
121+
TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema));
122122
```
123123

124124
#### Loading data into a table
@@ -232,7 +232,7 @@ public class GcloudBigQueryExample {
232232
// Table schema definition
233233
Schema schema = Schema.of(stringField);
234234
// Create a table
235-
BaseTableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema));
235+
TableInfo createdTableInfo = bigquery.create(TableInfo.of(tableId, schema));
236236

237237
// Define rows to insert
238238
Map<String, Object> firstRow = new HashMap<>();

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQuery.java

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -463,14 +463,14 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {
463463
*
464464
* @throws BigQueryException upon failure
465465
*/
466-
BaseTableInfo create(BaseTableInfo table, TableOption... options) throws BigQueryException;
466+
<T extends BaseTableInfo> T create(T table, TableOption... options) throws BigQueryException;
467467

468468
/**
469469
* Creates a new job.
470470
*
471471
* @throws BigQueryException upon failure
472472
*/
473-
JobInfo create(JobInfo job, JobOption... options) throws BigQueryException;
473+
<T extends JobInfo> T create(T job, JobOption... options) throws BigQueryException;
474474

475475
/**
476476
* Returns the requested dataset or {@code null} if not found.
@@ -541,22 +541,23 @@ public static QueryResultsOption maxWaitTime(long maxWaitTime) {
541541
*
542542
* @throws BigQueryException upon failure
543543
*/
544-
BaseTableInfo update(BaseTableInfo table, TableOption... options) throws BigQueryException;
544+
<T extends BaseTableInfo> T update(T table, TableOption... options) throws BigQueryException;
545545

546546
/**
547547
* Returns the requested table or {@code null} if not found.
548548
*
549549
* @throws BigQueryException upon failure
550550
*/
551-
BaseTableInfo getTable(String datasetId, String tableId, TableOption... options)
551+
<T extends BaseTableInfo> T getTable(String datasetId, String tableId, TableOption... options)
552552
throws BigQueryException;
553553

554554
/**
555555
* Returns the requested table or {@code null} if not found.
556556
*
557557
* @throws BigQueryException upon failure
558558
*/
559-
BaseTableInfo getTable(TableId tableId, TableOption... options) throws BigQueryException;
559+
<T extends BaseTableInfo> T getTable(TableId tableId, TableOption... options)
560+
throws BigQueryException;
560561

561562
/**
562563
* Lists the tables in the dataset. This method returns partial information on each table
@@ -610,14 +611,14 @@ Page<List<FieldValue>> listTableData(TableId tableId, TableDataListOption... opt
610611
*
611612
* @throws BigQueryException upon failure
612613
*/
613-
JobInfo getJob(String jobId, JobOption... options) throws BigQueryException;
614+
<T extends JobInfo> T getJob(String jobId, JobOption... options) throws BigQueryException;
614615

615616
/**
616617
* Returns the requested job or {@code null} if not found.
617618
*
618619
* @throws BigQueryException upon failure
619620
*/
620-
JobInfo getJob(JobId jobId, JobOption... options) throws BigQueryException;
621+
<T extends JobInfo> T getJob(JobId jobId, JobOption... options) throws BigQueryException;
621622

622623
/**
623624
* Lists the jobs.

gcloud-java-bigquery/src/main/java/com/google/gcloud/bigquery/BigQueryImpl.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public Dataset call() {
199199
}
200200

201201
@Override
202-
public BaseTableInfo create(BaseTableInfo table, TableOption... options)
202+
public <T extends BaseTableInfo> T create(T table, TableOption... options)
203203
throws BigQueryException {
204204
final Table tablePb = setProjectId(table).toPb();
205205
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
@@ -216,7 +216,7 @@ public Table call() {
216216
}
217217

218218
@Override
219-
public JobInfo create(JobInfo job, JobOption... options) throws BigQueryException {
219+
public <T extends JobInfo> T create(T job, JobOption... options) throws BigQueryException {
220220
final Job jobPb = setProjectId(job).toPb();
221221
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
222222
try {
@@ -335,7 +335,7 @@ public Dataset call() {
335335
}
336336

337337
@Override
338-
public BaseTableInfo update(BaseTableInfo table, TableOption... options)
338+
public <T extends BaseTableInfo> T update(T table, TableOption... options)
339339
throws BigQueryException {
340340
final Table tablePb = setProjectId(table).toPb();
341341
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
@@ -352,13 +352,13 @@ public Table call() {
352352
}
353353

354354
@Override
355-
public BaseTableInfo getTable(final String datasetId, final String tableId,
355+
public <T extends BaseTableInfo> T getTable(final String datasetId, final String tableId,
356356
TableOption... options) throws BigQueryException {
357357
return getTable(TableId.of(datasetId, tableId), options);
358358
}
359359

360360
@Override
361-
public BaseTableInfo getTable(final TableId tableId, TableOption... options)
361+
public <T extends BaseTableInfo> T getTable(final TableId tableId, TableOption... options)
362362
throws BigQueryException {
363363
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
364364
try {
@@ -368,7 +368,7 @@ public Table call() {
368368
return bigQueryRpc.getTable(tableId.dataset(), tableId.table(), optionsMap);
369369
}
370370
}, options().retryParams(), EXCEPTION_HANDLER);
371-
return answer == null ? null : BaseTableInfo.fromPb(answer);
371+
return answer == null ? null : BaseTableInfo.<T>fromPb(answer);
372372
} catch (RetryHelper.RetryHelperException e) {
373373
throw BigQueryException.translateAndThrow(e);
374374
}
@@ -466,12 +466,13 @@ public List<FieldValue> apply(TableRow rowPb) {
466466
}
467467

468468
@Override
469-
public JobInfo getJob(String jobId, JobOption... options) throws BigQueryException {
469+
public <T extends JobInfo> T getJob(String jobId, JobOption... options) throws BigQueryException {
470470
return getJob(JobId.of(jobId), options);
471471
}
472472

473473
@Override
474-
public JobInfo getJob(final JobId jobId, JobOption... options) throws BigQueryException {
474+
public <T extends JobInfo> T getJob(final JobId jobId, JobOption... options)
475+
throws BigQueryException {
475476
final Map<BigQueryRpc.Option, ?> optionsMap = optionMap(options);
476477
try {
477478
Job answer = runWithRetries(new Callable<Job>() {
@@ -480,7 +481,7 @@ public Job call() {
480481
return bigQueryRpc.getJob(jobId.job(), optionsMap);
481482
}
482483
}, options().retryParams(), EXCEPTION_HANDLER);
483-
return answer == null ? null : JobInfo.fromPb(answer);
484+
return answer == null ? null : JobInfo.<T>fromPb(answer);
484485
} catch (RetryHelper.RetryHelperException e) {
485486
throw BigQueryException.translateAndThrow(e);
486487
}

0 commit comments

Comments
 (0)