read_op.go 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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 "golang.org/x/net/context"
  16. // RecordsPerRequest returns a ReadOption that sets the number of records to fetch per request when streaming data from BigQuery.
  17. func RecordsPerRequest(n int64) ReadOption { return recordsPerRequest(n) }
  18. type recordsPerRequest int64
  19. func (opt recordsPerRequest) customizeRead(conf *pagingConf) {
  20. conf.recordsPerRequest = int64(opt)
  21. conf.setRecordsPerRequest = true
  22. }
  23. // StartIndex returns a ReadOption that sets the zero-based index of the row to start reading from.
  24. func StartIndex(i uint64) ReadOption { return startIndex(i) }
  25. type startIndex uint64
  26. func (opt startIndex) customizeRead(conf *pagingConf) {
  27. conf.startIndex = uint64(opt)
  28. }
  29. func (conf *readTableConf) fetch(ctx context.Context, c *Client, token string) (*readDataResult, error) {
  30. return c.service.readTabledata(ctx, conf, token)
  31. }
  32. func (c *Client) readTable(t *Table, options []ReadOption) (*Iterator, error) {
  33. conf := &readTableConf{}
  34. t.customizeReadSrc(conf)
  35. for _, o := range options {
  36. o.customizeRead(&conf.paging)
  37. }
  38. return newIterator(c, conf), nil
  39. }
  40. func (conf *readQueryConf) fetch(ctx context.Context, c *Client, token string) (*readDataResult, error) {
  41. return c.service.readQuery(ctx, conf, token)
  42. }
  43. func (c *Client) readQueryResults(job *Job, options []ReadOption) (*Iterator, error) {
  44. conf := &readQueryConf{}
  45. if err := job.customizeReadQuery(conf); err != nil {
  46. return nil, err
  47. }
  48. for _, o := range options {
  49. o.customizeRead(&conf.paging)
  50. }
  51. return newIterator(c, conf), nil
  52. }