| 1 | package DBIx::Model; | ||||||||||||||||||||
| 2 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 3 | 1 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use DBIx::Model::DB; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | 1 | our $VERSION = '0.0.1'; | |||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 1 | my %columns; | |||||||||||||||||||
| 9 | my %forward; | ||||||||||||||||||||
| 10 | 1 | my %backward; | |||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | 0 | ▶ | sub DBI::db::model { | ||||||||||||||||||
|
Subroutine
model: not called CC=15 SCAR=54.8
| |||||||||||||||||||||
| 13 | 0 | my $dbh = shift; | |||||||||||||||||||
| 14 | 0 | my $catalog = shift; | |||||||||||||||||||
| 15 | 0 | my $schema = shift; | |||||||||||||||||||
| 16 | 0 | ▶ | my $names = shift // '%'; | ||||||||||||||||||
|
Condition0%0/2
shift() // "%"
MC/DC0%0/1
shift() // "%"
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 17 | 0 | ▶ | my $type = shift // 'TABLE,VIEW'; | ||||||||||||||||||
|
Condition0%0/2
shift() // "TABLE,VIEW"
MC/DC0%0/1
shift() // "TABLE,VIEW"
shift()
Truth table0%0/2
A
A= shift()
| |||||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | my $db = DBIx::Model::DB->new( | ||||||||||||||||||||
| 20 | name => $dbh->{Name}, | ||||||||||||||||||||
| 21 | 0 | catalog => $catalog, | |||||||||||||||||||
| 22 | schema => $schema, | ||||||||||||||||||||
| 23 | table_types => $type, | ||||||||||||||||||||
| 24 | ); | ||||||||||||||||||||
| 25 | 0 | my @raw_fk; | |||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | 0 | my $t_sth = | |||||||||||||||||||
| 28 | $dbh->table_info( $db->catalog, $db->schema, $names, $db->table_types ); | ||||||||||||||||||||
| 29 | |||||||||||||||||||||
| 30 | 0 | my $trefs = $t_sth->fetchall_hashref('TABLE_NAME'); | |||||||||||||||||||
| 31 | 0 | foreach my $tname ( sort keys %$trefs ) { | |||||||||||||||||||
| 32 | my $table = $db->add_table( | ||||||||||||||||||||
| 33 | name => $tname, | ||||||||||||||||||||
| 34 | type => $trefs->{$tname}->{TABLE_TYPE} | ||||||||||||||||||||
| 35 | 0 | ); | |||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | 0 | my @primary = $dbh->primary_key( $db->catalog, $db->schema, $tname ); | |||||||||||||||||||
| 38 | 0 | my $c_sth = $dbh->column_info( $db->catalog, $db->schema, $tname, '%' ); | |||||||||||||||||||
| 39 | |||||||||||||||||||||
| 40 | 0 | ▶ | while ( my $c_ref = $c_sth->fetchrow_hashref ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 41 | 0 | my $pri = grep { $c_ref->{COLUMN_NAME} eq $_ } @primary; | |||||||||||||||||||
| 42 | $table->add_column( | ||||||||||||||||||||
| 43 | name => $c_ref->{COLUMN_NAME}, | ||||||||||||||||||||
| 44 | nullable => $c_ref->{NULLABLE}, | ||||||||||||||||||||
| 45 | size => $c_ref->{COLUMN_SIZE}, | ||||||||||||||||||||
| 46 | 0 | ▶ | type => $c_ref->{TYPE_NAME} || '*UNKNOWN*', | ||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$c_ref->{'TYPE_NAME'} || '*UNKNOWN*'
MC/DC0%0/1
$c_ref->{'TYPE_NAME'} || '*UNKNOWN*'
$c_ref->{'TYPE_NAME'}
Truth table0%0/2
A
A= $c_ref->{'TYPE_NAME'}
| |||||||||||||||||||||
| 47 | primary => $pri ? 1 : 0, | ||||||||||||||||||||
| 48 | ); | ||||||||||||||||||||
| 49 | } | ||||||||||||||||||||
| 50 | |||||||||||||||||||||
| 51 | 0 | my $fk_sth = | |||||||||||||||||||
| 52 | $dbh->foreign_key_info( $db->catalog, $db->schema, undef, | ||||||||||||||||||||
| 53 | $db->catalog, $db->schema, $tname ); | ||||||||||||||||||||
| 54 | |||||||||||||||||||||
| 55 | 0 | my @x; | |||||||||||||||||||
| 56 | 0 | ▶ | while ( my $fk_ref = $fk_sth->fetchrow_hashref ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 57 | 0 | ▶ | next unless defined $fk_ref->{PKCOLUMN_NAME}; # mysql? | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 58 | |||||||||||||||||||||
| 59 | 0 | ▶ | if ( $fk_ref->{KEY_SEQ} == 1 ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | 0 | ▶ | if (@x) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 61 | 0 | push( @raw_fk, [@x] ); | |||||||||||||||||||
| 62 | } | ||||||||||||||||||||
| 63 | @x = ( | ||||||||||||||||||||
| 64 | lc $tname, | ||||||||||||||||||||
| 65 | lc $fk_ref->{PKTABLE_NAME}, | ||||||||||||||||||||
| 66 | [ | ||||||||||||||||||||
| 67 | lc $fk_ref->{FKCOLUMN_NAME}, lc $fk_ref->{PKCOLUMN_NAME} | ||||||||||||||||||||
| 68 | 0 | ] | |||||||||||||||||||
| 69 | ); | ||||||||||||||||||||
| 70 | } | ||||||||||||||||||||
| 71 | else { | ||||||||||||||||||||
| 72 | push( | ||||||||||||||||||||
| 73 | @x, | ||||||||||||||||||||
| 74 | [ | ||||||||||||||||||||
| 75 | lc $fk_ref->{FKCOLUMN_NAME}, lc $fk_ref->{PKCOLUMN_NAME} | ||||||||||||||||||||
| 76 | 0 | ] | |||||||||||||||||||
| 77 | ); | ||||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | } | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | 0 | ▶ | if (@x) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 0 | push( @raw_fk, [@x] ); | |||||||||||||||||||
| 83 | } | ||||||||||||||||||||
| 84 | } | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | 0 | foreach my $fk (@raw_fk) { | |||||||||||||||||||
| 87 | my ($from) = | ||||||||||||||||||||
| 88 | 0 | grep { $_->name_lc eq $fk->[0] } $db->tables; | |||||||||||||||||||
| 89 | 0 | my ($to) = grep { $_->name_lc eq $fk->[1] } $db->tables; | |||||||||||||||||||
| 90 | 0 | shift @$fk; | |||||||||||||||||||
| 91 | 0 | shift @$fk; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | 0 | my @from; | |||||||||||||||||||
| 94 | my @to; | ||||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | 0 | foreach my $pair (@$fk) { | |||||||||||||||||||
| 97 | 0 | push( @from, grep { $_->name_lc eq $pair->[0] } $from->columns ); | |||||||||||||||||||
| 98 | 0 | push( @to, grep { $_->name_lc eq $pair->[1] } $to->columns ); | |||||||||||||||||||
| 99 | } | ||||||||||||||||||||
| 100 | |||||||||||||||||||||
| 101 | $from->add_foreign_key( | ||||||||||||||||||||
| 102 | 0 | to_table => $to, | |||||||||||||||||||
| 103 | columns => \@from, | ||||||||||||||||||||
| 104 | to_columns => \@to, | ||||||||||||||||||||
| 105 | ); | ||||||||||||||||||||
| 106 | |||||||||||||||||||||
| 107 | 0 | map { $columns{ $_->full_name_lc } = $_ } @from, @to; | |||||||||||||||||||
| 108 | map { | ||||||||||||||||||||
| 109 | 0 | $forward{ $to[$_]->full_name_lc }->{ $from[$_]->full_name_lc }++; | |||||||||||||||||||
| 110 | 0 | $backward{ $from[$_]->full_name_lc }->{ $to[$_]->full_name_lc }++; | |||||||||||||||||||
| 111 | } 0 .. ( ( scalar @from ) - 1 ); | ||||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 0 | my $chain = 1; | |||||||||||||||||||
| 115 | 0 | ▶ | while ( my $key = ( sort keys %forward, keys %backward )[0] ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 116 | 0 | chainer( $key, $chain++ ); | |||||||||||||||||||
| 117 | } | ||||||||||||||||||||
| 118 | |||||||||||||||||||||
| 119 | 0 | $db->chains( $chain - 1 ); | |||||||||||||||||||
| 120 | 0 | %columns = %forward = %backward = (); | |||||||||||||||||||
| 121 | 0 | return $db; | |||||||||||||||||||
| 122 | } | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | ▶ | sub chainer { | ||||||||||||||||||
|
Subroutine
chainer: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 125 | 0 | my $key = shift; | |||||||||||||||||||
| 126 | 0 | my $chain = shift; | |||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 0 | $columns{$key}->chain($chain); | |||||||||||||||||||
| 129 | |||||||||||||||||||||
| 130 | 0 | ▶ | if ( my $val = delete $forward{$key} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 131 | 0 | foreach my $new ( sort keys %$val ) { | |||||||||||||||||||
| 132 | 0 | chainer( $new, $chain ); | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | } | ||||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | ▶ | if ( my $val = delete $backward{$key} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 137 | 0 | foreach my $new ( sort keys %$val ) { | |||||||||||||||||||
| 138 | 0 | chainer( $new, $chain ); | |||||||||||||||||||
| 139 | } | ||||||||||||||||||||
| 140 | } | ||||||||||||||||||||
| 141 | } | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 1 | 1; | |||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | __END__ | ||||||||||||||||||||