Model/Schema/SQL.pm

filter: Statementstmt 53.1% 68 / 128 Branchbran 17.1% 11 / 64 Conditioncond 0.0% 0 / 12 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 73.6% 14 / 19 Podpod 0.0% 0 / 16 totaltotal 37.8% 93 / 246 CC 41 SCAR 40.1
CC
41
Cov
39%
as_column_type
182.0
as_column
56.0
as_type_attributes
42.0
CRAP 55.0
« Model/Schema/Properties.pm ↑ Summary Model.pm »
1 package Data::Model::Schema::SQL;
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 1 ▶use Carp ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 $Carp::Internal{(__PACKAGE__)}++;
7  
8 1 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod undocumented
9 1 my($class, $schema) = @_;
10 1 bless { schema => $schema }, $class;
11 }
12  
13 7 ▶sub call_method {
Subroutine call_method: called CC=3 SCAR=11.0
Pod undocumented
14 7 my $self = shift;
15 7 my $method = shift;
16  
17 7 ▶ $self->$method(@_) unless $self->{schema}->driver;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"schema"}->driver 0 7 7
18 7 my @ret = $self->{schema}->driver->_as_sql_hook( $self, $method => @_ );
19 7 ▶ return @ret if defined $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
if defined $ret[0] 2 5 7
20 5 return $self->$method(@_);
21 }
22  
23 0 ▶sub as_column_type {
Subroutine as_column_type: not called CC=13 SCAR=52.0
Pod undocumented
24 0 my($self, $column, $args) = @_;
25 0 my $type = uc($args->{type});
26  
27 0 ▶ my $size = $args->{options}->{size} || 0;
Condition0%0/2
$args->{'options'}{'size'} || 0
l!l
00
MC/DC0%0/1
$args->{'options'}{'size'} || 0
$args->{'options'}{'size'}
Truth table0%0/2
A
A= $args->{'options'}{'size'}
Aresult
11
00
28 0 ▶ $size = 0 unless $size =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $size =~ /^\d+$/ 0 0 0
29 0 ▶ if ($type =~ m/int/i) {
Branch: 3 branches
BranchTrueFalseTotal
if ($type =~ /int/i) { } 0 0 0
elsif ($type =~ /(?:real|float|double|numeric|decimal)/i) { } 0 0 0
elsif ($type =~ /char/i) { } 0 0 0
30 0 ▶ $type .= "($size)" if $size;
Branch: 1 branch
BranchTrueFalseTotal
if $size 0 0 0
31 } elsif ($type =~ m/(?:real|float|double|numeric|decimal)/i) {
32 0 ▶ my $decimals = $args->{options}->{decimals} || 0;
Condition0%0/2
$args->{'options'}{'decimals'} || 0
l!l
00
MC/DC0%0/1
$args->{'options'}{'decimals'} || 0
$args->{'options'}{'decimals'}
Truth table0%0/2
A
A= $args->{'options'}{'decimals'}
Aresult
11
00
33 0 ▶ $decimals = 0 unless $decimals =~ /^\d+$/;
Branch: 1 branch
BranchTrueFalseTotal
unless $decimals =~ /^\d+$/ 0 0 0
34 0 ▶ if ($size && $decimals) {
Branch: 2 branches
BranchTrueFalseTotal
if ($size and $decimals) { } 0 0 0
elsif ($size) { } 0 0 0
Condition0%0/3
$size and $decimals
!ll&&!rl&&r
000
MC/DC0%0/2
$size and $decimals
$size $decimals
Truth table0%0/3
A and B
A= $sizeB= $decimals
ABresult
0X0
100
111
35 0 $type .= "($size,$decimals)";
36 } elsif ($size) {
37 0 $type .= "($size)";
38 }
39 } elsif ($type =~ m/char/i) {
40 0 ▶ $size ||= 255;
Condition0%0/2
$size ||= 255
l!l
00
MC/DC0%0/1
$size ||= 255
$size
Truth table0%0/2
A
A= $size
Aresult
11
00
41 0 $type .= "($size)";;
42 }
43 0 $type;
44 }
45  
46 0 ▶sub as_type_attributes {
Subroutine as_type_attributes: not called CC=6 SCAR=37.4
Pod undocumented
47 0 my($self, $column, $args) = @_;
48 0 my $sql;
49 0 ▶ $sql .= $args->{options}->{unsigned} ? ' UNSIGNED' : '';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'options'}{'unsigned'} ? : 0 0 0
50 0 ▶ $sql .= $args->{options}->{zerofill} ? ' ZEROFILL' : '';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'options'}{'zerofill'} ? : 0 0 0
51 0 ▶ $sql .= $args->{options}->{binary} ? ' BINARY' : '';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'options'}{'binary'} ? : 0 0 0
52 0 ▶ $sql .= $args->{options}->{ascii} ? ' ASCII' : '';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'options'}{'ascii'} ? : 0 0 0
53 0 ▶ $sql .= $args->{options}->{unicode} ? ' UNICODE' : '';
Branch: 1 branch
BranchTrueFalseTotal
$args->{'options'}{'unicode'} ? : 0 0 0
54 0 $sql;
55 }
56  
57 0 ▶sub as_default {
Subroutine as_default: not called CC=5 SCAR=34.0
Pod undocumented
58 0 my($self, $column, $args) = @_;
59 0 my $default = $args->{options}->{default};
60 0 ▶ if (!defined($default)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $default) 0 0 0
61 0 return '';
62 }
63 0 ▶ if (CORE::ref($default) and CORE::ref($default) eq 'CODE') {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $default and ref $default eq "CODE") 0 0 0
Condition0%0/3
ref $default and ref $default eq "CODE"
!ll&&!rl&&r
000
MC/DC0%0/2
ref $default and ref $default eq "CODE"
ref $default ref $default eq "CODE"
Truth table0%0/3
A and B
A= ref $defaultB= ref $default eq "CODE"
ABresult
0X0
100
111
64 0 return '';
65 }
66  
67 0 ▶ if ($args->{type} =~ m/(?:int|real|float|double|numeric|decimal|bit)/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args->{"type"} =~ /(?:int|real|float|double|numeric|decimal|bit)/i) 0 0 0
68 0 return ' DEFAULT ' . $default
69 }
70 0 return " DEFAULT '" . $default ."'";
71 }
72  
73 0 ▶sub as_column {
Subroutine as_column: not called CC=7 SCAR=40.3
Pod undocumented
74 0 my($self, $column, $args) = @_;
75  
76 0 my $opts = $args->{options};
77 return sprintf('%-15s %-15s', $column, $self->call_method( as_column_type => $column, $args ))
78 . $self->call_method( as_type_attributes => $column, $args )
79 . ($opts->{required} ? ' NOT NULL' : ($opts->{null} ? ' NULL' : ''))
80 . $self->call_method( as_default => $column, $args )
81 . ($opts->{auto_increment} ? ' AUTO_INCREMENT' : '')
82 . ($self->{unique} ? ' UNIQUE' : '')
83 . ($self->{primary_key} ? ' PRIMARY KEY' : '')
84 . ($self->{references} ? ' REFERENCES '
85 . $self->{references}->{table}->{name} .'('
86 0 ▶ . $self->{references}->{name} .')' : '')
Branch: 6 branches
BranchTrueFalseTotal
$opts->{'required'} ? : 0 0 0
$opts->{'null'} ? : 0 0 0
$opts->{'auto_increment'} ? : 0 0 0
$self->{'unique'} ? : 0 0 0
$self->{'primary_key'} ? : 0 0 0
$self->{'references'} ? : 0 0 0
87 ;
88 }
89  
90 1 ▶sub as_primary_key {
Subroutine as_primary_key: called CC=2 SCAR=7.0
Pod undocumented
91 1 my($self, $key) = @_;
92 1 ▶ return () unless @{ $key };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$key;} 0 1 1
93 1 return 'PRIMARY KEY (' . join(', ', @{ $key }) .')';
94 }
95  
96 1 ▶sub as_unique {
Subroutine as_unique: called CC=3 SCAR=17.9
Pod undocumented
97 1 my($self, $unique) = @_;
98 1 ▶ return () unless @{ $unique };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$unique;} 1 0 1
99  
100 0 my @sql = ();
101 0 for my $data (@{ $unique }) {
102 0 my($name, $columns) = @{ $data };
103 0 push(@sql, 'UNIQUE ' . $name . ' (' . join(', ', @{ $columns }) . ')');
104 }
105 0 return @sql;
106 }
107  
108 1 ▶sub as_foreign {
Subroutine as_foreign: called CC=3 SCAR=18.4
Pod undocumented
109 1 my $self = shift;
110 1 ▶ return () unless @{ $self->{schema}->{foreign} };
Branch: 1 branch
BranchTrueFalseTotal
unless @{$self->{"schema"}{"foreign"};} 1 0 1
111  
112 0 my $sql = '';
113 0 for my $foreign (@{ $self->{schema}->{foreign} }) {
114 0 my @cols = @{ $foreign->{columns} };
115 0 my @refs = @{ $foreign->{references} };
116 $sql .= 'FOREIGN KEY ('
117 . join(', ', @cols)
118 0 . ') REFERENCES ' . $refs[0]->{table}->{name} .' ('
119 . join(', ', @refs)
120 . ')'
121 ;
122 }
123 0 return $sql;
124 }
125  
126 1 ▶sub as_table_attributes {
Subroutine as_table_attributes: called CC=3 SCAR=11.3
Pod undocumented
127 1 my $self = shift;
128 1 my $hash = $self->{schema}->options->{create_sql_attributes};
129 1 ▶ $hash = +{} unless ref($hash) eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
unless ref $hash eq "HASH" 1 0 1
130 1 my($ret) = $self->call_method( 'get_table_attributes', $hash );
131 1 ▶ $ret ? " $ret" : '';
Branch: 1 branch
BranchTrueFalseTotal
$ret ? : 0 1 1
132 }
133 0 ▶sub get_table_attributes {}
Subroutine get_table_attributes: not called CC=1 SCAR=0.0
Pod undocumented
134  
135 1 ▶sub as_create_table {
Subroutine as_create_table: called CC=3 SCAR=11.4
Pod undocumented
136 1 my $self = shift;
137 1 my $schema = $self->{schema};
138  
139 1 my @values;
140 1 my %columns = %{ $schema->column };
141 1 for my $column ($schema->column_names) {
142 1 push @values, $self->call_method( as_column => $column, $schema->column->{$column} );
143 }
144  
145 1 my @key = @{ $schema->key };
146 1 my $unique_hash = $schema->unique;
147 0 my @unique = sort { $a->[0] cmp $b->[0] }
148 0 map { [ $_ => $unique_hash->{$_} ] }
149 1 keys %{ $unique_hash };
150  
151 1 ▶ if (my $name = $schema->options->{key_as_unique}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = $schema->options->{"key_as_unique"}) 0 1 1
152 0 unshift @unique, [ $name, [ @key ] ];
153 0 @key = ();
154 }
155  
156 1 my $index_hash = $schema->index;
157 0 my @index = sort { $a->[0] cmp $b->[0] }
158 0 map { [ $_ => $index_hash->{$_} ] }
159 1 keys %{ $index_hash };
160  
161 1 push(@values, $self->call_method( 'as_primary_key', \@key ));
162 1 push(@values, $self->call_method( 'as_unique', \@unique ));
163 1 push(@values, $self->call_method( 'as_inner_index', \@index ));
164 1 push(@values, $self->call_method( 'as_foreign' ));
165  
166 return 'CREATE TABLE '
167 . $self->{schema}->model
168 1 . " (\n " . join(",\n ", grep { $_ } @values) . "\n)"
169 . $self->as_table_attributes,
170 ;
171 }
172  
173 1 ▶sub as_inner_index {
Subroutine as_inner_index: called CC=1 SCAR=0.0
Pod undocumented
174 1 ();
175 }
176  
177 1 ▶sub as_index {
Subroutine as_index: called CC=2 SCAR=7.6
Pod undocumented
178 1 my $self = shift;
179 1 my @sql = ();
180  
181 1 ▶ while (my($name, $columns) = each %{ $self->{schema}->{index} }) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$$self{"schema"}{"index"};}) 0 1 1
182 push(@sql, 'CREATE'
183 . ' INDEX '
184 . $name
185 . ' ON ' . $self->{schema}->model
186 0 . ' (' . join(', ', @{ $columns } ) . ')'
187 );
188 }
189 1 return @sql;
190 }
191  
192 1 ▶sub as_create_indexes {
Subroutine as_create_indexes: called CC=2 SCAR=7.6
Pod undocumented
193 1 my $self = shift;
194 1 my @ret = $self->call_method( 'as_index' );
195 1 ▶ return () unless $ret[0];
Branch: 1 branch
BranchTrueFalseTotal
unless $ret[0] 1 0 1
196 0 return @ret;
197 }
198  
199  
200 1 ▶sub as_sql {
Subroutine as_sql: called CC=1 SCAR=0.0
Pod undocumented
201 1 my $self = shift;
202 1 return ($self->as_create_table, $self->as_create_indexes);
203 }
204  
205  
206 1 1;
207  
208 __END__
« Model/Schema/Properties.pm ↑ Summary Model.pm »