TreePath/Backend/DBIx.pm

filter: Statementstmt 97.3% 110 / 113 Branchbran 58.8% 20 / 34 Conditioncond 33.3% 3 / 9 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 100% 13 / 13 Podpod n/a totaltotal 83.9% 146 / 174 CC 25 SCAR 20.6
CC
25
Cov
85%
_load
15.5
_connect_info
6.3
_row_to_obj
6.0
CRAP 7.9
↑ Summary TreePath.pm »
1 package TreePath::Backend::DBIx;
2 4 $TreePath::Backend::DBIx::VERSION = '0.22';
3 4 ▶use Moose::Role;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use base 'DBIx::Class::Schema';
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use Carp qw/croak/;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 4 ▶use Path::Class;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 4 ▶use Hash::Merge;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 4 ▶use FindBin '$Bin';
Subroutine BEGIN: called CC=1 SCAR=0.0
10 4 require UNIVERSAL::require;
11  
12  
13 4 my $attrs = {
14 # starting with v3.3, SQLite supports the "IF EXISTS" clause to "DROP TABLE",
15 # even though SQL::Translator::Producer::SQLite 1.59 isn't passed along this option
16 # see https://rt.cpan.org/Ticket/Display.html?id=48688
17 sqlite_version => 3.3,
18 add_drop_table => 0,
19 no_comments => 0,
20 RaiseError => 1,
21 PrintError => 0,
22 };
23  
24 has dsn => (
25 is => 'rw',
26 3 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
27 3 my $self = shift;
28 3 return $self->model_config->{'connect_info'}->{dsn};
29 }
30 4 );
31  
32 4 has model_config => (
33 is => 'rw',
34 lazy_build => 1,
35 );
36  
37 4 has 'schema' => (
38 is => 'rw',
39 predicate => 'has_schema',
40 lazy_build => 1,
41 );
42  
43 4 has '_source_name' => (
44 is => 'rw',
45 isa => 'Str',
46 );
47  
48 4 has '_populate_backend' => (
49 is => 'rw',
50 isa => 'Int',
51 );
52  
53 3 ▶sub _build_model_config {
Subroutine _build_model_config: called CC=2 SCAR=7.1
54 3 my $self = shift;
55  
56 my $model_config = $self->conf->{$self->config->{backend}->{args}->{model}}
57 3 ▶ or croak "'backend/args/model' is not defined in conf file !";
Branch: 1 branch
BranchTrueFalseTotal
my $model_config = $self->conf->{$self->config->{"backend"}{"args"}{"model"}} or croak("'backend/args/model' is not defined in conf file !") 3 0 3
58 3 return $model_config
59 }
60  
61 2 ▶sub _build_schema {
Subroutine _build_schema: called CC=2 SCAR=7.1
62 2 my $self = shift;
63  
64 2 my($dsn, $user, $password, $allattrs) = $self->_connect_info;
65  
66 2 my $schema_class = $self->model_config->{schema_class};
67 2 eval "require $schema_class";
68 2 ▶ if( $@ ){
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 2 2
69 0 die("Cannot load $schema_class : $@");
70 }
71 2 return $schema_class->connect($dsn,$user,$password,$allattrs);
72 }
73  
74  
75 8 ▶sub _connect_info {
Subroutine _connect_info: called CC=6 SCAR=18.4
76 8 my $self = shift;
77  
78 8 my $model_config = $self->model_config;
79  
80 8 my ($dsn, $user, $password, $unicode_option, $db_type);
81 8 eval {
82 8 ▶ if (!$dsn)
Branch: 1 branch
BranchTrueFalseTotal
unless ($dsn) 8 0 8
83 {
84 8 ▶ if (ref $model_config->{'connect_info'}) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $model_config->{'connect_info'}) { } 8 0 8
85  
86 8 $dsn = $model_config->{'connect_info'}->{dsn};
87 8 $user = $model_config->{'connect_info'}->{user};
88 8 $password = $model_config->{'connect_info'}->{password};
89  
90 # Determine database type amongst: SQLite, Pg or MySQL
91 8 $dsn =~ m/^dbi:(\w+)/;
92 8 $db_type = lc($1);
93 8 my %unicode_connection_for_db = (
94 'sqlite' => { sqlite_unicode => 1 },
95 'pg' => { pg_enable_utf8 => 1 },
96 'mysql' => { mysql_enable_utf8 => 1 },
97  
98 );
99 8 $unicode_option = $unicode_connection_for_db{$db_type};
100 }
101 else {
102 0 $dsn = $model_config->{'connect_info'};
103 }
104 }
105 };
106  
107 8 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 8 8
108 0 die "Your DSN line in " . $self->conf . " doesn't look like a valid DSN.";
109 }
110 8 ▶ die "No valid Data Source Name (DSN).\n" if !$dsn;
Branch: 1 branch
BranchTrueFalseTotal
unless $dsn 0 8 8
111 8 $dsn =~ s/__HOME__/$FindBin::Bin\/\.\./g;
112  
113 8 ▶ if ( $db_type eq 'sqlite' ){
Branch: 1 branch
BranchTrueFalseTotal
if ($db_type eq "sqlite") 8 0 8
114 8 $dsn =~ m/.*:(.*)$/;
115 8 my $dir = dir($1)->parent;
116 8 $dir->mkpath;
117 }
118  
119 8 my $merge = Hash::Merge->new( 'LEFT_PRECEDENT' );
120 8 my $allattrs = $merge->merge( $unicode_option, $attrs );
121  
122 8 return $dsn, $user, $password, $allattrs;
123 }
124  
125  
126  
127 2 ▶sub _load {
Subroutine _load: called CC=13 SCAR=27.4
128 2 my $self = shift;
129  
130 2 $self->_log("Loading tree from dbix");
131  
132 $self->_populate_backend($self->config->{backend}->{args}->{'populate_backend'})
133 2 ▶ if ( $self->can('_populate_backend') && ! defined $self->_populate_backend && defined $self->config->{backend}->{args}->{'populate_backend'} );
Branch: 1 branch
BranchTrueFalseTotal
if $self->can("_populate_backend") and not defined $self->_populate_backend and defined $self->config->{"backend"}{"args"}{"populate_backend"} 2 0 2
Condition33%2/6
$self->can("_populate_backend") and not defined $self->_populate_backend and defined $self->config->{"backend"}{"args"}{"populate_backend"}
!ll&&!rl&&r
002
$self->can("_populate_backend") and not defined $self->_populate_backend
!ll&&!rl&&r
002
MC/DC0%0/3
$self->can("_populate_backend") and not defined $self->_populate_backend and defined $self->config->{"backend"}{"args"}{"populate_backend"}
$self->can("_populate_backend") not defined $self->_populate_backend defined $self->config->{"backend"}{"args"}{"populate_backend"}
Truth table25%1/4
A and not(B) and C
A= $self->can("_populate_backend")B= not defined $self->_populate_backendC= defined $self->config->{"backend"}{"args"}{"populate_backend"}
ABCresult
0XX0
10X0
1100
1111
134  
135 2 my $schema = $self->schema;
136 2 ▶ my @sources_name = keys %{$self->config->{backend}->{args}->{sources_name}}
Branch: 1 branch
BranchTrueFalseTotal
my(@sources_name) = keys %{$self->config->{"backend"}{"args"}{"sources_name"};} or croak("'backend/args/sources_name' is not defined in conf file !") 2 0 2
137 or croak "'backend/args/sources_name' is not defined in conf file !";
138  
139 2 my @datas;
140 2 foreach my $source_name ( @sources_name ) {
141  
142 6 my($dsn, $user, $password, $allattrs) = $self->_connect_info;
143 6 $self->_source_name($source_name);
144 6 eval { $schema->resultset($source_name)->count };
145  
146 6 ▶ if ( $@ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 2 4 6
147 2 ▶ print "Deploy and populate $dsn\n" if $self->debug;
Branch: 1 branch
BranchTrueFalseTotal
if $self->debug 0 2 2
148 2 $schema->deploy;
149 2 ▶ $schema->_populate if ( $schema->can('_populate') && $self->_populate_backend);
Branch: 1 branch
BranchTrueFalseTotal
if $schema->can("_populate") and $self->_populate_backend 2 0 2
Condition33%1/3
$schema->can("_populate") and $self->_populate_backend
!ll&&!rl&&r
002
MC/DC0%0/2
$schema->can("_populate") and $self->_populate_backend
$schema->can("_populate") $self->_populate_backend
Truth table33%1/3
A and B
A= $schema->can("_populate")B= $self->_populate_backend
ABresult
0X0
100
111
150 }
151 6 my $rs = $self->schema->resultset($source_name)->search();
152  
153 6 my @primary_columns = $rs->result_source->primary_columns;
154 6 ▶ croak "Multi-column primary keys are not supported." if (scalar @primary_columns > 1 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @primary_columns > 1 0 6 6
155 6 ▶ croak "No primary key found." if (scalar @primary_columns == 0 );
Branch: 1 branch
BranchTrueFalseTotal
if scalar @primary_columns == 0 0 6 6
156 6 my $primary_key = shift @primary_columns;
157  
158 6 foreach my $row ( $rs->all) {
159 42 my $obj = $self->_row_to_obj($row);
160 42 $obj->{$primary_key} = $row->$primary_key;
161 42 push(@datas,$obj);
162 }
163 }
164 2 return \@datas;
165 }
166  
167  
168 68 ▶sub _row_to_obj {
Subroutine _row_to_obj: called CC=6 SCAR=17.9
169 68 my $self = shift;
170 68 my $row = shift;
171  
172  
173 68 my $result_source = ref($row);
174 68 my ($source) = ($result_source =~ /^.*::(.*)/s);
175  
176 68 my $obj = {};
177  
178 my $cols = $self->config->{backend}->{args}->{sources_name}->{$source}->{columns}
179 68 ▶ or croak "backend/args/sources_name/$source/columns is not defined in conf file !";
Branch: 1 branch
BranchTrueFalseTotal
my $cols = $self->config->{"backend"}{"args"}{"sources_name"}{$source}{"columns"} or croak("backend/args/sources_name/$source/columns is not defined in conf file !") 68 0 68
180  
181 68 foreach my $col (@$cols) {
182  
183 166 my $value = $row->$col;
184  
185 # it's not a relationship'
186 166 ▶ if ( ! ref($value)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not ref $value) { } 73 93 166
elsif (ref $value eq 'DBIx::Class::ResultSet') { } 36 57 93
187 73 $obj->{$col} = $value;
188 }
189 # relationship has_many (not already tested)
190 elsif ( ref($value) eq 'DBIx::Class::ResultSet') {
191 36 my @results = $row->$col;
192 36 $obj->{$col} = [];
193 # save rs in array
194 36 foreach my $res (@results) {
195 6 push( @{$obj->{$col}}, $self->_rs_to_obj_key($res));
196 }
197 }
198 # relationship belongs_to
199 else {
200 # save result in hashref
201 57 $obj->{$col} = { $self->_rs_to_obj_key($row->$col) => 1 };
202 }
203 }
204 # Ajout de la source a l'objet
205 68 $obj->{source} = $source;
206 68 return $obj;
207 }
208  
209  
210 64 ▶sub _rs_to_obj_key {
Subroutine _rs_to_obj_key: called CC=1 SCAR=0.0
211 64 my $self = shift;
212 64 my $row = shift;
213  
214 64 my $result_source = ref($row);
215 64 my ($source) = ($result_source =~ /^.*::(.*)/s);
216 64 return $source . '_' . $row->id ;
217 }
218  
219  
220 # sub _create {
221 # my $self = shift;
222 # my $node = shift;
223  
224 # my $clone = $self->_clone_node($node);
225 # $self->schema->resultset($self->_source_name)->create($clone);
226 # }
227  
228 # sub _update {
229 # my $self = shift;
230 # my $node = shift;
231  
232 # my $clone = $self->_clone_node($node);
233 # $self->schema->resultset($self->_source_name)->update_or_create($clone);
234 # }
235  
236 # sub _delete {
237 # my $self = shift;
238 # my $nodes = shift;
239  
240 # foreach my $node (@$nodes) {
241 # $self->schema->resultset($self->_source_name)->find($node->{id})->delete;
242 # }
243 # }
244  
245  
246  
247 =head1 NAME
248  
249 TreePath::Backend::DBIx - Backend 'DBIx' for TreePath
250  
251 =head1 VERSION
252  
253 version 0.22
254  
255 =head1 CONFIGURATION
256  
257 $tp = TreePath->new( conf => 't/conf/treefromdbix.yml' );
258  
259 # t/conf/treefromdbix.yml
260 Model::TPath:
261 schema_class: Schema::TPath
262 connect_info:
263 dsn: 'dbi:SQLite:dbname=:memory:'
264  
265 TreePath:
266 debug: 0
267 backend:
268 name: DBIx
269 args:
270 model: Model::TPath
271 populate_backend: 1
272 sources_name:
273 Page:
274 columns:
275 - name
276 - parent
277 search_field: name
278 parent_field: parent
279  
280 Comment:
281 columns:
282 - page
283 parent_field: page
284  
285  
286 =head2 REQUIRED SCHEMA
287  
288 See t/lib/Schema/TPath.pm
289  
290 =head1 AUTHOR
291  
292 Daniel Brosseau, C<< <dab at catapulse.org> >>
293  
294 =head1 LICENSE AND COPYRIGHT
295  
296 Copyright 2014 Daniel Brosseau.
297  
298 This program is free software; you can redistribute it and/or modify it
299 under the terms of either: the GNU General Public License as published
300 by the Free Software Foundation; or the Artistic License.
301  
302 See http://dev.perl.org/licenses/ for more information.
303  
304  
305 =cut
306  
307 4 1;
↑ Summary TreePath.pm »