| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::PgSQL::Role::Owner; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use parent 'FusqlFS::Backend::PgSQL::Role::Base'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | FusqlFS::Backend::PgSQL::Role::Owner - FusqlFS class to expose PostgreSQL | ||
| 12 | artifact's owner | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | package FusqlFS::Backend::PgSQL::Tables; | ||
| 17 | use parent 'FusqlFS::Artifact'; | ||
| 18 | |||
| 19 | use FusqlFS::Backend::PgSQL::Role::Owner; | ||
| 20 | |||
| 21 | sub init | ||
| 22 | { | ||
| 23 | my $self = shift; | ||
| 24 | |||
| 25 | # initialize class | ||
| 26 | |||
| 27 | $self->{owner} = FusqlFS::Backend::PgSQL::Role::Owner->new('r'); | ||
| 28 | } | ||
| 29 | |||
| 30 | sub get | ||
| 31 | { | ||
| 32 | my $self = shift; | ||
| 33 | my ($name) = @_; | ||
| 34 | my $result = {}; | ||
| 35 | |||
| 36 | # load structures into $result | ||
| 37 | |||
| 38 | $result->{owner} = $self->{owner}; | ||
| 39 | return $result; | ||
| 40 | } | ||
| 41 | |||
| 42 | =head1 DESCRIPTION | ||
| 43 | |||
| 44 | This class exposes PostgreSQL artifact's owner as a symlink to role in | ||
| 45 | F</roles> directory. It is best used with plugged in | ||
| 46 | L<FusqlFS::Backend::PgSQL::Roles> module (see L<FusqlFS::Backend::Base> for | ||
| 47 | more info on plugging in different modules). | ||
| 48 | |||
| 49 | The class's C<new> constructor accepts single char argument designating type of | ||
| 50 | artifact the owner of which is to be exposed. Possible values of this argument | ||
| 51 | can be seen in L<FusqlFS::Backend::PgSQL::Role::Base> module. | ||
| 52 | |||
| 53 | =cut | ||
| 54 | |||
| 55 | sub init | ||
| 56 | { | ||
| 57 | my $self = shift; | ||
| 58 | my $relkind = shift; | ||
| 59 | |||
| 60 | my @kind = $self->kind($relkind); | ||
| 61 | |||
| 62 | $self->{get_expr} = $self->expr('SELECT pg_catalog.pg_get_userbyid(%2$sowner) FROM pg_catalog.%3$s WHERE %4$s = ? %5$s', @kind); | ||
| 63 | $self->{store_expr} = sprintf('ALTER %1$s "%%s" OWNER TO "%%s"', @kind); | ||
| 64 | } | ||
| 65 | |||
| 66 | sub get | ||
| 67 | { | ||
| 68 | my $self = shift; | ||
| 69 | my $name = pop; | ||
| 70 | my $owner = $self->all_col($self->{get_expr}, $name); | ||
| 71 | return \"roles/$owner->[0]" if $owner; | ||
| 72 | } | ||
| 73 | |||
| 74 | sub store | ||
| 75 | { | ||
| 76 | my $self = shift; | ||
| 77 | my $data = pop; | ||
| 78 | my $name = pop; | ||
| 79 | $data = $$data if ref $data eq 'SCALAR'; | ||
| 80 | return if ref $data || $data !~ m#^roles/([^/]+)$#; | ||
| 81 | $self->do($self->{store_expr}, [$name, $1]); | ||
| 82 | } | ||
| 83 | |||
| 84 | 1; |