FusqlFS/Backend/PgSQL/Languages.pm untested

filter: Statementstmt 0.0% 0 / 64 Branchbran 0.0% 0 / 10 Conditioncond 0.0% 0 / 14 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 0.0% 0 / 12 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 116 CC 6 SCAR 20.8
CC
6
Cov
0%
get
20.0
store
12.0
init
2.0
CRAP 8.0
1 use strict;
2 use 5.010;
3  
4 =head1 NAME
5  
6 FusqlFS::Backend::PgSQL::Languages - FusqlFS class to interface with PostgreSQL languages
7  
8 =head1 SYNOPSIS
9  
10 use FusqlFS::Backend::PgSQL::Languages;
11  
12 my $languages = FusqlFS::Backend::PgSQL::Languages->new();
13 $languages->create('plperl');
14 my $data = $languages->get('plperl');
15 $data->{struct} =~ s/^trusted: 0$/trusted: 1/m;
16 $languages->store('plperl', $data);
17  
18 =head1 DESCRIPTION
19  
20 This class is used by L<FusqlFS::Backend::PgSQL> to represent F<languages> subtree and not to be used by itself.
21  
22 =head1 EXPOSED STRUCTURE
23  
24 =over
25  
26 =item F<./handler>, F<./validator>
27  
28 Symlinks to functions in F<../../functions> set as handler and validator for the language.
29  
30 =item F<./struct>
31  
32 Formatted info about language:
33  
34 =over
35  
36 =item C<ispl>
37  
38 I<boolean> true is it's a procedural language (true for all languages except for special internal handlers).
39  
40 =item C<trusted>
41  
42 I<boolean> true for trusted languages, i.e. languages usable by non-superusers.
43  
44 =back
45  
46 =item F<./owner>
47  
48 Synlink to language owner role in F<../../roles>.
49  
50 =back
51  
52 =head1 METHODS
53  
54 =over
55  
56 =cut
57  
58 package FusqlFS::Backend::PgSQL::Languages;
59 use FusqlFS::Version;
60 our $VERSION = $FusqlFS::Version::VERSION;
61 use parent 'FusqlFS::Artifact';
62  
63 use FusqlFS::Backend::PgSQL::Role::Owner;
64  
65 =item new
66  
67 Class constructor.
68  
69 Output: $languages_instance.
70  
71 =begin testing new
72  
73 my $instance = {_tpkg}->new();
74 isa_ok $instance, $_tcls;
75  
76 =end testing
77 =cut
78 sub init
79 {
80 my $self = shift;
81  
82 $self->{get_expr} = $self->expr('SELECT l.lanispl AS ispl, l.lanpltrusted AS trusted,
83 hp.proname||\'(\'||pg_catalog.pg_get_function_arguments(hp.oid)||\')\' AS handler,
84 vp.proname||\'(\'||pg_catalog.pg_get_function_arguments(vp.oid)||\')\' AS validator
85 FROM pg_catalog.pg_language AS l
86 LEFT JOIN pg_catalog.pg_proc AS hp ON (l.lanplcallfoid = hp.oid)
87 LEFT JOIN pg_catalog.pg_proc AS vp ON (l.lanvalidator = vp.oid)
88 WHERE lanname = ?');
89 $self->{list_expr} = $self->expr('SELECT lanname FROM pg_catalog.pg_language');
90  
91 $self->{create_expr} = 'CREATE LANGUAGE %s';
92 $self->{drop_expr} = 'DROP LANGUAGE %s';
93 $self->{rename_expr} = 'ALTER LANGUAGE %s RENAME TO %s';
94  
95 $self->{owner} = FusqlFS::Backend::PgSQL::Role::Owner->new('_L');
96 }
97  
98 =item get
99  
100 =begin testing get
101  
102 is $_tobj->get('xxxxxx'), undef;
103 my $data = $_tobj->get('internal');
104 is_deeply $data, {
105 owner => $_tobj->{owner},
106 validator => \"functions/fmgr_internal_validator(oid)",
107 struct => { ispl => 0, trusted => 0, },
108 };
109  
110 =end testing
111 =cut
112 sub get
113 {
114 my $self = shift;
115 my ($name) = @_;
116 my $data = $self->one_row($self->{get_expr}, $name);
117 return unless $data;
118  
119 my $result = {};
120 $result->{handler} = \"functions/$data->{handler}" if $data->{handler};
121 $result->{validator} = \"functions/$data->{validator}" if $data->{validator};
122 delete $data->{handler};
123 delete $data->{validator};
124  
125 $result->{struct} = $self->dump($data);
126 $result->{owner} = $self->{owner};
127  
128 return $result;
129 }
130  
131 =item list
132  
133 =begin testing list
134  
135 my $list = $_tobj->list();
136 isa_ok $list, 'ARRAY';
137 cmp_set $list, [ qw(c internal sql plpgsql) ];
138  
139 =end testing
140 =cut
141 sub list
142 {
143 my $self = shift;
144 return $self->all_col($self->{list_expr});
145 }
146  
147 =item drop
148  
149 =begin testing drop after rename
150  
151 isnt $_tobj->drop('plperl1'), undef;
152 cmp_set $_tobj->list(), [ qw(c internal sql plpgsql) ];
153 is $_tobj->get('plperl1'), undef;
154  
155 =end testing
156 =cut
157 sub drop
158 {
159 my $self = shift;
160 my ($name) = @_;
161 $self->do($self->{drop_expr}, [$name]);
162 }
163  
164 =item create
165  
166 =begin testing create after get list
167  
168 isnt $_tobj->create('plperl'), undef;
169 is_deeply $_tobj->get('plperl'), $new_lang;
170 cmp_set $_tobj->list(), [ qw(c internal sql plpgsql plperl) ];
171  
172 =end testing
173 =cut
174 sub create
175 {
176 my $self = shift;
177 my ($name) = @_;
178 $self->do($self->{create_expr}, [$name]);
179 }
180  
181 =item rename
182  
183 =begin testing rename after store
184  
185 isnt $_tobj->rename('plperl', 'plperl1'), undef;
186 cmp_set $_tobj->list(), [ qw(c internal sql plpgsql plperl1) ];
187 is $_tobj->get('plperl'), undef;
188 is_deeply $_tobj->get('plperl1'), $new_lang;
189  
190 =end testing
191 =cut
192 sub rename
193 {
194 my $self = shift;
195 my ($name, $newname) = @_;
196 $self->do($self->{rename_expr}, [$name, $newname]);
197 }
198  
199 =item store
200  
201 =begin testing store after create
202  
203 isnt $_tobj->store('plperl', $new_lang), undef;
204 is_deeply $_tobj->get('plperl'), $new_lang;
205  
206 =end testing
207 =cut
208 sub store
209 {
210 my $self = shift;
211 my ($name, $data) = @_;
212 return unless $data;
213  
214 my $struct = $self->validate($data, {
215 -validator => ['SCALAR', sub { $$_ =~ /^functions\/(\S+)\(.*\)$/ && $1 }],
216 -handler => ['SCALAR', sub { $$_ =~ /^functions\/(\S+)\(.*\)$/ && $1 }],
217 struct => {
218 trusted => '',
219 ispl => '',
220 }
221 }, sub{ exists $_->{validator} || exists $_->{handler} })
222 or return;
223  
224 my $trusted = $struct->{struct}->{trusted}? 'TRUSTED ': '';
225 my $sql = "CREATE ${trusted}LANGUAGE $name";
226 $sql .= ' '.uc($_).' '.$struct->{$_} foreach (qw(handler validator));
227  
228 $self->drop($name) and $self->do($sql);
229 }
230  
231 1;
232  
233 __END__