2525import com .google .api .services .bigquery .model .Table ;
2626import com .google .api .services .bigquery .model .TableDataInsertAllRequest ;
2727import com .google .api .services .bigquery .model .TableDataInsertAllRequest .Rows ;
28- import com .google .api .services .bigquery .model .TableReference ;
2928import com .google .api .services .bigquery .model .TableRow ;
3029import com .google .common .base .Function ;
3130import com .google .common .collect .ImmutableList ;
@@ -159,7 +158,7 @@ public QueryResult nextPage() {
159158 @ Override
160159 public DatasetInfo create (DatasetInfo dataset , DatasetOption ... options )
161160 throws BigQueryException {
162- final Dataset datasetPb = setProjectId (dataset ).toPb ();
161+ final Dataset datasetPb = dataset . setProjectId (options (). projectId () ).toPb ();
163162 final Map <BigQueryRpc .Option , ?> optionsMap = optionMap (options );
164163 try {
165164 return DatasetInfo .fromPb (runWithRetries (new Callable <Dataset >() {
@@ -176,7 +175,7 @@ public Dataset call() {
176175 @ Override
177176 public <T extends BaseTableInfo > T create (T table , TableOption ... options )
178177 throws BigQueryException {
179- final Table tablePb = setProjectId (table ).toPb ();
178+ final Table tablePb = table . setProjectId (options (). projectId () ).toPb ();
180179 final Map <BigQueryRpc .Option , ?> optionsMap = optionMap (options );
181180 try {
182181 return BaseTableInfo .fromPb (runWithRetries (new Callable <Table >() {
@@ -191,8 +190,8 @@ public Table call() {
191190 }
192191
193192 @ Override
194- public < T extends JobInfo > T create (T job , JobOption ... options ) throws BigQueryException {
195- final Job jobPb = setProjectId (job ).toPb ();
193+ public JobInfo create (JobInfo job , JobOption ... options ) throws BigQueryException {
194+ final Job jobPb = job . setProjectId (options (). projectId () ).toPb ();
196195 final Map <BigQueryRpc .Option , ?> optionsMap = optionMap (options );
197196 try {
198197 return JobInfo .fromPb (runWithRetries (new Callable <Job >() {
@@ -295,7 +294,7 @@ public Boolean call() {
295294 @ Override
296295 public DatasetInfo update (DatasetInfo dataset , DatasetOption ... options )
297296 throws BigQueryException {
298- final Dataset datasetPb = setProjectId (dataset ).toPb ();
297+ final Dataset datasetPb = dataset . setProjectId (options (). projectId () ).toPb ();
299298 final Map <BigQueryRpc .Option , ?> optionsMap = optionMap (options );
300299 try {
301300 return DatasetInfo .fromPb (runWithRetries (new Callable <Dataset >() {
@@ -312,7 +311,7 @@ public Dataset call() {
312311 @ Override
313312 public <T extends BaseTableInfo > T update (T table , TableOption ... options )
314313 throws BigQueryException {
315- final Table tablePb = setProjectId (table ).toPb ();
314+ final Table tablePb = table . setProjectId (options (). projectId () ).toPb ();
316315 final Map <BigQueryRpc .Option , ?> optionsMap = optionMap (options );
317316 try {
318317 return BaseTableInfo .fromPb (runWithRetries (new Callable <Table >() {
@@ -442,12 +441,12 @@ public List<FieldValue> apply(TableRow rowPb) {
442441 }
443442
444443 @ Override
445- public < T extends JobInfo > T getJob (String jobId , JobOption ... options ) throws BigQueryException {
444+ public JobInfo getJob (String jobId , JobOption ... options ) throws BigQueryException {
446445 return getJob (JobId .of (jobId ), options );
447446 }
448447
449448 @ Override
450- public < T extends JobInfo > T getJob (final JobId jobId , JobOption ... options )
449+ public JobInfo getJob (final JobId jobId , JobOption ... options )
451450 throws BigQueryException {
452451 final Map <BigQueryRpc .Option , ?> optionsMap = optionMap (options );
453452 try {
@@ -457,7 +456,7 @@ public Job call() {
457456 return bigQueryRpc .getJob (jobId .job (), optionsMap );
458457 }
459458 }, options ().retryParams (), EXCEPTION_HANDLER );
460- return answer == null ? null : JobInfo .< T > fromPb (answer );
459+ return answer == null ? null : JobInfo .fromPb (answer );
461460 } catch (RetryHelper .RetryHelperException e ) {
462461 throw BigQueryException .translateAndThrow (e );
463462 }
@@ -508,7 +507,7 @@ public QueryResponse query(final QueryRequest request) throws BigQueryException
508507 runWithRetries (new Callable <com .google .api .services .bigquery .model .QueryResponse >() {
509508 @ Override
510509 public com .google .api .services .bigquery .model .QueryResponse call () {
511- return bigQueryRpc .query (setProjectId (request ).toPb ());
510+ return bigQueryRpc .query (request . setProjectId (options (). projectId () ).toPb ());
512511 }
513512 }, options ().retryParams (), EXCEPTION_HANDLER );
514513 QueryResponse .Builder builder = QueryResponse .builder ();
@@ -596,8 +595,9 @@ private static QueryResult.Builder transformQueryResults(JobId jobId, List<Table
596595 .results (transformTableData (rowsPb ));
597596 }
598597
599- public TableDataWriteChannel writer (LoadConfiguration loadConfiguration ) {
600- return new TableDataWriteChannel (options (), setProjectId (loadConfiguration ));
598+ public TableDataWriteChannel writer (WriteChannelConfiguration writeChannelConfiguration ) {
599+ return new TableDataWriteChannel (options (),
600+ writeChannelConfiguration .setProjectId (options ().projectId ()));
601601 }
602602
603603 private Map <BigQueryRpc .Option , ?> optionMap (Option ... options ) {
@@ -608,93 +608,4 @@ public TableDataWriteChannel writer(LoadConfiguration loadConfiguration) {
608608 }
609609 return optionMap ;
610610 }
611-
612- private DatasetInfo setProjectId (DatasetInfo dataset ) {
613- DatasetInfo .Builder datasetBuilder = dataset .toBuilder ();
614- datasetBuilder .datasetId (setProjectId (dataset .datasetId ()));
615- if (dataset .acl () != null ) {
616- List <Acl > acls = Lists .newArrayListWithCapacity (dataset .acl ().size ());
617- for (Acl acl : dataset .acl ()) {
618- if (acl .entity ().type () == Acl .Entity .Type .VIEW ) {
619- Dataset .Access accessPb = acl .toPb ();
620- TableReference viewReferencePb = accessPb .getView ();
621- if (viewReferencePb .getProjectId () == null ) {
622- viewReferencePb .setProjectId (options ().projectId ());
623- }
624- acls .add (Acl .of (new Acl .View (TableId .fromPb (viewReferencePb ))));
625- } else {
626- acls .add (acl );
627- }
628- }
629- datasetBuilder .acl (acls );
630- }
631- return datasetBuilder .build ();
632- }
633-
634- private DatasetId setProjectId (DatasetId dataset ) {
635- return dataset .project () != null ? dataset
636- : DatasetId .of (options ().projectId (), dataset .dataset ());
637- }
638-
639- private BaseTableInfo setProjectId (BaseTableInfo table ) {
640- return table .toBuilder ().tableId (setProjectId (table .tableId ())).build ();
641- }
642-
643- private TableId setProjectId (TableId table ) {
644- return table .project () != null ? table
645- : TableId .of (options ().projectId (), table .dataset (), table .table ());
646- }
647-
648- private JobInfo setProjectId (JobInfo job ) {
649- if (job instanceof CopyJobInfo ) {
650- CopyJobInfo copyJob = (CopyJobInfo ) job ;
651- CopyJobInfo .Builder copyBuilder = copyJob .toBuilder ();
652- copyBuilder .destinationTable (setProjectId (copyJob .destinationTable ()));
653- copyBuilder .sourceTables (
654- Lists .transform (copyJob .sourceTables (), new Function <TableId , TableId >() {
655- @ Override
656- public TableId apply (TableId tableId ) {
657- return setProjectId (tableId );
658- }
659- }));
660- return copyBuilder .build ();
661- }
662- if (job instanceof QueryJobInfo ) {
663- QueryJobInfo queryJob = (QueryJobInfo ) job ;
664- QueryJobInfo .Builder queryBuilder = queryJob .toBuilder ();
665- if (queryJob .destinationTable () != null ) {
666- queryBuilder .destinationTable (setProjectId (queryJob .destinationTable ()));
667- }
668- if (queryJob .defaultDataset () != null ) {
669- queryBuilder .defaultDataset (setProjectId (queryJob .defaultDataset ()));
670- }
671- return queryBuilder .build ();
672- }
673- if (job instanceof ExtractJobInfo ) {
674- ExtractJobInfo extractJob = (ExtractJobInfo ) job ;
675- ExtractJobInfo .Builder extractBuilder = extractJob .toBuilder ();
676- extractBuilder .sourceTable (setProjectId (extractJob .sourceTable ()));
677- return extractBuilder .build ();
678- }
679- if (job instanceof LoadJobInfo ) {
680- LoadJobInfo loadJob = (LoadJobInfo ) job ;
681- LoadJobInfo .Builder loadBuilder = loadJob .toBuilder ();
682- return loadBuilder .configuration (setProjectId (loadJob .configuration ())).build ();
683- }
684- return job ;
685- }
686-
687- private QueryRequest setProjectId (QueryRequest request ) {
688- QueryRequest .Builder builder = request .toBuilder ();
689- if (request .defaultDataset () != null ) {
690- builder .defaultDataset (setProjectId (request .defaultDataset ()));
691- }
692- return builder .build ();
693- }
694-
695- private LoadConfiguration setProjectId (LoadConfiguration configuration ) {
696- LoadConfiguration .Builder builder = configuration .toBuilder ();
697- builder .destinationTable (setProjectId (configuration .destinationTable ()));
698- return builder .build ();
699- }
700611}
0 commit comments