prototyp/prototyp.sql
author František Kučera <franta-hg@frantovo.cz>
Mon, 01 Sep 2014 15:36:16 +0200
changeset 2 e4e0975dbe85
child 3 2cd2860c8fb3
permissions -rw-r--r--
prototyp: fstab
     1 -- CREATE SCHEMA unix_sql_api;
     2 
     3 SET search_path TO unix_sql_api;
     4 
     5 DROP VIEW IF EXISTS fstab;
     6 DROP FUNCTION IF EXISTS fstab();
     7 DROP TYPE IF EXISTS unix_sql_api_fstab;
     8 
     9 CREATE TYPE unix_sql_api_fstab AS (
    10 	device VARCHAR,
    11 	device_type VARCHAR,
    12 	device_value VARCHAR,
    13 	mount_point VARCHAR,
    14 	type VARCHAR,
    15 	types VARCHAR[],
    16 	options VARCHAR[],
    17 	dump INTEGER,
    18 	pass INTEGER
    19 );
    20 
    21 CREATE OR REPLACE FUNCTION fstab()
    22 RETURNS SETOF unix_sql_api_fstab AS $$
    23 	use strict;
    24 	use warnings;
    25 	
    26 	open(FSTAB, "<", "/etc/fstab") or die $!;
    27 	
    28 	while (<FSTAB>) {
    29 		if (/^([^\s#]+)\s+([^\s]+)\s+([^\s]+)\s+([^\s]+)\s+(\d+)\s+(\d+)\s*$/) {
    30 			
    31 			my $device_spec_type;
    32 			my $device_spec_value;
    33 			{
    34 				if ($1 =~ /([^=]+)=(.*)/) {
    35 					($device_spec_type, $device_spec_value) = ($1, $2);
    36 				}
    37 			}
    38 			
    39 			return_next({
    40 				device => $1,
    41 				device_type => $device_spec_type,
    42 				device_value => $device_spec_value,
    43 				mount_point => $2,
    44 				type => $3,
    45 				types => [split(",", $3)],
    46 				options => [split(",", $4)],
    47 				dump => $5,
    48 				pass => $6
    49 			});
    50 		}
    51 	}
    52 	
    53 	return undef;
    54 $$ LANGUAGE plperlu;
    55 
    56 CREATE OR REPLACE VIEW fstab AS
    57 	SELECT * FROM fstab()
    58 ;