def get_count(class_, cursor, column_value_pairs=None, **columns): if not column_value_pairs: column_value_pairs = columns.items() column_value_pairs.sort() query, bind_vars = sql_builder.build_count_query(class_.table_name, column_value_pairs) cursor.execute(query, bind_vars) return cursor.fetch_aggregate_function(sum)
def test_build_count_query(self): query, bind_vars = sql_builder.build_count_query(table_name='my_table', column_value_pairs=[ ('col_a', 2) ]) self.assertEqual( query, 'SELECT COUNT(1) FROM my_table WHERE col_a = %(col_a_1)s') self.assertEqual(bind_vars, dict(col_a_1=2))
def get_count(class_, cursor, column_value_pairs=None, **columns): if not column_value_pairs: column_value_pairs = columns.items() column_value_pairs.sort() query, bind_vars = sql_builder.build_count_query( class_.table_name, column_value_pairs) cursor.execute(query, bind_vars) return cursor.fetch_aggregate_function(sum)
def test_build_count_query(self): query, bind_vars = sql_builder.build_count_query( table_name='my_table', column_value_pairs=[('col_a', 2)]) self.assertEqual( query, 'SELECT COUNT(1) FROM my_table WHERE col_a = %(col_a_1)s') self.assertEqual(bind_vars, dict(col_a_1=2))