Diff/Table.pm

filter: Statementstmt 56.1% 64 / 114 Branchbran 34.0% 17 / 50 Conditioncond 4.7% 1 / 21 MC/DCmcdc 0.0% 0 / 14 Subroutinesub 30.0% 6 / 20 Podpod 93.3% 14 / 15 totaltotal 43.5% 102 / 234 CC 35 SCAR 31.5
CC
35
Cov
44%
_parse
50.5
isa_field
12.0
isa_primary
12.0
CRAP 23.4
« Diff.pm ↑ Summary Diff/Database.pm »
1 package MySQL::Diff::Table;
2  
3 =head1 NAME
4  
5 MySQL::Diff::Table - Table Definition Class
6  
7 =head1 SYNOPSIS
8  
9 use MySQL::Diff::Table
10  
11 my $db = MySQL::Diff::Database->new(%options);
12 my $def = $db->def();
13 my $name = $db->name();
14 my $field = $db->field();
15 my $fields = $db->fields(); # %$fields
16 my $primary_key = $db->primary_key();
17 my $indices = $db->indices(); # %$indices
18 my $options = $db->options();
19  
20 my $isfield = $db->isa_field($field);
21 my $isprimary = $db->isa_primary($field);
22 my $isindex = $db->isa_index($field);
23 my $isunique = $db->is_unique($field);
24 my $isfulltext = $db->is_fulltext($field);
25  
26 =head1 DESCRIPTION
27  
28 Parses a table definition into component parts.
29  
30 =cut
31  
32 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
33 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 4 our $VERSION = '0.60';
36  
37 # ------------------------------------------------------------------------------
38 # Libraries
39  
40 4 ▶use Carp qw(:DEFAULT);
Subroutine BEGIN: called CC=1 SCAR=0.0
41 4 ▶use MySQL::Diff::Utils qw(debug);
Subroutine BEGIN: called CC=1 SCAR=0.0
42  
43 # ------------------------------------------------------------------------------
44  
45 =head1 METHODS
46  
47 =head2 Constructor
48  
49 =over 4
50  
51 =item new( %options )
52  
53 Instantiate the objects, providing the command line options for database
54 access and process requirements.
55  
56 =cut
57  
58 2 ▶sub new {
Subroutine new: called CC=4 SCAR=14.2
Pod documented
59 2 my $class = shift;
60 2 my %hash = @_;
61 2 my $self = {};
62 2 ▶ bless $self, ref $class || $class;
Condition33%1/3
ref $class || $class
l!l&&r!l&&!r
020
MC/DC0%0/2
ref $class || $class
ref $class $class
Truth table33%1/3
A || B
A= ref $classB= $class
ABresult
1X1
011
000
63  
64 2 $self->{$_} = $hash{$_} for(keys %hash);
65  
66 2 debug(3,"\nconstructing new MySQL::Diff::Table");
67 2 ▶ croak "MySQL::Diff::Table::new called without def params" unless $self->{def};
Branch: 1 branch
BranchTrueFalseTotal
unless $self->{"def"} 0 2 2
68 2 $self->_parse;
69 1 return $self;
70 }
71  
72 =back
73  
74 =head2 Public Methods
75  
76 Fuller documentation will appear here in time :)
77  
78 =over 4
79  
80 =item * def
81  
82 Returns the table definition as a string.
83  
84 =item * name
85  
86 Returns the name of the current table.
87  
88 =item * field
89  
90 Returns the current field definition of the given field.
91  
92 =item * fields
93  
94 Returns an array reference to a list of fields.
95  
96 =item * primary_key
97  
98 Returns a hash reference to fields used as primary key fields.
99  
100 =item * indices
101  
102 Returns a hash reference to fields used as index fields.
103  
104 =item * options
105  
106 Returns the additional options added to the table definition.
107  
108 =item * isa_field
109  
110 Returns 1 if given field is used in the current table definition, otherwise
111 returns 0.
112  
113 =item * isa_primary
114  
115 Returns 1 if given field is defined as a primary key, otherwise returns 0.
116  
117 =item * isa_index
118  
119 Returns 1 if given field is used as an index field, otherwise returns 0.
120  
121 =item * is_unique
122  
123 Returns 1 if given field is used as unique index field, otherwise returns 0.
124  
125 =item * is_fulltext
126  
127 Returns 1 if given field is used as fulltext index field, otherwise returns 0.
128  
129 =item * is_auto_inc
130  
131 Returns 1 if given field is defined as an auto increment field, otherwise returns 0.
132  
133 =back
134  
135 =cut
136  
137 0 ▶sub def { my $self = shift; return $self->{def}; }
Subroutine def: not called CC=1 SCAR=6.9
Pod documented
138 0 ▶sub name { my $self = shift; return $self->{name}; }
Subroutine name: not called CC=1 SCAR=6.9
Pod documented
139 0 ▶sub field { my $self = shift; return $self->{fields}{$_[0]}; }
Subroutine field: not called CC=1 SCAR=6.9
Pod documented
140 0 ▶sub fields { my $self = shift; return $self->{fields}; }
Subroutine fields: not called CC=1 SCAR=6.9
Pod documented
141 0 ▶sub primary_key { my $self = shift; return $self->{primary_key}; }
Subroutine primary_key: not called CC=1 SCAR=6.9
Pod documented
142 0 ▶sub indices { my $self = shift; return $self->{indices}; }
Subroutine indices: not called CC=1 SCAR=6.9
Pod documented
143 0 ▶sub options { my $self = shift; return $self->{options}; }
Subroutine options: not called CC=1 SCAR=6.9
Pod documented
144 0 ▶sub foreign_key { my $self = shift; return $self->{foreign_key}; }
Subroutine foreign_key: not called CC=1 SCAR=6.9
Pod undocumented
145  
146 0 ▶sub isa_field { my $self = shift; return $_[0] && $self->{fields}{$_[0]} ? 1 : 0; }
Subroutine isa_field: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $self->{'fields'}{$_[0]} ? : 0 0 0
Condition0%0/3
$_[0] and $self->{"fields"}{$_[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $self->{"fields"}{$_[0]}
$_[0] $self->{"fields"}{$_[0]}
Truth table0%0/3
A and B
A= $_[0]B= $self->{"fields"}{$_[0]}
ABresult
0X0
100
111
147 0 ▶sub isa_primary { my $self = shift; return $_[0] && $self->{primary}{$_[0]} ? 1 : 0; }
Subroutine isa_primary: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $self->{'primary'}{$_[0]} ? : 0 0 0
Condition0%0/3
$_[0] and $self->{"primary"}{$_[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $self->{"primary"}{$_[0]}
$_[0] $self->{"primary"}{$_[0]}
Truth table0%0/3
A and B
A= $_[0]B= $self->{"primary"}{$_[0]}
ABresult
0X0
100
111
148 0 ▶sub isa_index { my $self = shift; return $_[0] && $self->{indices}{$_[0]} ? 1 : 0; }
Subroutine isa_index: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $self->{'indices'}{$_[0]} ? : 0 0 0
Condition0%0/3
$_[0] and $self->{"indices"}{$_[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $self->{"indices"}{$_[0]}
$_[0] $self->{"indices"}{$_[0]}
Truth table0%0/3
A and B
A= $_[0]B= $self->{"indices"}{$_[0]}
ABresult
0X0
100
111
149 0 ▶sub is_unique { my $self = shift; return $_[0] && $self->{unique}{$_[0]} ? 1 : 0; }
Subroutine is_unique: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $self->{'unique'}{$_[0]} ? : 0 0 0
Condition0%0/3
$_[0] and $self->{"unique"}{$_[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $self->{"unique"}{$_[0]}
$_[0] $self->{"unique"}{$_[0]}
Truth table0%0/3
A and B
A= $_[0]B= $self->{"unique"}{$_[0]}
ABresult
0X0
100
111
150 0 ▶sub is_fulltext { my $self = shift; return $_[0] && $self->{fulltext}{$_[0]} ? 1 : 0; }
Subroutine is_fulltext: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $self->{'fulltext'}{$_[0]} ? : 0 0 0
Condition0%0/3
$_[0] and $self->{"fulltext"}{$_[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $self->{"fulltext"}{$_[0]}
$_[0] $self->{"fulltext"}{$_[0]}
Truth table0%0/3
A and B
A= $_[0]B= $self->{"fulltext"}{$_[0]}
ABresult
0X0
100
111
151 0 ▶sub is_auto_inc { my $self = shift; return $_[0] && $self->{auto_inc}{$_[0]} ? 1 : 0; }
Subroutine is_auto_inc: not called CC=3 SCAR=24.8
Pod documented
Branch: 1 branch
BranchTrueFalseTotal
$_[0] && $self->{'auto_inc'}{$_[0]} ? : 0 0 0
Condition0%0/3
$_[0] and $self->{"auto_inc"}{$_[0]}
!ll&&!rl&&r
000
MC/DC0%0/2
$_[0] and $self->{"auto_inc"}{$_[0]}
$_[0] $self->{"auto_inc"}{$_[0]}
Truth table0%0/3
A and B
A= $_[0]B= $self->{"auto_inc"}{$_[0]}
ABresult
0X0
100
111
152  
153 # ------------------------------------------------------------------------------
154 # Private Methods
155  
156 2 ▶sub _parse {
Subroutine _parse: called CC=20 SCAR=39.2
157 2 my $self = shift;
158  
159 2 $self->{def} =~ s/`([^`]+)`/$1/gs; # later versions quote names
160 2 $self->{def} =~ s/\n+/\n/;
161 2 $self->{lines} = [ grep ! /^\s*$/, split /(?=^)/m, $self->{def} ];
162 2 my @lines = @{$self->{lines}};
163 2 debug(4,"parsing table def '$self->{def}'");
164  
165 2 my $name;
166 2 ▶ if ($lines[0] =~ /^\s*create\s+table\s+(\S+)\s+\(\s*$/i) {
Branch: 1 branch
BranchTrueFalseTotal
if ($lines[0] =~ /^\s*create\s+table\s+(\S+)\s+\(\s*$/i) { } 2 0 2
167 2 $self->{name} = $1;
168 2 debug(3,"got table name '$self->{name}'");
169 2 shift @lines;
170 } else {
171 0 croak "couldn't figure out table name";
172 }
173  
174 2 ▶ while (@lines) {
Branch: 1 branch
BranchTrueFalseTotal
if (@lines) 7 0 7
175 7 $_ = shift @lines;
176 7 s/^\s*(.*?),?\s*$/$1/; # trim whitespace and trailing commas
177 7 debug(4,"line: [$_]");
178 7 ▶ if (/^PRIMARY\s+KEY\s+(.+)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^PRIMARY\s+KEY\s+(.+)$/) 1 6 7
179 1 my $primary = $1;
180 croak "two primary keys in table '$self->{name}': '$primary', '$self->{primary_key}'\n"
181 1 ▶ if $self->{primary_key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"primary_key"} 0 1 1
182 1 debug(4,"got primary key $primary");
183 1 $self->{primary_key} = $primary;
184 1 $primary =~ s/\((.*?)\)/$1/;
185 1 $self->{primary}{$_} = 1 for(split(/,/, $primary));
186  
187 1 next;
188 }
189
190 6 ▶ if (/^(?:CONSTRAINT\s+(.*)?)?\s+FOREIGN\s+KEY\s+(.*)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(?:CONSTRAINT\s+(.*)?)?\s+FOREIGN\s+KEY\s+(.*)$/) 0 6 6
191 0 my ($key, $val) = ($1, $2);
192 croak "foreign key '$key' duplicated in table '$name'\n"
193 0 ▶ if $self->{foreign_key}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"foreign_key"}{$key} 0 0 0
194 0 debug(1,"got foreign key $key");
195 0 $self->{foreign_key}{$key} = $val;
196 0 next;
197 }
198  
199 6 ▶ if (/^(KEY|UNIQUE(?: KEY)?)\s+(\S+?)(?:\s+USING\s+(?:BTREE|HASH|RTREE))?\s*\((.*)\)(?:\s+USING\s+(?:BTREE|HASH|RTREE))?$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(KEY|UNIQUE(?: KEY)?)\s+(\S+?)(?:\s+USING\s+(?:BTREE|HASH|RTREE))?\s*\((.*)\)(?:\s+USING\s+(?:BTREE|HASH|RTREE))?$/) 0 6 6
200 0 my ($type, $key, $val) = ($1, $2, $3);
201 croak "index '$key' duplicated in table '$self->{name}'\n"
202 0 ▶ if $self->{indices}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"indices"}{$key} 0 0 0
203 0 $self->{indices}{$key} = $val;
204 0 ▶ $self->{unique}{$key} = 1 if($type =~ /unique/i);
Branch: 1 branch
BranchTrueFalseTotal
if $type =~ /unique/i 0 0 0
205 0 ▶ debug(4, "got ", defined $self->{unique}{$key} ? 'unique ' : '', "index key '$key': ($val)");
Branch: 1 branch
BranchTrueFalseTotal
defined $self->{'unique'}{$key} ? : 0 0 0
206 0 next;
207 }
208  
209 6 ▶ if (/^(FULLTEXT(?:\s+KEY|INDEX)?)\s+(\S+?)\s*\((.*)\)$/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(FULLTEXT(?:\s+KEY|INDEX)?)\s+(\S+?)\s*\((.*)\)$/) 0 6 6
210 0 my ($type, $key, $val) = ($1, $2, $3);
211 croak "FULLTEXT index '$key' duplicated in table '$self->{name}'\n"
212 0 ▶ if $self->{fulltext}{$key};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fulltext"}{$key} 0 0 0
213 0 $self->{indices}{$key} = $val;
214 0 $self->{fulltext}{$key} = 1;
215 0 debug(4,"got FULLTEXT index '$key': ($val)");
216 0 next;
217 }
218  
219 6 ▶ if (/^\)\s*(.*?);$/) { # end of table definition
Branch: 1 branch
BranchTrueFalseTotal
if (/^\)\s*(.*?);$/) 1 5 6
220 1 $self->{options} = $1;
221 1 debug(4,"got table options '$self->{options}'");
222 1 last;
223 }
224  
225 5 ▶ if (/^(\S+)\s*(.*)/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\S+)\s*(.*)/) 5 0 5
226 5 my ($field, $fdef) = ($1, $2);
227 croak "definition for field '$field' duplicated in table '$self->{name}'\n"
228 5 ▶ if $self->{fields}{$field};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"fields"}{$field} 1 4 5
229 4 $self->{fields}{$field} = $fdef;
230 4 debug(4,"got field def '$field': $fdef");
231 4 ▶ next unless $fdef =~ /\s+AUTO_INCREMENT\b/;
Branch: 1 branch
BranchTrueFalseTotal
unless $fdef =~ /\s+AUTO_INCREMENT\b/ 4 0 4
232 0 $self->{auto_inc}{$field} = 1;
233 0 debug(4,"got AUTO_INCREMENT field '$field'");
234 0 next;
235 }
236  
237 0 croak "unparsable line in definition for table '$self->{name}':\n$_";
238 }
239  
240 warn "table '$self->{name}' didn't have terminator\n"
241 1 ▶ unless defined $self->{options};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $self->{"options"} 0 1 1
242  
243 1 @lines = grep ! m{^/\*!40\d{3} .*? \*/;}, @lines;
244 1 @lines = grep ! m{^(SET |DROP TABLE)}, @lines;
245  
246 1 ▶ warn "table '$self->{name}' had trailing garbage:\n", join '', @lines
Branch: 1 branch
BranchTrueFalseTotal
if @lines 0 1 1
247 if @lines;
248 }
249  
250 4 1;
251  
252 __END__
« Diff.pm ↑ Summary Diff/Database.pm »