~ubuntu-branches/ubuntu/trusty/python-boto/trusty

« back to all changes in this revision

Viewing changes to boto/dynamodb2/table.py

  • Committer: Package Import Robot
  • Author(s): Eric Evans
  • Date: 2013-08-13 13:56:47 UTC
  • mfrom: (1.1.12)
  • Revision ID: package-import@ubuntu.com-20130813135647-o43z7y15uid87fzl
Tags: 2.10.0-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
611
611
                'AttributeValueList': [],
612
612
                'ComparisonOperator': op,
613
613
            }
614
 
 
 
614
 
615
615
            # Special-case the ``NULL/NOT_NULL`` case.
616
616
            if field_bits[-1] == 'null':
617
617
                del lookup['AttributeValueList']
644
644
        return filters
645
645
 
646
646
    def query(self, limit=None, index=None, reverse=False, consistent=False,
647
 
              **filter_kwargs):
 
647
              attributes=None, **filter_kwargs):
648
648
        """
649
649
        Queries for a set of matching items in a DynamoDB table.
650
650
 
674
674
        the data (more expensive). (Default: ``False`` - use eventually
675
675
        consistent reads)
676
676
 
 
677
        Optionally accepts a ``attributes`` parameter, which should be a
 
678
        tuple. If you provide any attributes only these will be fetched
 
679
        from DynamoDB. This uses the ``AttributesToGet`` and set's
 
680
        ``Select`` to ``SPECIFIC_ATTRIBUTES`` API.
 
681
 
677
682
        Returns a ``ResultSet``, which transparently handles the pagination of
678
683
        results you get back.
679
684
 
719
724
                    "You must specify more than one key to filter on."
720
725
                )
721
726
 
 
727
        if attributes is not None:
 
728
            select = 'SPECIFIC_ATTRIBUTES'
 
729
        else:
 
730
            select = None
 
731
 
722
732
        results = ResultSet()
723
733
        kwargs = filter_kwargs.copy()
724
734
        kwargs.update({
726
736
            'index': index,
727
737
            'reverse': reverse,
728
738
            'consistent': consistent,
 
739
            'select': select,
 
740
            'attributes_to_get': attributes
729
741
        })
730
742
        results.to_call(self._query, **kwargs)
731
743
        return results
732
744
 
 
745
    def query_count(self, index=None, consistent=False, **filter_kwargs):
 
746
        """
 
747
        Queries the exact count of matching items in a DynamoDB table.
 
748
 
 
749
        Queries can be performed against a hash key, a hash+range key or
 
750
        against any data stored in your local secondary indexes.
 
751
 
 
752
        To specify the filters of the items you'd like to get, you can specify
 
753
        the filters as kwargs. Each filter kwarg should follow the pattern
 
754
        ``<fieldname>__<filter_operation>=<value_to_look_for>``.
 
755
 
 
756
        Optionally accepts an ``index`` parameter, which should be a string of
 
757
        name of the local secondary index you want to query against.
 
758
        (Default: ``None``)
 
759
 
 
760
        Optionally accepts a ``consistent`` parameter, which should be a
 
761
        boolean. If you provide ``True``, it will force a consistent read of
 
762
        the data (more expensive). (Default: ``False`` - use eventually
 
763
        consistent reads)
 
764
 
 
765
        Returns an integer which represents the exact amount of matched
 
766
        items.
 
767
 
 
768
        Example::
 
769
 
 
770
            # Look for last names equal to "Doe".
 
771
            >>> users.query_count(last_name__eq='Doe')
 
772
            5
 
773
 
 
774
            # Use an LSI & a consistent read.
 
775
            >>> users.query_count(
 
776
            ...     date_joined__gte=1236451000,
 
777
            ...     owner__eq=1,
 
778
            ...     index='DateJoinedIndex',
 
779
            ...     consistent=True
 
780
            ... )
 
781
            2
 
782
 
 
783
        """
 
784
        key_conditions = self._build_filters(
 
785
            filter_kwargs,
 
786
            using=QUERY_OPERATORS
 
787
        )
 
788
 
 
789
        raw_results = self.connection.query(
 
790
            self.table_name,
 
791
            index_name=index,
 
792
            consistent_read=consistent,
 
793
            select='COUNT',
 
794
            key_conditions=key_conditions,
 
795
        )
 
796
        return int(raw_results.get('Count', 0))
 
797
 
733
798
    def _query(self, limit=None, index=None, reverse=False, consistent=False,
734
 
               exclusive_start_key=None, **filter_kwargs):
 
799
               exclusive_start_key=None, select=None, attributes_to_get=None,
 
800
               **filter_kwargs):
735
801
        """
736
802
        The internal method that performs the actual queries. Used extensively
737
803
        by ``ResultSet`` to perform each (paginated) request.
741
807
            'index_name': index,
742
808
            'scan_index_forward': reverse,
743
809
            'consistent_read': consistent,
 
810
            'select': select,
 
811
            'attributes_to_get': attributes_to_get
744
812
        }
745
813
 
746
814
        if exclusive_start_key: