| 1 | package DB::Introspector::MySQL::Introspector; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | 1 | ▶ | use base qw( DB::Introspector ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | |||||||||||||||||||||
| 7 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 8 | 0 | my $class = shift; | |||||||||||||||||||
| 9 | 0 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||
| 10 | |||||||||||||||||||||
| 11 | 0 | $self->{_db_tables} = {}; | |||||||||||||||||||
| 12 | 0 | $self->lookup_mysql_tables; | |||||||||||||||||||
| 13 | 0 | return $self; | |||||||||||||||||||
| 14 | } | ||||||||||||||||||||
| 15 | |||||||||||||||||||||
| 16 | 0 | ▶ | sub lookup_table { | ||||||||||||||||||
|
Subroutine
lookup_table: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 17 | 0 | my $self = shift; | |||||||||||||||||||
| 18 | 0 | my $table_name = shift; | |||||||||||||||||||
| 19 | |||||||||||||||||||||
| 20 | 0 | ▶ | return $self->{_db_tables}{$table_name} if( defined $table_name ); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 21 | } | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | 0 | ▶ | sub lookup_all_tables { | ||||||||||||||||||
|
Subroutine
lookup_all_tables: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||
| 24 | 0 | my $self = shift; | |||||||||||||||||||
| 25 | |||||||||||||||||||||
| 26 | 0 | return values %{$self->{_db_tables}} | |||||||||||||||||||
| 27 | } | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | 0 | ▶ | sub add_table { | ||||||||||||||||||
|
Subroutine
add_table: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||
| 30 | 0 | my $self = shift; | |||||||||||||||||||
| 31 | 0 | my $table = shift; | |||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | 0 | ▶ | $self->{_db_tables} ||= {}; | ||||||||||||||||||
|
Condition0%0/2
$self->{"_db_tables"} ||= {}
MC/DC0%0/1
$self->{"_db_tables"} ||= {}
$self->{"_db_tables"}
Truth table0%0/2
A
A= $self->{"_db_tables"}
| |||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | 0 | $self->{_db_tables}{$table->name} = $table; | |||||||||||||||||||
| 36 | } | ||||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 0 | ▶ | sub lookup_mysql_tables { | ||||||||||||||||||
|
Subroutine
lookup_mysql_tables: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||||||||
| 39 | 0 | my $self = shift; | |||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | |||||||||||||||||||||
| 42 | 0 | my $sth = $self->dbh->prepare('SHOW TABLE STATUS'); | |||||||||||||||||||
| 43 | 0 | $sth->execute(); | |||||||||||||||||||
| 44 | |||||||||||||||||||||
| 45 | 0 | ▶ | while( my $row = $sth->fetchrow_hashref('NAME_lc') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 46 | 0 | my $class = $self->_get_mysql_table_class_for_type($row->{type}); | |||||||||||||||||||
| 47 | 0 | my $table = $class->new($self, $row->{name}); | |||||||||||||||||||
| 48 | |||||||||||||||||||||
| 49 | 0 | my @table_info = split(';', $row->{comment}); | |||||||||||||||||||
| 50 | 0 | foreach my $create_option (@table_info) { | |||||||||||||||||||
| 51 | 0 | ▶ | if( $create_option =~ /\sREFER\s/ ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 0 | my ($foreign_key, $dependency) = | |||||||||||||||||||
| 53 | $self->_get_foreign_key_and_dependency_for_table_option( | ||||||||||||||||||||
| 54 | $table, | ||||||||||||||||||||
| 55 | $create_option); | ||||||||||||||||||||
| 56 | 0 | my $foreign_table = $self->lookup_table( | |||||||||||||||||||
| 57 | $foreign_key->foreign_table_name); | ||||||||||||||||||||
| 58 | 0 | ▶ | unless( defined $foreign_table ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 59 | 0 | $self->_save_dependency( | |||||||||||||||||||
| 60 | $foreign_key->foreign_table_name, $dependency); | ||||||||||||||||||||
| 61 | } else { | ||||||||||||||||||||
| 62 | 0 | $foreign_table->add_dependencies($dependency); | |||||||||||||||||||
| 63 | } | ||||||||||||||||||||
| 64 | |||||||||||||||||||||
| 65 | 0 | $table->add_foreign_keys($foreign_key); | |||||||||||||||||||
| 66 | } | ||||||||||||||||||||
| 67 | } | ||||||||||||||||||||
| 68 | $table->add_dependencies( | ||||||||||||||||||||
| 69 | 0 | $self->_flush_saved_foreign_key_dependencies($table->name)); | |||||||||||||||||||
| 70 | |||||||||||||||||||||
| 71 | 0 | $self->add_table($table); | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | 0 | $sth->finish; | |||||||||||||||||||
| 74 | } | ||||||||||||||||||||
| 75 | |||||||||||||||||||||
| 76 | # (column1,column2,...) REFER database/table_name(column1,column2,...) | ||||||||||||||||||||
| 77 | 0 | ▶ | sub _get_foreign_key_and_dependency_for_table_option { | ||||||||||||||||||
|
Subroutine
_get_foreign_key_and_dependency_for_table_option: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 78 | 0 | my $self = shift; | |||||||||||||||||||
| 79 | 0 | my $table = shift; | |||||||||||||||||||
| 80 | 0 | my $table_option = shift; | |||||||||||||||||||
| 81 | |||||||||||||||||||||
| 82 | 0 | ▶ | die("$table_option is not a valid reference") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | unless($table_option =~ /\sREFER\s/); | ||||||||||||||||||||
| 84 | |||||||||||||||||||||
| 85 | 0 | my ($local_side, $foreign_side) = split(/\sREFER\s/, $table_option); | |||||||||||||||||||
| 86 | 0 | $local_side =~ s/\s+//g; | |||||||||||||||||||
| 87 | 0 | $local_side =~ s/.*\((.*)\)/$1/g; | |||||||||||||||||||
| 88 | 0 | my @local_column_names = split(',', $local_side); | |||||||||||||||||||
| 89 | |||||||||||||||||||||
| 90 | 0 | $foreign_side =~ s/\s+//g; | |||||||||||||||||||
| 91 | 0 | my ($foreign_database, $foreign_table_name, $foreign_column_list) = | |||||||||||||||||||
| 92 | ($foreign_side =~ /(\w+)\/(\w+)\((.*)\).*/); | ||||||||||||||||||||
| 93 | |||||||||||||||||||||
| 94 | 0 | my @foreign_column_names = split(',', $foreign_column_list); | |||||||||||||||||||
| 95 | |||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | 0 | my $foreign_key = DB::Introspector::MySQL::ForeignKey->new($self, | |||||||||||||||||||
| 98 | $table, | ||||||||||||||||||||
| 99 | \@local_column_names, | ||||||||||||||||||||
| 100 | $foreign_table_name, | ||||||||||||||||||||
| 101 | \@foreign_column_names); | ||||||||||||||||||||
| 102 | 0 | my $dependency = $foreign_key->clone(); | |||||||||||||||||||
| 103 | 0 | $dependency->_set_is_dependency(1); | |||||||||||||||||||
| 104 | |||||||||||||||||||||
| 105 | 0 | return ($foreign_key, $dependency); | |||||||||||||||||||
| 106 | } | ||||||||||||||||||||
| 107 | |||||||||||||||||||||
| 108 | 0 | ▶ | sub _save_dependency { | ||||||||||||||||||
|
Subroutine
_save_dependency: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 109 | 0 | my $self = shift; | |||||||||||||||||||
| 110 | 0 | my $table_name = shift; | |||||||||||||||||||
| 111 | 0 | my $dependency = shift; | |||||||||||||||||||
| 112 | |||||||||||||||||||||
| 113 | 0 | ▶ | $self->{_saved_dependencies} ||= {}; | ||||||||||||||||||
|
Condition0%0/2
$self->{"_saved_dependencies"} ||= {}
MC/DC0%0/1
$self->{"_saved_dependencies"} ||= {}
$self->{"_saved_dependencies"}
Truth table0%0/2
A
A= $self->{"_saved_dependencies"}
| |||||||||||||||||||||
| 114 | 0 | ▶ | $self->{_saved_dependencies}{$table_name} ||= {}; | ||||||||||||||||||
|
Condition0%0/2
$self->{"_saved_dependencies"}{$table_name} ||= {}
MC/DC0%0/1
$self->{"_saved_dependencies"}{$table_name} ||= {}
$self->{"_saved_dependencies"}{$table_name}
Truth table0%0/2
A
A= $self->{"_saved_dependencies"}{$table_name}
| |||||||||||||||||||||
| 115 | |||||||||||||||||||||
| 116 | 0 | my $name = join(",",$dependency->local_column_names); | |||||||||||||||||||
| 117 | 0 | $self->{_saved_dependencies}{$table_name}{$name} = $dependency; | |||||||||||||||||||
| 118 | } | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | 0 | ▶ | sub _flush_saved_foreign_key_dependencies { | ||||||||||||||||||
|
Subroutine
_flush_saved_foreign_key_dependencies: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 121 | 0 | my $self = shift; | |||||||||||||||||||
| 122 | 0 | my $table_name = shift; | |||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 0 | return values %{$self->{_saved_dependencies}{$table_name}}; | |||||||||||||||||||
| 125 | } | ||||||||||||||||||||
| 126 | |||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 1 | ▶ | use constant CLASS_FOR_TYPE => { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 129 | 'innodb' => 'DB::Introspector::MySQL::InnoDBTable', | ||||||||||||||||||||
| 130 | 1 | }; | |||||||||||||||||||
| 131 | 1 | ▶ | use constant DEFAULT_CLASS => 'DB::Introspector::MySQL::Table'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 132 | 0 | ▶ | sub _get_mysql_table_class_for_type { | ||||||||||||||||||
|
Subroutine
_get_mysql_table_class_for_type: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 133 | 0 | my $self = shift; | |||||||||||||||||||
| 134 | 0 | my $type = lc(shift); | |||||||||||||||||||
| 135 | |||||||||||||||||||||
| 136 | 0 | ▶ | return CLASS_FOR_TYPE()->{$type} || DEFAULT_CLASS(); | ||||||||||||||||||
|
Condition0%0/2
{innodb => 'DB::Introspector::MySQL::InnoDBTable'}->{$type} || 'DB::Introspector::MySQL::Table'
MC/DC0%0/1
{innodb => 'DB::Introspector::MySQL::InnoDBTable'}->{$type} || 'DB::Introspector::MySQL::Table'
{innodb => 'DB::Introspector::MySQL::InnoDBTable'}->{$type}
Truth table0%0/2
A
A= {innodb => 'DB::Introspector::MySQL::InnoDBTable'}->{$type}
| |||||||||||||||||||||
| 137 | } | ||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | |||||||||||||||||||||
| 140 | |||||||||||||||||||||
| 141 | package DB::Introspector::MySQL::ForeignKey; | ||||||||||||||||||||
| 142 | |||||||||||||||||||||
| 143 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 144 | |||||||||||||||||||||
| 145 | 1 | ▶ | use base qw( DB::Introspector::Base::ForeignKey ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 146 | |||||||||||||||||||||
| 147 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 148 | 0 | my $class = shift; | |||||||||||||||||||
| 149 | 0 | my $introspector = shift; | |||||||||||||||||||
| 150 | 0 | my $self = $class->SUPER::new(shift()); | |||||||||||||||||||
| 151 | |||||||||||||||||||||
| 152 | 0 | $self->{_introspector} = $introspector; | |||||||||||||||||||
| 153 | 0 | $self->{_local_column_names} = shift; | |||||||||||||||||||
| 154 | 0 | $self->{_foreign_table_name} = shift; | |||||||||||||||||||
| 155 | 0 | $self->{_foreign_column_names} = shift; | |||||||||||||||||||
| 156 | 0 | $self; | |||||||||||||||||||
| 157 | } | ||||||||||||||||||||
| 158 | |||||||||||||||||||||
| 159 | 0 | ▶ | sub local_column_names { | ||||||||||||||||||
|
Subroutine
local_column_names: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 160 | 0 | my $self = shift; | |||||||||||||||||||
| 161 | 0 | return @{$self->{_local_column_names}}; | |||||||||||||||||||
| 162 | } | ||||||||||||||||||||
| 163 | |||||||||||||||||||||
| 164 | 0 | ▶ | sub foreign_column_names { | ||||||||||||||||||
|
Subroutine
foreign_column_names: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 165 | 0 | my $self = shift; | |||||||||||||||||||
| 166 | 0 | return @{$self->{_foreign_column_names}}; | |||||||||||||||||||
| 167 | } | ||||||||||||||||||||
| 168 | |||||||||||||||||||||
| 169 | 0 | ▶ | sub foreign_table_name { | ||||||||||||||||||
|
Subroutine
foreign_table_name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 170 | 0 | my $self = shift; | |||||||||||||||||||
| 171 | 0 | return $self->{_foreign_table_name}; | |||||||||||||||||||
| 172 | } | ||||||||||||||||||||
| 173 | |||||||||||||||||||||
| 174 | 0 | ▶ | sub _introspector { | ||||||||||||||||||
|
Subroutine
_introspector: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 175 | 0 | my $self = shift; | |||||||||||||||||||
| 176 | 0 | return $self->{_introspector}; | |||||||||||||||||||
| 177 | } | ||||||||||||||||||||
| 178 | |||||||||||||||||||||
| 179 | 0 | ▶ | sub foreign_table { | ||||||||||||||||||
|
Subroutine
foreign_table: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 180 | 0 | my $self = shift; | |||||||||||||||||||
| 181 | 0 | ▶ | unless( defined $self->{_foreign_table} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 182 | $self->{_foreign_table} = | ||||||||||||||||||||
| 183 | 0 | $self->_introspector->find_table($self->foreign_table_name); | |||||||||||||||||||
| 184 | } | ||||||||||||||||||||
| 185 | 0 | return $self->{_foreign_table}; | |||||||||||||||||||
| 186 | } | ||||||||||||||||||||
| 187 | |||||||||||||||||||||
| 188 | 0 | ▶ | sub _set_is_dependency { | ||||||||||||||||||
|
Subroutine
_set_is_dependency: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 189 | 0 | my $self = shift; | |||||||||||||||||||
| 190 | 0 | ▶ | if(@_) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 191 | 0 | $self->{_is_dependency} = shift; | |||||||||||||||||||
| 192 | } | ||||||||||||||||||||
| 193 | } | ||||||||||||||||||||
| 194 | |||||||||||||||||||||
| 195 | 0 | ▶ | sub clone { | ||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 196 | 0 | my $self = shift; | |||||||||||||||||||
| 197 | 0 | my %internal_data = %$self; | |||||||||||||||||||
| 198 | 0 | return bless(\%internal_data, ref($self)); | |||||||||||||||||||
| 199 | } | ||||||||||||||||||||
| 200 | |||||||||||||||||||||
| 201 | |||||||||||||||||||||
| 202 | |||||||||||||||||||||
| 203 | package DB::Introspector::MySQL::Table; | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 206 | 1 | ▶ | use base qw( DB::Introspector::Base::Table ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 209 | 0 | my $class = shift; | |||||||||||||||||||
| 210 | 0 | my $introspector = shift; | |||||||||||||||||||
| 211 | |||||||||||||||||||||
| 212 | 0 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||
| 213 | |||||||||||||||||||||
| 214 | 0 | $self->{_dependencies} = {}; | |||||||||||||||||||
| 215 | 0 | $self->{_foreign_keys} = {}; | |||||||||||||||||||
| 216 | 0 | $self->{primary_key} = []; | |||||||||||||||||||
| 217 | 0 | $self->{columns} = []; | |||||||||||||||||||
| 218 | 0 | $self->{introspector} = $introspector; | |||||||||||||||||||
| 219 | |||||||||||||||||||||
| 220 | 0 | $self; | |||||||||||||||||||
| 221 | } | ||||||||||||||||||||
| 222 | |||||||||||||||||||||
| 223 | 0 | ▶ | sub introspector { | ||||||||||||||||||
|
Subroutine
introspector: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 224 | 0 | my $self = shift; | |||||||||||||||||||
| 225 | 0 | return $self->{introspector}; | |||||||||||||||||||
| 226 | } | ||||||||||||||||||||
| 227 | |||||||||||||||||||||
| 228 | 0 | ▶ | sub add_dependencies { | ||||||||||||||||||
|
Subroutine
add_dependencies: not called CC=5 SCAR=34.0
| |||||||||||||||||||||
| 229 | 0 | my $self = shift; | |||||||||||||||||||
| 230 | 0 | foreach my $dependency (@_) { | |||||||||||||||||||
| 231 | 0 | ▶ | next unless defined $dependency; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 232 | 0 | ▶ | if(UNIVERSAL::isa($dependency, 'DB::Introspector::Base::ForeignKey')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 233 | 0 | ▶ | die("foreign_key is not a dependency") | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 234 | unless($dependency->is_dependency); | ||||||||||||||||||||
| 235 | 0 | my $id = join("|", $dependency->local_table->name, | |||||||||||||||||||
| 236 | $dependency->local_column_names); | ||||||||||||||||||||
| 237 | 0 | $self->{_dependencies}{$id} = $dependency; | |||||||||||||||||||
| 238 | } else { | ||||||||||||||||||||
| 239 | 0 | die("found ".ref($dependency) | |||||||||||||||||||
| 240 | ." instance, expected DB::Introspector::Base::ForeignKey"); | ||||||||||||||||||||
| 241 | } | ||||||||||||||||||||
| 242 | } | ||||||||||||||||||||
| 243 | } | ||||||||||||||||||||
| 244 | |||||||||||||||||||||
| 245 | 0 | ▶ | sub add_foreign_keys { | ||||||||||||||||||
|
Subroutine
add_foreign_keys: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 246 | 0 | my $self = shift; | |||||||||||||||||||
| 247 | 0 | foreach my $foreign_key (@_) { | |||||||||||||||||||
| 248 | 0 | ▶ | next unless defined $foreign_key; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 249 | 0 | ▶ | if(UNIVERSAL::isa($foreign_key, 'DB::Introspector::Base::ForeignKey')) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 250 | 0 | my $id = join("|", $foreign_key->local_column_names); | |||||||||||||||||||
| 251 | 0 | $self->{_foreign_keys}{$id} = $foreign_key; | |||||||||||||||||||
| 252 | } else { | ||||||||||||||||||||
| 253 | 0 | die("found ".ref($foreign_key) | |||||||||||||||||||
| 254 | ." instance, expected DB::Introspector::Base::ForeignKey"); | ||||||||||||||||||||
| 255 | } | ||||||||||||||||||||
| 256 | } | ||||||||||||||||||||
| 257 | } | ||||||||||||||||||||
| 258 | |||||||||||||||||||||
| 259 | 0 | ▶ | sub indexes { | ||||||||||||||||||
|
Subroutine
indexes: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 260 | 0 | my $self = shift; | |||||||||||||||||||
| 261 | 0 | ▶ | unless( $self->{_fetched_indexes} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 262 | 0 | $self->_fetch_indexes; | |||||||||||||||||||
| 263 | } | ||||||||||||||||||||
| 264 | 0 | return @{$self->{_indexes}}; | |||||||||||||||||||
| 265 | } | ||||||||||||||||||||
| 266 | |||||||||||||||||||||
| 267 | 0 | ▶ | sub columns { | ||||||||||||||||||
|
Subroutine
columns: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 268 | 0 | my $self = shift; | |||||||||||||||||||
| 269 | 0 | ▶ | unless( $self->{_fetched_columns} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 270 | 0 | $self->_fetch_columns; | |||||||||||||||||||
| 271 | } | ||||||||||||||||||||
| 272 | 0 | return @{$self->{columns}}; | |||||||||||||||||||
| 273 | } | ||||||||||||||||||||
| 274 | |||||||||||||||||||||
| 275 | 0 | ▶ | sub primary_key { | ||||||||||||||||||
|
Subroutine
primary_key: not called CC=2 SCAR=17.9
| |||||||||||||||||||||
| 276 | 0 | my $self = shift; | |||||||||||||||||||
| 277 | 0 | ▶ | unless( $self->{_fetched_columns} ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 278 | 0 | $self->_fetch_columns; | |||||||||||||||||||
| 279 | } | ||||||||||||||||||||
| 280 | 0 | return @{$self->{primary_key}}; | |||||||||||||||||||
| 281 | } | ||||||||||||||||||||
| 282 | |||||||||||||||||||||
| 283 | 1 | ▶ | use constant COLUMN_CALLBACKS => { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 284 | q(tinyint) => 'construct_integer_column', | ||||||||||||||||||||
| 285 | q(smallint) => 'construct_integer_column', | ||||||||||||||||||||
| 286 | q(mediumint) => 'construct_integer_column', | ||||||||||||||||||||
| 287 | q(int) => 'construct_integer_column', | ||||||||||||||||||||
| 288 | q(bigint) => 'construct_integer_column', | ||||||||||||||||||||
| 289 | q(datetime) => 'construct_datetime_column', | ||||||||||||||||||||
| 290 | q(date) => 'construct_datetime_column', | ||||||||||||||||||||
| 291 | q(timestamp) => 'construct_datetime_column', | ||||||||||||||||||||
| 292 | q(time) => 'construct_datetime_column', | ||||||||||||||||||||
| 293 | q(year) => 'construct_datetime_column', | ||||||||||||||||||||
| 294 | q(tinytext) => 'construct_clob_column', | ||||||||||||||||||||
| 295 | q(text) => 'construct_clob_column', | ||||||||||||||||||||
| 296 | q(mediumtext) => 'construct_clob_column', | ||||||||||||||||||||
| 297 | q(longtext) => 'construct_clob_column', | ||||||||||||||||||||
| 298 | q(long) => 'construct_string_column', | ||||||||||||||||||||
| 299 | q(varchar) => 'construct_string_column', | ||||||||||||||||||||
| 300 | q(char) => 'construct_char_column', | ||||||||||||||||||||
| 301 | 1 | }; | |||||||||||||||||||
| 302 | # TODO: add further support for time columns (year,timestamp,etc.) | ||||||||||||||||||||
| 303 | # TODO: add further support for blob columns (allthe texts) | ||||||||||||||||||||
| 304 | |||||||||||||||||||||
| 305 | 0 | ▶ | sub _fetch_columns { | ||||||||||||||||||
|
Subroutine
_fetch_columns: not called CC=4 SCAR=30.0
| |||||||||||||||||||||
| 306 | 0 | my $self = shift; | |||||||||||||||||||
| 307 | |||||||||||||||||||||
| 308 | 0 | my $sth = $self->introspector->dbh->prepare_cached( | |||||||||||||||||||
| 309 | 'SHOW COLUMNS FROM '.$self->name); | ||||||||||||||||||||
| 310 | 0 | $sth->execute(); | |||||||||||||||||||
| 311 | |||||||||||||||||||||
| 312 | 0 | ▶ | while( my $row = $sth->fetchrow_hashref('NAME_lc') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 313 | 0 | my ($type, $attr) = ($row->{type} =~ /(\w+)\((\d+)\)/); | |||||||||||||||||||
| 314 | 0 | $type = lc($type); | |||||||||||||||||||
| 315 | 0 | my $callback = COLUMN_CALLBACKS()->{$type}; | |||||||||||||||||||
| 316 | my $column = (defined $callback) | ||||||||||||||||||||
| 317 | ? DB::Introspector::MySQL::ColumnFactory | ||||||||||||||||||||
| 318 | ->$callback($row->{field},$type, $row->{null}, $attr) | ||||||||||||||||||||
| 319 | 0 | ▶ | : DB::Introspector::Base::SpecialColumn->new($row->{field}, $type); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 320 | 0 | push(@{$self->{columns}}, $column); | |||||||||||||||||||
| 321 | 0 | ▶ | push(@{$self->{primary_key}}, $column) if($row->{key} eq 'PRI'); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 322 | } | ||||||||||||||||||||
| 323 | |||||||||||||||||||||
| 324 | 0 | $self->{_fetched_columns} = 1; | |||||||||||||||||||
| 325 | 0 | $sth->finish; | |||||||||||||||||||
| 326 | } | ||||||||||||||||||||
| 327 | |||||||||||||||||||||
| 328 | 0 | ▶ | sub _fetch_indexes { | ||||||||||||||||||
|
Subroutine
_fetch_indexes: not called CC=3 SCAR=24.8
| |||||||||||||||||||||
| 329 | 0 | my $self = shift; | |||||||||||||||||||
| 330 | |||||||||||||||||||||
| 331 | 0 | my $sth = $self->introspector->dbh->prepare_cached( | |||||||||||||||||||
| 332 | 'SHOW INDEX FROM '.$self->name); | ||||||||||||||||||||
| 333 | 0 | $sth->execute(); | |||||||||||||||||||
| 334 | |||||||||||||||||||||
| 335 | 0 | my %indexes; | |||||||||||||||||||
| 336 | 0 | ▶ | while( my $row = $sth->fetchrow_hashref('NAME_lc') ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 337 | $indexes{key_name} ||= DB::Introspector::MySQL::Index->new($self, | ||||||||||||||||||||
| 338 | !$row->{non_unique}, | ||||||||||||||||||||
| 339 | 0 | ▶ | $row->{key_name}); | ||||||||||||||||||
|
Condition0%0/3
$indexes{"key_name"} ||= "DB::Introspector::MySQL::Index"->new($self, !$row->{"non_unique"}, $row->{"key_name"})
MC/DC0%0/2
$indexes{"key_name"} ||= "DB::Introspector::MySQL::Index"->new($self, !$row->{"non_unique"}, $row->{"key_name"})
$indexes{"key_name"}
"DB::Introspector::MySQL::Index"->new($self, !$row->{"non_unique"}, $row->{"key_name"})
Truth table0%0/3
A ||= B
A= $indexes{"key_name"}B= "DB::Introspector::MySQL::Index"->new($self, !$row->{"non_unique"}, $row->{"key_name"})
| |||||||||||||||||||||
| 340 | $indexes{key_name}->_set_column_name_for_index( | ||||||||||||||||||||
| 341 | 0 | $row->{column_name},$row->{seq_in_index}-1); | |||||||||||||||||||
| 342 | } | ||||||||||||||||||||
| 343 | |||||||||||||||||||||
| 344 | 0 | $self->{_indexes} = [values %indexes]; | |||||||||||||||||||
| 345 | 0 | $self->{_fetched_indexes} = 1; | |||||||||||||||||||
| 346 | 0 | $sth->finish; | |||||||||||||||||||
| 347 | } | ||||||||||||||||||||
| 348 | |||||||||||||||||||||
| 349 | |||||||||||||||||||||
| 350 | 0 | ▶ | sub foreign_keys { | ||||||||||||||||||
|
Subroutine
foreign_keys: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 351 | 0 | my $self = shift; | |||||||||||||||||||
| 352 | 0 | return values %{$self->{_foreign_keys}}; | |||||||||||||||||||
| 353 | } | ||||||||||||||||||||
| 354 | |||||||||||||||||||||
| 355 | 0 | ▶ | sub dependencies { | ||||||||||||||||||
|
Subroutine
dependencies: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 356 | 0 | my $self = shift; | |||||||||||||||||||
| 357 | 0 | return values %{$self->{_dependencies}}; | |||||||||||||||||||
| 358 | } | ||||||||||||||||||||
| 359 | |||||||||||||||||||||
| 360 | package DB::Introspector::MySQL::InnoDBTable; | ||||||||||||||||||||
| 361 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 362 | 1 | ▶ | use base qw( DB::Introspector::MySQL::Table ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 363 | |||||||||||||||||||||
| 364 | |||||||||||||||||||||
| 365 | package DB::Introspector::MySQL::ColumnFactory; | ||||||||||||||||||||
| 366 | |||||||||||||||||||||
| 367 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 368 | 1 | ▶ | use DB::Introspector::Base::BooleanColumn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 369 | 1 | ▶ | use DB::Introspector::Base::CLOBColumn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 370 | 1 | ▶ | use DB::Introspector::Base::CharColumn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 371 | 1 | ▶ | use DB::Introspector::Base::Column; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 372 | 1 | ▶ | use DB::Introspector::Base::DateTimeColumn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 373 | 1 | ▶ | use DB::Introspector::Base::IntegerColumn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 374 | 1 | ▶ | use DB::Introspector::Base::SpecialColumn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 375 | 1 | ▶ | use DB::Introspector::Base::StringColumn; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 376 | |||||||||||||||||||||
| 377 | 1 | ▶ | use constant INTEGER_MIN_MAX => { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 378 | 'tinyint' => [ '-128', '127' ], | ||||||||||||||||||||
| 379 | 'smallint' => [ '-32768', '32767' ], | ||||||||||||||||||||
| 380 | 'mediumint' => [ '-8388608', '8388607' ], | ||||||||||||||||||||
| 381 | 'int' => [ '-2147483648', '2147483647' ], | ||||||||||||||||||||
| 382 | 'bigint' => [ '-9223372036854775808', '9223372036854775807'] | ||||||||||||||||||||
| 383 | 1 | }; | |||||||||||||||||||
| 384 | 0 | ▶ | sub construct_integer_column { | ||||||||||||||||||
|
Subroutine
construct_integer_column: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 385 | 0 | my $class = shift; | |||||||||||||||||||
| 386 | 0 | my $name = shift; | |||||||||||||||||||
| 387 | 0 | my $type = shift; | |||||||||||||||||||
| 388 | 0 | my $nullable = shift; | |||||||||||||||||||
| 389 | 0 | my $default_padding_attr = shift; | |||||||||||||||||||
| 390 | |||||||||||||||||||||
| 391 | # normally I would calculate the min and max values for this int; however | ||||||||||||||||||||
| 392 | # there are some values that are too large to deal with so i am going to | ||||||||||||||||||||
| 393 | # hardcode this info | ||||||||||||||||||||
| 394 | 0 | my ($min,$max) = @{ INTEGER_MIN_MAX()->{$type} }; | |||||||||||||||||||
| 395 | 0 | my $column = new DB::Introspector::Base::IntegerColumn->new($name, $min, $max); | |||||||||||||||||||
| 396 | 0 | $column->nullable($nullable); | |||||||||||||||||||
| 397 | 0 | return $column; | |||||||||||||||||||
| 398 | } | ||||||||||||||||||||
| 399 | |||||||||||||||||||||
| 400 | 0 | ▶ | sub construct_datetime_column { | ||||||||||||||||||
|
Subroutine
construct_datetime_column: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 401 | 0 | my $class = shift; | |||||||||||||||||||
| 402 | 0 | my $name = shift; | |||||||||||||||||||
| 403 | 0 | my $type = shift; | |||||||||||||||||||
| 404 | 0 | my $nullable = shift; | |||||||||||||||||||
| 405 | 0 | my $default_padding_attr = shift; | |||||||||||||||||||
| 406 | |||||||||||||||||||||
| 407 | 0 | my $column = new DB::Introspector::Base::DateTimeColumn($name); | |||||||||||||||||||
| 408 | 0 | $column->nullable($nullable); | |||||||||||||||||||
| 409 | 0 | return $column; | |||||||||||||||||||
| 410 | } | ||||||||||||||||||||
| 411 | |||||||||||||||||||||
| 412 | |||||||||||||||||||||
| 413 | 0 | ▶ | sub construct_clob_column { | ||||||||||||||||||
|
Subroutine
construct_clob_column: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 414 | 0 | my $class = shift; | |||||||||||||||||||
| 415 | 0 | my $name = shift; | |||||||||||||||||||
| 416 | 0 | my $type = shift; | |||||||||||||||||||
| 417 | 0 | my $nullable = shift; | |||||||||||||||||||
| 418 | 0 | my $default_padding_attr = shift; | |||||||||||||||||||
| 419 | |||||||||||||||||||||
| 420 | 0 | my $column = new DB::Introspector::Base::CLOBColumn($name); | |||||||||||||||||||
| 421 | 0 | $column->nullable($nullable); | |||||||||||||||||||
| 422 | 0 | return $column; | |||||||||||||||||||
| 423 | } | ||||||||||||||||||||
| 424 | |||||||||||||||||||||
| 425 | 0 | ▶ | sub construct_string_column { | ||||||||||||||||||
|
Subroutine
construct_string_column: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 426 | 0 | my $class = shift; | |||||||||||||||||||
| 427 | 0 | my $name = shift; | |||||||||||||||||||
| 428 | 0 | my $type = shift; | |||||||||||||||||||
| 429 | 0 | my $nullable = shift; | |||||||||||||||||||
| 430 | 0 | my $max_width = shift; | |||||||||||||||||||
| 431 | |||||||||||||||||||||
| 432 | 0 | my $column = new DB::Introspector::Base::StringColumn($name, 0, $max_width); | |||||||||||||||||||
| 433 | 0 | $column->nullable($nullable); | |||||||||||||||||||
| 434 | 0 | return $column; | |||||||||||||||||||
| 435 | } | ||||||||||||||||||||
| 436 | |||||||||||||||||||||
| 437 | 0 | ▶ | sub construct_char_column { | ||||||||||||||||||
|
Subroutine
construct_char_column: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 438 | 0 | my $class = shift; | |||||||||||||||||||
| 439 | 0 | my $name = shift; | |||||||||||||||||||
| 440 | 0 | my $type = shift; | |||||||||||||||||||
| 441 | 0 | my $nullable = shift; | |||||||||||||||||||
| 442 | 0 | my $max_width = shift; | |||||||||||||||||||
| 443 | |||||||||||||||||||||
| 444 | 0 | my $column = new DB::Introspector::Base::CharColumn($name, 0, $max_width); | |||||||||||||||||||
| 445 | 0 | $column->nullable($nullable); | |||||||||||||||||||
| 446 | 0 | return $column; | |||||||||||||||||||
| 447 | } | ||||||||||||||||||||
| 448 | |||||||||||||||||||||
| 449 | package DB::Introspector::MySQL::Index; | ||||||||||||||||||||
| 450 | |||||||||||||||||||||
| 451 | 1 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 452 | 1 | ▶ | use base qw( DB::Introspector::Base::Index ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 453 | |||||||||||||||||||||
| 454 | |||||||||||||||||||||
| 455 | 0 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 456 | 0 | my $class = shift; | |||||||||||||||||||
| 457 | |||||||||||||||||||||
| 458 | 0 | my $self = $class->SUPER::new(@_); | |||||||||||||||||||
| 459 | 0 | $self->{_name} = pop(@_); | |||||||||||||||||||
| 460 | 0 | $self->{_column_names} = []; | |||||||||||||||||||
| 461 | 0 | $self; | |||||||||||||||||||
| 462 | } | ||||||||||||||||||||
| 463 | |||||||||||||||||||||
| 464 | 0 | ▶ | sub name { my $self = shift; return $self->{_name}; } | ||||||||||||||||||
|
Subroutine
name: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 465 | |||||||||||||||||||||
| 466 | 0 | ▶ | sub column_names { | ||||||||||||||||||
|
Subroutine
column_names: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 467 | 0 | my $self = shift; | |||||||||||||||||||
| 468 | 0 | return @{$self->{_column_names}}; | |||||||||||||||||||
| 469 | } | ||||||||||||||||||||
| 470 | |||||||||||||||||||||
| 471 | 0 | ▶ | sub _set_column_name_for_index { | ||||||||||||||||||
|
Subroutine
_set_column_name_for_index: not called CC=1 SCAR=6.9
| |||||||||||||||||||||
| 472 | 0 | my $self = shift; | |||||||||||||||||||
| 473 | 0 | my $column_name = shift; | |||||||||||||||||||
| 474 | 0 | my $index = shift; | |||||||||||||||||||
| 475 | 0 | $self->{_column_names}[$index] = $column_name; | |||||||||||||||||||
| 476 | } | ||||||||||||||||||||
| 477 | |||||||||||||||||||||
| 478 | 1 | 1; | |||||||||||||||||||