|
26 | 26 | from google.cloud.bigquery._http import Connection |
27 | 27 | from google.cloud.bigquery.dataset import Dataset |
28 | 28 | from google.cloud.bigquery.dataset import DatasetReference |
29 | | -from google.cloud.bigquery.table import Table |
| 29 | +from google.cloud.bigquery.table import Table, _TABLE_HAS_NO_SCHEMA |
30 | 30 | from google.cloud.bigquery.table import TableReference |
31 | 31 | from google.cloud.bigquery.job import CopyJob |
32 | 32 | from google.cloud.bigquery.job import ExtractJob |
33 | 33 | from google.cloud.bigquery.job import LoadJob |
34 | 34 | from google.cloud.bigquery.job import QueryJob |
35 | 35 | from google.cloud.bigquery.job import QueryJobConfig |
36 | 36 | from google.cloud.bigquery.query import QueryResults |
| 37 | +from google.cloud.bigquery._helpers import _item_to_row |
| 38 | +from google.cloud.bigquery._helpers import _rows_page_start |
37 | 39 |
|
38 | 40 |
|
39 | 41 | class Project(object): |
@@ -346,7 +348,6 @@ def delete_table(self, table): |
346 | 348 | :type table: One of: |
347 | 349 | :class:`~google.cloud.bigquery.table.Table` |
348 | 350 | :class:`~google.cloud.bigquery.table.TableReference` |
349 | | -
|
350 | 351 | :param table: the table to delete, or a reference to it. |
351 | 352 | """ |
352 | 353 | if not isinstance(table, (Table, TableReference)): |
@@ -667,6 +668,80 @@ def query_rows(self, query, job_config=None, job_id=None, timeout=None): |
667 | 668 | job.begin() |
668 | 669 | return job.result(timeout=timeout) |
669 | 670 |
|
| 671 | + def list_rows(self, table, selected_fields=None, max_results=None, |
| 672 | + page_token=None, start_index=None): |
| 673 | + """List the rows of the table. |
| 674 | +
|
| 675 | + See |
| 676 | + https://cloud.google.com/bigquery/docs/reference/rest/v2/tabledata/list |
| 677 | +
|
| 678 | + .. note:: |
| 679 | +
|
| 680 | + This method assumes that the provided schema is up-to-date with the |
| 681 | + schema as defined on the back-end: if the two schemas are not |
| 682 | + identical, the values returned may be incomplete. To ensure that the |
| 683 | + local copy of the schema is up-to-date, call ``client.get_table``. |
| 684 | +
|
| 685 | + :type table: One of: |
| 686 | + :class:`~google.cloud.bigquery.table.Table` |
| 687 | + :class:`~google.cloud.bigquery.table.TableReference` |
| 688 | + :param table: the table to list, or a reference to it. |
| 689 | +
|
| 690 | + :type selected_fields: list of :class:`SchemaField` |
| 691 | + :param selected_fields: |
| 692 | + The fields to return. Required if ``table`` is a |
| 693 | + :class:`~google.cloud.bigquery.table.TableReference`. |
| 694 | +
|
| 695 | + :type max_results: int |
| 696 | + :param max_results: maximum number of rows to return. |
| 697 | +
|
| 698 | + :type page_token: str |
| 699 | + :param page_token: (Optional) Token representing a cursor into the |
| 700 | + table's rows. |
| 701 | +
|
| 702 | + :type start_index: int |
| 703 | + :param page_token: (Optional) The zero-based index of the starting |
| 704 | + row to read. |
| 705 | +
|
| 706 | + :rtype: :class:`~google.api.core.page_iterator.Iterator` |
| 707 | + :returns: Iterator of row data :class:`tuple`s. During each page, the |
| 708 | + iterator will have the ``total_rows`` attribute set, |
| 709 | + which counts the total number of rows **in the table** |
| 710 | + (this is distinct from the total number of rows in the |
| 711 | + current page: ``iterator.page.num_items``). |
| 712 | +
|
| 713 | + """ |
| 714 | + if selected_fields is not None: |
| 715 | + schema = selected_fields |
| 716 | + elif isinstance(table, TableReference): |
| 717 | + raise ValueError('need selected_fields with TableReference') |
| 718 | + elif isinstance(table, Table): |
| 719 | + if len(table._schema) == 0: |
| 720 | + raise ValueError(_TABLE_HAS_NO_SCHEMA) |
| 721 | + schema = table.schema |
| 722 | + else: |
| 723 | + raise TypeError('table should be Table or TableReference') |
| 724 | + |
| 725 | + params = {} |
| 726 | + if selected_fields is not None: |
| 727 | + params['selectedFields'] = [f.name for f in selected_fields] |
| 728 | + if start_index is not None: |
| 729 | + params['startIndex'] = start_index |
| 730 | + |
| 731 | + iterator = page_iterator.HTTPIterator( |
| 732 | + client=self, |
| 733 | + api_request=self._connection.api_request, |
| 734 | + path='%s/data' % (table.path,), |
| 735 | + item_to_value=_item_to_row, |
| 736 | + items_key='rows', |
| 737 | + page_token=page_token, |
| 738 | + next_token='pageToken', |
| 739 | + max_results=max_results, |
| 740 | + page_start=_rows_page_start, |
| 741 | + extra_params=params) |
| 742 | + iterator.schema = schema |
| 743 | + return iterator |
| 744 | + |
670 | 745 |
|
671 | 746 | # pylint: disable=unused-argument |
672 | 747 | def _item_to_project(iterator, resource): |
|
0 commit comments