1 -- SQL-UNIX-API (prototype)
2 -- Copyright © 2014 František Kučera (frantovo.cz)
4 -- This program is free software: you can redistribute it and/or modify
5 -- it under the terms of the GNU General Public License as published by
6 -- the Free Software Foundation, either version 3 of the License, or
7 -- (at your option) any later version.
9 -- This program is distributed in the hope that it will be useful,
10 -- but WITHOUT ANY WARRANTY; without even the implied warranty of
11 -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 -- GNU General Public License for more details.
14 -- You should have received a copy of the GNU General Public License
15 -- along with this program. If not, see <http://www.gnu.org/licenses/>.
19 -- CREATE SCHEMA unix_sql_api;
21 SET search_path TO unix_sql_api;
24 -- fstab: --------------------------------------------------------------------
26 DROP VIEW IF EXISTS fstab;
27 DROP FUNCTION IF EXISTS fstab();
28 DROP TYPE IF EXISTS unix_sql_api_fstab;
30 CREATE TYPE unix_sql_api_fstab AS (
42 CREATE OR REPLACE FUNCTION fstab()
43 RETURNS SETOF unix_sql_api_fstab AS $$
47 open(FSTAB, "<", "/etc/fstab") or die $!;
50 if (/^([^\s#]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+(\d+)\s+(\d+)\s*$/) {
53 my $device_spec_value;
55 if ($1 =~ /([^=]+)=(.*)/) {
56 ($device_spec_type, $device_spec_value) = ($1, $2);
62 device_type => $device_spec_type,
63 device_value => $device_spec_value,
66 types => [split(",", $3)],
67 options => [split(",", $4)],
77 CREATE OR REPLACE VIEW fstab AS
82 -- user groups: --------------------------------------------------------------
84 DROP VIEW IF EXISTS groups;
85 DROP FUNCTION IF EXISTS groups();
86 DROP TYPE IF EXISTS unix_sql_api_groups;
88 CREATE TYPE unix_sql_api_groups AS (
94 CREATE OR REPLACE FUNCTION groups()
95 RETURNS SETOF unix_sql_api_groups AS $$
101 while (my $group = getgrent()) {
104 name => $group->name,
105 members => [@{$group->members}]
109 elog(NOTICE, "members field does not contain users who have this group as primary one");
114 CREATE OR REPLACE VIEW groups AS
115 SELECT * FROM groups()
117 COMMENT ON COLUMN groups.members IS 'does not contain users who have this group as primary one';
120 -- user: ---------------------------------------------------------------------
122 DROP VIEW IF EXISTS users;
123 DROP FUNCTION IF EXISTS users();
124 DROP TYPE IF EXISTS unix_sql_api_users;
126 CREATE TYPE unix_sql_api_users AS (
137 CREATE OR REPLACE FUNCTION users()
138 RETURNS SETOF unix_sql_api_users AS $$
142 use encoding "UTF-8";
145 while (my $user = getpwent()) {
150 # comment => $user->comment,
151 gecos => [split(",", $user->gecos)],
153 shell => $user->shell,
154 # expire => $user->expire
162 CREATE OR REPLACE VIEW users AS
163 SELECT * FROM users()