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