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;
86 DROP VIEW IF EXISTS groups;
87 DROP FUNCTION IF EXISTS groups();
88 DROP TYPE IF EXISTS unix_sql_api_groups;
90 CREATE TYPE unix_sql_api_groups AS (
96 CREATE OR REPLACE FUNCTION groups()
97 RETURNS SETOF unix_sql_api_groups STABLE AS $$
104 while (my $group = getgrent()) {
107 name => $group->name,
108 members => [@{$group->members}]
116 CREATE OR REPLACE VIEW groups AS
117 SELECT * FROM groups()
119 COMMENT ON COLUMN groups.members IS 'does not contain users who have this group as primary one – see view: users_groups';
122 -- users: --------------------------------------------------------------------
124 DROP VIEW IF EXISTS users;
125 DROP FUNCTION IF EXISTS users();
126 DROP TYPE IF EXISTS unix_sql_api_users;
128 CREATE TYPE unix_sql_api_users AS (
139 CREATE OR REPLACE FUNCTION users()
140 RETURNS SETOF unix_sql_api_users STABLE AS $$
144 use encoding "UTF-8";
148 while (my $user = getpwent()) {
153 # comment => $user->comment,
154 gecos => [split(",", $user->gecos)],
156 shell => $user->shell,
157 # expire => $user->expire
165 CREATE OR REPLACE VIEW users AS
166 SELECT * FROM users()
170 -- users_groups: -------------------------------------------------------------
172 CREATE OR REPLACE VIEW users_groups AS
180 JOIN groups AS g1 ON (u1.gid = g1.id)
189 unnest(members) AS user,
192 JOIN users AS u2 ON (u2.name = g2.user)
194 COMMENT ON COLUMN users_groups.initial IS 'whether this group is the „initial login group“ of given user (the primary group)';
197 -- processes: ----------------------------------------------------------------
199 DROP VIEW IF EXISTS processes;
200 DROP FUNCTION IF EXISTS processes();
201 DROP TYPE IF EXISTS unix_sql_api_processes;
203 CREATE TYPE unix_sql_api_processes AS (
212 CREATE OR REPLACE FUNCTION processes()
213 RETURNS SETOF unix_sql_api_processes STABLE AS $$
217 use encoding "UTF-8";
223 command => "/bin/omg",
224 arguments => ["a", "b"],
225 working_dir => "/tmp"
231 CREATE OR REPLACE VIEW processes AS
232 SELECT * FROM processes()