FusqlFS/Backend/Base.pm

filter: Statementstmt 83.0% 49 / 59 Branchbran 41.6% 5 / 12 Conditioncond 50.0% 5 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 66.6% 12 / 18 Podpod 100% 8 / 8 totaltotal 70.5% 79 / 112 CC 12 SCAR 12.7
CC
12
Cov
73%
new
7.8
reconnect
6.0
dsn
3.1
CRAP 3.5
« FusqlFS/Backend/MySQL/Variables.pm ↑ Summary FusqlFS/Backend/PgSQL/Queries.pm »
1 18 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
2 18 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 package FusqlFS::Backend::Base;
5 18 ▶use FusqlFS::Version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 18 our $VERSION = $FusqlFS::Version::VERSION;
7 18 ▶use parent 'FusqlFS::Artifact';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 =head1 NAME
10  
11 FusqlFS::Backend::Base - base FusqlFS class for database backend implementations
12  
13 =head1 SYNOPSIS
14  
15 use FusqlFS::Backend::PgSQL;
16 use parent 'FusqlFS::Backend::Base';
17  
18 use FusqlFS::Backend::PgSQL::Tables;
19 use FusqlFS::Backend::PgSQL::Views;
20 use FusqlFS::Backend::PgSQL::Sequences;
21 use FusqlFS::Backend::PgSQL::Roles;
22  
23 sub init
24 {
25 my $self = shift;
26 $self->{subpackages} = {
27 tables => new FusqlFS::Backend::PgSQL::Tables(),
28 views => new FusqlFS::Backend::PgSQL::Views(),
29 sequences => new FusqlFS::Backend::PgSQL::Sequences(),
30 roles => new FusqlFS::Backend::PgSQL::Roles(),
31 };
32 }
33  
34 sub dsn
35 {
36 my $self = shift;
37 return 'Pg:'.$shift->SUPER::dsn(@_);
38 }
39  
40 1;
41  
42 =head1 DESCRIPTION
43  
44 This is the base abstract class and start point for any FusqlFS database
45 backend implementation. The instance of this class's subclass is a "root" of
46 fusqlfs file system.
47  
48 You start your backend implementation by subclassing C<FusqlFS::Backend::Base>
49 and overriding some methods as described in L</SYNOPSIS>.
50  
51 See L</METHODS> section for detailed description of what you should and should
52 not override.
53  
54 You should define C<subpackages> instance variable among other operations in
55 order for your backend class to be usable. The value of this property must be a
56 hashref which describes root of fusqlfs subsystem: its keys will be file names,
57 and values will be these files' content (well, the values are usually
58 L<FusqlFS::Artifact> instances interfacing to different database artifacts, so
59 "file" here means not only plain file, but directories, symlinks and
60 pseudopipes as well). See L<FusqlFS::Artifact/autopackages> for a way to
61 automate this process.
62  
63 See also L<FusqlFS::Entry> to learn how this instance variable is used, how
64 file paths are mapped to backend objects and how file type is determined.
65  
66 =head1 METHODS
67  
68 =over
69  
70 =cut
71  
72 18 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
73 18 ▶use FusqlFS::Entry;
Subroutine BEGIN: called CC=1 SCAR=0.0
74 18 ▶use FusqlFS::Formatter;
Subroutine BEGIN: called CC=1 SCAR=0.0
75  
76 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
77  
78 =item new
79  
80 Class constructor.
81  
82 Input: %options.
83 Output: $backend_base_instance.
84  
85 This method does a lot of initialization work, including DBI connection
86 initialization and setup of different inner variables, data representation
87 layer setup etc., so do not override and redefine it unless you really know
88 what you are doing. And if you really need to override it, consider calling it
89 with C<$class-E<gt>SUPER::new(...)> at some point to avoid unnecessary work.
90  
91 If you need to do some initialization work, consider overriding L</init> method
92 which is created to be overridden and redefined.
93  
94 =cut
95 sub new
96 2 ▶{
Subroutine new: called CC=7 SCAR=20.5
Pod documented
97 2 ▶ return $FusqlFS::Artifact::instance if $FusqlFS::Artifact::instance;
Branch: 1 branch
BranchTrueFalseTotal
if $FusqlFS::Artifact::instance 0 2 2
98  
99 2 my $class = shift;
100 2 my %options = @_;
101 2 my $dsn = 'DBI:'.$class->dsn(@options{qw(host port database)});
102 2 ▶ my $debug = $options{debug}||0;
Condition50%1/2
$options{'debug'} || 0
l!l
02
MC/DC0%0/1
$options{'debug'} || 0
$options{'debug'}
Truth table50%1/2
A
A= $options{'debug'}
Aresult
11
00
103 2 ▶ my $fnsep = $options{fnsep}||'.';
Condition50%1/2
$options{'fnsep'} || '.'
l!l
02
MC/DC0%0/1
$options{'fnsep'} || '.'
$options{'fnsep'}
Truth table50%1/2
A
A= $options{'fnsep'}
Aresult
11
00
104 2 ▶ my $format = $options{format}||'';
Condition50%1/2
$options{'format'} || ''
l!l
20
MC/DC0%0/1
$options{'format'} || ''
$options{'format'}
Truth table50%1/2
A
A= $options{'format'}
Aresult
11
00
105  
106 2 $Carp::Verbose = $debug > 3;
107 my $self = {
108 subpackages => {},
109 limit => 0 + ($options{limit}||0),
110 charset => $options{charset}||'',
111 fnsep => $fnsep,
112 fnsplit => qr/[$fnsep]/,
113 2 ▶ connect => sub () {
Subroutine __ANON__: called CC=2 SCAR=9.2
114 DBI->connect($dsn, @options{qw(user password)},
115 {
116 PrintError => $debug > 0,
117 PrintWarn => $debug > 1,
118 ShowErrorStatement => $debug > 2,
119 0 ▶ HandleError => sub { carp(shift); },
Subroutine __ANON__: not called CC=1 SCAR=6.9
120 2 ▶ }) or die "Failed to connect to $dsn: $DBI::err $DBI::state $DBI::errstr";
Branch: 1 branch
BranchTrueFalseTotal
"DBI"->connect($dsn, @options{"user", "password"}, {"PrintError", $debug > 0, "PrintWarn", $debug > 1, "ShowErrorStatement", $debug > 2, "HandleError", sub { carp(shift()); } }) or die "Failed to connect to ${dsn}: $DBI::err $DBI::state $DBI::errstr" 2 0 2
121 },
122 2 ▶ };
Condition50%2/4
$options{'limit'} || 0
l!l
02
$options{'charset'} || ''
l!l
02
MC/DC0%0/1
$options{'limit'} || 0
$options{'limit'}
MC/DC0%0/1
$options{'charset'} || ''
$options{'charset'}
Truth table50%1/2
A
A= $options{'limit'}
Aresult
11
00
Truth table50%1/2
A
A= $options{'charset'}
Aresult
11
00
123 2 $self->{dbh} = $self->{connect}();
124  
125 2 ($self->{dumper}, $self->{loader}) = FusqlFS::Formatter->init($format);
126 2 $self->{namemap} = $options{namemap};
127  
128 2 bless $self, $class;
129  
130 2 $FusqlFS::Artifact::instance = $self;
131 2 $self->init();
132 2 return $self;
133 }
134  
135 =item connect, disconnect, reconnect
136  
137 These methods can be used to control database connection in runtime.
138 Please use them instead of direct DBH object access via $fusqlh->{dbh},
139 as they make some more work, than simple database {dis,re}connection.
140  
141 They use credentials, provided on first backend object initialization
142 with L</new> method above, so no parameters are required.
143  
144 C<connect> establish new database connection and reinitializes backend.
145 Backend reinitialization is required, because some backends make some
146 query preparation, linked to current database connection.
147  
148 C<disconnect> drops database connection, and C<reconnect> drops database
149 connection is it's active (checked with L<DBI::ping> method) and
150 then establish connection anew. This method is used in C<HUP>
151 signal handler to reset database connection.
152  
153 =cut
154 sub connect
155 0 ▶{
Subroutine connect: not called CC=1 SCAR=6.9
Pod documented
156 0 my $self = shift;
157 0 $self->{dbh} = $self->{connect}();
158 0 $self->init();
159 }
160  
161 sub disconnect
162 0 ▶{
Subroutine disconnect: not called CC=1 SCAR=6.9
Pod documented
163 0 $_[0]->{dbh}->disconnect();
164 }
165  
166 sub reconnect
167 0 ▶{
Subroutine reconnect: not called CC=2 SCAR=17.9
Pod documented
168 0 my $self = shift;
169 0 ▶ $self->disconnect() if $self->{dbh}->ping();
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"dbh"}->ping 0 0 0
170 0 $self->connect();
171 }
172  
173 =item by_path
174  
175 Returns L<FusqlFS::Entry> entry by path.
176  
177 Input: $path, $leaf_absent=undef.
178 Output: $entry_instance.
179  
180 See L<FusqlFS::Entry> for detailed description. This method is just a
181 convenient way of constructing C<FusqlFS::Entry>'s instance as you don't need
182 to pass first C<$fs> argument to it.
183  
184 =cut
185 sub by_path
186 0 ▶{
Subroutine by_path: not called CC=1 SCAR=6.9
Pod documented
187 0 return FusqlFS::Entry->new(@_);
188 }
189  
190 =item dsn
191  
192 Compose DSN string for the L<DBI/connect> method.
193  
194 Input: $host, $port, $database.
195 Output: $dsn.
196  
197 This method composes basic database type agnostic DSN string, e.g. without any
198 database driver prefix. You should override this method to prepend it with DBD
199 prefix like `Pg:' or `mysql:' or modify it in some other way as needed.
200  
201 =begin testing dsn
202  
203 #!noinst
204  
205 is FusqlFS::Backend::Base->dsn('host', 'port', 'database'), 'host=host;port=port;database=database;', 'FusqlFS::Backend::Base->dsn is sane';
206  
207 =end testing
208 =cut
209 sub dsn
210 1 ▶{
Subroutine dsn: called CC=3 SCAR=11.3
Pod documented
211 1 my $dsn = "";
212 1 ▶ $dsn .= "host=$_[1];" if $_[1];
Branch: 1 branch
BranchTrueFalseTotal
if $_[1] 1 0 1
213 1 ▶ $dsn .= "port=$_[2];" if $_[2];
Branch: 1 branch
BranchTrueFalseTotal
if $_[2] 1 0 1
214 1 $dsn .= "database=$_[3];";
215 1 return $dsn;
216 }
217  
218 =item init
219  
220 I<Abstract method> called after main initialization work in L</new> is done.
221  
222 No data is passed to this method, except for class instance reference as first
223 argument, and all data returned from it are ignored.
224  
225 This is an abstract method called as instance method after L</new> is done all
226 initialization work and you should override it if you have some additional
227 initialization work to do. You will override it most times, actually.
228  
229 =cut
230 sub init
231 0 ▶{
Subroutine init: not called CC=1 SCAR=6.9
Pod documented
232 0 return;
233 }
234  
235 =item destroy
236  
237 Destroy instance state variable.
238  
239 The C<FusqlFS::Backend::Base> class is a singleton. It is initialized only once
240 and every subsequent call to L</new> method returns the same class instance,
241 stored in inner state variable.
242  
243 Sometimes you really need to reset this instance and reinitialize this
244 singleton. If this is the case, use this method.
245  
246 Do it only if you really understand all the sequences and you don't have any
247 other way to do the thing you want to do.
248  
249 =cut
250 sub destroy
251 2 ▶{
Subroutine destroy: called CC=2 SCAR=7.2
Pod documented
252 2 ▶ if ($FusqlFS::Artifact::instance)
Branch: 1 branch
BranchTrueFalseTotal
if ($FusqlFS::Artifact::instance) 2 0 2
253 {
254 2 undef $FusqlFS::Artifact::instance;
255 }
256 }
257  
258 18 1;
259  
« FusqlFS/Backend/MySQL/Variables.pm ↑ Summary FusqlFS/Backend/PgSQL/Queries.pm »