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 >() {
@@ -192,7 +191,7 @@ public Table call() {
192191
193192 @ Override
194193 public JobInfo create (JobInfo job , JobOption ... options ) throws BigQueryException {
195- final Job jobPb = setProjectId (job ).toPb ();
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 >() {
@@ -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 ();
@@ -597,7 +596,8 @@ private static QueryResult.Builder transformQueryResults(JobId jobId, List<Table
597596 }
598597
599598 public TableDataWriteChannel writer (WriteChannelConfiguration writeChannelConfiguration ) {
600- return new TableDataWriteChannel (options (), setProjectId (writeChannelConfiguration ));
599+ return new TableDataWriteChannel (options (),
600+ writeChannelConfiguration .setProjectId (options ().projectId ()));
601601 }
602602
603603 private Map <BigQueryRpc .Option , ?> optionMap (Option ... options ) {
@@ -608,100 +608,4 @@ public TableDataWriteChannel writer(WriteChannelConfiguration writeChannelConfig
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- JobConfiguration configuration = job .configuration ();
650- JobInfo .Builder jobBuilder = job .toBuilder ();
651- switch (configuration .type ()) {
652- case COPY :
653- CopyJobConfiguration copyConfiguration = (CopyJobConfiguration ) configuration ;
654- CopyJobConfiguration .Builder copyBuilder = copyConfiguration .toBuilder ();
655- copyBuilder .sourceTables (
656- Lists .transform (copyConfiguration .sourceTables (), new Function <TableId , TableId >() {
657- @ Override
658- public TableId apply (TableId tableId ) {
659- return setProjectId (tableId );
660- }
661- }));
662- copyBuilder .destinationTable (setProjectId (copyConfiguration .destinationTable ()));
663- jobBuilder .configuration (copyBuilder .build ());
664- break ;
665- case QUERY :
666- QueryJobConfiguration queryConfiguration = (QueryJobConfiguration ) configuration ;
667- QueryJobConfiguration .Builder queryBuilder = queryConfiguration .toBuilder ();
668- if (queryConfiguration .destinationTable () != null ) {
669- queryBuilder .destinationTable (setProjectId (queryConfiguration .destinationTable ()));
670- }
671- if (queryConfiguration .defaultDataset () != null ) {
672- queryBuilder .defaultDataset (setProjectId (queryConfiguration .defaultDataset ()));
673- }
674- jobBuilder .configuration (queryBuilder .build ());
675- break ;
676- case EXTRACT :
677- ExtractJobConfiguration extractConfiguration = (ExtractJobConfiguration ) configuration ;
678- ExtractJobConfiguration .Builder extractBuilder = extractConfiguration .toBuilder ();
679- extractBuilder .sourceTable (setProjectId (extractConfiguration .sourceTable ()));
680- jobBuilder .configuration (extractBuilder .build ());
681- break ;
682- case LOAD :
683- LoadJobConfiguration loadConfiguration = (LoadJobConfiguration ) configuration ;
684- jobBuilder .configuration (setProjectId (loadConfiguration ));
685- break ;
686- default :
687- // never reached
688- throw new IllegalArgumentException ("Job configuration is not supported" );
689- }
690- return jobBuilder .build ();
691- }
692-
693- private QueryRequest setProjectId (QueryRequest request ) {
694- QueryRequest .Builder builder = request .toBuilder ();
695- if (request .defaultDataset () != null ) {
696- builder .defaultDataset (setProjectId (request .defaultDataset ()));
697- }
698- return builder .build ();
699- }
700-
701- @ SuppressWarnings ("unchecked" )
702- private <T extends LoadConfiguration > T setProjectId (T configuration ) {
703- LoadConfiguration .Builder builder = configuration .toBuilder ();
704- builder .destinationTable (setProjectId (configuration .destinationTable ()));
705- return (T ) builder .build ();
706- }
707611}
0 commit comments