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