line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package SQL::Translator::Producer::JSON; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
=head1 NAME |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
SQL::Translator::Producer::JSON - A JSON producer for SQL::Translator |
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
=head1 SYNOPSIS |
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
use SQL::Translator; |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
my $translator = SQL::Translator->new(producer => 'JSON'); |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 DESCRIPTION |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
This module serializes a schema to a JSON string. |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
=cut |
18
|
|
|
|
|
|
|
|
19
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
30
|
|
20
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
21
|
|
|
|
|
|
|
our $VERSION = '1.63'; |
22
|
|
|
|
|
|
|
|
23
|
1
|
|
|
1
|
|
513
|
use JSON::MaybeXS 'to_json'; |
|
1
|
|
|
|
|
6227
|
|
|
1
|
|
|
|
|
1020
|
|
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
sub produce { |
26
|
1
|
|
|
1
|
0
|
3
|
my $translator = shift; |
27
|
1
|
|
|
|
|
144
|
my $schema = $translator->schema; |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
return to_json({ |
30
|
|
|
|
|
|
|
schema => { |
31
|
|
|
|
|
|
|
tables => { |
32
|
2
|
|
|
|
|
41
|
map { ($_->name => view_table($_)) } |
33
|
|
|
|
|
|
|
$schema->get_tables, |
34
|
|
|
|
|
|
|
}, |
35
|
|
|
|
|
|
|
views => { |
36
|
1
|
|
|
|
|
12
|
map { ($_->name => view_view($_)) } |
37
|
|
|
|
|
|
|
$schema->get_views, |
38
|
|
|
|
|
|
|
}, |
39
|
|
|
|
|
|
|
triggers => { |
40
|
1
|
|
|
|
|
21
|
map { ($_->name => view_trigger($_)) } |
41
|
|
|
|
|
|
|
$schema->get_triggers, |
42
|
|
|
|
|
|
|
}, |
43
|
|
|
|
|
|
|
procedures => { |
44
|
0
|
|
|
|
|
0
|
map { ($_->name => view_procedure($_)) } |
45
|
|
|
|
|
|
|
$schema->get_procedures, |
46
|
|
|
|
|
|
|
}, |
47
|
|
|
|
|
|
|
}, |
48
|
|
|
|
|
|
|
translator => { |
49
|
|
|
|
|
|
|
add_drop_table => $translator->add_drop_table, |
50
|
|
|
|
|
|
|
filename => $translator->filename, |
51
|
|
|
|
|
|
|
no_comments => $translator->no_comments, |
52
|
|
|
|
|
|
|
parser_args => $translator->parser_args, |
53
|
|
|
|
|
|
|
producer_args => $translator->producer_args, |
54
|
|
|
|
|
|
|
parser_type => $translator->parser_type, |
55
|
|
|
|
|
|
|
producer_type => $translator->producer_type, |
56
|
|
|
|
|
|
|
show_warnings => $translator->show_warnings, |
57
|
|
|
|
|
|
|
trace => $translator->trace, |
58
|
|
|
|
|
|
|
version => $translator->version, |
59
|
|
|
|
|
|
|
}, |
60
|
1
|
|
|
|
|
23
|
keys %{$schema->extra} ? ('extra' => { $schema->extra } ) : (), |
61
|
|
|
|
|
|
|
}, { |
62
|
|
|
|
|
|
|
allow_blessed => 1, |
63
|
|
|
|
|
|
|
allow_unknown => 1, |
64
|
2
|
|
|
|
|
40
|
( map { $_ => $translator->producer_args->{$_} } |
65
|
1
|
50
|
|
|
|
16
|
grep { defined $translator->producer_args->{$_} } |
|
3
|
|
|
|
|
55
|
|
66
|
|
|
|
|
|
|
qw[ pretty indent canonical ] ), |
67
|
|
|
|
|
|
|
}); |
68
|
|
|
|
|
|
|
} |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
sub view_table { |
71
|
2
|
|
|
2
|
0
|
54
|
my $table = shift; |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
return { |
74
|
|
|
|
|
|
|
'name' => $table->name, |
75
|
|
|
|
|
|
|
'order' => $table->order, |
76
|
|
|
|
|
|
|
'options' => $table->options || [], |
77
|
|
|
|
|
|
|
$table->comments ? ('comments' => [ $table->comments ] ) : (), |
78
|
|
|
|
|
|
|
'constraints' => [ |
79
|
5
|
|
|
|
|
35
|
map { view_constraint($_) } $table->get_constraints |
80
|
|
|
|
|
|
|
], |
81
|
|
|
|
|
|
|
'indices' => [ |
82
|
0
|
|
|
|
|
0
|
map { view_index($_) } $table->get_indices |
83
|
|
|
|
|
|
|
], |
84
|
|
|
|
|
|
|
'fields' => { |
85
|
10
|
|
|
|
|
189
|
map { ($_->name => view_field($_)) } |
86
|
|
|
|
|
|
|
$table->get_fields |
87
|
|
|
|
|
|
|
}, |
88
|
2
|
100
|
50
|
|
|
39
|
keys %{$table->extra} ? ('extra' => { $table->extra } ) : (), |
|
2
|
50
|
|
|
|
53
|
|
89
|
|
|
|
|
|
|
}; |
90
|
|
|
|
|
|
|
} |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
sub view_constraint { |
93
|
5
|
|
|
5
|
0
|
11
|
my $constraint = shift; |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
return { |
96
|
|
|
|
|
|
|
'deferrable' => scalar $constraint->deferrable, |
97
|
|
|
|
|
|
|
'expression' => scalar $constraint->expression, |
98
|
5
|
50
|
|
|
|
88
|
'fields' => [ map { ref $_ ? $_->name : $_ } $constraint->field_names ], |
99
|
|
|
|
|
|
|
'match_type' => scalar $constraint->match_type, |
100
|
|
|
|
|
|
|
'name' => scalar $constraint->name, |
101
|
|
|
|
|
|
|
'options' => scalar $constraint->options, |
102
|
|
|
|
|
|
|
'on_delete' => scalar $constraint->on_delete, |
103
|
|
|
|
|
|
|
'on_update' => scalar $constraint->on_update, |
104
|
1
|
50
|
|
|
|
24
|
'reference_fields' => [ map { ref $_ ? $_->name : $_ } $constraint->reference_fields ], |
105
|
|
|
|
|
|
|
'reference_table' => scalar $constraint->reference_table, |
106
|
|
|
|
|
|
|
'type' => scalar $constraint->type, |
107
|
5
|
50
|
|
|
|
90
|
keys %{$constraint->extra} ? ('extra' => { $constraint->extra } ) : (), |
|
5
|
|
|
|
|
190
|
|
108
|
|
|
|
|
|
|
}; |
109
|
|
|
|
|
|
|
} |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub view_field { |
112
|
10
|
|
|
10
|
0
|
200
|
my $field = shift; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
return { |
115
|
|
|
|
|
|
|
'order' => scalar $field->order, |
116
|
|
|
|
|
|
|
'name' => scalar $field->name, |
117
|
|
|
|
|
|
|
'data_type' => scalar $field->data_type, |
118
|
|
|
|
|
|
|
'size' => [ $field->size ], |
119
|
|
|
|
|
|
|
'default_value' => scalar $field->default_value, |
120
|
|
|
|
|
|
|
'is_nullable' => scalar $field->is_nullable, |
121
|
|
|
|
|
|
|
'is_primary_key' => scalar $field->is_primary_key, |
122
|
|
|
|
|
|
|
'is_unique' => scalar $field->is_unique, |
123
|
|
|
|
|
|
|
$field->is_auto_increment ? ('is_auto_increment' => 1) : (), |
124
|
|
|
|
|
|
|
$field->comments ? ('comments' => [ $field->comments ]) : (), |
125
|
10
|
100
|
|
|
|
192
|
keys %{$field->extra} ? ('extra' => { $field->extra } ) : (), |
|
10
|
100
|
|
|
|
244
|
|
|
|
50
|
|
|
|
|
|
126
|
|
|
|
|
|
|
}; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub view_procedure { |
130
|
0
|
|
|
0
|
0
|
0
|
my $procedure = shift; |
131
|
|
|
|
|
|
|
|
132
|
|
|
|
|
|
|
return { |
133
|
|
|
|
|
|
|
'order' => scalar $procedure->order, |
134
|
|
|
|
|
|
|
'name' => scalar $procedure->name, |
135
|
|
|
|
|
|
|
'sql' => scalar $procedure->sql, |
136
|
|
|
|
|
|
|
'parameters' => scalar $procedure->parameters, |
137
|
|
|
|
|
|
|
'owner' => scalar $procedure->owner, |
138
|
|
|
|
|
|
|
$procedure->comments ? ('comments' => [ $procedure->comments ] ) : (), |
139
|
0
|
0
|
|
|
|
0
|
keys %{$procedure->extra} ? ('extra' => { $procedure->extra } ) : (), |
|
0
|
0
|
|
|
|
0
|
|
140
|
|
|
|
|
|
|
}; |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
sub view_trigger { |
144
|
1
|
|
|
1
|
0
|
6
|
my $trigger = shift; |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
return { |
147
|
|
|
|
|
|
|
'order' => scalar $trigger->order, |
148
|
|
|
|
|
|
|
'name' => scalar $trigger->name, |
149
|
|
|
|
|
|
|
'perform_action_when' => scalar $trigger->perform_action_when, |
150
|
|
|
|
|
|
|
'database_events' => scalar $trigger->database_events, |
151
|
|
|
|
|
|
|
'fields' => scalar $trigger->fields, |
152
|
|
|
|
|
|
|
'on_table' => scalar $trigger->on_table, |
153
|
|
|
|
|
|
|
'action' => scalar $trigger->action, |
154
|
|
|
|
|
|
|
(defined $trigger->scope ? ( |
155
|
|
|
|
|
|
|
'scope' => scalar $trigger->scope, |
156
|
|
|
|
|
|
|
) : ()), |
157
|
1
|
50
|
|
|
|
21
|
keys %{$trigger->extra} ? ('extra' => { $trigger->extra } ) : (), |
|
1
|
50
|
|
|
|
53
|
|
158
|
|
|
|
|
|
|
}; |
159
|
|
|
|
|
|
|
} |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub view_view { |
162
|
1
|
|
|
1
|
0
|
5
|
my $view = shift; |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
return { |
165
|
|
|
|
|
|
|
'order' => scalar $view->order, |
166
|
|
|
|
|
|
|
'name' => scalar $view->name, |
167
|
|
|
|
|
|
|
'sql' => scalar $view->sql, |
168
|
|
|
|
|
|
|
'fields' => scalar $view->fields, |
169
|
1
|
50
|
|
|
|
20
|
keys %{$view->extra} ? ('extra' => { $view->extra } ) : (), |
|
1
|
|
|
|
|
23
|
|
170
|
|
|
|
|
|
|
}; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
sub view_index { |
174
|
0
|
|
|
0
|
0
|
|
my $index = shift; |
175
|
|
|
|
|
|
|
|
176
|
|
|
|
|
|
|
return { |
177
|
|
|
|
|
|
|
'name' => scalar $index->name, |
178
|
|
|
|
|
|
|
'type' => scalar $index->type, |
179
|
|
|
|
|
|
|
'fields' => scalar $index->fields, |
180
|
|
|
|
|
|
|
'options' => scalar $index->options, |
181
|
0
|
0
|
|
|
|
|
keys %{$index->extra} ? ('extra' => { $index->extra } ) : (), |
|
0
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
}; |
183
|
|
|
|
|
|
|
} |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
1; |
186
|
|
|
|
|
|
|
|
187
|
|
|
|
|
|
|
=head1 SEE ALSO |
188
|
|
|
|
|
|
|
|
189
|
|
|
|
|
|
|
SQL::Translator, JSON::MaybeXS, http://www.json.org/. |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=head1 AUTHORS |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
darren chamberlain Edarren@cpan.orgE, |
194
|
|
|
|
|
|
|
Ken Youens-Clark Ekclark@cpan.orgE. |
195
|
|
|
|
|
|
|
Jon Jensen Ejonj@cpan.orgE. |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |