blib/lib/Otogiri/Plugin/TableInfo.pm

filter: Statementstmt 82.4% 47 / 57 Branchbran 50.0% 9 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 100% 4 / 4 totaltotal 78.8% 71 / 90 CC 10 SCAR 14.9
CC
10
Cov
75%
show_create_view
8.1
show_create_table
7.3
show_tables
2.0
CRAP 4.5
« blib/lib/Otogiri/Plugin/TableInfo/Pg.pm ↑ Summary Build »
1 package Otogiri::Plugin::TableInfo;
2 2 ▶use 5.008005;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Otogiri;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Otogiri::Plugin;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use DBIx::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Otogiri::Plugin::TableInfo::Pg;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 our $VERSION = "0.02";
12  
13 2 our @EXPORT = qw(show_tables show_create_table show_create_view desc);
14  
15 sub show_tables {
16 2 ▶ my ($self, $like_regex) = @_;
Subroutine show_tables: called CC=2 SCAR=6.9
Pod documented
17 2 my $inspector = DBIx::Inspector->new(dbh => $self->dbh);
18 2 my @tables = $inspector->tables;
19 2 my @result = map { $_->name } $inspector->tables;
20 2 ▶ @result = grep { $_ =~ /$like_regex/ } @result if ( defined $like_regex );
Branch: 1 branch
BranchTrueFalseTotal
if defined $like_regex 1 1 2
21 2 return @result;
22 }
23  
24 sub show_create_table {
25 3 ▶ my ($self, $table_name) = @_;
Subroutine show_create_table: called CC=5 SCAR=19.8
Pod documented
26 3 my $inspector = DBIx::Inspector->new(dbh => $self->dbh);
27 3 my $table = $inspector->table($table_name);
28  
29 3 ▶ return if ( !defined $table );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 1 2 3
30  
31 2 my $driver_name = $self->{dsn}->{driver};
32  
33 2 ▶ if ( $driver_name eq 'mysql' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($driver_name eq 'mysql') { } 0 2 2
elsif ($driver_name eq 'SQLite') { } 2 0 2
elsif ($driver_name eq 'Pg') { } 0 0 0
34 0 my ($row) = $self->search_by_sql("SHOW CREATE TABLE $table_name");
35 0 return $row->{'Create Table'};
36 }
37 elsif ( $driver_name eq 'SQLite' ) {
38 2 return $table->{SQLITE_SQL};
39 }
40 elsif ( $driver_name eq 'Pg' ) {
41 0 my $pg = Otogiri::Plugin::TableInfo::Pg->new($self);
42 0 return $pg->show_create_table($table_name);
43 }
44 0 return;
45 }
46  
47 sub show_create_view {
48 1 ▶ my ($self, $view_name) = @_;
Subroutine show_create_view: called CC=5 SCAR=20.9
Pod documented
49 1 my $inspector = DBIx::Inspector->new(dbh => $self->dbh);
50 1 my $view = $inspector->view($view_name);
51  
52 1 ▶ return if ( !defined $view );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $view 0 1 1
53  
54 1 my $driver_name = $self->{dsn}->{driver};
55  
56 1 ▶ if ( $driver_name eq 'mysql' ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($driver_name eq 'mysql') { } 0 1 1
elsif ($driver_name eq 'SQLite') { } 1 0 1
elsif ($driver_name eq 'Pg') { } 0 0 0
57 0 my ($row) = $self->search_by_sql("SHOW CREATE VIEW $view_name");
58 0 return $row->{'Create View'};
59 }
60 elsif ( $driver_name eq 'SQLite' ) {
61 1 return $view->{SQLITE_SQL};
62 }
63 elsif ( $driver_name eq 'Pg' ) {
64 0 my $pg = Otogiri::Plugin::TableInfo::Pg->new($self);
65 0 return $pg->show_create_view($view_name);
66 }
67 0 return;
68 }
69  
70 sub desc {
71 2 ▶ my ($self, $table_name) = @_;
Subroutine desc: called CC=1 SCAR=0.0
Pod documented
72 2 $self->show_create_table($table_name);
73 }
74  
75  
76  
77 2 1;
78 __END__
79  
80 =encoding utf-8
81  
82 =for stopwords desc
83  
84 =head1 NAME
85  
86 Otogiri::Plugin::TableInfo - retrieve table information from database
87  
88 =head1 SYNOPSIS
89  
90 use Otogiri::Plugin::TableInfo;
91 my $db = Otogiri->new( connect_info => [ ... ] );
92 $db->load_plugin('TableInfo');
93 my @table_names = $db->show_tables();
94  
95  
96 =head1 DESCRIPTION
97  
98 Otogiri::Plugin::TableInfo is Otogiri plugin to fetch table information from database.
99  
100 =head1 METHODS
101  
102 =head2 my @table_names = $self->show_tables([$like_regex]);
103  
104 returns table names in database.
105  
106 parameter C<$like_regex> is optional. If it is passed, table name is filtered by regex like MySQL's C<SHOW TABLES LIKE ...> statement.
107  
108 my @table_names = $db->show_tables(qr/^user_/); # return table names that starts with 'user_'
109  
110 If C<$like_regex> is not passed, all table_names in current database are returned.
111  
112 =head2 my $create_table_ddl = $self->desc($table_name);
113  
114 =head2 my $create_table_ddl = $self->show_create_table($table_name);
115  
116 returns create table statement like MySQL's 'show create table'.
117  
118  
119 =head2 my $create_view_sql = $self->show_create_view($view_name);
120  
121 returns create view SQL like MySQL's 'show create view'.
122  
123 =head1 LICENSE
124  
125 Copyright (C) Takuya Tsuchida.
126  
127 This library is free software; you can redistribute it and/or modify
128 it under the same terms as Perl itself.
129  
130 =head1 AUTHOR
131  
132 Takuya Tsuchida E<lt>tsucchi@cpan.orgE<gt>
133  
134 =cut
135  
« blib/lib/Otogiri/Plugin/TableInfo/Pg.pm ↑ Summary Build »