| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Data::Generator::FromDDL::CLI; |
|
2
|
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
110713
|
use Pod::Usage; |
|
|
2
|
|
|
|
|
119349
|
|
|
|
2
|
|
|
|
|
337
|
|
|
4
|
2
|
|
|
2
|
|
2694
|
use Getopt::Long qw(:config posix_default no_ignore_case gnu_compat); |
|
|
2
|
|
|
|
|
24163
|
|
|
|
2
|
|
|
|
|
15
|
|
|
5
|
2
|
|
|
2
|
|
3142
|
use Term::ANSIColor qw(colored); |
|
|
2
|
|
|
|
|
17715
|
|
|
|
2
|
|
|
|
|
1080
|
|
|
6
|
|
|
|
|
|
|
use Class::Accessor::Lite ( |
|
7
|
2
|
|
|
|
|
23
|
new => 1, |
|
8
|
|
|
|
|
|
|
rw => [qw(ddl out_fh include exclude num bytes_per_sql)], |
|
9
|
2
|
|
|
2
|
|
909
|
); |
|
|
2
|
|
|
|
|
1088
|
|
|
10
|
|
|
|
|
|
|
|
|
11
|
2
|
|
|
2
|
|
827
|
use Data::Generator::FromDDL; |
|
|
2
|
|
|
|
|
6
|
|
|
|
2
|
|
|
|
|
70
|
|
|
12
|
2
|
|
|
2
|
|
19
|
use Data::Generator::FromDDL::Util; |
|
|
2
|
|
|
|
|
3
|
|
|
|
2
|
|
|
|
|
2108
|
|
|
13
|
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
sub parse_num_option { |
|
15
|
|
|
|
|
|
|
# $num_option_str is like 'users:20,bugs:30,100' |
|
16
|
1
|
|
|
1
|
0
|
3
|
my ($self, $num_option_str) = @_; |
|
17
|
1
|
|
|
|
|
4
|
my @nums = split ',', $num_option_str; |
|
18
|
|
|
|
|
|
|
|
|
19
|
1
|
|
|
|
|
6
|
my $parsed = { |
|
20
|
|
|
|
|
|
|
all => undef, |
|
21
|
|
|
|
|
|
|
tables => {}, |
|
22
|
|
|
|
|
|
|
}; |
|
23
|
1
|
|
|
|
|
3
|
for (@nums) { |
|
24
|
1
|
|
|
|
|
2
|
my ($table, $n) = split ':', $_; |
|
25
|
1
|
50
|
|
|
|
4
|
if ($n) { |
|
26
|
0
|
|
|
|
|
0
|
$parsed->{tables}{$table} = $n; |
|
27
|
|
|
|
|
|
|
} else { |
|
28
|
|
|
|
|
|
|
# $table contains number |
|
29
|
1
|
|
|
|
|
5
|
$parsed->{all} = $table; |
|
30
|
|
|
|
|
|
|
} |
|
31
|
|
|
|
|
|
|
} |
|
32
|
|
|
|
|
|
|
|
|
33
|
1
|
|
|
|
|
4
|
$self->num($parsed); |
|
34
|
1
|
|
|
|
|
7
|
return $parsed; |
|
35
|
|
|
|
|
|
|
} |
|
36
|
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
sub parse_byte_string { |
|
38
|
7
|
|
|
7
|
0
|
1036
|
my ($self, $byte_string) = @_; |
|
39
|
|
|
|
|
|
|
|
|
40
|
7
|
|
|
|
|
37
|
my ($numeric, $unit) = ($byte_string =~ m/(\d+)([^\d]*)/); |
|
41
|
7
|
100
|
|
|
|
32
|
my $factor = $unit =~ /KB?/i ? 1024 |
|
|
|
100
|
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
: $unit =~ /MB?/i ? 1024 * 1024 |
|
43
|
|
|
|
|
|
|
: $unit =~ /GB?/i ? 1024 * 1024 * 1024 |
|
44
|
|
|
|
|
|
|
: 1 |
|
45
|
|
|
|
|
|
|
; |
|
46
|
7
|
|
|
|
|
17
|
my $bytes = $numeric * $factor; |
|
47
|
|
|
|
|
|
|
|
|
48
|
7
|
|
|
|
|
23
|
$self->bytes_per_sql($bytes); |
|
49
|
7
|
|
|
|
|
61
|
return $bytes; |
|
50
|
|
|
|
|
|
|
} |
|
51
|
|
|
|
|
|
|
|
|
52
|
|
|
|
|
|
|
sub read_ddl { |
|
53
|
1
|
|
|
1
|
0
|
2
|
my ($self, $ddl_files) = @_; |
|
54
|
|
|
|
|
|
|
|
|
55
|
1
|
|
|
|
|
4
|
local $/; |
|
56
|
1
|
|
|
|
|
2
|
my $ddl; |
|
57
|
1
|
50
|
|
|
|
3
|
if (@$ddl_files) { |
|
58
|
|
|
|
|
|
|
# read from multiple files |
|
59
|
1
|
|
|
|
|
3
|
my $ddl_str = ''; |
|
60
|
1
|
|
|
|
|
3
|
for my $ddl_file (@$ddl_files) { |
|
61
|
1
|
50
|
|
|
|
48
|
open my $fh, '<', $ddl_file |
|
62
|
|
|
|
|
|
|
or die("Can't open $ddl_file to read\n"); |
|
63
|
1
|
|
|
|
|
71
|
$ddl_str .= <$fh>; |
|
64
|
|
|
|
|
|
|
} |
|
65
|
1
|
|
|
|
|
3
|
$ddl = $ddl_str; |
|
66
|
|
|
|
|
|
|
} else { |
|
67
|
0
|
|
|
|
|
0
|
$ddl = ; |
|
68
|
|
|
|
|
|
|
} |
|
69
|
|
|
|
|
|
|
|
|
70
|
1
|
|
|
|
|
7
|
$self->ddl($ddl); |
|
71
|
1
|
|
|
|
|
9
|
return $ddl; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
sub setup_out_fh { |
|
75
|
1
|
|
|
1
|
0
|
2
|
my ($self, $out) = @_; |
|
76
|
|
|
|
|
|
|
|
|
77
|
1
|
|
|
|
|
1
|
my $out_fh; |
|
78
|
1
|
50
|
|
|
|
4
|
if ($out) { |
|
79
|
1
|
50
|
|
|
|
61
|
open $out_fh, '>', $out |
|
80
|
|
|
|
|
|
|
or die("Can't open $out to write\n"); |
|
81
|
|
|
|
|
|
|
} else { |
|
82
|
0
|
|
|
|
|
0
|
$out_fh = *STDOUT; |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
|
|
85
|
1
|
|
|
|
|
5
|
$self->out_fh($out_fh); |
|
86
|
1
|
|
|
|
|
7
|
return $out_fh; |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
sub setup_include_exclude { |
|
90
|
1
|
|
|
1
|
0
|
3
|
my ($self, $include, $exclude) = @_; |
|
91
|
1
|
|
|
|
|
4
|
my @include = split ',', $include; |
|
92
|
1
|
|
|
|
|
2
|
my @exclude = split ',', $exclude; |
|
93
|
1
|
|
|
|
|
6
|
$self->include(\@include); |
|
94
|
1
|
|
|
|
|
7
|
$self->exclude(\@exclude); |
|
95
|
|
|
|
|
|
|
} |
|
96
|
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
sub run { |
|
98
|
1
|
|
|
1
|
0
|
3274
|
my ($self, @args) = @_; |
|
99
|
|
|
|
|
|
|
|
|
100
|
1
|
|
|
|
|
2
|
my $help; |
|
101
|
1
|
|
|
|
|
3
|
my $n = 10;; |
|
102
|
1
|
|
|
|
|
2
|
my $parser = 'mysql'; |
|
103
|
1
|
|
|
|
|
2
|
my $include = ''; |
|
104
|
1
|
|
|
|
|
2
|
my $exclude = ''; |
|
105
|
1
|
|
|
|
|
1
|
my $out; |
|
106
|
1
|
|
|
|
|
2
|
my $format = 'sql'; |
|
107
|
1
|
|
|
|
|
2
|
my $pretty; |
|
108
|
1
|
|
|
|
|
1
|
my $bytes_per_sql = 1024 * 1024; # 1MB |
|
109
|
|
|
|
|
|
|
|
|
110
|
1
|
|
|
|
|
4
|
local @ARGV = @args; |
|
111
|
1
|
50
|
|
|
|
10
|
GetOptions( |
|
112
|
|
|
|
|
|
|
"help|h" => \$help, |
|
113
|
|
|
|
|
|
|
"num|n=s" => \$n, |
|
114
|
|
|
|
|
|
|
"parser|p=s" => \$parser, |
|
115
|
|
|
|
|
|
|
"include|i=s" => \$include, |
|
116
|
|
|
|
|
|
|
"exclude|e=s" => \$exclude, |
|
117
|
|
|
|
|
|
|
"out|o=s" => \$out, |
|
118
|
|
|
|
|
|
|
"format|f=s" => \$format, |
|
119
|
|
|
|
|
|
|
"pretty" => \$pretty, |
|
120
|
|
|
|
|
|
|
"bytes_per_sql=s" => \$bytes_per_sql, |
|
121
|
|
|
|
|
|
|
) or pod2usage(2); |
|
122
|
|
|
|
|
|
|
|
|
123
|
1
|
50
|
|
|
|
1047
|
pod2usage({ |
|
124
|
|
|
|
|
|
|
-exitval => 0, |
|
125
|
|
|
|
|
|
|
-verbose => 99, |
|
126
|
|
|
|
|
|
|
-noperldoc => 1, |
|
127
|
|
|
|
|
|
|
-sections => 'SYNOPSIS|OPTIONS', |
|
128
|
|
|
|
|
|
|
}) if $help; |
|
129
|
|
|
|
|
|
|
|
|
130
|
1
|
50
|
33
|
|
|
11
|
pod2usage({ |
|
131
|
|
|
|
|
|
|
-message => "Can't specify both of --include and --exclude options", |
|
132
|
|
|
|
|
|
|
-exitval => 1, |
|
133
|
|
|
|
|
|
|
-verbose => 99, |
|
134
|
|
|
|
|
|
|
-noperldoc => 1, |
|
135
|
|
|
|
|
|
|
-sections => 'SYNOPSIS|OPTIONS', |
|
136
|
|
|
|
|
|
|
}) if $include && $exclude; |
|
137
|
|
|
|
|
|
|
|
|
138
|
1
|
|
|
|
|
7
|
$self->read_ddl(\@ARGV); |
|
139
|
1
|
|
|
|
|
4
|
$self->setup_out_fh($out); |
|
140
|
1
|
|
|
|
|
5
|
$self->setup_include_exclude($include, $exclude); |
|
141
|
1
|
|
|
|
|
9
|
$self->parse_num_option($n); |
|
142
|
1
|
|
|
|
|
3
|
$self->parse_byte_string($bytes_per_sql); |
|
143
|
|
|
|
|
|
|
|
|
144
|
1
|
|
|
|
|
4
|
my $generator = Data::Generator::FromDDL->new({ |
|
145
|
|
|
|
|
|
|
ddl => $self->ddl, |
|
146
|
|
|
|
|
|
|
parser => $parser, |
|
147
|
|
|
|
|
|
|
include => $self->include, |
|
148
|
|
|
|
|
|
|
exclude => $self->exclude, |
|
149
|
|
|
|
|
|
|
}); |
|
150
|
|
|
|
|
|
|
|
|
151
|
1
|
|
|
|
|
31
|
eval { |
|
152
|
1
|
|
|
|
|
7
|
$generator->generate( |
|
153
|
|
|
|
|
|
|
$self->num, |
|
154
|
|
|
|
|
|
|
$self->out_fh, |
|
155
|
|
|
|
|
|
|
$format, |
|
156
|
|
|
|
|
|
|
$pretty, |
|
157
|
|
|
|
|
|
|
$self->bytes_per_sql |
|
158
|
|
|
|
|
|
|
); |
|
159
|
1
|
|
|
|
|
39
|
close $self->out_fh; |
|
160
|
|
|
|
|
|
|
}; |
|
161
|
1
|
50
|
|
|
|
117
|
if (my $err = $@) { |
|
162
|
0
|
|
|
|
|
|
$err =~ s/([^\n]*)\n.*/$1/; |
|
163
|
0
|
|
|
|
|
|
print colored($err, 'red'); |
|
164
|
0
|
|
|
|
|
|
exit 1; |
|
165
|
|
|
|
|
|
|
} |
|
166
|
|
|
|
|
|
|
} |
|
167
|
|
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
1; |