def _make_privileges_list_query(name, object_type, prepend):
    '''
    Generate the SQL required for specific object type
    '''
    if object_type == 'table':
        query = (' '.join([
            'SELECT relacl AS name',
            'FROM pg_catalog.pg_class c',
            'JOIN pg_catalog.pg_namespace n',
            'ON n.oid = c.relnamespace',
            "WHERE nspname = '{0}'",
            "AND relname = '{1}'",
            "AND relkind = 'r'",
            'ORDER BY relname',
        ])).format(prepend, name)
    elif object_type == 'sequence':
        query = (' '.join([
            'SELECT relacl AS name',
            'FROM pg_catalog.pg_class c',
            'JOIN pg_catalog.pg_namespace n',
            'ON n.oid = c.relnamespace',
            "WHERE nspname = '{0}'",
            "AND relname = '{1}'",
            "AND relkind = 'S'",
            'ORDER BY relname',
        ])).format(prepend, name)
    elif object_type == 'schema':
        query = (' '.join([
            'SELECT nspacl AS name',
            'FROM pg_catalog.pg_namespace',
            "WHERE nspname = '{0}'",
            'ORDER BY nspname',
        ])).format(name)
    elif object_type == 'function':
        query = (' '.join([
            'SELECT proacl AS name',
            'FROM pg_catalog.pg_proc p',
            'JOIN pg_catalog.pg_namespace n',
            'ON n.oid = p.pronamespace',
            "WHERE nspname = '{0}'",
            "AND p.oid::regprocedure::text = '{1}'",
            'ORDER BY proname, proargtypes',
        ])).format(prepend, name)
    elif object_type == 'tablespace':
        query = (' '.join([
            'SELECT spcacl AS name',
            'FROM pg_catalog.pg_tablespace',
            "WHERE spcname = '{0}'",
            'ORDER BY spcname',
        ])).format(name)
    elif object_type == 'language':
        query = (' '.join([
            'SELECT lanacl AS name',
            'FROM pg_catalog.pg_language',
            "WHERE lanname = '{0}'",
            'ORDER BY lanname',
        ])).format(name)
    elif object_type == 'database':
        query = (' '.join([
            'SELECT datacl AS name',
            'FROM pg_catalog.pg_database',
            "WHERE datname = '{0}'",
            'ORDER BY datname',
        ])).format(name)
    elif object_type == 'group':
        query = (' '.join([
            'SELECT rolname, admin_option',
            'FROM pg_catalog.pg_auth_members m',
            'JOIN pg_catalog.pg_roles r',
            'ON m.member=r.oid',
            'WHERE m.roleid IN',
            '(SELECT oid',
            'FROM pg_catalog.pg_roles',
            "WHERE rolname='{0}')",
            'ORDER BY rolname',
        ])).format(name)

    return query
def _make_privileges_list_query ( name , object_type , prepend ) : if object_type == 'table' : query = ( ' ' . join ( [ 'SELECT relacl AS name' , 'FROM pg_catalog.pg_class c' , 'JOIN pg_catalog.pg_namespace n' , 'ON n.oid = c.relnamespace' , "WHERE nspname = '{0}'" , "AND relname = '{1}'" , "AND relkind = 'r'" , 'ORDER BY relname' , ] ) ) . format ( prepend , name ) elif object_type == 'sequence' : query = ( ' ' . join ( [ 'SELECT relacl AS name' , 'FROM pg_catalog.pg_class c' , 'JOIN pg_catalog.pg_namespace n' , 'ON n.oid = c.relnamespace' , "WHERE nspname = '{0}'" , "AND relname = '{1}'" , "AND relkind = 'S'" , 'ORDER BY relname' , ] ) ) . format ( prepend , name ) elif object_type == 'schema' : query = ( ' ' . join ( [ 'SELECT nspacl AS name' , 'FROM pg_catalog.pg_namespace' , "WHERE nspname = '{0}'" , 'ORDER BY nspname' , ] ) ) . format ( name ) elif object_type == 'function' : query = ( ' ' . join ( [ 'SELECT proacl AS name' , 'FROM pg_catalog.pg_proc p' , 'JOIN pg_catalog.pg_namespace n' , 'ON n.oid = p.pronamespace' , "WHERE nspname = '{0}'" , "AND p.oid::regprocedure::text = '{1}'" , 'ORDER BY proname, proargtypes' , ] ) ) . format ( prepend , name ) elif object_type == 'tablespace' : query = ( ' ' . join ( [ 'SELECT spcacl AS name' , 'FROM pg_catalog.pg_tablespace' , "WHERE spcname = '{0}'" , 'ORDER BY spcname' , ] ) ) . format ( name ) elif object_type == 'language' : query = ( ' ' . join ( [ 'SELECT lanacl AS name' , 'FROM pg_catalog.pg_language' , "WHERE lanname = '{0}'" , 'ORDER BY lanname' , ] ) ) . format ( name ) elif object_type == 'database' : query = ( ' ' . join ( [ 'SELECT datacl AS name' , 'FROM pg_catalog.pg_database' , "WHERE datname = '{0}'" , 'ORDER BY datname' , ] ) ) . format ( name ) elif object_type == 'group' : query = ( ' ' . join ( [ 'SELECT rolname, admin_option' , 'FROM pg_catalog.pg_auth_members m' , 'JOIN pg_catalog.pg_roles r' , 'ON m.member=r.oid' , 'WHERE m.roleid IN' , '(SELECT oid' , 'FROM pg_catalog.pg_roles' , "WHERE rolname='{0}')" , 'ORDER BY rolname' , ] ) ) . format ( name ) return query
Generate the SQL required for specific object type