Loader.pm

filter: Statementstmt 100% 111 / 111 Branchbran 90.0% 27 / 30 Conditioncond 59.2% 16 / 27 MC/DCmcdc 7.1% 1 / 14 Subroutinesub 100% 19 / 19 Podpod 60.0% 3 / 5 totaltotal 85.9% 177 / 206 CC 32 SCAR 18.7
CC
32
Cov
92%
connect
15.0
_get_tables
6.0
make_schema_at
5.0
CRAP 6.5
« Loader/DBI/mysql.pm ↑ Summary Loader/DBI/Pg.pm »
1 package DBIx::Skinny::Schema::Loader;
2 17 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 17 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 17 our $VERSION = '0.25';
6  
7 17 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 17 ▶use DBI;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 17 ▶use DBIx::Skinny::Schema;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 13 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
12 13 my ($class, @args) = @_;
13 13 my $caller = caller;
14  
15 13 my @functions = qw(
16 make_schema_at
17 );
18  
19 13 for my $func ( @args ) {
20 3 ▶ if ( grep { $func } @functions ) {
Branch: 1 branch
BranchTrueFalseTotal
if (grep {$func;} @functions) 3 0 3
21 17 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
22 3 *{"$caller\::$func"} = \&$func;
23 }
24 }
25 }
26  
27 17 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
28 17 my ($class) = @_;
29 17 bless {}, $class;
30 }
31  
32 15 ▶sub supported_drivers {
Subroutine supported_drivers: called CC=1 SCAR=0.0
Pod undocumented
33 15 qw(
34 SQLite
35 mysql
36 Pg
37 );
38 }
39  
40 18 ▶sub connect {
Subroutine connect: called CC=14 SCAR=27.1
Pod documented
41 18 my $self = shift;
42 18 ▶ return if defined $self->{ impl };
Branch: 1 branch
BranchTrueFalseTotal
if defined $self->{"impl"} 1 17 18
43  
44 17 my $opts;
45 17 ▶ if (@_ == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ == 1) { } 2 15 17
46 $opts = +{
47 dsn => $_[0]->{dsn} || '',
48 user => $_[0]->{username} || '',
49 pass => $_[0]->{password} || '',
50 connect_options => $_[0]->{connect_options} || {},
51 2 ▶ };
Condition50%4/8
$_[0]{'dsn'} || ''
l!l
20
$_[0]{'username'} || ''
l!l
02
$_[0]{'password'} || ''
l!l
02
$_[0]{'connect_options'} || {}
l!l
02
MC/DC0%0/1
$_[0]{'dsn'} || ''
$_[0]{'dsn'}
MC/DC0%0/1
$_[0]{'username'} || ''
$_[0]{'username'}
MC/DC0%0/1
$_[0]{'password'} || ''
$_[0]{'password'}
MC/DC0%0/1
$_[0]{'connect_options'} || {}
$_[0]{'connect_options'}
Truth table50%1/2
A
A= $_[0]{'dsn'}
Aresult
11
00
Truth table50%1/2
A
A= $_[0]{'username'}
Aresult
11
00
Truth table50%1/2
A
A= $_[0]{'password'}
Aresult
11
00
Truth table50%1/2
A
A= $_[0]{'connect_options'}
Aresult
11
00
52 } else {
53 15 my ($dsn, $user, $pass, $connect_options) = @_;
54 15 ▶ $opts = {
Condition62%5/8
$dsn || ''
l!l
132
$user || ''
l!l
015
$pass || ''
l!l
015
$connect_options || {}
l!l
015
MC/DC100%1/1
$dsn || ''
$dsn
MC/DC0%0/1
$user || ''
$user
MC/DC0%0/1
$pass || ''
$pass
MC/DC0%0/1
$connect_options || {}
$connect_options
Truth table100%2/2
A
A= $dsn
Aresult
11
00
Truth table50%1/2
A
A= $user
Aresult
11
00
Truth table50%1/2
A
A= $pass
Aresult
11
00
Truth table50%1/2
A
A= $connect_options
Aresult
11
00
55 dsn => $dsn || '',
56 user => $user || '',
57 pass => $pass || '',
58 connect_options => $connect_options || {},
59 };
60 }
61 17 $opts->{dsn} =~ /^dbi:([^:]+):/i;
62 17 ▶ my $driver = $1 or croak "Could not parse DSN";
Branch: 1 branch
BranchTrueFalseTotal
my $driver = $1 or croak("Could not parse DSN") 15 2 17
63 croak "$driver is not supported by DBIx::Skinny::Schema::Loader yet"
64 15 ▶ unless grep { /^$driver$/ } $self->supported_drivers;
Branch: 1 branch
BranchTrueFalseTotal
unless grep {/^$driver$/;} $self->supported_drivers 1 14 15
65 14 my $impl = __PACKAGE__ . "::DBI::$driver";
66 14 ▶ eval "use $impl"; ## no critic
Subroutine BEGIN: called CC=1 SCAR=0.0
Subroutine BEGIN: called CC=1 SCAR=0.0
67 14 ▶ die $@ if $@;
Branch: 1 branch
BranchTrueFalseTotal
if $@ 0 14 14
68 14 $self->{ impl } = $impl->new($opts);
69 }
70  
71 6 ▶sub load_schema {
Subroutine load_schema: called CC=4 SCAR=13.9
Pod documented
72 6 my ($class, $connect_info) = @_;
73 6 my $self = $class->new;
74 6 ▶ $connect_info ||= $class->_get_skinny_connect_info;
Condition66%2/3
$connect_info ||= $class->_get_skinny_connect_info
l!l&&r!l&&!r
240
MC/DC0%0/2
$connect_info ||= $class->_get_skinny_connect_info
$connect_info $class->_get_skinny_connect_info
Truth table66%2/3
A ||= B
A= $connect_infoB= $class->_get_skinny_connect_info
ABresult
1X1
011
000
75 $self->connect(
76 $connect_info->{ dsn },
77 $connect_info->{ username },
78 $connect_info->{ password },
79 $connect_info->{ connect_options },
80 6 );
81  
82 6 my $schema = $class->schema_info;
83 6 for my $table ( @{ $self->{ impl }->tables } ) {
84 36 my $pk = $self->{ impl }->table_pk($table);
85 36 ▶ $schema->{ $table }->{ pk } = $pk if $pk;
Branch: 1 branch
BranchTrueFalseTotal
if $pk 36 0 36
86 36 $schema->{ $table }->{ columns } = $self->{ impl }->table_columns($table);
87 36 $schema->{ $table }->{ row_class } = DBIx::Skinny::Util::mk_row_class($class, $table);
88 }
89 6 return $self;
90 }
91  
92 5 ▶sub _get_skinny_connect_info {
Subroutine _get_skinny_connect_info: called CC=2 SCAR=7.0
93 5 my $class = shift;
94 5 ▶ $class = ref $class || $class;
Condition66%2/3
ref $class || $class
l!l&&r!l&&!r
140
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table66%2/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
95 5 (my $skinny_class = $class) =~ s/::Schema//;
96 5 return $skinny_class->connect_info;
97 }
98  
99 6 ▶sub make_schema_at {
Subroutine make_schema_at: called CC=5 SCAR=16.1
Pod documented
100 6 ▶ my $self = (ref $_[0] eq 'DBIx::Skinny::Schema::Loader') ? shift : __PACKAGE__->new;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] eq 'DBIx::Skinny::Schema::Loader' ? : 2 4 6
101 6 my ($schema_class, $options, $connect_info) = @_;
102  
103 6 ▶ $self->connect(ref $connect_info eq 'HASH' ? $connect_info : @{ $connect_info });
Branch: 1 branch
BranchTrueFalseTotal
ref $connect_info eq 'HASH' ? : 1 5 6
104  
105 5 my $schema = $self->_insert_header;
106 5 $schema .= "package $schema_class;\nuse DBIx::Skinny::Schema;\n\n";
107  
108 5 $schema .= $self->_insert_template($options->{ before_template });
109 5 $schema .= $self->_insert_template($options->{ template });
110  
111 5 for my $table ( @{ $self->_get_tables($options->{ ignore_rules }) } ) {
112 27 my $pk = $self->{ impl }->table_pk($table);
113 $schema .= $self->_make_install_table_text(
114 {
115 table => $table,
116 pk => ref $pk ? $pk : [ $pk ],
117 columns => $self->{ impl }->table_columns($table),
118 },
119 $options->{ table_template }
120 27 ▶ );
Branch: 1 branch
BranchTrueFalseTotal
ref $pk ? : 17 10 27
121 }
122  
123 5 $schema .= $self->_insert_template($options->{ after_template });
124  
125 5 $schema .= "1;";
126 5 return $schema;
127 }
128  
129 5 ▶sub _insert_header {
Subroutine _insert_header: called CC=1 SCAR=0.0
130 5 "# THIS FILE IS AUTOGENERATED BY DBIx::Skinny::Schema::Loader $VERSION, DO NOT EDIT DIRECTLY.\n\n";
131 }
132  
133 15 ▶sub _insert_template {
Subroutine _insert_template: called CC=2 SCAR=6.9
134 15 my ($self, $template) = @_;
135 15 ▶ return '' unless $template;
Branch: 1 branch
BranchTrueFalseTotal
unless $template 12 3 15
136 3 chomp $template;
137  
138 3 "# ---- beginning of custom template ----\n" .
139 $template . "\n" .
140 "# ---- end of custom template ----\n\n";
141 }
142  
143 27 ▶sub _make_install_table_text {
Subroutine _make_install_table_text: called CC=3 SCAR=11.0
144 27 my ($self, $params, $template) = @_;
145 27 my $table = $params->{ table };
146 27 my $pk = join " ", @{ $params->{ pk } };
147 27 my $columns = join " ", @{ $params->{ columns } };
148 27 ▶ unless ($template) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($template) 21 6 27
149 21 $template = "install_table [% table %] => schema {\n";
150 21 ▶ $template .= " pk qw/[% pk %]/;\n" if $pk;
Branch: 1 branch
BranchTrueFalseTotal
if $pk 12 9 21
151 21 $template .= " columns qw/[% columns %]/;\n};\n\n";
152 }
153  
154 27 $template =~ s/\[% table %\]/$table/g;
155 27 $template =~ s/\[% pk %\]/$pk/g;
156 27 $template =~ s/\[% columns %\]/$columns/g;
157 27 return $template;
158 }
159  
160 5 ▶sub _get_tables {
Subroutine _get_tables: called CC=6 SCAR=18.0
161 5 my ($self, $ignore_rules) = @_;
162 5 my @tables;
163 5 for my $table ( @{ $self->{ impl }->tables } ) {
164 30 my $ignore;
165 30 for my $rule ( @$ignore_rules ) {
166 12 ▶ $ignore++ and last if $table =~ $rule;
Branch: 1 branch
BranchTrueFalseTotal
if $table =~ /$rule/ 3 9 12
Condition60%3/5
$table =~ /$rule/ && ($ignore++ && (last))
!ll&&!rl&&r
930
$ignore++ and last
!ll
30
MC/DC0%0/2
$table =~ /$rule/ && ($ignore++ && (last))
$table =~ /$rule/ $ignore++
Truth table66%2/3
A && B
A= $table =~ /$rule/B= $ignore++
ABresult
0X0
100
111
167 }
168 30 ▶ push @tables, $table unless $ignore;
Branch: 1 branch
BranchTrueFalseTotal
unless $ignore 27 3 30
169 }
170 5 return \@tables;
171 }
172  
173 17 1;
174 __END__
« Loader/DBI/mysql.pm ↑ Summary Loader/DBI/Pg.pm »