- role_max = Roles.role_max
-
- # N.B.: Site IDs returned may be deleted. Persons returned are
- # never deleted, but may not be enabled.
- sql = "SELECT persons.*" \
- ", roles.role_id, roles.name AS role" \
- ", person_site.site_id" \
-
- # N.B.: Joined IDs may be marked as deleted in their primary tables
- join_tables = {
- # extra_field: (extra_table, extra_column, join_using)
- 'address_ids': ('person_address', 'address_id', 'person_id'),
- 'key_ids': ('person_keys', 'key_id', 'person_id'),
- 'slice_ids': ('dslice03_sliceuser', 'slice_id', 'person_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 persons" \
- " LEFT JOIN person_roles USING (person_id)" \
- " LEFT JOIN roles USING (role_id)" \
- " LEFT JOIN person_site USING (person_id)"
-
- if extra_tables:
- sql += " LEFT JOIN " + " LEFT JOIN ".join(extra_tables)
-
- # So that people with no roles have empty role_ids and roles values
- sql += " WHERE (role_id IS NULL or role_id <= %(role_max)d)"