line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Jorge::Generator::Model; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
891
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
22
|
|
4
|
1
|
|
|
1
|
|
4
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
34
|
|
5
|
|
|
|
|
|
|
|
6
|
|
|
|
|
|
|
=head1 NAME |
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
Jorge::Generator::Model - Jorge based Models generator. Runs with jorge-generate |
9
|
|
|
|
|
|
|
|
10
|
|
|
|
|
|
|
=head1 VERSION |
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
Version 0.02 |
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
=cut |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
17
|
|
|
|
|
|
|
|
18
|
1
|
|
|
1
|
|
1092
|
use Data::Dumper; |
|
1
|
|
|
|
|
6964
|
|
|
1
|
|
|
|
|
59
|
|
19
|
1
|
|
|
1
|
|
1139
|
use Getopt::Long; |
|
1
|
|
|
|
|
11170
|
|
|
1
|
|
|
|
|
6
|
|
20
|
1
|
|
|
1
|
|
1095
|
use Pod::Usage; |
|
1
|
|
|
|
|
55581
|
|
|
1
|
|
|
|
|
149
|
|
21
|
1
|
|
|
1
|
|
11
|
use Carp qw( croak cluck); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1003
|
|
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 FUNCTIONS |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
=head2 run |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=cut |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
my @types = qw( |
30
|
|
|
|
|
|
|
bit |
31
|
|
|
|
|
|
|
char |
32
|
|
|
|
|
|
|
datetime |
33
|
|
|
|
|
|
|
enum |
34
|
|
|
|
|
|
|
int |
35
|
|
|
|
|
|
|
text |
36
|
|
|
|
|
|
|
timestamp |
37
|
|
|
|
|
|
|
varchar |
38
|
|
|
|
|
|
|
pk |
39
|
|
|
|
|
|
|
); |
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
our $char_lenght = 255; |
42
|
|
|
|
|
|
|
our $varchar_lenght = 255; |
43
|
|
|
|
|
|
|
our $int_lenght = 11; |
44
|
|
|
|
|
|
|
|
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
my %sql_types = ( |
47
|
|
|
|
|
|
|
bit => 'TINYINT(1) NOT NULL', |
48
|
|
|
|
|
|
|
char => "CHAR($char_lenght) NOT NULL", |
49
|
|
|
|
|
|
|
date => 'DATETIME NOT NULL', |
50
|
|
|
|
|
|
|
enum => 'ENUM("") NOT NULL', |
51
|
|
|
|
|
|
|
int => "INT($int_lenght) NOT NULL", |
52
|
|
|
|
|
|
|
pk => "INT($int_lenght) NOT NULL AUTO_INCREMENT", |
53
|
|
|
|
|
|
|
text => 'TEXT NOT NULL', |
54
|
|
|
|
|
|
|
timestamp => 'TIMESTAMP NOT NULL default CURRENT_TIMESTAMP', |
55
|
|
|
|
|
|
|
varchar => "VARCHAR($char_lenght) NOT NULL", |
56
|
|
|
|
|
|
|
); |
57
|
|
|
|
|
|
|
|
58
|
|
|
|
|
|
|
sub run { |
59
|
0
|
0
|
|
0
|
1
|
|
pod2usage() unless @ARGV; |
60
|
|
|
|
|
|
|
|
61
|
0
|
|
|
|
|
|
my %config; |
62
|
|
|
|
|
|
|
GetOptions( |
63
|
|
|
|
|
|
|
'model=s' => \$config{_model}, |
64
|
|
|
|
|
|
|
'plural=s' => \$config{_plural}, |
65
|
|
|
|
|
|
|
'fields=s' => \$config{_fields}, |
66
|
0
|
|
|
0
|
|
|
help => sub { pod2usage(); }, |
67
|
0
|
0
|
|
|
|
|
) or pod2usage(); |
68
|
|
|
|
|
|
|
|
69
|
0
|
0
|
0
|
|
|
|
pod2usage() unless ( $config{_model} && $config{_fields} ); |
70
|
|
|
|
|
|
|
|
71
|
0
|
|
|
|
|
|
$config{_model} = ucfirst( $config{_model} ); |
72
|
0
|
|
0
|
|
|
|
$config{_plural} = ucfirst( $config{_plural} || $config{_model} . 's'); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
|
|
|
#now, parse the fields we received. |
75
|
0
|
|
|
|
|
|
my @fields = split( /,/, $config{_fields} ); |
76
|
0
|
|
|
|
|
|
foreach my $f (@fields) { |
77
|
0
|
|
|
|
|
|
my ( $field, $value ) = split( ':', $f ); |
78
|
0
|
0
|
0
|
|
|
|
croak "missing param or value" unless $field && $value; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
my $its_a_class = $value =~ m/[A-Z](.*)/; |
81
|
0
|
|
|
|
|
|
croak "invalid data type \'$value\' on $field" |
82
|
0
|
0
|
0
|
|
|
|
if !( grep { $_ eq $value } @types or $its_a_class ); |
83
|
|
|
|
|
|
|
|
84
|
0
|
|
|
|
|
|
$value = ucfirst $value; |
85
|
0
|
|
|
|
|
|
$field = ucfirst $field; |
86
|
|
|
|
|
|
|
|
87
|
0
|
0
|
|
|
|
|
if ($its_a_class){ |
88
|
0
|
|
|
|
|
|
push( @{ $config{_classes} }, $value ); |
|
0
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
} |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
$config{$field} = $value; |
92
|
0
|
|
|
|
|
|
push( @{ $config{_order} }, $field ); |
|
0
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
} |
94
|
0
|
|
|
|
|
|
write_file($config{_model}, singular(%config)); |
95
|
0
|
|
|
|
|
|
write_file($config{_plural}, plural(%config)); |
96
|
|
|
|
|
|
|
|
97
|
|
|
|
|
|
|
} |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
|
sub singular { |
101
|
0
|
|
|
0
|
0
|
|
my %config = @_; |
102
|
0
|
0
|
|
|
|
|
my @use = @{$config{_classes}} if defined $config{_classes}; |
|
0
|
|
|
|
|
|
|
103
|
0
|
|
|
|
|
|
my @fields_raw = @{$config{_order}}; |
|
0
|
|
|
|
|
|
|
104
|
0
|
|
|
|
|
|
my $use_line = join( "", map { "use $_;\n" } @use ); |
|
0
|
|
|
|
|
|
|
105
|
0
|
|
|
|
|
|
my @fields = grep { $config{$_} =~ m/[a-z]+/ } @fields_raw; |
|
0
|
|
|
|
|
|
|
106
|
|
|
|
|
|
|
|
107
|
0
|
|
|
|
|
|
my $fields_list = join( "\n ", map { $_ } @fields ); |
|
0
|
|
|
|
|
|
|
108
|
|
|
|
|
|
|
#note the spacing. must match the number of spaces in the $tmpl var in |
109
|
|
|
|
|
|
|
# order to properly allign the fields |
110
|
0
|
|
|
|
|
|
my @pks = grep { $config{$_} eq 'Pk' } @fields; |
|
0
|
|
|
|
|
|
|
111
|
0
|
|
|
|
|
|
my $pks_line = join("\n ", map { "$_ => { pk => 1, read_only => 1 }," } @pks); |
|
0
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
|
my @datetimes = grep { $config{$_} eq 'Timestamp' || $config{$_} eq 'Datetime' } @fields; |
|
0
|
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
my $datetime_line = join("\n ", map { "$_ => { datetime => 1 }," } @datetimes); |
|
0
|
|
|
|
|
|
|
114
|
0
|
|
|
|
|
|
my $classes_line = join("\n ", map { "$_ => { class => new $_() }," } @use); |
|
0
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
#sql code |
117
|
0
|
|
|
|
|
|
my $sql_pks = join(",",@pks); |
118
|
0
|
|
0
|
|
|
|
my $sql_fields = join("\n ", map { "`$_` ". ( $sql_types{lc($config{$_})} || 'INT('.$int_lenght.') NOT NULL ') ." ," } @fields); |
|
0
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
|
121
|
0
|
|
|
|
|
|
my $tmpl = <
|
122
|
|
|
|
|
|
|
package $config{_model}; |
123
|
|
|
|
|
|
|
use base 'Jorge::DBEntity'; |
124
|
|
|
|
|
|
|
use Jorge::Plugin::Md5; |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
#Insert any dependencies here. |
127
|
|
|
|
|
|
|
#Example: |
128
|
|
|
|
|
|
|
$use_line |
129
|
|
|
|
|
|
|
|
130
|
|
|
|
|
|
|
use strict; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
sub _fields { |
133
|
|
|
|
|
|
|
|
134
|
|
|
|
|
|
|
my \$table_name = '$config{_model}'; |
135
|
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
my \@fields = qw( |
137
|
|
|
|
|
|
|
$fields_list |
138
|
|
|
|
|
|
|
); |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
my \%fields = ( |
141
|
|
|
|
|
|
|
$pks_line |
142
|
|
|
|
|
|
|
$datetime_line |
143
|
|
|
|
|
|
|
$classes_line |
144
|
|
|
|
|
|
|
); |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
return [ \\\@fields, \\\%fields, \$table_name ]; |
147
|
|
|
|
|
|
|
} |
148
|
|
|
|
|
|
|
|
149
|
|
|
|
|
|
|
1; |
150
|
|
|
|
|
|
|
__END__ |