Ejemplo n.º 1
0
    def optimized_count(self, **kwargs):
        """Return count in optimized manner

        Apple.objects.filter(indice=4).count()
        Apple.objects.optimized_count(indice=4)

        """
        if (len(kwargs) == 0):
            raise MissingKwargs("you must specify the filter clause")
        if (len(kwargs) > 1):
            raise TooManyKwargs()
        # do not care about more than one kwarg
        (key, value) = kwargs.items()[0]
        (nbe, column, qfilter) = tool.parse_kwarg(key)

        agg = 'agg_count'
        agf = agg.split('_')

        agt = util.AggTrigger(util.PG_BACKEND, self.model._meta.db_table,
                              column)

        if nbe == 1:
            agf = agg
        elif nbe == 2:
            agf = agt.agg_function(agg, agf[1])

        cursor = connection.cursor()
        qry = """SELECT {} FROM {} WHERE {} %s"""

        tbname = util.table_name(self.model._meta.db_table, column)
        cursor.execute(qry.format(agf, tbname, qfilter), [value])
        row = cursor.fetchone()
        return row[0]
Ejemplo n.º 2
0
 def test_less_or_equal_than(self):
     res = tool.parse_kwarg("foo__lte")
     self.assertEqual(res, (2, 'foo', 'foo <='))
Ejemplo n.º 3
0
 def test_greater_or_equal_than(self):
     res = tool.parse_kwarg("foo__gte")
     self.assertEqual(res, (2, 'foo', 'foo >='))
Ejemplo n.º 4
0
 def test_equal(self):
     res = tool.parse_kwarg("foo")
     self.assertEqual(res, (1, 'foo', 'foo='))
Ejemplo n.º 5
0
 def test_less_or_equal_than(self):
     res = tool.parse_kwarg("foo__lte")
     self.assertEqual(res, (2, 'foo', 'foo <='))
Ejemplo n.º 6
0
 def test_greater_or_equal_than(self):
     res = tool.parse_kwarg("foo__gte")
     self.assertEqual(res, (2, 'foo', 'foo >='))
Ejemplo n.º 7
0
 def test_equal(self):
     res = tool.parse_kwarg("foo")
     self.assertEqual(res, (1, 'foo', 'foo='))