| 1 | use strict; | ||
| 2 | use 5.010; | ||
| 3 | |||
| 4 | package FusqlFS::Backend::PgSQL::Role::Base; | ||
| 5 | use FusqlFS::Version; | ||
| 6 | our $VERSION = $FusqlFS::Version::VERSION; | ||
| 7 | use base 'FusqlFS::Artifact'; | ||
| 8 | |||
| 9 | =head1 NAME | ||
| 10 | |||
| 11 | FusqlFS::Backend::PgSQL::Role::Base - abstract PostgreSQL configuration | ||
| 12 | complexity for FusqlFS::Backend::PgSQL::Role::* classes | ||
| 13 | |||
| 14 | =head1 SYNOPSIS | ||
| 15 | |||
| 16 | package FusqlFS::Backend::PgSQL::Role::Acl; | ||
| 17 | use parent 'FusqlFS::Backend::PgSQL::Role::Base'; | ||
| 18 | |||
| 19 | sub init | ||
| 20 | { | ||
| 21 | my $self = shift; | ||
| 22 | my $relkind = shift; | ||
| 23 | my @kind = $self->kind($relkind); | ||
| 24 | |||
| 25 | # initialize instance | ||
| 26 | } | ||
| 27 | |||
| 28 | =head1 DESCRIPTION | ||
| 29 | |||
| 30 | This class hides configuration complexity for | ||
| 31 | C<FusqlFS::Backend::PgSQL::Role::*> family of modules including | ||
| 32 | L<FusqlFS::Backend::PgSQL::Role::Acl> and | ||
| 33 | L<FusqlFS::Backend::PgSQL::Role::Owner>. | ||
| 34 | |||
| 35 | Its single method C<kind()> accepts single character argument designating the | ||
| 36 | kind of referenced database artifact and returns a number of configuration | ||
| 37 | parameters to construct correct SQL statements for the artifact's kind. The | ||
| 38 | possible values of this argument are: | ||
| 39 | |||
| 40 | =over | ||
| 41 | |||
| 42 | =item C<r> | ||
| 43 | |||
| 44 | Table (a.k.a. relation). | ||
| 45 | |||
| 46 | =item C<i> | ||
| 47 | |||
| 48 | Table's index. | ||
| 49 | |||
| 50 | =item C<S> | ||
| 51 | |||
| 52 | Sequence. | ||
| 53 | |||
| 54 | =item C<v> | ||
| 55 | |||
| 56 | View. | ||
| 57 | |||
| 58 | =item C<_F> | ||
| 59 | |||
| 60 | Function (a.k.a. stored procedure). | ||
| 61 | |||
| 62 | =item C<_L> | ||
| 63 | |||
| 64 | Language. | ||
| 65 | |||
| 66 | =back | ||
| 67 | |||
| 68 | If list context C<kind()> method returns a list of C<($kind, $pfx, $table, | ||
| 69 | $filter)> where | ||
| 70 | |||
| 71 | =over | ||
| 72 | |||
| 73 | =item C<$kind> | ||
| 74 | |||
| 75 | is the name of artifact type, e.g. C<TABLE>, C<INDEX>, C<SEQUENCE>, C<VIEW>, | ||
| 76 | C<FUNCTION> or C<LANGUAGE>, | ||
| 77 | |||
| 78 | =item C<$pfx> | ||
| 79 | |||
| 80 | is the prefix for fields in C<pg_catalog> schema's table with data of the | ||
| 81 | artifact type, e.g. C<rel> for C<pg_class> or C<pro> for C<pg_proc>, | ||
| 82 | |||
| 83 | =item C<$table> | ||
| 84 | |||
| 85 | is the table in C<pg_catalog> schema with information about artifacts of this | ||
| 86 | kind, e.g. C<pg_class>, C<pg_proc> or C<pg_language>, | ||
| 87 | |||
| 88 | =item C<$name> | ||
| 89 | |||
| 90 | is the SQL statement to get artifact's name (usually it's just C<${pfx}name>, | ||
| 91 | but can be rather different, e.g. in case of functions), use this instead of | ||
| 92 | self-composed name fields in both C<WHERE> and C<SELECT> expressions. | ||
| 93 | |||
| 94 | =item C<$filter> | ||
| 95 | |||
| 96 | is the additional C<WHERE> clause for C<pg_class> table to filter data by | ||
| 97 | required C<relkind> field value, contains empty string for tables other than | ||
| 98 | C<pg_class>. | ||
| 99 | |||
| 100 | =back | ||
| 101 | |||
| 102 | In scalar context this method returns hashref with keys named C<kind>, C<pfx>, | ||
| 103 | C<table>, C<name>, C<filter> and values as described above, so this hashref is | ||
| 104 | usable with C<FusqlFS::Artifact/hprintf> method. | ||
| 105 | |||
| 106 | =cut | ||
| 107 | |||
| 108 | our %relkinds = ( | ||
| 109 | r => [ qw(TABLE rel) ], | ||
| 110 | i => [ qw(INDEX rel) ], | ||
| 111 | S => [ qw(SEQUENCE rel) ], | ||
| 112 | v => [ qw(VIEW rel) ], | ||
| 113 | |||
| 114 | _F => [ 'FUNCTION', 'pro', q<proname||'('||pg_catalog.pg_get_function_arguments(pg_proc.oid)||')'> ], | ||
| 115 | _L => [ qw(LANGUAGE lan) ], | ||
| 116 | ); | ||
| 117 | |||
| 118 | our %reltables = qw( | ||
| 119 | rel pg_class | ||
| 120 | pro pg_proc | ||
| 121 | lan pg_language | ||
| 122 | ); | ||
| 123 | |||
| 124 | sub kind | ||
| 125 | { | ||
| 126 | my $class = shift; | ||
| 127 | my ($relkind) = @_; | ||
| 128 | |||
| 129 | my ($kind, $pfx, $name) = @{$relkinds{$relkind}}; | ||
| 130 | my $table = $reltables{$pfx}; | ||
| 131 | my $filter = $table eq 'pg_class'? "AND relkind = '$relkind'": ""; | ||
| 132 | $name ||= "${pfx}name"; | ||
| 133 | |||
| 134 | return wantarray? ($kind, $pfx, $table, $name, $filter): | ||
| 135 | { kind => $kind, pfx => $pfx, table => $table, name => $name, filter => $filter }; | ||
| 136 | } | ||
| 137 | |||
| 138 | 1; |