prototyp/prototyp.sql
author František Kučera <franta-hg@frantovo.cz>
Mon, 01 Sep 2014 17:47:00 +0200
changeset 5 bb406ad320b6
parent 4 b0bbfff8eca1
child 6 dc83d208e862
permissions -rw-r--r--
prototyp: users
     1 -- CREATE SCHEMA unix_sql_api;
     2 
     3 SET search_path TO unix_sql_api;
     4 
     5 
     6 -- fstab: --------------------------------------------------------------------
     7 
     8 DROP VIEW IF EXISTS fstab;
     9 DROP FUNCTION IF EXISTS fstab();
    10 DROP TYPE IF EXISTS unix_sql_api_fstab;
    11 
    12 CREATE TYPE unix_sql_api_fstab AS (
    13 	device VARCHAR,
    14 	device_type VARCHAR,
    15 	device_value VARCHAR,
    16 	mount_point VARCHAR,
    17 	type VARCHAR,
    18 	types VARCHAR[],
    19 	options VARCHAR[],
    20 	dump INTEGER,
    21 	pass INTEGER
    22 );
    23 
    24 CREATE OR REPLACE FUNCTION fstab()
    25 RETURNS SETOF unix_sql_api_fstab AS $$
    26 	use strict;
    27 	use warnings;
    28 	
    29 	open(FSTAB, "<", "/etc/fstab") or die $!;
    30 	
    31 	while (<FSTAB>) {
    32 		if (/^([^\s#]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+(\d+)\s+(\d+)\s*$/) {
    33 			
    34 			my $device_spec_type;
    35 			my $device_spec_value;
    36 			{
    37 				if ($1 =~ /([^=]+)=(.*)/) {
    38 					($device_spec_type, $device_spec_value) = ($1, $2);
    39 				}
    40 			}
    41 			
    42 			return_next({
    43 				device => $1,
    44 				device_type => $device_spec_type,
    45 				device_value => $device_spec_value,
    46 				mount_point => $2,
    47 				type => $3,
    48 				types => [split(",", $3)],
    49 				options => [split(",", $4)],
    50 				dump => $5,
    51 				pass => $6
    52 			});
    53 		}
    54 	}
    55 	
    56 	return undef;
    57 $$ LANGUAGE plperlu;
    58 
    59 CREATE OR REPLACE VIEW fstab AS
    60 	SELECT * FROM fstab()
    61 ;
    62 
    63 
    64 -- user groups: --------------------------------------------------------------
    65 
    66 DROP VIEW IF EXISTS groups;
    67 DROP FUNCTION IF EXISTS groups();
    68 DROP TYPE IF EXISTS unix_sql_api_groups;
    69 
    70 CREATE TYPE unix_sql_api_groups AS (
    71 	id INTEGER,
    72 	name VARCHAR,
    73 	members VARCHAR[]
    74 );
    75 
    76 CREATE OR REPLACE FUNCTION groups()
    77 RETURNS SETOF unix_sql_api_groups AS $$
    78 	use strict;
    79 	use warnings;
    80 
    81 	use User::grent;
    82 
    83 	while (my $group = getgrent()) {
    84 		return_next({
    85 			id => $group->gid,
    86 			name => $group->name,
    87 			members => [@{$group->members}]
    88 		});
    89 	}
    90 	
    91 	elog(NOTICE, "members field does not contain users who have this group as primary one");
    92 	
    93 	return undef;
    94 $$ LANGUAGE plperlu;
    95 
    96 CREATE OR REPLACE VIEW groups AS
    97 	SELECT * FROM groups()
    98 ;
    99 COMMENT ON COLUMN groups.members IS 'does not contain users who have this group as primary one';
   100 
   101 
   102 -- user: ---------------------------------------------------------------------
   103 
   104 DROP VIEW IF EXISTS users;
   105 DROP FUNCTION IF EXISTS users();
   106 DROP TYPE IF EXISTS unix_sql_api_users;
   107 
   108 CREATE TYPE unix_sql_api_users AS (
   109 	id INTEGER,
   110 	gid INTEGER,
   111 	name VARCHAR,
   112 	-- comment VARCHAR,
   113 	gecos VARCHAR[],
   114 	home VARCHAR,
   115 	shell VARCHAR
   116 	-- expire VARCHAR
   117 );
   118 
   119 CREATE OR REPLACE FUNCTION users()
   120 RETURNS SETOF unix_sql_api_users AS $$
   121 	use strict;
   122 	use warnings;
   123 	
   124 	use Encode; # FIXME: see below
   125 	use User::pwent;
   126 	
   127 	while (my $user = getpwent()) {
   128 		return_next({
   129 			id => $user->uid,
   130 			gid => $user->gid,
   131 			name => $user->name,
   132 			# comment => $user->comment,
   133 			gecos => [split(",", Encode::decode("utf8", $user->gecos))], # FIXME: ugly hack – should be properly decoded in getpwent()
   134 			home => $user->dir,
   135 			shell => $user->shell,
   136 			# expire => $user->expire
   137 			
   138 		});
   139 	}
   140 	
   141 	return undef;
   142 $$ LANGUAGE plperlu;
   143 
   144 CREATE OR REPLACE VIEW users AS
   145 	SELECT * FROM users()
   146 ;
   147