Ejemplo n.º 1
0
    def count(cls, transaction: Optional[spanner_transaction.Transaction],
              *conditions: condition.Condition) -> int:
        """Returns the number of objects in Spanner that match the given conditions.

    Args:
      transaction: The existing transaction to use, or None to start a new
        transaction
      *conditions: Instances of subclasses of Condition that help specify which
        rows should be included in the count. The includes condition is not
        allowed here

    Returns:
      The integer result of the COUNT query
    """
        builder = query.CountQuery(cls, conditions)
        args = [builder.sql(), builder.parameters(), builder.types()]
        results = cls._execute_read(table_apis.sql_query, transaction, args)
        return builder.process_results(results)
Ejemplo n.º 2
0
 def test_count_only_allows_where_and_from_segment_conditions(
         self, condition):
     with self.assertRaises(error.SpannerError):
         query.CountQuery(models.UnittestModel, [condition])
Ejemplo n.º 3
0
 def test_count_allows_force_index(self):
     force_index = condition.force_index('test_index')
     count_query = query.CountQuery(models.UnittestModel, [force_index])
     sql = count_query.sql()
     expected_sql = 'SELECT COUNT(*) FROM table@{FORCE_INDEX=test_index}'
     self.assertEqual(expected_sql, sql)