From d66162ad308878d2f3fb505a05742798283a8854 Mon Sep 17 00:00:00 2001 From: Arthur de Jong Date: Mon, 12 Aug 2013 22:42:56 +0200 Subject: Use retrieve_by, group_by and group_columns in the cache This removes custom retrieve() functions and Query classes from the database modules and uses retrieve_sql retrieve_by, group_by and group_columns to make a custom retrieval query. In the cache module this completely replaces how the query grouping is done. The Query class is now only used inside the cache and the CnAliasedQuery, RowGrouper and related classed have been removed. --- pynslcd/host.py | 51 ++++++++++++++++++++++++++++++--------------------- 1 file changed, 30 insertions(+), 21 deletions(-) (limited to 'pynslcd/host.py') diff --git a/pynslcd/host.py b/pynslcd/host.py index 91c3fa0..04f5337 100644 --- a/pynslcd/host.py +++ b/pynslcd/host.py @@ -34,23 +34,6 @@ class Search(search.LDAPSearch): required = ('cn', ) -class HostQuery(cache.CnAliasedQuery): - - sql = ''' - SELECT `host_cache`.`cn` AS `cn`, - `host_alias_cache`.`cn` AS `alias`, - `host_address_cache`.`ipHostNumber` AS `ipHostNumber` - FROM `host_cache` - LEFT JOIN `host_alias_cache` - ON `host_alias_cache`.`host` = `host_cache`.`cn` - LEFT JOIN `host_address_cache` - ON `host_address_cache`.`host` = `host_cache`.`cn` - ''' - - def __init__(self, parameters): - super(HostQuery, self).__init__('host', parameters) - - class Cache(cache.Cache): tables = ('host_cache', 'host_alias_cache', 'host_address_cache') @@ -73,10 +56,36 @@ class Cache(cache.Cache): CREATE INDEX IF NOT EXISTS `host_address_idx` ON `host_address_cache`(`host`); ''' - def retrieve(self, parameters): - query = HostQuery(parameters) - for row in cache.RowGrouper(query.execute(self.con), ('cn', ), ('alias', 'ipHostNumber', )): - yield row['cn'], row['alias'], row['ipHostNumber'] + retrieve_sql = ''' + SELECT `host_cache`.`cn` AS `cn`, + `host_alias_cache`.`cn` AS `alias`, + `host_address_cache`.`ipHostNumber` AS `ipHostNumber`, + `host_cache`.`mtime` AS `mtime` + FROM `host_cache` + LEFT JOIN `host_alias_cache` + ON `host_alias_cache`.`host` = `host_cache`.`cn` + LEFT JOIN `host_address_cache` + ON `host_address_cache`.`host` = `host_cache`.`cn` + ''' + + retrieve_by = dict( + cn=''' + ( `host_cache`.`cn` = ? OR + `host_cache`.`cn` IN ( + SELECT `by_alias`.`host` + FROM `host_alias_cache` `by_alias` + WHERE `by_alias`.`cn` = ?)) + ''', + ipHostNumber=''' + `host_cache`.`cn` IN ( + SELECT `by_ipHostNumber`.`host` + FROM `host_address_cache` `by_ipHostNumber` + WHERE `by_ipHostNumber`.`ipHostNumber` = ?) + ''', + ) + + group_by = (0, ) # cn + group_columns = (1, 2) # alias, ipHostNumber class HostRequest(common.Request): -- cgit v1.2.3