NestedSet.pm

filter: Statementstmt 3.6% 11 / 305 Branchbran 0.0% 0 / 74 Conditioncond 0.0% 0 / 64 MC/DCmcdc 0.0% 0 / 37 Subroutinesub 7.8% 3 / 38 Podpod 69.2% 18 / 26 totaltotal 5.8% 32 / 544 CC 75 SCAR 42.8
CC
75
Cov
2%
new
272.0
add_child_to_right
90.0
add_child_to_left
90.0
CRAP 72.1
↑ Summary NestedSet/Manage.pm »
1 package DBIx::Tree::NestedSet;
2  
3 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $DBIx::Tree::NestedSet::VERSION='0.16';
7  
8 #POD Below!!
9  
10 ################################################################################
11 0 ▶sub new{
Subroutine new: not called CC=16 SCAR=56.1
Pod documented
12 0 my $class=shift;
13 0 ▶ $class=ref($class)||$class;
Condition0%0/3
ref $class || $class
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table0%0/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
14 0 my %params=@_;
15 my $self={
16 dbh => $params{dbh},
17 left_column_name => $params{left_column_name} || 'lft',
18 right_column_name => $params{right_column_name} || 'rght',
19 no_id_creation => $params{no_id_creation} || '',
20 table_name => $params{table_name} || 'nested_set',
21 id_name => $params{id_name} || 'id',
22 no_alter_table => $params{no_alter_table} || undef,
23 db_type => $params{db_type} || 'MySQL',
24 no_locking => $params{no_locking} || undef
25 0 ▶ };
Condition0%0/16
$params{'left_column_name'} || 'lft'
l!l
00
$params{'right_column_name'} || 'rght'
l!l
00
$params{'no_id_creation'} || ''
l!l
00
$params{'table_name'} || 'nested_set'
l!l
00
$params{'id_name'} || 'id'
l!l
00
$params{'no_alter_table'} || undef
l!l
00
$params{'db_type'} || 'MySQL'
l!l
00
$params{'no_locking'} || undef
l!l
00
MC/DC0%0/1
$params{'left_column_name'} || 'lft'
$params{'left_column_name'}
MC/DC0%0/1
$params{'right_column_name'} || 'rght'
$params{'right_column_name'}
MC/DC0%0/1
$params{'no_id_creation'} || ''
$params{'no_id_creation'}
MC/DC0%0/1
$params{'table_name'} || 'nested_set'
$params{'table_name'}
MC/DC0%0/1
$params{'id_name'} || 'id'
$params{'id_name'}
MC/DC0%0/1
$params{'no_alter_table'} || undef
$params{'no_alter_table'}
MC/DC0%0/1
$params{'db_type'} || 'MySQL'
$params{'db_type'}
MC/DC0%0/1
$params{'no_locking'} || undef
$params{'no_locking'}
Truth table0%0/2
A
A= $params{'left_column_name'}
Aresult
11
00
Truth table0%0/2
A
A= $params{'right_column_name'}
Aresult
11
00
Truth table0%0/2
A
A= $params{'no_id_creation'}
Aresult
11
00
Truth table0%0/2
A
A= $params{'table_name'}
Aresult
11
00
Truth table0%0/2
A
A= $params{'id_name'}
Aresult
11
00
Truth table0%0/2
A
A= $params{'no_alter_table'}
Aresult
11
00
Truth table0%0/2
A
A= $params{'db_type'}
Aresult
11
00
Truth table0%0/2
A
A= $params{'no_locking'}
Aresult
11
00
26 0 bless $self, $class;
27 croak("Not a DBI connection")
28 0 ▶ unless($params{dbh}->isa('DBI::db'));
Branch: 1 branch
BranchTrueFalseTotal
unless $params{"dbh"}->isa("DBI::db") 0 0 0
29  
30 0 foreach('left_column_name','right_column_name','table_name','id_name'){
31 croak('"'.$self->{$_}."\" doesn't look like a valid SQL table or column name to me")
32 0 ▶ unless ($self->{$_} =~ m/^[_A-Za-z\d]+$/);
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{$_} =~ /^[_A-Za-z\d]+$/ 0 0 0
33 }
34  
35 0 my $db_type=$self->{db_type};
36
37 0 my $driver = "DBIx::Tree::NestedSet::$db_type";
38 0 ▶ eval "require $driver;" or
Branch: 1 branch
BranchTrueFalseTotal
eval "require $driver;" or croak("That DBD source doesn't have a driver implemented yet") 0 0 0
39 croak("That DBD source doesn't have a driver implemented yet");
40  
41 my $db_obj=$driver->new(
42 dbh => $self->{dbh},
43 left_column_name => $self->{left_column_name},
44 right_column_name => $self->{right_column_name},
45 table_name => $self->{table_name},
46 no_alter_table => $self->{no_alter_table},
47 id_name => $self->{id_name},
48 no_locking => $self->{no_locking}
49 0 );
50 0 $self->{_db_obj}=$db_obj;
51 #$self->{start_id}=$params{start_id}|| scalar $self->{dbh}->selectrow_array('select min('.$self->{left_column_name}.') from '.$self->{table_name} );
52 0 ▶ $params{dbh}->{RaiseError} = 1 if(not defined $params{No_RaiseError});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $params{"No_RaiseError"} 0 0 0
53 0 ▶ $params{dbh}->trace($params{trace}) if($params{trace});
Branch: 1 branch
BranchTrueFalseTotal
if $params{"trace"} 0 0 0
54 0 return $self;
55 }
56 ########################################
57  
58  
59 ################################################################################
60 0 ▶sub get_table_name{
Subroutine get_table_name: not called CC=1 SCAR=6.9
Pod undocumented
61 0 return $_[0]->{table_name};
62 }
63 ########################################
64  
65  
66 ################################################################################
67 0 ▶sub get_left_column_name{
Subroutine get_left_column_name: not called CC=1 SCAR=6.9
Pod undocumented
68 0 return $_[0]->{left_column_name};
69 }
70 ########################################
71  
72  
73 ################################################################################
74 0 ▶sub get_right_column_name{
Subroutine get_right_column_name: not called CC=1 SCAR=6.9
Pod undocumented
75 0 return $_[0]->{right_column_name};
76 }
77 ########################################
78  
79  
80 ################################################################################
81 0 ▶sub get_id_name{
Subroutine get_id_name: not called CC=1 SCAR=6.9
Pod undocumented
82 0 return $_[0]->{id_name};
83 }
84 ########################################
85  
86  
87 ################################################################################
88 0 ▶sub get_dbh{
Subroutine get_dbh: not called CC=1 SCAR=6.9
Pod undocumented
89 0 return $_[0]->{dbh};
90 }
91 ########################################
92  
93  
94 ################################################################################
95 0 ▶sub get_db_type{
Subroutine get_db_type: not called CC=1 SCAR=6.9
Pod undocumented
96 0 return $_[0]->{db_type};
97 }
98 ########################################
99  
100  
101 ################################################################################
102 0 ▶sub get_no_alter_table{
Subroutine get_no_alter_table: not called CC=1 SCAR=6.9
Pod undocumented
103 0 return $_[0]->{no_alter_table};
104 }
105 ########################################
106  
107  
108 ################################################################################
109 0 ▶sub get_no_locking{
Subroutine get_no_locking: not called CC=1 SCAR=6.9
Pod undocumented
110 0 return $_[0]->{no_locking};
111 }
112 ########################################
113  
114  
115 ################################################################################
116 0 ▶sub get_root{
Subroutine get_root: not called CC=1 SCAR=6.9
Pod documented
117 0 my $self=shift;
118 0 my $left=$self->{left_column_name};
119 0 my $table=$self->{table_name};
120 0 my $id_name=$self->{id_name};
121 0 my ($min_left)=$self->{dbh}->selectrow_array("select min($left) from $table");
122 0 return scalar $self->{dbh}->selectrow_array("select $id_name from ".$self->{table_name}." where $left=?",undef,($min_left));
123 }
124 ########################################
125  
126  
127 ################################################################################
128 0 ▶sub _lock_tables{
Subroutine _lock_tables: not called CC=1 SCAR=6.9
129 0 my $self=shift;
130 0 $self->{_db_obj}->_lock_tables;
131 }
132 ########################################
133  
134  
135 ################################################################################
136 0 ▶sub _unlock_tables{
Subroutine _unlock_tables: not called CC=1 SCAR=6.9
137 0 my $self=shift;
138 0 $self->{_db_obj}->_unlock_tables;
139 }
140 ########################################
141  
142  
143 ################################################################################
144 0 ▶sub add_child_to_right{
Subroutine add_child_to_right: not called CC=9 SCAR=45.0
Pod documented
145 0 my($self,%params)=@_;
146 0 my $dbh=$self->{dbh};
147 0 my $left=$self->{left_column_name};
148 0 my $right=$self->{right_column_name};
149 0 my $table=$self->{table_name};
150 0 my $id_name=$self->{id_name};
151 0 ▶ if((defined $params{id}) and ($id_name ne 'id')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"id"} and $id_name ne "id") 0 0 0
Condition0%0/3
defined $params{"id"} and $id_name ne "id"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"id"} and $id_name ne "id"
defined $params{"id"} $id_name ne "id"
Truth table0%0/3
A and B
A= defined $params{"id"}B= $id_name ne "id"
ABresult
0X0
100
111
152 #If they have changed the id_name but still pass in the id to act upon via the "id" parameter,
153 #fix it here. This is for backwards compatibility
154 $params{$id_name}=$params{id}
155 0 }
156 0 $self->_lock_tables();
157 0 ▶ if(!$params{$id_name} && scalar $dbh->selectrow_array("select count(*) from $table")){
Branch: 1 branch
BranchTrueFalseTotal
if (not $params{$id_name} and scalar $dbh->selectrow_array("select count(*) from $table")) 0 0 0
Condition0%0/2
not $params{$id_name} and scalar $dbh->selectrow_array("select count(*) from $table")
!ll
00
MC/DC0%0/1
not $params{$id_name} and scalar $dbh->selectrow_array("select count(*) from $table")
not $params{$id_name}
Truth table0%0/2
not(A)
A= not $params{$id_name}
Aresult
00
11
158 #They haven't given us an id for this child. Assume they want to add a child DIRECTLY
159 #under the parent, as they can't have more than one root.
160 0 $params{$id_name}=$self->get_root();
161 }
162 0 my $prepared_rightmost_SQL_statement=
163 $dbh->prepare_cached("SELECT $right FROM $table WHERE $id_name=?",
164 {dbi_dummy=>__FILE__.__LINE__}
165 );
166  
167 0 my ($rightmost)=$dbh->selectrow_array($prepared_rightmost_SQL_statement,undef,($params{$id_name}));
168 0 my $prepared_rightmost_SQL_tree_fix_statement=
169 $dbh->prepare_cached(
170 "UPDATE $table SET $left = CASE WHEN $left > ? THEN $left + 2 ELSE $left END,
171 $right = CASE WHEN $right >= ? THEN $right + 2 ELSE $right END WHERE $right >= ?",
172 {dbi_dummy=>__FILE__.__LINE__}
173 );
174 0 $prepared_rightmost_SQL_tree_fix_statement->execute($rightmost,$rightmost,$rightmost);
175 0 $prepared_rightmost_SQL_tree_fix_statement->finish();
176 0 my ($params,$values)=$self->_get_params_and_values(\%params,$left,$right,$id_name);
177 0 my ($columns,$placeholders)=_prepare_columns_and_placeholders_for_adding_child_to_right($params,$left,$right);
178 0 $self->_alter_table_if_needed($params);
179 0 ▶ if ($self->{no_id_creation}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"no_id_creation"}) 0 0 0
180 #We are manually passing in IDs. This is kinda a kludge to make the WebGUI folks happy.
181 0 $self->_alter_sql_for_provided_primary_key_edits(\$columns,\$placeholders,$values,\%params);
182 }
183 0 my $insert=$dbh->prepare_cached("INSERT INTO $table ($columns) VALUES($placeholders)",{dbi_dummy=>__FILE__.__LINE__});
184 0 ▶ $insert->execute($rightmost||1,$rightmost||1,@$values);
Condition0%0/4
$rightmost || 1
l!l
00
$rightmost || 1
l!l
00
MC/DC0%0/1
$rightmost || 1
$rightmost
MC/DC0%0/1
$rightmost || 1
$rightmost
Truth table0%0/2
A
A= $rightmost
Aresult
11
00
Truth table0%0/2
A
A= $rightmost
Aresult
11
00
185 0 $insert->finish();
186 0 my $new_id;
187 0 ▶ if ($self->{no_id_creation}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'no_id_creation'}) { } 0 0 0
188 0 $new_id=$params{provided_primary_key};
189 } else {
190 0 ($new_id)=$dbh->selectrow_array("select max($id_name) from $table");
191 }
192 0 $self->_unlock_tables();
193 0 return $new_id;
194 }
195 ########################################
196  
197  
198 ################################################################################
199 0 ▶sub add_child_to_left{
Subroutine add_child_to_left: not called CC=9 SCAR=45.0
Pod documented
200 0 my($self,%params)=@_;
201 0 my $dbh=$self->{dbh};
202
203 0 my $left=$self->{left_column_name};
204 0 my $right=$self->{right_column_name};
205 0 my $table=$self->{table_name};
206 0 my $id_name=$self->{id_name};
207 0 ▶ if((defined $params{id}) and ($id_name ne 'id')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"id"} and $id_name ne "id") 0 0 0
Condition0%0/3
defined $params{"id"} and $id_name ne "id"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"id"} and $id_name ne "id"
defined $params{"id"} $id_name ne "id"
Truth table0%0/3
A and B
A= defined $params{"id"}B= $id_name ne "id"
ABresult
0X0
100
111
208 #If they have changed the id_name but still pass in the id to act upon via the "id" parameter,
209 #fix it here. This is for backwards compatibility
210 $params{$id_name}=$params{id}
211 0 }
212 0 $self->_lock_tables();
213 0 ▶ if(!$params{$id_name} && scalar $dbh->selectrow_array("select count(*) from $table")){
Branch: 1 branch
BranchTrueFalseTotal
if (not $params{$id_name} and scalar $dbh->selectrow_array("select count(*) from $table")) 0 0 0
Condition0%0/2
not $params{$id_name} and scalar $dbh->selectrow_array("select count(*) from $table")
!ll
00
MC/DC0%0/1
not $params{$id_name} and scalar $dbh->selectrow_array("select count(*) from $table")
not $params{$id_name}
Truth table0%0/2
not(A)
A= not $params{$id_name}
Aresult
00
11
214 #They haven't given us an id for this child. Assume they want to add a child DIRECTLY
215 #under the parent, as they can't have more than one root.
216 0 $params{$id_name}=$self->get_root();
217 }
218
219 0 my $prepared_leftmost_SQL_statement=$dbh->prepare_cached("SELECT $left FROM $table WHERE $id_name=?",{dbi_dummy=>__FILE__.__LINE__});
220
221 0 my ($leftmost)=$dbh->selectrow_array($prepared_leftmost_SQL_statement,undef,($params{$id_name}));
222 0 $prepared_leftmost_SQL_statement->finish();
223 0 my $prepared_leftmost_SQL_tree_fix_statement=
224 $dbh->prepare_cached(
225 qq|UPDATE $table
226 SET $right =
227 CASE WHEN $right > ?
228 THEN $right + 2
229 ELSE $right END,
230 $left =
231 CASE WHEN $left > ?
232 THEN $left + 2
233 ELSE $left
234 END
235 |,
236 {dbi_dummy=>__FILE__.__LINE__}
237 );
238
239 0 $prepared_leftmost_SQL_tree_fix_statement->execute($leftmost,$leftmost);
240 0 $prepared_leftmost_SQL_tree_fix_statement->finish();
241 0 my ($params,$values)=$self->_get_params_and_values(\%params,$left,$right,$id_name);
242 0 my ($columns,$placeholders)=_prepare_columns_and_placeholders_for_adding_child_to_left($params,$left,$right);
243 0 $self->_alter_table_if_needed($params);
244 0 ▶ if ($self->{no_id_creation}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"no_id_creation"}) 0 0 0
245 #We are manually passing in IDs. This is kinda a kludge to make the WebGUI folks happy.
246 0 $self->_alter_sql_for_provided_primary_key_edits(\$columns,\$placeholders,$values,\%params);
247 }
248 0 my $insert=$dbh->prepare_cached("INSERT INTO $table ($columns) VALUES($placeholders)",{dbi_dummy=>__FILE__.__LINE__});
249 0 ▶ $insert->execute($leftmost||1,$leftmost||1,@$values);
Condition0%0/4
$leftmost || 1
l!l
00
$leftmost || 1
l!l
00
MC/DC0%0/1
$leftmost || 1
$leftmost
MC/DC0%0/1
$leftmost || 1
$leftmost
Truth table0%0/2
A
A= $leftmost
Aresult
11
00
Truth table0%0/2
A
A= $leftmost
Aresult
11
00
250 0 $insert->finish();
251 0 my $new_id;
252 0 ▶ if ($self->{no_id_creation}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'no_id_creation'}) { } 0 0 0
253 0 $new_id=$params{provided_primary_key};
254 } else {
255 0 ($new_id)=$dbh->selectrow_array("select max($id_name) from $table");
256 }
257 0 $self->_unlock_tables();
258 0 return $new_id;
259 }
260 ########################################
261  
262  
263 ################################################################################
264 0 ▶sub _alter_sql_for_provided_primary_key_edits{
Subroutine _alter_sql_for_provided_primary_key_edits: not called CC=1 SCAR=6.9
265 0 my($self,$columns,$placeholders,$values,$params)=@_;
266 0 my $id_name=$self->{id_name};
267 0 $$columns.=",$id_name";
268 0 $$placeholders.=',?';
269 0 push @$values, $params->{provided_primary_key};
270 }
271 ########################################
272  
273  
274 ################################################################################
275 0 ▶sub _alter_table_if_needed{
Subroutine _alter_table_if_needed: not called CC=8 SCAR=42.8
276 #$params is an arrayref with all the proper columns in order.
277 0 my ($self,$params)=@_;
278  
279 #We don't want to invoke the "automagical" table altering behavior
280 0 ▶ return if(defined $self->{no_alter_table});
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"no_alter_table"} 0 0 0
281  
282 0 my $left=$self->{left_column_name};
283 0 my $right=$self->{right_column_name};
284 0 my $table=$self->{table_name};
285 0 my $dbh=$self->{dbh};
286 0 my $id_name=$self->{id_name};
287  
288 #my %columns_we_are_requesting=map{$_=>1} @$params;
289 0 my @columns_we_need_to_create;
290 #With MySQL I could use "Explain $table" but I'd like this to be a bit more cross-RDBMS
291 0 my $get_columns=$dbh->prepare_cached("select *,count(*) as _ignore_me_sdfas from $table group by $id_name",
292 {dbi_dummy=>__FILE__.__LINE__});
293 0 $get_columns->execute();
294 0 my %columns_that_we_have=();
295 0 foreach(@{$get_columns->{NAME}}){
296 0 ▶ $columns_that_we_have{$_}=1 if($_ ne '_ignore_me_sdfas');
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne "_ignore_me_sdfas" 0 0 0
297 }
298 0 $get_columns->finish();
299 0 foreach(@$params){
300 0 ▶ push @columns_we_need_to_create, $_ if(not defined $columns_that_we_have{$_});
Branch: 1 branch
BranchTrueFalseTotal
unless defined $columns_that_we_have{$_} 0 0 0
301 }
302 0 my $db_obj=$self->{_db_obj};
303 0 foreach(@columns_we_need_to_create){
304 0 ▶ croak('"'.$_."\" doesn't look like a valid SQL table or column name to me")
Branch: 1 branch
BranchTrueFalseTotal
unless $_ =~ /^[_A-Za-z\d]+$/ 0 0 0
305 unless ($_ =~ m/^[_A-Za-z\d]+$/);
306 0 $db_obj->_alter_table($_);
307 0 $dbh->do("create index $_ on $table($_)");
308 }
309 }
310 ########################################
311  
312  
313 ################################################################################
314 0 ▶sub _get_params_and_values{
Subroutine _get_params_and_values: not called CC=5 SCAR=34.0
315 0 my ($self,$params,$left,$right,$id_name,$no_left_or_right)=@_;
316 0 my %ignore=(
317 $left=>1,
318 $right=>1,
319 $id_name=>1,
320 provided_primary_key=>1
321 );
322 0 ▶ my @params=($no_left_or_right) ? () :($left,$right); #Keep in order. . .
Branch: 1 branch
BranchTrueFalseTotal
$no_left_or_right ? : 0 0 0
323 0 my @values;
324 0 foreach my $column (keys %$params){
325 0 ▶ if (not defined $ignore{$column}){
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $ignore{$column}) 0 0 0
326 0 push @params, $column;
327 0 ▶ push @values, $params->{$column}||''
Condition0%0/2
$params->{$column} || ''
l!l
00
MC/DC0%0/1
$params->{$column} || ''
$params->{$column}
Truth table0%0/2
A
A= $params->{$column}
Aresult
11
00
328 }
329 }
330 0 return (\@params,\@values);
331 }
332 ########################################
333  
334  
335 ################################################################################
336 0 ▶sub edit_node{
Subroutine edit_node: not called CC=3 SCAR=24.8
Pod documented
337 0 my ($self,%params)=@_;
338 0 my $dbh=$self->{dbh};
339
340 0 my $left=$self->{left_column_name};
341 0 my $right=$self->{right_column_name};
342 0 my $table=$self->{table_name};
343 0 my $id_name=$self->{id_name};
344 0 ▶ if((defined $params{id}) and ($id_name ne 'id')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"id"} and $id_name ne "id") 0 0 0
Condition0%0/3
defined $params{"id"} and $id_name ne "id"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"id"} and $id_name ne "id"
defined $params{"id"} $id_name ne "id"
Truth table0%0/3
A and B
A= defined $params{"id"}B= $id_name ne "id"
ABresult
0X0
100
111
345 #If they have changed the id_name but still pass in the id to act upon via the "id" parameter,
346 #fix it here. This is for backwards compatibility
347 0 $params{$id_name}=$params{id};
348 }
349 0 $self->_lock_tables();
350 0 my ($params,$values)=$self->_get_params_and_values(\%params,$left,$right,$id_name,1);
351 0 my ($columns)=_prepare_columns_and_placeholders_for_edit($params);
352 0 $self->_alter_table_if_needed($params);
353 0 my $update=$dbh->prepare_cached(
354 "update $table set $columns where $id_name=?",
355 {dbi_dummy=>__FILE__.__LINE__}
356 );
357 0 my $id_value=$params{$id_name};
358 0 $update->execute(@$values,$id_value);
359 0 $update->finish();
360 0 $self->_unlock_tables();
361 }
362 ########################################
363  
364  
365 ################################################################################
366 0 ▶sub _prepare_columns_and_placeholders_for_edit{
Subroutine _prepare_columns_and_placeholders_for_edit: not called CC=1 SCAR=6.9
367 0 my ($params)=@_;
368 0 my $columns=join('=? ,',(@$params)).'=?';
369 0 return ($columns);
370 }
371 ########################################
372  
373  
374 ################################################################################
375 0 ▶sub _prepare_columns_and_placeholders_for_adding_child_to_right{
Subroutine _prepare_columns_and_placeholders_for_adding_child_to_right: not called CC=2 SCAR=17.9
376 0 my ($params,$left,$right)=@_;
377 0 my $columns=join(',',(@$params));
378 0 ▶ my $placeholders=join(',',('?','? + 1')).
Branch: 1 branch
BranchTrueFalseTotal
scalar @$params - 2 > 0 ? : 0 0 0
379 ((scalar @$params -2 > 0) ? ',':''). #If there isn't more than 2 params, don't put in a comma
380 substr(('?,' x (scalar @$params -2 )),0,-1);
381 0 return ($columns,$placeholders);
382 }
383 ########################################
384  
385  
386 ################################################################################
387 0 ▶sub _prepare_columns_and_placeholders_for_adding_child_to_left{
Subroutine _prepare_columns_and_placeholders_for_adding_child_to_left: not called CC=2 SCAR=17.9
388 0 my ($params,$left,$right)=@_;
389 0 my $columns=join(',',(@$params));
390 0 ▶ my $placeholders=join(',',('? + 1','? + 2')).
Branch: 1 branch
BranchTrueFalseTotal
scalar @$params - 2 > 0 ? : 0 0 0
391 ((scalar @$params -2 > 0) ? ',':''). #If there isn't more than 2 params, don't put in a comma
392 substr(('?,' x (scalar @$params -2 )),0,-1);
393 0 return ($columns,$placeholders);
394 }
395 ########################################
396  
397  
398 ################################################################################
399 0 ▶sub get_id_by_key{
Subroutine get_id_by_key: not called CC=4 SCAR=30.0
Pod documented
400 0 my($self,%params)=@_;
401 0 my $left=$self->{left_column_name};
402 0 my $right=$self->{right_column_name};
403 0 my $table=$self->{table_name};
404 0 my $key_name=$params{key_name};
405 0 my $id_name=$self->{id_name};
406 0 ▶ if((defined $params{id}) and ($id_name ne 'id')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"id"} and $id_name ne "id") 0 0 0
Condition0%0/3
defined $params{"id"} and $id_name ne "id"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"id"} and $id_name ne "id"
defined $params{"id"} $id_name ne "id"
Truth table0%0/3
A and B
A= defined $params{"id"}B= $id_name ne "id"
ABresult
0X0
100
111
407 #If they have changed the id_name but still pass in the id to act upon via the "id" parameter,
408 #fix it here. This is for backwards compatibility
409 0 $params{$id_name}=$params{id};
410 }
411 0 my $ids=$self->{dbh}->selectcol_arrayref("select $id_name from $table where $key_name = ?",undef,($params{key_value}));
412 0 ▶ return (@$ids > 1) ? $ids : $ids->[0] ;
Branch: 1 branch
BranchTrueFalseTotal
@$ids > 1 ? : 0 0 0
413 }
414 ########################################
415  
416  
417 ################################################################################
418 0 ▶sub get_self_and_parents_flat{
Subroutine get_self_and_parents_flat: not called CC=5 SCAR=34.0
Pod documented
419 0 my($self,%params)=@_;
420 0 my $dbh=$self->{dbh};
421 0 my $left=$self->{left_column_name};
422 0 my $right=$self->{right_column_name};
423 0 my $table=$self->{table_name};
424 0 my $id_name=$self->{id_name};
425 0 ▶ if((defined $params{id}) and ($id_name ne 'id')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"id"} and $id_name ne "id") 0 0 0
Condition0%0/3
defined $params{"id"} and $id_name ne "id"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"id"} and $id_name ne "id"
defined $params{"id"} $id_name ne "id"
Truth table0%0/3
A and B
A= defined $params{"id"}B= $id_name ne "id"
ABresult
0X0
100
111
426 #If they have changed the id_name but still pass in the id to act upon via the "id" parameter,
427 #fix it here. This is for backwards compatibility
428 0 $params{$id_name}=$params{id};
429 }
430 0 my $prepared_get_self_and_parents_flat_SQL_statement=
431 $dbh->prepare_cached(
432 "select n2.* from $table as n1, $table as n2 where (n1.$left between n2.$left and n2.$right) and (n1.$id_name=?) order by n2.$left",
433 {dbi_dummy=>__FILE__.__LINE__}
434 );
435
436 my $tree_structure=$dbh->selectall_arrayref($prepared_get_self_and_parents_flat_SQL_statement,
437 {Columns=>{}},
438 0 ▶ ($params{$id_name} || 1)
Condition0%0/2
$params{$id_name} || 1
l!l
00
MC/DC0%0/1
$params{$id_name} || 1
$params{$id_name}
Truth table0%0/2
A
A= $params{$id_name}
Aresult
11
00
439 );
440 0 my $level=1;
441 0 foreach(@$tree_structure){
442 0 $_->{level}=$level;
443 0 $level++;
444 }
445 0 return $tree_structure;
446 }
447 ########################################
448  
449  
450 ################################################################################
451 0 ▶sub get_parents_flat{
Subroutine get_parents_flat: not called CC=2 SCAR=17.9
Pod documented
452 0 my $self=shift;
453 0 my $tree=$self->get_self_and_parents_flat(@_);
454 0 ▶ my $poo=pop @$tree if(@$tree);
Branch: 1 branch
BranchTrueFalseTotal
if @$tree 0 0 0
455 0 return $tree;
456 }
457 ########################################
458  
459  
460 ################################################################################
461 0 ▶sub delete_self_and_children{
Subroutine delete_self_and_children: not called CC=8 SCAR=42.8
Pod documented
462 0 my ($self,%params)=@_;
463 0 my $dbh=$self->{dbh};
464 0 my $left=$self->{left_column_name};
465 0 my $right=$self->{right_column_name};
466 0 my $table=$self->{table_name};
467 0 my $id_name=$self->{id_name};
468 0 ▶ if((defined $params{id}) and ($id_name ne 'id')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"id"} and $id_name ne "id") 0 0 0
Condition0%0/3
defined $params{"id"} and $id_name ne "id"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"id"} and $id_name ne "id"
defined $params{"id"} $id_name ne "id"
Truth table0%0/3
A and B
A= defined $params{"id"}B= $id_name ne "id"
ABresult
0X0
100
111
469 # If they have changed the name of the id field
470 # but still pass in IDs with the old "id" moniker, fix it here.
471  
472 0 $params{$id_name}=$params{id};
473 }
474 0 ▶ if(!$params{$id_name}){
Branch: 1 branch
BranchTrueFalseTotal
if (not $params{$id_name}) { } 0 0 0
475 0 carp("You didn't give us an ID that we could start the delete from");
476 0 return [];
477 } else {
478 0 $self->_lock_tables();
479 0 my $ids;
480 0 ▶ if($params{not_self}){
Branch: 1 branch
BranchTrueFalseTotal
if ($params{'not_self'}) { } 0 0 0
481 #We don't want to delete the starting node.
482 #Start with the next level and go through them.
483  
484 0 my $outer_tree=$self->get_children_flat(id=>$params{$id_name},depth=>1);
485 0 foreach my $outer_node(@$outer_tree){
486 0 my $temp_tree=$self->get_self_and_children_flat(id=>$outer_node->{$id_name});
487 0 $self->_delete_node(id=>$outer_node->{$id_name});
488 0 foreach my $inner_node (@$temp_tree){
489 0 push @$ids,$inner_node->{$id_name};
490 }
491 }
492
493 } else {
494 #Delete it all. Hasta la bye-bye!
495 0 my $tree=$self->get_self_and_children_flat(id=>$params{$id_name});
496 0 $self->_delete_node(%params);
497 0 foreach my $node (@$tree){
498 0 push @$ids,$node->{$id_name};
499 }
500 }
501 0 $self->_unlock_tables();
502 0 return $ids;
503 }
504 }
505 ########################################
506  
507  
508 ################################################################################
509 0 ▶sub delete_children{
Subroutine delete_children: not called CC=1 SCAR=6.9
Pod documented
510 0 my $self=shift;
511 0 $self->delete_self_and_children(@_,not_self=>1);
512 }
513 ########################################
514  
515  
516 ################################################################################
517 0 ▶sub _delete_node{
Subroutine _delete_node: not called CC=1 SCAR=6.9
518 0 my($self,%params)=@_;
519 0 my $left=$self->{left_column_name};
520 0 my $right=$self->{right_column_name};
521 0 my $table=$self->{table_name};
522 0 my $id_name=$self->{id_name};
523 0 my $dbh=$self->{dbh};
524 0 my $node_info=$self->get_hashref_of_info_by_id($params{$id_name});
525
526 0 my $prepared_delete_node_delete_statement=
527 $dbh->prepare_cached(
528 "delete from $table where $left between ? and ?",
529 {dbi_dummy=>__FILE__.__LINE__}
530 );
531
532 0 $prepared_delete_node_delete_statement->execute($node_info->{$left},$node_info->{$right});
533 0 $prepared_delete_node_delete_statement->finish();
534  
535 0 my $prepared_delete_node_fix_nodes=
536 $dbh->prepare_cached(
537 "UPDATE $table
538 SET $left = CASE
539 WHEN $left > ? THEN $left - (? - ? + 1)
540 ELSE $left
541 END,
542 $right = CASE
543 WHEN $right > ? THEN $right - (? - ? + 1)
544 ELSE $right
545 END
546 WHERE $right > ?",
547 {dbi_dummy=>__FILE__.__LINE__}
548 );
549
550 $prepared_delete_node_fix_nodes->execute(
551 $node_info->{$left},
552 $node_info->{$right},
553 $node_info->{$left},
554 $node_info->{$right},
555 $node_info->{$right},
556 $node_info->{$left},
557 0 $node_info->{$left},
558 );
559 0 $prepared_delete_node_fix_nodes->finish();
560 }
561 ########################################
562  
563  
564 ################################################################################
565 0 ▶sub get_self_and_children_flat{
Subroutine get_self_and_children_flat: not called CC=5 SCAR=34.0
Pod documented
566 0 my($self,%params)=@_;
567 0 my $dbh=$self->{dbh};
568 0 my $left=$self->{left_column_name};
569 0 my $right=$self->{right_column_name};
570 0 my $table=$self->{table_name};
571 0 my $id_name=$self->{id_name};
572 0 ▶ if((defined $params{id}) and ($id_name ne 'id')){
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{"id"} and $id_name ne "id") 0 0 0
Condition0%0/3
defined $params{"id"} and $id_name ne "id"
!ll&&!rl&&r
000
MC/DC0%0/2
defined $params{"id"} and $id_name ne "id"
defined $params{"id"} $id_name ne "id"
Truth table0%0/3
A and B
A= defined $params{"id"}B= $id_name ne "id"
ABresult
0X0
100
111
573 # If they have changed the id name but still pass in the ID value in the id parameter, fix it.
574 $params{$id_name}=$params{id}
575 0 }
576 0 my $id_SQL='';
577 0 my $start_node_info={};
578 0 ▶ if (defined $params{$id_name}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $params{$id_name}) 0 0 0
579 0 $start_node_info=$self->get_hashref_of_info_by_id_with_level($params{$id_name});
580 0 $id_SQL="and (n1.$left between " . $dbh->quote($start_node_info->{$left})." and ".$dbh->quote($start_node_info->{$right}).") ";
581 }
582 0 my $depth_having_SQL='';
583 0 ▶ if($params{depth}){
Branch: 1 branch
BranchTrueFalseTotal
if ($params{"depth"}) 0 0 0
584 0 $depth_having_SQL='having level <= '.$dbh->quote(($params{depth} + $start_node_info->{level}));
585 }
586 0 my $tree_structure=$dbh->selectall_arrayref("select count(n2.${id_name}) as level,n1.* from $table as n1, $table as n2 where (n1.$left between n2.$left and n2.$right) $id_SQL group by n1.${id_name} $depth_having_SQL order by n1.$left",{Columns=>{}});
587 0 return $tree_structure;
588 }
589 ########################################
590  
591  
592 ################################################################################
593 0 ▶sub get_children_flat{
Subroutine get_children_flat: not called CC=2 SCAR=17.9
Pod documented
594 0 my $self=shift;
595 0 my $tree=$self->get_self_and_children_flat(@_);
596 0 ▶ my $poo=shift @$tree if(@$tree);
Branch: 1 branch
BranchTrueFalseTotal
if @$tree 0 0 0
597 0 return $tree;
598 }
599 ########################################
600  
601  
602 ################################################################################
603 0 ▶sub swap_nodes{
Subroutine swap_nodes: not called CC=5 SCAR=34.0
Pod documented
604 0 my($self,%params)=@_;
605 0 my $dbh=$self->{dbh};
606 0 my $left=$self->{left_column_name};
607 0 my $right=$self->{right_column_name};
608 0 my $table=$self->{table_name};
609  
610 0 my $first_id=$params{first_id};
611 0 my $second_id=$params{second_id};
612 0 ▶ croak("You didn't give me valid IDs to swap!\n") if(! $first_id || ! $second_id);
Branch: 1 branch
BranchTrueFalseTotal
unless $first_id and $second_id 0 0 0
Condition0%0/3
$first_id and $second_id
!ll&&!rl&&r
000
MC/DC0%0/2
$first_id and $second_id
$first_id $second_id
Truth table0%0/3
A and B
A= $first_idB= $second_id
ABresult
0X0
100
111
613 #This is an "eq" below because we can now have non-numeric IDs.
614 0 ▶ croak("You can't switch a node with itself!\n") if($first_id eq $second_id);
Branch: 1 branch
BranchTrueFalseTotal
if $first_id eq $second_id 0 0 0
615  
616 0 $self->_lock_tables();
617 0 my $first_id_info=$self->get_hashref_of_info_by_id($first_id);
618 0 my $second_id_info=$self->get_hashref_of_info_by_id($second_id);
619  
620 0 my ($left_node,$right_node);
621 0 ▶ if($first_id_info->{$left} < $second_id_info->{$left}){
Branch: 1 branch
BranchTrueFalseTotal
if ($first_id_info->{$left} < $second_id_info->{$left}) { } 0 0 0
622 0 $left_node=$first_id_info;
623 0 $right_node=$second_id_info;
624 } else {
625 0 $left_node=$second_id_info;
626 0 $right_node=$first_id_info;
627 }
628 0 $dbh->do(qq|update $table set
629 $left =
630 CASE WHEN $left between $left_node->{$left} and $left_node->{$right}
631 THEN $right_node->{$right} + $left - $left_node->{$right}
632 WHEN $left between $right_node->{$left} and $right_node->{$right}
633 THEN $left_node->{$left} + $left - $right_node->{$left}
634 ELSE $left_node->{$left} + $right_node->{$right} + $left - $left_node->{$right} - $right_node->{$left} END,
635 $right =
636 CASE WHEN $right between $left_node->{$left} and $left_node->{$right}
637 THEN $right_node->{$right} + $right - $left_node->{$right}
638 WHEN $right between $right_node->{$left} and $right_node->{$right}
639 THEN $left_node->{$left} + $right - $right_node->{$left}
640 ELSE $left_node->{$left} + $right_node->{$right} + $right - $left_node->{$right} - $right_node->{$left} END
641 WHERE ($left between $left_node->{$left} and $right_node->{$right})
642 AND $left_node->{$left} < $left_node->{$right}
643 AND $left_node->{$right} < $right_node->{$left}
644 AND $right_node->{$left} < $right_node->{$right}|);
645
646 0 $self->_unlock_tables();
647 }
648 ########################################
649  
650  
651 ################################################################################
652 0 ▶sub get_hashref_of_info_by_id{
Subroutine get_hashref_of_info_by_id: not called CC=1 SCAR=6.9
Pod documented
653 0 my ($self,$value)=@_;
654 0 my $dbh=$self->{dbh};
655 0 my $id_name=$self->{id_name};
656 #This excessively explicit quoting is to work around the buggy Mandrake 10.0 version of DBD::mysql
657 0 return $dbh->selectrow_hashref("select * from ".$self->{table_name}." where $id_name = ".$dbh->quote($value));
658 }
659 ########################################
660  
661  
662 ################################################################################
663 0 ▶sub get_hashref_of_info_by_id_with_level{
Subroutine get_hashref_of_info_by_id_with_level: not called CC=1 SCAR=6.9
Pod documented
664 0 my $self=shift;
665 0 my $wanted_id=shift;
666 0 my $left=$self->{left_column_name};
667 0 my $right=$self->{right_column_name};
668 0 my $table=$self->{table_name};
669 0 my $id_name=$self->{id_name};
670 0 return $self->{dbh}->selectrow_hashref("select count(n2.$id_name) as level,n1.* from $table as n1, $table as n2 where (n1.$left between n2.$left and n2.$right) and n1.$id_name=? group by n1.$id_name",undef,($wanted_id));
671 }
672 ########################################
673  
674  
675 ################################################################################
676 0 ▶sub create_report{
Subroutine create_report: not called CC=5 SCAR=34.0
Pod documented
677 0 my ($self,%params)=@_;
678 0 my $id_name=$self->{id_name};
679 0 ▶ my $ancestors=$self->get_self_and_children_flat(id => $params{$id_name}||$self->get_root);
Condition0%0/3
$params{$id_name} || $self->get_root
l!l&&r!l&&!r
000
MC/DC0%0/2
$params{$id_name} || $self->get_root
$params{$id_name} $self->get_root
Truth table0%0/3
A || B
A= $params{$id_name}B= $self->get_root
ABresult
1X1
011
000
680 0 my $report;
681 0 foreach (@$ancestors) {
682 0 ▶ $report.= (($_->{level} > 1) ? ((" " x ($params{indent_level} || 2)) x ($_->{level} - 1)) :'');
Branch: 1 branch
BranchTrueFalseTotal
$_->{'level'} > 1 ? : 0 0 0
Condition0%0/2
$params{'indent_level'} || 2
l!l
00
MC/DC0%0/1
$params{'indent_level'} || 2
$params{'indent_level'}
Truth table0%0/2
A
A= $params{'indent_level'}
Aresult
11
00
683 0 $report.= $_->{name}." (".$_->{$id_name}.")(".$_->{level}.")\n";
684 }
685 0 return $report;
686 }
687 ########################################
688  
689  
690 ################################################################################
691 0 ▶sub create_default_table{
Subroutine create_default_table: not called CC=1 SCAR=6.9
Pod documented
692 0 my $self=shift;
693 0 $self->{_db_obj}->_create_default_table();
694 }
695 ########################################
696  
697  
698 ################################################################################
699 0 ▶sub get_default_create_table_statement{
Subroutine get_default_create_table_statement: not called CC=1 SCAR=6.9
Pod documented
700 0 my $self=shift;
701 0 $self->{_db_obj}->_get_default_create_table_statement();
702 }
703 ########################################
704  
705  
706 1 1;
707  
708 __END__
↑ Summary NestedSet/Manage.pm »