extract_op.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2015 Google Inc. All Rights Reserved.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package bigquery
  15. import (
  16. "fmt"
  17. "golang.org/x/net/context"
  18. bq "google.golang.org/api/bigquery/v2"
  19. )
  20. type extractOption interface {
  21. customizeExtract(conf *bq.JobConfigurationExtract, projectID string)
  22. }
  23. // DisableHeader returns an Option that disables the printing of a header row in exported data.
  24. func DisableHeader() Option { return disableHeader{} }
  25. type disableHeader struct{}
  26. func (opt disableHeader) implementsOption() {}
  27. func (opt disableHeader) customizeExtract(conf *bq.JobConfigurationExtract, projectID string) {
  28. f := false
  29. conf.PrintHeader = &f
  30. }
  31. func (c *Client) extract(ctx context.Context, dst *GCSReference, src *Table, options []Option) (*Job, error) {
  32. job, options := initJobProto(c.projectID, options)
  33. payload := &bq.JobConfigurationExtract{}
  34. dst.customizeExtractDst(payload, c.projectID)
  35. src.customizeExtractSrc(payload, c.projectID)
  36. for _, opt := range options {
  37. o, ok := opt.(extractOption)
  38. if !ok {
  39. return nil, fmt.Errorf("option (%#v) not applicable to dst/src pair: dst: %T ; src: %T", opt, dst, src)
  40. }
  41. o.customizeExtract(payload, c.projectID)
  42. }
  43. job.Configuration = &bq.JobConfiguration{
  44. Extract: payload,
  45. }
  46. return c.service.insertJob(ctx, job, c.projectID)
  47. }