- sql = "SELECT sites.*" \
- ", dslice03_siteinfo.max_slices"
-
- # N.B.: Joined IDs may be marked as deleted in their primary tables
- join_tables = {
- # extra_field: (extra_table, extra_column, join_using)
- 'person_ids': ('person_site', 'person_id', 'site_id'),
- 'slice_ids': ('dslice03_slices', 'slice_id', 'site_id'),
- 'defaultattribute_ids': ('dslice03_defaultattribute', 'defaultattribute_id', 'site_id'),
- 'pcu_ids': ('pcu', 'pcu_id', 'site_id'),
- 'node_ids': ('nodegroup_nodes', 'node_id', 'nodegroup_id'),
- }
-
- extra_fields = filter(join_tables.has_key, extra_fields)
- extra_tables = ["%s USING (%s)" % \
- (join_tables[field][0], join_tables[field][2]) \
- for field in extra_fields]
- extra_columns = ["%s.%s" % \
- (join_tables[field][0], join_tables[field][1]) \
- for field in extra_fields]
-
- if extra_columns:
- sql += ", " + ", ".join(extra_columns)
-
- sql += " FROM sites" \
- " LEFT JOIN dslice03_siteinfo USING (site_id)"
-
- if extra_tables:
- sql += " LEFT JOIN " + " LEFT JOIN ".join(extra_tables)
-
- sql += " WHERE sites.deleted IS False"