blib/lib/Otogiri/Plugin/TableInfo/Pg.pm

filter: Statementstmt 17.5% 21 / 120 Branchbran 0.0% 0 / 24 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 33.3% 7 / 21 Podpod 0.0% 0 / 3 totaltotal 16.1% 28 / 173 CC 20 SCAR 24.9
CC
20
Cov
14%
_build_column_defs
30.0
_build_fk_defs
30.0
_build_create_sequence_defs
12.0
CRAP 12.1
↑ Summary blib/lib/Otogiri/Plugin/TableInfo.pm »
1 package Otogiri::Plugin::TableInfo::Pg;
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 2 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use DBIx::Inspector;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use List::MoreUtils qw(any);
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Otogiri::Plugin::TableInfo::PgKeywords;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 sub new {
11 0 ▶ my ($class, $table_info) = @_;
Subroutine new: not called CC=1 SCAR=6.9
Pod undocumented
12 0 my $keywords = Otogiri::Plugin::TableInfo::PgKeywords->new();
13 0 my $self = {
14 table_info => $table_info,
15 keywords => $keywords,
16 };
17 0 bless $self, $class;
18 }
19  
20  
21 sub show_create_view {
22 0 ▶ my ($self, $view_name) = @_;
Subroutine show_create_view: not called CC=1 SCAR=6.9
Pod undocumented
23 0 my ($row) = $self->{table_info}->search_by_sql('SELECT definition FROM pg_views WHERE viewname = ?', [$view_name]);
24 0 return $row->{definition};
25 }
26  
27 sub show_create_table {
28 0 ▶ my ($self, $table_name) = @_;
Subroutine show_create_table: not called CC=2 SCAR=17.9
Pod undocumented
29 0 my $inspector = DBIx::Inspector->new(dbh => $self->{table_info}->dbh);
30 0 my $table = $inspector->table($table_name);
31  
32 0 ▶ return if ( !defined $table );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 0 0
33  
34 0 my @indexes = $self->{table_info}->select('pg_indexes', { tablename => $table->name }, { order_by => 'indexname' });
35 0 my $result = "CREATE TABLE " . $table->name . " (\n";
36 0 $result .= $self->_build_column_defs($table);
37 0 $result .= ");\n";
38 0 $result .= $self->_build_sequence_defs($table);
39 0 $result .= $self->_build_pk_defs($table);
40 0 $result .= $self->_build_uk_defs($table, @indexes);
41 0 $result .= $self->_build_index_defs($table, @indexes);
42 0 $result .= $self->_build_fk_defs($table);
43 0 return $result;
44 }
45  
46 sub _build_column_defs {
47 0 ▶ my ($self, $table) = @_;
Subroutine _build_column_defs: not called CC=5 SCAR=34.0
48 0 my $result = "";
49 0 for my $column ( $table->columns() ) {
50 0 my $column_name = $self->{keywords}->quote($column->name); #quote column name if it is need.
51 0 $result .= " " . $column_name . " " . $column->{PG_TYPE};
52 0 ▶ $result .= " DEFAULT " . $column->column_def if ( defined $column->column_def && !$self->_is_sequence_column($column) );
Branch: 1 branch
BranchTrueFalseTotal
if defined $column->column_def and not $self->_is_sequence_column($column) 0 0 0
Condition0%0/3
defined $column->column_def and not $self->_is_sequence_column($column)
!ll&&!rl&&r
000
MC/DC0%0/2
defined $column->column_def and not $self->_is_sequence_column($column)
defined $column->column_def not $self->_is_sequence_column($column)
Truth table0%0/3
A and not(B)
A= defined $column->column_defB= not $self->_is_sequence_column($column)
ABresult
0X0
100
111
53 0 ▶ $result .= " NOT NULL" if ( !$column->nullable );
Branch: 1 branch
BranchTrueFalseTotal
unless $column->nullable 0 0 0
54 0 $result .= ",\n";
55 }
56 0 $result =~ s/,\n\z/\n/;
57 0 return $result;
58 }
59  
60 sub _build_sequence_defs {
61 0 ▶ my ($self, $table) = @_;
Subroutine _build_sequence_defs: not called CC=2 SCAR=17.9
62 0 my $result = "";
63 0 my @sequence_columns = grep { $self->_is_sequence_column($_) } $table->columns();
64 0 for my $column ( @sequence_columns ) {
65 0 my $sequence_name = $self->_parse_sequence_name($column);
66 0 $result .= $self->_build_create_sequence_defs($sequence_name);
67 0 $result .= "ALTER SEQUENCE " . $sequence_name . " OWNED BY " . $table->name . "." . $column->name . ";\n";
68 0 $result .= "ALTER TABLE ONLY " . $table->name . " ALTER COLUMN " . $column->name . " SET DEFAULT " . $column->column_def . ";\n";
69 }
70 0 return $result;
71 }
72  
73 sub _parse_sequence_name {
74 0 ▶ my ($self, $column) = @_;
Subroutine _parse_sequence_name: not called CC=2 SCAR=17.9
75 0 ▶ if ( $column->column_def =~ qr/^nextval\('([^']+)'::regclass\)/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($column->column_def =~ qr/^nextval\('([^']+)'::regclass\)/) 0 0 0
76 0 return $1;
77 }
78 0 return;
79 }
80  
81 sub _build_create_sequence_defs {
82 0 ▶ my ($self, $sequence_name) = @_;
Subroutine _build_create_sequence_defs: not called CC=3 SCAR=24.8
83 0 my ($row) = $self->{table_info}->select($sequence_name);
84 0 my $result = "CREATE SEQUENCE $sequence_name\n";
85 0 $result .= " START WITH " . $row->{start_value} . "\n";
86 0 $result .= " INCREMENT BY " . $row->{increment_by} . "\n";
87 0 ▶ if ( $row->{min_value} eq '1' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{'min_value'} eq '1') { } 0 0 0
88 0 $result .= " NO MINVALUE\n";
89 }
90 else {
91 0 $result .= " MINVALUE " . $row->{min_value} . "\n";
92 }
93  
94 0 ▶ if ( $row->{max_value} eq '9223372036854775807' ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($row->{'max_value'} eq '9223372036854775807') { } 0 0 0
95 0 $result .= " NO MAXVALUE\n";
96 }
97 else {
98 0 $result .= " MAXVALUE " . $row->{max_value} . "\n";
99 }
100  
101 0 $result .= " CACHE " . $row->{cache_value} . ";\n";
102 0 return $result;
103 }
104  
105 sub _is_sequence_column {
106 0 ▶ my ($self, $column) = @_;
Subroutine _is_sequence_column: not called CC=2 SCAR=17.9
107 0 my $default_value = $column->column_def;
108 0 ▶ return if ( !defined $default_value );
Branch: 1 branch
BranchTrueFalseTotal
unless defined $default_value 0 0 0
109 0 return $default_value =~ qr/^nextval\(/;
110 }
111  
112 sub _build_pk_defs {
113 0 ▶ my ($self, $table) = @_;
Subroutine _build_pk_defs: not called CC=2 SCAR=17.9
114 0 my $result = "";
115 0 for my $column ( $table->primary_key() ) {
116 0 $result .= "ALTER TABLE ONLY " . $table->name . "\n";
117 0 $result .= " ADD CONSTRAINT " . $column->{PG_COLUMN} . " PRIMARY KEY (" . $column->name . ");\n";
118 }
119 0 return $result;
120 }
121  
122 sub _build_index_defs {
123 0 ▶ my ($self, $table, @indexes) = @_;
Subroutine _build_index_defs: not called CC=3 SCAR=24.8
124  
125 0 my @rows = grep { $_->{indexdef} !~ qr/\ACREATE UNIQUE INDEX/ } @indexes;
126  
127 0 my $result = '';
128 0 for my $row ( @rows ) {
129 0 ▶ next if ( $self->_is_pk($table, $row->{indexname}) );
Branch: 1 branch
BranchTrueFalseTotal
if $self->_is_pk($table, $row->{"indexname"}) 0 0 0
130 0 $result .= $row->{indexdef} . ";\n";
131 }
132 0 return $result;
133 }
134  
135 sub _is_pk {
136 0 ▶ my ($self, $table, $column_name) = @_;
Subroutine _is_pk: not called CC=1 SCAR=6.9
137 0 ▶ return any { $_->{PK_NAME} eq $column_name } $table->primary_key();
Subroutine __ANON__: not called CC=1 SCAR=6.9
138 }
139  
140 sub _build_uk_defs {
141 0 ▶ my ($self, $table, @indexes) = @_;
Subroutine _build_uk_defs: not called CC=3 SCAR=24.8
142 my @unique_indexes = grep {
143 0 $_->{indexdef} =~ qr/\ACREATE UNIQUE INDEX/ && !$self->_is_pk($table, $_->{indexname})
144 0 ▶ } @indexes;
Branch: 1 branch
BranchTrueFalseTotal
$_->{"indexdef"} =~ qr/\ACREATE UNIQUE INDEX/ and not $self->_is_pk($table, $_->{"indexname"}) 0 0 0
145 0 my $result = '';
146 0 for my $indexdef ( map{ $_->{indexdef} } @unique_indexes ) {
147 0 $result .= $indexdef . ";\n";
148 }
149 0 return $result;
150 }
151  
152 sub _build_fk_defs {
153 0 ▶ my ($self, $table) = @_;
Subroutine _build_fk_defs: not called CC=5 SCAR=34.0
154 0 my $result = '';
155 # UPDATE_RULE and DELETE_RULE are described in http://search.cpan.org/dist/DBI/DBI.pm#foreign_key_info
156 0 my %rule = (
157 0 => 'CASCADE',
158 1 => 'RESTRICT',
159 2 => 'SET NULL',
160 #3 => 'NO ACTION', # If NO ACTION, ON UPDATE/DELETE statament is not exist.
161 4 => 'SET DEFAULT',
162 );
163  
164 0 for my $fk_info ( $table->fk_foreign_keys() ) {
165 0 $result .= "ALTER TABLE ONLY " . $table->name . "\n";
166 0 $result .= " ADD CONSTRAINT " . $fk_info->fk_name . " FOREIGN KEY (" . $fk_info->fkcolumn_name . ")";
167 0 $result .= " REFERENCES " . $fk_info->pktable_name . "(" . $fk_info->pkcolumn_name . ")";
168 0 ▶ $result .= " ON UPDATE " . $rule{$fk_info->{UPDATE_RULE}} if ( exists $rule{$fk_info->{UPDATE_RULE}} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule{$fk_info->{"UPDATE_RULE"}} 0 0 0
169 0 ▶ $result .= " ON DELETE " . $rule{$fk_info->{DELETE_RULE}} if ( exists $rule{$fk_info->{DELETE_RULE}} );
Branch: 1 branch
BranchTrueFalseTotal
if exists $rule{$fk_info->{"DELETE_RULE"}} 0 0 0
170 0 ▶ $result .= " DEFERRABLE" if ( $fk_info->deferability ne '7' );
Branch: 1 branch
BranchTrueFalseTotal
if $fk_info->deferability ne 7 0 0 0
171 0 $result .= ";\n";
172 }
173 0 return $result;
174 }
175  
176  
177  
178 2 1;
179 __END__
180  
181 =encoding utf-8
182  
183 =for stopwords PostgreSQL
184  
185 =head1 NAME
186  
187 Otogiri::Plugin::TableInfo::Pg - build CREATE TABLE statement for PostgreSQL
188  
189 =head1 SYNOPSIS
190  
191 use Otogiri::Plugin::TableInfo;
192 my $db = Otogiri->new( connect_info => [ ... ] );
193 $db->load_plugin('TableInfo');
194 my @table_names = $db->show_tables();
195  
196  
197 =head1 DESCRIPTION
198  
199 Otogiri::Plugin::TableInfo is Otogiri plugin to fetch table information from database.
200 =cut
201  
↑ Summary blib/lib/Otogiri/Plugin/TableInfo.pm »