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 users_groups;
85 DROP VIEW IF EXISTS users_groups_primary;
86 DROP VIEW IF EXISTS users_groups_secondary;
88 DROP VIEW IF EXISTS groups;
89 DROP FUNCTION IF EXISTS groups();
90 DROP TYPE IF EXISTS unix_sql_api_groups;
92 CREATE TYPE unix_sql_api_groups AS (
98 CREATE OR REPLACE FUNCTION groups()
99 RETURNS SETOF unix_sql_api_groups STABLE AS $$
107 while (my $group = getgrent()) {
110 name => $group->name,
111 members => [@{$group->members}]
113 elog(NOTICE, "skupina je schizoidní " . $i++);
115 elog(NOTICE, "XXX skupina je schizoidní " . $i++);
117 elog(NOTICE, "members field does not contain users who have this group as primary one");
122 CREATE OR REPLACE VIEW groups AS
123 SELECT * FROM groups()
125 COMMENT ON COLUMN groups.members IS 'does not contain users who have this group as primary one';
128 -- users: --------------------------------------------------------------------
130 DROP VIEW IF EXISTS users;
131 DROP FUNCTION IF EXISTS users();
132 DROP TYPE IF EXISTS unix_sql_api_users;
134 CREATE TYPE unix_sql_api_users AS (
145 CREATE OR REPLACE FUNCTION users()
146 RETURNS SETOF unix_sql_api_users STABLE AS $$
150 use encoding "UTF-8";
155 while (my $user = getpwent()) {
160 # comment => $user->comment,
161 gecos => [split(",", $user->gecos)],
163 shell => $user->shell,
164 # expire => $user->expire
166 elog(NOTICE, "uživatel je schizoidní " . $i++);
169 elog(NOTICE, "XXX uživatel je schizoidní " . $i++);
174 CREATE OR REPLACE VIEW users AS
175 SELECT * FROM users()
179 -- users_groups: -------------------------------------------------------------
181 CREATE OR REPLACE VIEW users_groups_primary AS
189 JOIN groups AS g1 ON (u1.gid = g1.id)
192 CREATE OR REPLACE VIEW users_groups_secondary AS
200 unnest(members) AS user,
203 JOIN users AS u2 ON (u2.name = g2.user)
206 CREATE OR REPLACE VIEW users_groups AS
207 SELECT * FROM users_groups_primary
209 SELECT * FROM users_groups_secondary