| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package Otogiri::Plugin::DeleteCascade; | 
| 2 | 2 |  |  | 2 |  | 21702 | use 5.008005; | 
|  | 2 |  |  |  |  | 7 |  | 
|  | 2 |  |  |  |  | 67 |  | 
| 3 | 2 |  |  | 2 |  | 9 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 64 |  | 
| 4 | 2 |  |  | 2 |  | 9 | use warnings; | 
|  | 2 |  |  |  |  | 11 |  | 
|  | 2 |  |  |  |  | 90 |  | 
| 5 |  |  |  |  |  |  |  | 
| 6 |  |  |  |  |  |  | our $VERSION = "0.04"; | 
| 7 |  |  |  |  |  |  |  | 
| 8 | 2 |  |  | 2 |  | 689 | use Otogiri; | 
|  | 2 |  |  |  |  | 72086 |  | 
|  | 2 |  |  |  |  | 272 |  | 
| 9 | 2 |  |  | 2 |  | 984 | use Otogiri::Plugin; | 
|  | 2 |  |  |  |  | 532 |  | 
|  | 2 |  |  |  |  | 45 |  | 
| 10 | 2 |  |  | 2 |  | 1669 | use DBIx::Inspector; | 
|  | 2 |  |  |  |  | 17652 |  | 
|  | 2 |  |  |  |  | 555 |  | 
| 11 |  |  |  |  |  |  |  | 
| 12 |  |  |  |  |  |  | our @EXPORT = qw(delete_cascade); | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | sub delete_cascade { | 
| 15 | 2 |  |  | 2 | 1 | 2571 | my ($self, $table_name, $cond_href) = @_; | 
| 16 |  |  |  |  |  |  |  | 
| 17 | 2 |  |  |  |  | 11 | my @parent_rows = $self->select($table_name, $cond_href); | 
| 18 | 2 |  |  |  |  | 1730 | my $inspector = DBIx::Inspector->new(dbh => $self->dbh); | 
| 19 | 2 |  |  |  |  | 4278 | my $iter = $inspector->table($table_name)->pk_foreign_keys(); | 
| 20 | 2 |  |  |  |  | 8555 | my $affected_rows = 0; | 
| 21 | 2 |  |  |  |  | 10 | while( my $child_table_fk_info = $iter->next ) { | 
| 22 | 1 |  |  |  |  | 101 | $affected_rows += _delete_child($self, $child_table_fk_info, @parent_rows); | 
| 23 |  |  |  |  |  |  | } | 
| 24 | 2 |  |  |  |  | 86 | $affected_rows += $self->delete($table_name, $cond_href); | 
| 25 | 2 |  |  |  |  | 734 | return $affected_rows; | 
| 26 |  |  |  |  |  |  | } | 
| 27 |  |  |  |  |  |  |  | 
| 28 |  |  |  |  |  |  | sub _delete_child { | 
| 29 | 1 |  |  | 1 |  | 3 | my ($db, $child_table_fk_info, @parent_rows) = @_; | 
| 30 | 1 |  |  |  |  | 1 | my $affected_rows = 0; | 
| 31 | 1 |  |  |  |  | 3 | for my $parent_row ( @parent_rows ) { | 
| 32 | 1 |  |  |  |  | 5 | my $child_table_name   = $child_table_fk_info->fktable_name; | 
| 33 | 1 |  |  |  |  | 7 | my $parent_column_name = $child_table_fk_info->pkcolumn_name; | 
| 34 | 1 |  |  |  |  | 6 | my $child_column_name  = $child_table_fk_info->fkcolumn_name; | 
| 35 |  |  |  |  |  |  |  | 
| 36 | 1 |  |  |  |  | 5 | my $child_delete_condition = { | 
| 37 |  |  |  |  |  |  | $child_column_name => $parent_row->{$parent_column_name}, | 
| 38 |  |  |  |  |  |  | }; | 
| 39 | 1 |  |  |  |  | 7 | $affected_rows += $db->delete_cascade($child_table_name, $child_delete_condition); | 
| 40 |  |  |  |  |  |  | } | 
| 41 | 1 |  |  |  |  | 20 | return $affected_rows; | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  |  | 
| 44 |  |  |  |  |  |  |  | 
| 45 |  |  |  |  |  |  | 1; | 
| 46 |  |  |  |  |  |  | __END__ |