DumpFile/SQL.pm

filter: Statementstmt 89.8% 159 / 177 Branchbran 57.6% 45 / 78 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 93.3% 28 / 30 Podpod 27.7% 5 / 18 totaltotal 78.2% 237 / 303 CC 42 SCAR 16.6
CC
42
Cov
80%
unescape
12.2
__ANON__
12.0
parse
8.3
CRAP 5.2
« DumpFile/Pages.pm ↑ Summary DumpFile/Compat.pm »
1 package MediaWiki::DumpFile::SQL;
2  
3 7 our $VERSION = '0.2.2';
4  
5 7 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 7 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 7 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 7 ▶use Carp qw(croak);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 7 ▶use Scalar::Util qw(reftype);
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 7 ▶use IO::Uncompress::AnyUncompress qw($AnyUncompressError);
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 #public methods
14 4 ▶sub new {
Subroutine new: called CC=2 SCAR=7.0
Pod documented
15 4 my ($class, $file) = @_;
16 4 my $self = { };
17
18 4 ▶ if (! defined($file)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $file) 0 4 4
19 0 croak "must specify a filename or open filehandle";
20 }
21
22 4 bless($self, $class);
23
24 4 $self->{buffer} = [];
25 4 $self->{file} = $file;
26 4 $self->{fh} = undef;
27 4 $self->{table_name} = undef;
28 4 $self->{schema} = undef;
29 4 $self->{type_map} = undef;
30 4 $self->{table_statement} = undef;
31
32 4 $self->create_type_map;
33 4 $self->open_file;
34 4 $self->parse_table;
35
36 4 return $self;
37 }
38  
39 112 ▶sub next {
Subroutine next: called CC=3 SCAR=11.0
Pod documented
40 112 my ($self) = @_;
41 112 my $buffer = $self->{buffer};
42 112 my $next;
43
44 112 ▶ while(! defined($next = shift(@$buffer))) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined($next = shift @$buffer)) 6 109 115
45 6 ▶ if (! $self->parse_more) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self->parse_more) 3 3 6
46 3 return undef;
47 }
48 }
49
50 109 return $next;
51 }
52  
53 2 ▶sub table_name {
Subroutine table_name: called CC=1 SCAR=0.0
Pod documented
54 2 my ($self) = @_;
55 2 return $self->{table_name};
56 }
57  
58 2 ▶sub table_statement {
Subroutine table_statement: called CC=1 SCAR=0.0
Pod documented
59 2 my ($self) = @_;
60 2 return $self->{table_statement};
61 }
62  
63 2 ▶sub schema {
Subroutine schema: called CC=1 SCAR=0.0
Pod documented
64 2 my ($self) = @_;
65 2 return @{$self->{schema}};
66 }
67  
68 #private methods
69 4 ▶sub open_file {
Subroutine open_file: called CC=2 SCAR=7.1
Pod undocumented
70 4 my ($self) = @_;
71 4 my $file = $self->{file};
72 4 my $type = reftype($file);
73 4 my $fh;
74
75 4 $self->{fh} = $fh = IO::Uncompress::AnyUncompress->new($file);
76 4 my $line = <$fh>;
77
78 4 ▶ if ($line !~ m/^-- MySQL dump/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($line =~ /^-- MySQL dump/) 0 4 4
79 0 die "expected MySQL dump file";
80 }
81
82 4 return;
83 }
84  
85 4 ▶sub parse_table {
Subroutine parse_table: called CC=8 SCAR=21.0
Pod undocumented
86 4 my ($self) = @_;
87 4 my $fh = $self->{fh};
88 4 my $found = 0;
89 4 my $table;
90 my $table_statement;
91 4 my @cols;
92
93 #find the CREATE TABLE line and get the table name
94 4 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 60 0 60
95 60 ▶ if (m/^CREATE TABLE `([^`]+)` \(/) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^CREATE TABLE `([^`]+)` \(/) 4 56 60
96 4 $table = $1;
97 4 $table_statement = $_;
98
99 4 last;
100 }
101 }
102
103 4 ▶ die "expected CREATE TABLE" unless defined($table);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $table 0 4 4
104
105 4 ▶ while(<$fh>) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($_ = readline $fh)) 22 0 22
106 22 $table_statement .= $_;
107  
108 22 ▶ if (m/^\)/) {
Branch: 2 branches
BranchTrueFalseTotal
if (/^\)/) { } 4 18 22
elsif (/^\s+`([^`]+)` (\w+)/) { } 10 8 18
109 4 last;
110 } elsif (m/^\s+`([^`]+)` (\w+)/) {
111 #this regex ^^^^ matches column names and types
112 10 push(@cols, [$1, $2]);
113 }
114 }
115
116 4 ▶ if (! scalar(@cols)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (scalar @cols) 0 4 4
117 0 die "Could not find columns for $table";
118 }
119  
120 4 $self->{table_name} = $table;
121 4 $self->{schema} = \@cols;
122 4 $self->{table_statement} = $table_statement;
123
124 4 return 1;
125 }
126  
127 #returns false at EOF or true if more data was parsed
128 6 ▶sub parse_more {
Subroutine parse_more: called CC=5 SCAR=16.2
Pod undocumented
129 6 my ($self) = @_;
130 6 my $fh = $self->{fh};
131 6 my $insert;
132
133 6 ▶ if (! defined($fh)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fh) 0 6 6
134 0 return 0;
135 }
136
137 6 while(1) {
138 50 $insert = <$fh>;
139
140 50 ▶ if (! defined($insert)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $insert) 3 47 50
141 3 ▶ close($fh) or die "could not close: $!";
Branch: 1 branch
BranchTrueFalseTotal
close $fh or die "could not close: $!" 3 0 3
142 3 $self->{fh} = undef;
143
144 3 return 0;
145 }
146
147 47 ▶ if ($insert =~ m/^INSERT INTO/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($insert =~ /^INSERT INTO/) 3 44 47
148 3 $self->parse($insert);
149 3 return 1;
150 }
151 }
152 }
153  
154 #this parses a complete INSERT line into the individual
155 #components
156 3 ▶sub parse {
Subroutine parse: called CC=8 SCAR=21.2
Pod undocumented
157 3 my ($self, $string) = @_;
158 3 my $buffer = $self->{buffer};
159 3 my $compiled = $self->compile_config;
160 3 my $found = 0;
161
162 3 $_ = $string;
163
164 #check the table name
165 3 ▶ m/^INSERT INTO `(.*?)` VALUES /g or die "expected header";
Branch: 1 branch
BranchTrueFalseTotal
/^INSERT INTO `(.*?)` VALUES /g or die "expected header" 3 0 3
166 3 ▶ if ($self->{table_name} ne $1) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{"table_name"} ne $1) 0 3 3
167 0 die "table name mismatch: $1";
168 }
169
170 3 while(1) {
171 109 my %new;
172 109 my $depth = 0;
173
174 #apply the various regular expressions to the
175 #string in order
176 109 foreach my $handler (@$compiled) {
177 579 my ($col, $cb) = @$handler;
178 579 my $ret;
179
180 579 $depth++;
181
182 #these callbacks also use $_
183 579 eval { $ret = &$cb };
184
185 579 ▶ if ($@) {
Branch: 1 branch
BranchTrueFalseTotal
if ($@) 0 579 579
186 0 die "parse error pos:" . pos() . " depth:$depth error: $@";
187 }
188
189 #column names starting with # are part of the parser, not user data
190 579 ▶ if ($col !~ m/^#/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($col =~ /^#/) 235 344 579
191 235 $new{$col} = $ret;
192 }
193 }
194
195 109 push(@$buffer, \%new);
196 109 $found++;
197  
198 109 ▶ if (m/\G, ?/gc) {
Branch: 2 branches
BranchTrueFalseTotal
if (/\G, ?/cg) { } 106 3 109
elsif (/\G;$/cg) { } 3 0 3
199 #^^^^ match the delimiter between rows
200 106 next;
201 } elsif (m/\G;$/gc) {
202 #^^^^ match end of statement
203 3 last;
204 } else {
205 0 die "expected delimter or end of statement. pos:" . pos;
206 }
207 }
208
209 3 return $found;
210 }
211  
212 #functions for the parsing engine
213  
214 #maps between MySQL types and our types
215 4 ▶sub create_type_map {
Subroutine create_type_map: called CC=1 SCAR=0.0
Pod undocumented
216 4 my ($self) = @_;
217
218 $self->{type_map} = {
219 4 int => 'int',
220 tinyint => 'int',
221 bigint => 'int',
222
223 char => 'varchar',
224 varchar => 'varchar',
225 enum => 'varchar',
226
227 double => 'float',
228
229 timestamp => 'int',
230
231 blob => 'varchar',
232 mediumblob => 'varchar',
233 mediumtext => 'varchar',
234 tinyblob => 'varchar',
235 varbinary => 'varchar',
236
237 };
238
239 4 return 1;
240 }
241  
242 #convert the schema into a list of callbacks
243 #that match the schema and extract data from it
244 3 ▶sub compile_config {
Subroutine compile_config: called CC=6 SCAR=19.0
Pod undocumented
245 3 my ($self) = @_;
246 3 my $schema = $self->{schema};
247 3 my @handlers;
248
249 3 push(@handlers, ['#start', new_start_data()]);
250  
251 3 foreach (@$schema) {
252 7 my ($name, $type) = @$_;
253  
254 7 my $oldtype = $type;
255 7 $type = $self->{type_map}->{lc($type)};
256
257 7 ▶ if (! defined($type)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $type) 0 7 7
258 0 die "type map failed for $oldtype";
259 }
260  
261 7 ▶ if ($type eq 'int') {
Branch: 3 branches
BranchTrueFalseTotal
if ($type eq 'int') { } 4 3 7
elsif ($type eq 'varchar') { } 3 0 3
elsif ($type eq 'float') { } 0 0 0
262 4 push(@handlers, [$name, new_int()], ['#delim', new_delim()]);
263 } elsif ($type eq 'varchar') {
264 3 push(@handlers, [$name, new_varchar()], ['#delim', new_delim()]);
265 } elsif($type eq 'float') {
266 0 push(@handlers, [$name, new_float()], ['#delim', new_delim()]);
267 } else {
268 0 die "unknown type: $type";
269 }
270 }
271
272 3 pop(@handlers); #gets rid of that extra delimiter
273 3 push(@handlers, ['#end', new_end_data()]);
274
275 3 return \@handlers;
276 }
277  
278 10 ▶sub unescape {
Subroutine unescape: called CC=6 SCAR=25.0
Pod undocumented
279 10 my ($input) = @_;
280
281 10 ▶ if ($input eq '\\\\') {
Branch: 5 branches
BranchTrueFalseTotal
if ($input eq '\\\\') { } 0 10 10
elsif ($input eq q[\\']) { } 6 4 10
elsif ($input eq '\\"') { } 4 0 4
elsif ($input eq '\\n') { } 0 0 0
elsif ($input eq '\\t') { } 0 0 0
282 0 return '\\';
283 } elsif ($input eq "\\'") {
284 6 return("'");
285 } elsif ($input eq '\\"') {
286 4 return '"';
287 } elsif ($input eq '\\n') {
288 0 return "\n";
289 } elsif ($input eq '\\t') {
290 0 return "\t";
291 } else {
292 0 die "can not unescape $input";
293 }
294 }
295  
296  
297 #functions that create callbacks that match and extract
298 #data from INSERT lines
299  
300 #it is critical that these regular expressions use the /gc option
301 #or the parser will stop functioning as soon as a regex with
302 #out those options is encountered and debugging becomes
303 #almost impossible
304  
305 4 ▶sub new_int {
Subroutine new_int: called CC=1 SCAR=0.2
Pod undocumented
306 126 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
307 126 ▶ m/\GNULL/gc and return undef;
Branch: 1 branch
BranchTrueFalseTotal
/\GNULL/cg and return undef 0 126 126
308 126 ▶ m/\G(-?[\d]+)/gc or die "expected int"; return $1;
Branch: 1 branch
BranchTrueFalseTotal
/\G(-?[\d]+)/cg or die "expected int" 126 0 126
309 4 };
310 }
311  
312 0 ▶sub new_float {
Subroutine new_float: not called CC=1 SCAR=6.9
Pod undocumented
313 0 ▶ return sub {
Subroutine __ANON__: not called CC=3 SCAR=24.8
314 0 ▶ m/\GNULL/gc and return undef;
Branch: 1 branch
BranchTrueFalseTotal
/\GNULL/cg and return undef 0 0 0
315 0 ▶ m/\G(-?[\d]+(?:\.[\d]+(e-?[\d]+)?)?)/gc or die "expected float"; return $1;
Branch: 1 branch
BranchTrueFalseTotal
/\G(-?[\d]+(?:\.[\d]+(e-?[\d]+)?)?)/cg or die "expected float" 0 0 0
316 }
317 0 }
318  
319 3 ▶sub new_varchar {
Subroutine new_varchar: called CC=1 SCAR=0.0
Pod undocumented
320 109 ▶ return sub {
Subroutine __ANON__: called CC=3 SCAR=11.2
321 109 my $data;
322
323 109 ▶ m/\GNULL/gc and return undef;
Branch: 1 branch
BranchTrueFalseTotal
/\GNULL/cg and return undef 0 109 109
324  
325 #does not handle very long strings; crashes perl 5.8.9 causes 5.10.1 to error out
326 #m/\G'((\\.|[^'])*)'/gc or die "expected varchar";
327 #thanks somni!
328 109 ▶ m/'((?:[^\\']*(?:\\.[^\\']*)*))'/gc or die "expected varchar";
Branch: 1 branch
BranchTrueFalseTotal
/'((?:[^\\']*(?:\\.[^\\']*)*))'/cg or die "expected varchar" 109 0 109
329 109 $data = $1;
330 109 $data =~ s/(\\.)/unescape($1)/e;
331
332 109 return $data;
333 }
334 3 }
335  
336 7 ▶sub new_delim {
Subroutine new_delim: called CC=1 SCAR=0.1
Pod undocumented
337 126 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
338 126 ▶ m/\G, ?/gc or die "expected delimiter"; return undef;
Branch: 1 branch
BranchTrueFalseTotal
/\G, ?/cg or die "expected delimiter" 126 0 126
339 7 };
340 }
341  
342 3 ▶sub new_start_data {
Subroutine new_start_data: called CC=1 SCAR=0.1
Pod undocumented
343 109 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
344 109 ▶ m/\G\(/gc or die "expected start of data set"; return undef;
Branch: 1 branch
BranchTrueFalseTotal
/\G\(/cg or die "expected start of data set" 109 0 109
345 3 };
346 }
347  
348 3 ▶sub new_end_data {
Subroutine new_end_data: called CC=1 SCAR=0.1
Pod undocumented
349 109 ▶ return sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
350 109 ▶ m/\G\)/gc or die "expected end of data set"; return undef;
Branch: 1 branch
BranchTrueFalseTotal
/\G\)/cg or die "expected end of data set" 109 0 109
351 }
352 3 }
353  
354 7 1;
355  
356 __END__
« DumpFile/Pages.pm ↑ Summary DumpFile/Compat.pm »