File Coverage

blib/lib/Data/Generator.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Data::Generator;
2              
3 1     1   26361 use Moose;
  0            
  0            
4              
5             use Data::Dumper;
6             use SQL::Translator;
7              
8             use DateTime;
9             use DateTime::Format::MySQL;
10             use String::Random;
11              
12             with qw/
13             MooseX::Getopt
14             /;
15              
16             our $VERSION = "0.01_1";
17              
18             has 'ddl' => (is => 'rw', isa => 'Str', required => 1);
19             has 'lines' => (is => 'rw', isa => 'Int', required => 1);
20              
21             $| = 1;
22              
23             sub BUILD {
24             my ($self, $opt) = (@_);
25            
26             # ...
27            
28             }
29              
30              
31             sub run {
32             my ($self, $opt) = (@_);
33              
34             my $t = SQL::Translator->new(
35             show_warnings => 1,
36             add_drop_table => 1,
37             quote_table_names => 1,
38             quote_field_names => 1,
39             parser => 'MySQL',
40             producer => 'MySQL',
41             );
42              
43             $t->filters( sub { $self->_filter(@_) }) or die $t->error;
44            
45             print $self->ddl."\n";
46            
47             $t->translate( $self->ddl );
48              
49             }
50              
51              
52             sub _filter {
53             my ($self, $schema) = (@_);
54              
55             for my $table ( $schema->get_tables ) {
56              
57             my @fields_func;
58            
59             print $table->name.":\n";
60              
61             foreach my $field ($table->get_fields) {
62             $field->name(lc($field->name));
63             $field->data_type(lc($field->data_type));
64            
65             print $field->name.' / '.$field->data_type.' / '.$field->size.' / '.$field->comments."\n";
66            
67             if ($field->comments =~ /^rand_/) {
68             push @fields_func, $field->comments;
69             }
70             else {
71            
72             if ($field->data_type eq 'varchar') {
73             push @fields_func, "rand_varchar({size => ".$field->size.", regex => '\\w\\w\\w\\w'})";
74             }
75             elsif ($field->data_type eq 'longblob') {
76             push @fields_func, "rand_int()";
77             }
78             elsif ($field->data_type eq 'datetime') {
79             push @fields_func, "rand_datetime({ year_start => 2000, year_end => 2010,})";
80             }
81             elsif ($field->data_type eq 'char') {
82             push @fields_func, "chr(int(rand_int()))";
83             }
84             elsif ($field->data_type eq 'int') {
85             push @fields_func, "rand_int()";
86             }
87             elsif ($field->data_type eq 'float') {
88             push @fields_func, "rand_float()";
89             }
90            
91             }
92             }
93            
94             open (F, '>', './'.$table->name.'.csv');
95            
96             for (1 .. $self->lines) {
97             foreach my $func (@fields_func) {
98             print F eval($func).";";
99             print $@ if $@;
100             }
101             print F "\n";
102             }
103             close (F);
104            
105              
106             print "\n\n";
107              
108             }
109              
110             }
111              
112              
113              
114             sub rand_datetime {
115             my ($opt) = @_;
116            
117             my $dt = DateTime->new(
118             year => $opt->{year_start} + int(rand($opt->{year_end} - $opt->{year_start})),
119             month => 1 + int(rand(11)),
120             day => 1 + int(rand(30)),
121             );
122            
123             return DateTime::Format::MySQL->format_datetime($dt);
124            
125            
126             }
127              
128              
129              
130              
131             sub rand_varchar {
132             my ($opt) = @_;
133            
134             my $str_random = String::Random->new;
135            
136             $opt->{size} = 10 unless defined($opt->{size});
137            
138             my $string;
139            
140             while (length($string) < $opt->{size}) {
141             $string .= $str_random->randregex($opt->{regex}).' ';
142             }
143            
144             chop($string);
145            
146             return $string;
147            
148             }
149              
150             sub rand_int {
151            
152             return int(rand(10))
153            
154             }
155              
156             sub rand_float {
157            
158             return int(rand(10))
159            
160             }
161              
162             1;
163              
164              
165             __END__