1616
1717import static com .google .gcloud .spi .BigQueryRpc .Option .DELETE_CONTENTS ;
1818import static com .google .gcloud .spi .BigQueryRpc .Option .FIELDS ;
19+ import static com .google .gcloud .spi .BigQueryRpc .Option .MAX_RESULTS ;
20+ import static com .google .gcloud .spi .BigQueryRpc .Option .PAGE_TOKEN ;
1921import static com .google .gcloud .spi .BigQueryRpc .Option .START_INDEX ;
2022import static com .google .gcloud .spi .BigQueryRpc .Option .TIMEOUT ;
2123import static java .net .HttpURLConnection .HTTP_NOT_FOUND ;
3234import com .google .api .services .bigquery .model .GetQueryResultsResponse ;
3335import com .google .api .services .bigquery .model .Job ;
3436import com .google .api .services .bigquery .model .JobList ;
35- import com .google .api .services .bigquery .model .JobReference ;
3637import com .google .api .services .bigquery .model .JobStatus ;
3738import com .google .api .services .bigquery .model .QueryRequest ;
3839import com .google .api .services .bigquery .model .QueryResponse ;
4849import com .google .common .collect .ImmutableSet ;
4950import com .google .common .collect .Iterables ;
5051
51- import static com .google .gcloud .spi .BigQueryRpc .Option .MAX_RESULTS ;
52- import static com .google .gcloud .spi .BigQueryRpc .Option .PAGE_TOKEN ;
53-
5452import com .google .gcloud .bigquery .BigQueryException ;
5553import com .google .gcloud .bigquery .BigQueryOptions ;
5654
@@ -103,7 +101,7 @@ public Dataset getDataset(String datasetId, Map<Option, ?> options) throws BigQu
103101 .get (this .options .projectId (), datasetId )
104102 .setFields (FIELDS .getString (options ))
105103 .execute ();
106- } catch (IOException ex ) {
104+ } catch (IOException ex ) {
107105 BigQueryException serviceException = translate (ex );
108106 if (serviceException .code () == HTTP_NOT_FOUND ) {
109107 return null ;
@@ -124,15 +122,16 @@ public Tuple<String, Iterable<Dataset>> listDatasets(Map<Option, ?> options)
124122 .execute ();
125123 Iterable <DatasetList .Datasets > datasets = datasetsList .getDatasets ();
126124 return Tuple .of (datasetsList .getNextPageToken (),
127- Iterables .transform (datasets != null ? datasets : ImmutableList .<DatasetList .Datasets >of (),
125+ Iterables .transform (datasets != null ? datasets :
126+ ImmutableList .<DatasetList .Datasets >of (),
128127 new Function <DatasetList .Datasets , Dataset >() {
129128 @ Override
130- public Dataset apply (DatasetList .Datasets f ) {
129+ public Dataset apply (DatasetList .Datasets datasetPb ) {
131130 return new Dataset ()
132- .setDatasetReference (f .getDatasetReference ())
133- .setFriendlyName (f .getFriendlyName ())
134- .setId (f .getId ())
135- .setKind (f .getKind ());
131+ .setDatasetReference (datasetPb .getDatasetReference ())
132+ .setFriendlyName (datasetPb .getFriendlyName ())
133+ .setId (datasetPb .getId ())
134+ .setKind (datasetPb .getKind ());
136135 }
137136 }));
138137 } catch (IOException ex ) {
@@ -151,6 +150,33 @@ public Dataset create(Dataset dataset, Map<Option, ?> options) throws BigQueryEx
151150 }
152151 }
153152
153+ @ Override
154+ public Table create (Table table , Map <Option , ?> options )
155+ throws BigQueryException {
156+ try {
157+ // unset the type, as it is output only
158+ table .setType (null );
159+ return bigquery .tables ()
160+ .insert (this .options .projectId (), table .getTableReference ().getDatasetId (), table )
161+ .setFields (FIELDS .getString (options ))
162+ .execute ();
163+ } catch (IOException ex ) {
164+ throw translate (ex );
165+ }
166+ }
167+
168+ @ Override
169+ public Job create (Job job , Map <Option , ?> options ) throws BigQueryException {
170+ try {
171+ return bigquery .jobs ()
172+ .insert (this .options .projectId (), job )
173+ .setFields (FIELDS .getString (options ))
174+ .execute ();
175+ } catch (IOException ex ) {
176+ throw translate (ex );
177+ }
178+ }
179+
154180 @ Override
155181 public boolean deleteDataset (String datasetId , Map <Option , ?> options ) throws BigQueryException {
156182 try {
@@ -180,6 +206,21 @@ public Dataset patch(Dataset dataset, Map<Option, ?> options) throws BigQueryExc
180206 }
181207 }
182208
209+ @ Override
210+ public Table patch (Table table , Map <Option , ?> options ) throws BigQueryException {
211+ try {
212+ // unset the type, as it is output only
213+ table .setType (null );
214+ TableReference reference = table .getTableReference ();
215+ return bigquery .tables ()
216+ .patch (this .options .projectId (), reference .getDatasetId (), reference .getTableId (), table )
217+ .setFields (FIELDS .getString (options ))
218+ .execute ();
219+ } catch (IOException ex ) {
220+ throw translate (ex );
221+ }
222+ }
223+
183224 @ Override
184225 public Table getTable (String datasetId , String tableId , Map <Option , ?> options )
185226 throws BigQueryException {
@@ -188,7 +229,7 @@ public Table getTable(String datasetId, String tableId, Map<Option, ?> options)
188229 .get (this .options .projectId (), datasetId , tableId )
189230 .setFields (FIELDS .getString (options ))
190231 .execute ();
191- } catch (IOException ex ) {
232+ } catch (IOException ex ) {
192233 BigQueryException serviceException = translate (ex );
193234 if (serviceException .code () == HTTP_NOT_FOUND ) {
194235 return null ;
@@ -211,13 +252,13 @@ public Tuple<String, Iterable<Table>> listTables(String datasetId, Map<Option, ?
211252 Iterables .transform (tables != null ? tables : ImmutableList .<TableList .Tables >of (),
212253 new Function <TableList .Tables , Table >() {
213254 @ Override
214- public Table apply (TableList .Tables f ) {
255+ public Table apply (TableList .Tables tablePb ) {
215256 return new Table ()
216- .setFriendlyName (f .getFriendlyName ())
217- .setId (f .getId ())
218- .setKind (f .getKind ())
219- .setTableReference (f .getTableReference ())
220- .setType (f .getType ());
257+ .setFriendlyName (tablePb .getFriendlyName ())
258+ .setId (tablePb .getId ())
259+ .setKind (tablePb .getKind ())
260+ .setTableReference (tablePb .getTableReference ())
261+ .setType (tablePb .getType ());
221262 }
222263 }));
223264 } catch (IOException ex ) {
@@ -226,21 +267,7 @@ public Table apply(TableList.Tables f) {
226267 }
227268
228269 @ Override
229- public Table create (Table table , Map <Option , ?> options )
230- throws BigQueryException {
231- try {
232- return bigquery .tables ()
233- .insert (this .options .projectId (), table .getTableReference ().getDatasetId (), table )
234- .setFields (FIELDS .getString (options ))
235- .execute ();
236- } catch (IOException ex ) {
237- throw translate (ex );
238- }
239- }
240-
241- @ Override
242- public boolean deleteTable (String datasetId , String tableId , Map <Option , ?> options )
243- throws BigQueryException {
270+ public boolean deleteTable (String datasetId , String tableId ) throws BigQueryException {
244271 try {
245272 bigquery .tables ().delete (this .options .projectId (), datasetId , tableId ).execute ();
246273 return true ;
@@ -254,24 +281,11 @@ public boolean deleteTable(String datasetId, String tableId, Map<Option, ?> opti
254281 }
255282
256283 @ Override
257- public Table patch (Table table , Map <Option , ?> options ) throws BigQueryException {
258- try {
259- TableReference reference = table .getTableReference ();
260- return bigquery .tables ()
261- .patch (this .options .projectId (), reference .getDatasetId (), reference .getTableId (), table )
262- .setFields (FIELDS .getString (options ))
263- .execute ();
264- } catch (IOException ex ) {
265- throw translate (ex );
266- }
267- }
268-
269- @ Override
270- public TableDataInsertAllResponse insertAll (TableReference table ,
271- TableDataInsertAllRequest request , Map <Option , ?> options ) throws BigQueryException {
284+ public TableDataInsertAllResponse insertAll (String datasetId , String tableId ,
285+ TableDataInsertAllRequest request ) throws BigQueryException {
272286 try {
273287 return bigquery .tabledata ()
274- .insertAll (this .options .projectId (), table . getDatasetId (), table . getTableId () , request )
288+ .insertAll (this .options .projectId (), datasetId , tableId , request )
275289 .execute ();
276290 } catch (IOException ex ) {
277291 throw translate (ex );
@@ -286,8 +300,8 @@ public Tuple<String, Iterable<TableRow>> listTableData(String datasetId, String
286300 .list (this .options .projectId (), datasetId , tableId )
287301 .setMaxResults (MAX_RESULTS .getLong (options ))
288302 .setPageToken (PAGE_TOKEN .getString (options ))
289- .setStartIndex (START_INDEX .getLong (options ) != null ?
290- BigInteger .valueOf (START_INDEX .getLong (options )) : null )
303+ .setStartIndex (START_INDEX .getLong (options ) != null
304+ ? BigInteger .valueOf (START_INDEX .getLong (options )) : null )
291305 .execute ();
292306 return Tuple .<String , Iterable <TableRow >>of (tableDataList .getPageToken (),
293307 tableDataList .getRows ());
@@ -303,7 +317,7 @@ public Job getJob(String jobId, Map<Option, ?> options) throws BigQueryException
303317 .get (this .options .projectId (), jobId )
304318 .setFields (FIELDS .getString (options ))
305319 .execute ();
306- } catch (IOException ex ) {
320+ } catch (IOException ex ) {
307321 BigQueryException serviceException = translate (ex );
308322 if (serviceException .code () == HTTP_NOT_FOUND ) {
309323 return null ;
@@ -329,22 +343,23 @@ public Tuple<String, Iterable<Job>> listJobs(Map<Option, ?> options) throws BigQ
329343 Iterables .transform (jobs != null ? jobs : ImmutableList .<JobList .Jobs >of (),
330344 new Function <JobList .Jobs , Job >() {
331345 @ Override
332- public Job apply (JobList .Jobs f ) {
333- JobStatus statusPb = f .getStatus () != null ? f .getStatus () : new JobStatus ();
346+ public Job apply (JobList .Jobs jobPb ) {
347+ JobStatus statusPb = jobPb .getStatus () != null
348+ ? jobPb .getStatus () : new JobStatus ();
334349 if (statusPb .getState () == null ) {
335- statusPb .setState (f .getState ());
350+ statusPb .setState (jobPb .getState ());
336351 }
337352 if (statusPb .getErrorResult () == null ) {
338- statusPb .setErrorResult (f .getErrorResult ());
353+ statusPb .setErrorResult (jobPb .getErrorResult ());
339354 }
340355 return new Job ()
341- .setConfiguration (f .getConfiguration ())
342- .setId (f .getId ())
343- .setJobReference (f .getJobReference ())
344- .setKind (f .getKind ())
345- .setStatistics (f .getStatistics ())
346- .setStatus (f . getStatus () )
347- .setUserEmail (f .getUserEmail ());
356+ .setConfiguration (jobPb .getConfiguration ())
357+ .setId (jobPb .getId ())
358+ .setJobReference (jobPb .getJobReference ())
359+ .setKind (jobPb .getKind ())
360+ .setStatistics (jobPb .getStatistics ())
361+ .setStatus (statusPb )
362+ .setUserEmail (jobPb .getUserEmail ());
348363 }
349364 }));
350365 } catch (IOException ex ) {
@@ -353,19 +368,7 @@ public Job apply(JobList.Jobs f) {
353368 }
354369
355370 @ Override
356- public Job create (Job job , Map <Option , ?> options ) throws BigQueryException {
357- try {
358- return bigquery .jobs ()
359- .insert (this .options .projectId (), job )
360- .setFields (FIELDS .getString (options ))
361- .execute ();
362- } catch (IOException ex ) {
363- throw translate (ex );
364- }
365- }
366-
367- @ Override
368- public boolean cancel (String jobId , Map <Option , ?> options ) throws BigQueryException {
371+ public boolean cancel (String jobId ) throws BigQueryException {
369372 try {
370373 bigquery .jobs ().cancel (this .options .projectId (), jobId ).execute ();
371374 return true ;
@@ -379,17 +382,17 @@ public boolean cancel(String jobId, Map<Option, ?> options) throws BigQueryExcep
379382 }
380383
381384 @ Override
382- public GetQueryResultsResponse getQueryResults (JobReference job , Map <Option , ?> options )
385+ public GetQueryResultsResponse getQueryResults (String jobId , Map <Option , ?> options )
383386 throws BigQueryException {
384387 try {
385- return bigquery .jobs ().getQueryResults (this .options .projectId (), job . getJobId () )
388+ return bigquery .jobs ().getQueryResults (this .options .projectId (), jobId )
386389 .setMaxResults (MAX_RESULTS .getLong (options ))
387390 .setPageToken (PAGE_TOKEN .getString (options ))
388- .setStartIndex (START_INDEX .getLong (options ) != null ?
389- BigInteger .valueOf (START_INDEX .getLong (options )) : null )
391+ .setStartIndex (START_INDEX .getLong (options ) != null
392+ ? BigInteger .valueOf (START_INDEX .getLong (options )) : null )
390393 .setTimeoutMs (TIMEOUT .getLong (options ))
391394 .execute ();
392- } catch (IOException ex ) {
395+ } catch (IOException ex ) {
393396 BigQueryException serviceException = translate (ex );
394397 if (serviceException .code () == HTTP_NOT_FOUND ) {
395398 return null ;
@@ -399,8 +402,7 @@ public GetQueryResultsResponse getQueryResults(JobReference job, Map<Option, ?>
399402 }
400403
401404 @ Override
402- public QueryResponse query (QueryRequest request , Map <Option , ?> options )
403- throws BigQueryException {
405+ public QueryResponse query (QueryRequest request ) throws BigQueryException {
404406 try {
405407 return bigquery .jobs ().query (this .options .projectId (), request ).execute ();
406408 } catch (IOException ex ) {
0 commit comments