@@ -43,7 +43,6 @@ static bool describeOneTableDetails(const char *schemaname,
43
43
bool verbose );
44
44
static void add_tablespace_footer (printTableContent * const cont , char relkind ,
45
45
Oid tablespace , const bool newline );
46
- static void add_role_attribute (PQExpBuffer buf , const char * const str );
47
46
static bool listTSParsersVerbose (const char * pattern );
48
47
static bool describeOneTSParser (const char * oid , const char * nspname ,
49
48
const char * prsname );
@@ -3712,34 +3711,47 @@ describeRoles(const char *pattern, bool verbose, bool showSystem)
3712
3711
{
3713
3712
PQExpBufferData buf ;
3714
3713
PGresult * res ;
3715
- printTableContent cont ;
3716
- printTableOpt myopt = pset .popt .topt ;
3717
- int ncols = 2 ;
3718
- int nrows = 0 ;
3719
- int i ;
3720
- int conns ;
3721
- const char align = 'l' ;
3722
- char * * attr ;
3723
-
3724
- myopt .default_footer = false;
3714
+ printQueryOpt myopt = pset .popt ;
3725
3715
3726
3716
initPQExpBuffer (& buf );
3727
-
3728
3717
printfPQExpBuffer (& buf ,
3729
- "SELECT r.rolname, r.rolsuper, r.rolinherit,\n"
3730
- " r.rolcreaterole, r.rolcreatedb, r.rolcanlogin,\n"
3731
- " r.rolconnlimit, r.rolvaliduntil" );
3732
-
3733
- if (verbose )
3734
- {
3735
- appendPQExpBufferStr (& buf , "\n, pg_catalog.shobj_description(r.oid, 'pg_authid') AS description" );
3736
- ncols ++ ;
3737
- }
3738
- appendPQExpBufferStr (& buf , "\n, r.rolreplication" );
3718
+ "SELECT r.rolname AS \"%s\",\n"
3719
+ " CASE WHEN r.rolcanlogin THEN '%s' ELSE '%s' END \"%s\",\n"
3720
+ " pg_catalog.concat_ws(E'\\n',\n"
3721
+ " CASE WHEN r.rolsuper THEN '%s' END,\n"
3722
+ " CASE WHEN r.rolcreatedb THEN '%s' END,\n"
3723
+ " CASE WHEN r.rolcreaterole THEN '%s' END,\n"
3724
+ " CASE WHEN r.rolinherit THEN '%s' END,\n"
3725
+ " CASE WHEN r.rolreplication THEN '%s' END" ,
3726
+ gettext_noop ("Role name" ),
3727
+ gettext_noop ("yes" ), gettext_noop ("no" ),
3728
+ gettext_noop ("Login" ),
3729
+ gettext_noop ("Superuser" ),
3730
+ gettext_noop ("Create DB" ),
3731
+ gettext_noop ("Create role" ),
3732
+ gettext_noop ("Inherit" ),
3733
+ gettext_noop ("Replication" ));
3739
3734
3740
3735
if (pset .sversion >= 90500 )
3736
+ appendPQExpBuffer (& buf ,
3737
+ ",\n CASE WHEN r.rolbypassrls THEN '%s' END" ,
3738
+ gettext_noop ("Bypass RLS" ));
3739
+
3740
+ appendPQExpBuffer (& buf , "\n ) AS \"%s\"" , gettext_noop ("Attributes" ));
3741
+
3742
+ appendPQExpBuffer (& buf ,
3743
+ ",\n r.rolvaliduntil AS \"%s\",\n"
3744
+ " CASE WHEN r.rolconnlimit = -1 THEN NULL\n"
3745
+ " ELSE r.rolconnlimit\n"
3746
+ " END \"%s\"" ,
3747
+ gettext_noop ("Valid until" ),
3748
+ gettext_noop ("Connection limit" ));
3749
+
3750
+ if (verbose )
3741
3751
{
3742
- appendPQExpBufferStr (& buf , "\n, r.rolbypassrls" );
3752
+ appendPQExpBuffer (& buf ,
3753
+ ",\n pg_catalog.shobj_description(r.oid, 'pg_authid') AS \"%s\"" ,
3754
+ gettext_noop ("Description" ));
3743
3755
}
3744
3756
3745
3757
appendPQExpBufferStr (& buf , "\nFROM pg_catalog.pg_roles r\n" );
@@ -3758,99 +3770,19 @@ describeRoles(const char *pattern, bool verbose, bool showSystem)
3758
3770
appendPQExpBufferStr (& buf , "ORDER BY 1;" );
3759
3771
3760
3772
res = PSQLexec (buf .data );
3773
+ termPQExpBuffer (& buf );
3761
3774
if (!res )
3762
3775
return false;
3763
3776
3764
- nrows = PQntuples (res );
3765
- attr = pg_malloc0 ((nrows + 1 ) * sizeof (* attr ));
3766
-
3767
- printTableInit (& cont , & myopt , _ ("List of roles" ), ncols , nrows );
3768
-
3769
- printTableAddHeader (& cont , gettext_noop ("Role name" ), true, align );
3770
- printTableAddHeader (& cont , gettext_noop ("Attributes" ), true, align );
3771
-
3772
- if (verbose )
3773
- printTableAddHeader (& cont , gettext_noop ("Description" ), true, align );
3774
-
3775
- for (i = 0 ; i < nrows ; i ++ )
3776
- {
3777
- printTableAddCell (& cont , PQgetvalue (res , i , 0 ), false, false);
3778
-
3779
- resetPQExpBuffer (& buf );
3780
- if (strcmp (PQgetvalue (res , i , 1 ), "t" ) == 0 )
3781
- add_role_attribute (& buf , _ ("Superuser" ));
3782
-
3783
- if (strcmp (PQgetvalue (res , i , 2 ), "t" ) != 0 )
3784
- add_role_attribute (& buf , _ ("No inheritance" ));
3785
-
3786
- if (strcmp (PQgetvalue (res , i , 3 ), "t" ) == 0 )
3787
- add_role_attribute (& buf , _ ("Create role" ));
3788
-
3789
- if (strcmp (PQgetvalue (res , i , 4 ), "t" ) == 0 )
3790
- add_role_attribute (& buf , _ ("Create DB" ));
3791
-
3792
- if (strcmp (PQgetvalue (res , i , 5 ), "t" ) != 0 )
3793
- add_role_attribute (& buf , _ ("Cannot login" ));
3794
-
3795
- if (strcmp (PQgetvalue (res , i , (verbose ? 9 : 8 )), "t" ) == 0 )
3796
- add_role_attribute (& buf , _ ("Replication" ));
3797
-
3798
- if (pset .sversion >= 90500 )
3799
- if (strcmp (PQgetvalue (res , i , (verbose ? 10 : 9 )), "t" ) == 0 )
3800
- add_role_attribute (& buf , _ ("Bypass RLS" ));
3801
-
3802
- conns = atoi (PQgetvalue (res , i , 6 ));
3803
- if (conns >= 0 )
3804
- {
3805
- if (buf .len > 0 )
3806
- appendPQExpBufferChar (& buf , '\n' );
3807
-
3808
- if (conns == 0 )
3809
- appendPQExpBufferStr (& buf , _ ("No connections" ));
3810
- else
3811
- appendPQExpBuffer (& buf , ngettext ("%d connection" ,
3812
- "%d connections" ,
3813
- conns ),
3814
- conns );
3815
- }
3816
-
3817
- if (strcmp (PQgetvalue (res , i , 7 ), "" ) != 0 )
3818
- {
3819
- if (buf .len > 0 )
3820
- appendPQExpBufferChar (& buf , '\n' );
3821
- appendPQExpBufferStr (& buf , _ ("Password valid until " ));
3822
- appendPQExpBufferStr (& buf , PQgetvalue (res , i , 7 ));
3823
- }
3824
-
3825
- attr [i ] = pg_strdup (buf .data );
3826
-
3827
- printTableAddCell (& cont , attr [i ], false, false);
3828
-
3829
- if (verbose )
3830
- printTableAddCell (& cont , PQgetvalue (res , i , 8 ), false, false);
3831
- }
3832
- termPQExpBuffer (& buf );
3833
-
3834
- printTable (& cont , pset .queryFout , false, pset .logfile );
3835
- printTableCleanup (& cont );
3777
+ myopt .title = _ ("List of roles" );
3778
+ myopt .translate_header = true;
3836
3779
3837
- for (i = 0 ; i < nrows ; i ++ )
3838
- free (attr [i ]);
3839
- free (attr );
3780
+ printQuery (res , & myopt , pset .queryFout , false, pset .logfile );
3840
3781
3841
3782
PQclear (res );
3842
3783
return true;
3843
3784
}
3844
3785
3845
- static void
3846
- add_role_attribute (PQExpBuffer buf , const char * const str )
3847
- {
3848
- if (buf -> len > 0 )
3849
- appendPQExpBufferStr (buf , ", " );
3850
-
3851
- appendPQExpBufferStr (buf , str );
3852
- }
3853
-
3854
3786
/*
3855
3787
* \drds
3856
3788
*/
0 commit comments