Esempio n. 1
0
 def filter_objs_count(self, curs):
     cond = self._cond_by_filter_params()
     q = Select(self.obj_class.table, columns=[Columns.COUNT_ALL], cond=cond)
     curs.execute(*q.glue())
     count_dict = fetchone_dict(curs)
     _, count = count_dict.popitem()
     return int(count)
Esempio n. 2
0
    def test_nested_select(self):
        nested = Select('service_set_descr', columns=['name'], cond=Eq('name', 'registration ru'))
        c, p = nested.glue()
        self.assertEqual(c, 'SELECT "name" FROM "service_set_descr" WHERE "name" = %s')
        self.assertEqual(p, ['registration ru'])

        scoped_cond = Scoped(nested)
        c, p = scoped_cond.glue()
        self.assertEqual(c, '(SELECT "name" FROM "service_set_descr" WHERE "name" = %s)')
        self.assertEqual(p, ['registration ru'])

        cond_eq = Eq('service_set_descr_id', Scoped(nested))
        c, p = cond_eq.glue()
        self.assertEqual(c, '"service_set_descr_id" = (SELECT "name" FROM "service_set_descr" WHERE "name" = %s)')
        self.assertEqual(p, ['registration ru'])

        nested = Select('service_set', columns=['service_type_id'], cond=cond_eq)
        c, p = nested.glue()
        self.assertEqual(c, 'SELECT "service_type_id" FROM "service_set" WHERE "service_set_descr_id" = (SELECT "name" FROM "service_set_descr" WHERE "name" = %s)')
        self.assertEqual(p, ['registration ru'])

        in_cond = In('id', nested)
        c, p = in_cond.glue()
        self.assertEqual(c, '''"id" IN (SELECT "service_type_id" FROM "service_set" WHERE "service_set_descr_id" = (SELECT "name" FROM "service_set_descr" WHERE "name" = %s))''')
        self.assertEqual(p, ['registration ru'])

        any_cond = Any('id', nested)
        c, p = any_cond.glue()
        self.assertEqual(c, '''%s = ANY (SELECT "service_type_id" FROM "service_set" WHERE "service_set_descr_id" = (SELECT "name" FROM "service_set_descr" WHERE "name" = %s))''')
        self.assertEqual(p, ['id', 'registration ru'])
Esempio n. 3
0
 def get_last_applied(self, curs=None):
     if not self.is_table_exist(curs):
         return None
     else:
         q = Select(self.table, order_by=['-id'], limit=1)
         curs.execute(*q.glue())
         result = wrapper.fetchall_dicts(curs)
         return result[0]['name'] if len(result) else None
Esempio n. 4
0
 def is_table_exist(self, curs):
     q = Select('pg_tables', cond=Eq('tablename', self.table))
     curs.execute(*q.glue())
     return len(curs.fetchall()) > 0