prototyp: users_groups fixed: getpwent/getgrent needs setpwent/setgrent and endpwent/endgrent if we want to call them multiple times
authorFrantišek Kučera <franta-hg@frantovo.cz>
Mon, 01 Sep 2014 21:08:55 +0200
changeset 99963cf89ffd7
parent 8 0f6df6850ba6
child 10 88bf2cb5e757
prototyp: users_groups fixed: getpwent/getgrent needs setpwent/setgrent and endpwent/endgrent if we want to call them multiple times
prototyp/prototyp.sql
     1.1 --- a/prototyp/prototyp.sql	Mon Sep 01 20:03:39 2014 +0200
     1.2 +++ b/prototyp/prototyp.sql	Mon Sep 01 21:08:55 2014 +0200
     1.3 @@ -82,8 +82,6 @@
     1.4  -- user groups: --------------------------------------------------------------
     1.5  
     1.6  DROP VIEW IF EXISTS users_groups;
     1.7 -DROP VIEW IF EXISTS users_groups_primary;
     1.8 -DROP VIEW IF EXISTS users_groups_secondary;
     1.9  
    1.10  DROP VIEW IF EXISTS groups;
    1.11  DROP FUNCTION IF EXISTS groups();
    1.12 @@ -104,17 +102,15 @@
    1.13  	
    1.14  	my $i = 0;
    1.15  
    1.16 +	setgrent();
    1.17  	while (my $group = getgrent()) {
    1.18  		return_next({
    1.19  			id => $group->gid,
    1.20  			name => $group->name,
    1.21  			members => [@{$group->members}]
    1.22  		});
    1.23 -		elog(NOTICE, "skupina je schizoidní " . $i++);
    1.24  	}
    1.25 -	elog(NOTICE, "XXX skupina je schizoidní " . $i++);
    1.26 -	
    1.27 -	elog(NOTICE, "members field does not contain users who have this group as primary one");
    1.28 +	endgrent();
    1.29  	
    1.30  	return undef;
    1.31  $$ LANGUAGE plperlu;
    1.32 @@ -122,7 +118,7 @@
    1.33  CREATE OR REPLACE VIEW groups AS
    1.34  	SELECT * FROM groups()
    1.35  ;
    1.36 -COMMENT ON COLUMN groups.members IS 'does not contain users who have this group as primary one';
    1.37 +COMMENT ON COLUMN groups.members IS 'does not contain users who have this group as primary one – see view: users_groups';
    1.38  
    1.39  
    1.40  -- users: --------------------------------------------------------------------
    1.41 @@ -152,6 +148,7 @@
    1.42  	
    1.43  	my $i = 0;
    1.44  	
    1.45 +	setpwent();
    1.46  	while (my $user = getpwent()) {
    1.47  		return_next({
    1.48  			id => $user->uid,
    1.49 @@ -163,10 +160,8 @@
    1.50  			shell => $user->shell,
    1.51  			# expire => $user->expire
    1.52  		});
    1.53 -		elog(NOTICE, "uživatel je schizoidní " . $i++);
    1.54  	}
    1.55 -	
    1.56 -	elog(NOTICE, "XXX uživatel je schizoidní " . $i++);
    1.57 +	endpwent();
    1.58  	
    1.59  	return undef;
    1.60  $$ LANGUAGE plperlu;
    1.61 @@ -178,22 +173,20 @@
    1.62  
    1.63  -- users_groups: -------------------------------------------------------------
    1.64  
    1.65 -CREATE OR REPLACE VIEW users_groups_primary AS
    1.66 +CREATE OR REPLACE VIEW users_groups AS
    1.67  	SELECT
    1.68  		u1.id AS uid,
    1.69  		u1.gid AS gid,
    1.70  		u1.name AS user,
    1.71  		g1.name AS group,
    1.72 -		true AS primary
    1.73 +		true AS initial
    1.74  	FROM users AS u1
    1.75  	JOIN groups AS g1 ON (u1.gid = g1.id)
    1.76 -;
    1.77 -
    1.78 -CREATE OR REPLACE VIEW users_groups_secondary AS
    1.79 +	UNION
    1.80  	SELECT
    1.81  		u2.id AS uid,
    1.82  		g2.*,
    1.83 -		false AS primary
    1.84 +		false AS initial
    1.85  	FROM
    1.86  		(SELECT
    1.87  			id AS gid,
    1.88 @@ -202,10 +195,5 @@
    1.89  		FROM groups) AS g2
    1.90  	JOIN users AS u2 ON (u2.name = g2.user)
    1.91  ;
    1.92 +COMMENT ON COLUMN users_groups.initial IS 'whether this group is the „initial login group“ of given user (the primary group)';
    1.93  
    1.94 -CREATE OR REPLACE VIEW users_groups AS
    1.95 -	SELECT * FROM users_groups_primary
    1.96 -	UNION
    1.97 -	SELECT * FROM users_groups_secondary
    1.98 -;
    1.99 -