| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::PgSQL::Role::Owned; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use base 'FusqlFS::Backend::PgSQL::Role::Base'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | FusqlFS::Backend::PgSQL::Role::Owned - FusqlFS module to aggregate all | ||
| 12 | PostgreSQL artifacts owned by a role into single place | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | package FusqlFS::Backend::PgSQL::Roles; | ||
| 17 | use base 'FusqlFS::Artifact'; | ||
| 18 | |||
| 19 | use FusqlFS::Backend::PgSQL::Role::Owned; | ||
| 20 | |||
| 21 | sub init | ||
| 22 | { | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | # initialize instance and class | ||
| 26 | |||
| 27 | $self->{owned} = FusqlFS::Backend::PgSQL::Role::Owned->new(); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub get | ||
| 31 | { | ||
| 32 | my $class = shift; | ||
| 33 | my ($role) = @_; | ||
| 34 | my $result = {}; | ||
| 35 | |||
| 36 | # get data about $role into $result | ||
| 37 | |||
| 38 | $result->{owned} = $self->{owned}; | ||
| 39 | |||
| 40 | return $result; | ||
| 41 | } | ||
| 42 | |||
| 43 | =head1 DESCRIPTION | ||
| 44 | |||
| 45 | This module gathers all database artifacts owned by single role into one single | ||
| 46 | place. It does it by providing symlinks to artifacts sorted by type (`tables', | ||
| 47 | `functions' etc., see L</EXPOSED STRUCTURE> for details). | ||
| 48 | |||
| 49 | This module is not to be plugged in any place of tree, it is actually to be | ||
| 50 | used in L</FusqlFS::Backend::PgSQL::Roles> module. | ||
| 51 | |||
| 52 | =head1 EXPOSED STRUCTURE | ||
| 53 | |||
| 54 | =over | ||
| 55 | |||
| 56 | =item F<./tables>, F<./functions>, F<./indices>, F<./sequences>, F<./languages> | ||
| 57 | |||
| 58 | This is the first level of hierarchy to sort owned artifacts by type and | ||
| 59 | provide separate namespaces for different objects in database. | ||
| 60 | |||
| 61 | There are symlinks to artifacts of correspondent type in each of these | ||
| 62 | subdirectories, e.g. if a role owns table C<sometable> and stored procedure | ||
| 63 | C<someproc> there will be symlinks in F<./tables/sometable> and | ||
| 64 | F<./functions/someproc> pointing to F</tables/sometable> and | ||
| 65 | F</functions/someproc>. | ||
| 66 | |||
| 67 | =back | ||
| 68 | |||
| 69 | =cut | ||
| 70 | |||
| 71 | our %kinds = qw( | ||
| 72 | tables r | ||
| 73 | sequences S | ||
| 74 | functions _F | ||
| 75 | languages _L | ||
| 76 | ); | ||
| 77 | |||
| 78 | sub init | ||
| 79 | { | ||
| 80 | my $self = shift; | ||
| 81 | |||
| 82 | while (my ($kind, $rel) = each %kinds) | ||
| 83 | { | ||
| 84 | my @kind = $self->kind($rel); | ||
| 85 | $self->{$kind} = [ | ||
| 86 | $self->expr('SELECT %4$s FROM pg_catalog.%3$s WHERE pg_catalog.pg_get_userbyid(%2$sowner) = ? %5$s', @kind), | ||
| 87 | sprintf('ALTER %1$s "%%s" OWNER TO "%%s"', @kind), | ||
| 88 | ]; | ||
| 89 | } | ||
| 90 | } | ||
| 91 | |||
| 92 | sub get | ||
| 93 | { | ||
| 94 | my $self = shift; | ||
| 95 | my ($role, $kind) = @_; | ||
| 96 | return unless exists $self->{$kind}; | ||
| 97 | my $data = $self->all_col($self->{$kind}->[0], $role); | ||
| 98 | return unless $data; | ||
| 99 | return { map { $_ => \"$kind/$_" } @$data }; | ||
| 100 | } | ||
| 101 | |||
| 102 | sub list | ||
| 103 | { | ||
| 104 | my $self = shift; | ||
| 105 | my ($role) = @_; | ||
| 106 | return [ keys %kinds ]; | ||
| 107 | } | ||
| 108 | |||
| 109 | sub store | ||
| 110 | { | ||
| 111 | my $self = shift; | ||
| 112 | my ($role, $kind, $data) = @_; | ||
| 113 | my $list = $self->validate($data, ['HASH', sub{ [ keys %$_ ] }]) or return; | ||
| 114 | $self->do($self->{$kind}->[1], $_, $role) foreach @$list; | ||
| 115 | } | ||
| 116 | |||
| 117 | 1; |