Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/pynslcd/host.py
diff options
context:
space:
mode:
authorArthur de Jong <arthur@arthurdejong.org>2013-08-12 22:42:56 +0200
committerArthur de Jong <arthur@arthurdejong.org>2013-08-17 12:31:36 +0200
commitd66162ad308878d2f3fb505a05742798283a8854 (patch)
treea7736f6cc1a1c065f69fb02ee49dfc8054d32886 /pynslcd/host.py
parentbfe22cc93563d86c8c35cd068810d7f8ac2dee33 (diff)
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.
Diffstat (limited to 'pynslcd/host.py')
-rw-r--r--pynslcd/host.py51
1 files changed, 30 insertions, 21 deletions
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):