| 1 | package DBIO::MySQL::Introspect::Tables; | ||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Introspect MySQL/MariaDB tables and views | ||||||||||||||||||||||||||||
| 3 | |||||||||||||||||||||||||||||
| 4 | 42 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 42 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 42 | our $VERSION = '0.900007'; | |||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 42 | ▶ | use DBIO::MySQL::Identifier qw(quote_ident); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 0 | ▶ | sub fetch { | ||||||||||||||||||||||||||
|
Subroutine
fetch: not called CC=11 SCAR=48.8
Pod
documented
| |||||||||||||||||||||||||||||
| 14 | 0 | my ($class, $dbh) = @_; | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | 0 | my $sth = $dbh->prepare(q{ | |||||||||||||||||||||||||||
| 17 | SELECT | ||||||||||||||||||||||||||||
| 18 | table_name AS name, | ||||||||||||||||||||||||||||
| 19 | table_type AS table_type, | ||||||||||||||||||||||||||||
| 20 | engine AS engine, | ||||||||||||||||||||||||||||
| 21 | table_collation AS table_collation, | ||||||||||||||||||||||||||||
| 22 | row_format AS row_format, | ||||||||||||||||||||||||||||
| 23 | table_comment AS comment | ||||||||||||||||||||||||||||
| 24 | FROM information_schema.tables | ||||||||||||||||||||||||||||
| 25 | WHERE table_schema = DATABASE() | ||||||||||||||||||||||||||||
| 26 | ORDER BY table_name | ||||||||||||||||||||||||||||
| 27 | }); | ||||||||||||||||||||||||||||
| 28 | 0 | $sth->execute; | |||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | 0 | my %tables; | |||||||||||||||||||||||||||
| 31 | 0 | ▶ | while (my $row = $sth->fetchrow_hashref) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 32 | 0 | ▶ | my $kind = ($row->{table_type} // '') eq 'VIEW' ? 'view' : 'table'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$row->{"table_type"} // ""
MC/DC0%0/1
$row->{"table_type"} // ""
$row->{"table_type"}
Truth table0%0/2
A
A= $row->{"table_type"}
| |||||||||||||||||||||||||||||
| 33 | $tables{ $row->{name} } = { | ||||||||||||||||||||||||||||
| 34 | table_name => $row->{name}, | ||||||||||||||||||||||||||||
| 35 | kind => $kind, | ||||||||||||||||||||||||||||
| 36 | engine => $row->{engine}, | ||||||||||||||||||||||||||||
| 37 | table_collation => $row->{table_collation}, | ||||||||||||||||||||||||||||
| 38 | row_format => $row->{row_format}, | ||||||||||||||||||||||||||||
| 39 | comment => $row->{comment}, | ||||||||||||||||||||||||||||
| 40 | 0 | }; | |||||||||||||||||||||||||||
| 41 | } | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | # The definition of every view, without the name of this database in front | ||||||||||||||||||||||||||||
| 44 | # of the names the server qualified (see above). | ||||||||||||||||||||||||||||
| 45 | 0 | ▶ | if (grep { $_->{kind} eq 'view' } values %tables) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | 0 | my ($database) = $dbh->selectrow_array('SELECT DATABASE()'); | |||||||||||||||||||||||||||
| 47 | 0 | ▶ | my $qualifier = defined $database ? quote_ident($database) . '.' : ''; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 48 | 0 | my $views = $dbh->selectall_arrayref( | |||||||||||||||||||||||||||
| 49 | q{SELECT table_name, view_definition FROM information_schema.views | ||||||||||||||||||||||||||||
| 50 | WHERE table_schema = DATABASE()}); | ||||||||||||||||||||||||||||
| 51 | 0 | for my $view (@$views) { | |||||||||||||||||||||||||||
| 52 | 0 | my ($name, $def) = @$view; | |||||||||||||||||||||||||||
| 53 | 0 | ▶ | next unless $tables{$name} && $tables{$name}{kind} eq 'view'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$tables{$name} and $tables{$name}{"kind"} eq "view"
MC/DC0%0/2
$tables{$name} and $tables{$name}{"kind"} eq "view"
$tables{$name}
$tables{$name}{"kind"} eq "view"
Truth table0%0/3
A and B
A= $tables{$name}B= $tables{$name}{"kind"} eq "view"
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $def =~ s/\Q$qualifier\E//g if defined $def && length $qualifier; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
defined $def and length $qualifier
MC/DC0%0/2
defined $def and length $qualifier
defined $def
length $qualifier
Truth table0%0/3
A and B
A= defined $defB= length $qualifier
| |||||||||||||||||||||||||||||
| 55 | 0 | $tables{$name}{view_definition} = $def; | |||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||||||||
| 58 | 0 | return \%tables; | |||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 42 | 1; | |||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | __END__ | ||||||||||||||||||||||||||||