blib/lib/SQL/Translator/Producer/JSON.pm

filter: Statementstmt 78.0% 39 / 50 Branchbran 50.0% 16 / 32 Conditioncond 12.5% 1 / 8 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 81.8% 9 / 11 Podpod 0.0% 0 / 8 totaltotal 57.5% 65 / 113 CC 20 SCAR 21.5
CC
20
Cov
62%
view_index
30.0
view_procedure
12.0
view_constraint
4.3
CRAP 8.6
« blib/lib/SQL/Translator/Schema/Field.pm ↑ Summary blib/lib/SQL/Translator/Producer.pm »
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 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
20 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 our $VERSION = '1.66';
22  
23 1 ▶use JSON::MaybeXS 'to_json';
Subroutine BEGIN: called CC=1 SCAR=0.0
24  
25 1 ▶sub produce {
Subroutine produce: called CC=2 SCAR=7.0
Pod undocumented
26 1 my $translator = shift;
27 1 my $schema = $translator->schema;
28  
29 return to_json(
30 {
31 schema => {
32 2 tables => { map { ($_->name => view_table($_)) } $schema->get_tables, },
33 1 views => { map { ($_->name => view_view($_)) } $schema->get_views, },
34 1 triggers => { map { ($_->name => view_trigger($_)) } $schema->get_triggers, },
35 0 ▶ procedures => { map { ($_->name => view_procedure($_)) } $schema->get_procedures, },
Branch: 1 branch
BranchTrueFalseTotal
keys %{$schema->extra;} ? : 0 1 1
36 },
37 translator => {
38 add_drop_table => $translator->add_drop_table,
39 filename => $translator->filename,
40 no_comments => $translator->no_comments,
41 parser_args => $translator->parser_args,
42 producer_args => $translator->producer_args,
43 parser_type => $translator->parser_type,
44 producer_type => $translator->producer_type,
45 show_warnings => $translator->show_warnings,
46 trace => $translator->trace,
47 version => $translator->version,
48 },
49 1 keys %{ $schema->extra } ? ('extra' => { $schema->extra }) : (),
50 },
51 {
52 allow_blessed => 1,
53 allow_unknown => 1,
54 (
55 2 map { $_ => $translator->producer_args->{$_} }
56 1 grep { defined $translator->producer_args->{$_} } qw[ pretty indent canonical ]
57 ),
58 }
59 );
60 }
61  
62 2 ▶sub view_table {
Subroutine view_table: called CC=4 SCAR=14.5
Pod undocumented
63 2 my $table = shift;
64  
65 return {
66 'name' => $table->name,
67 'order' => $table->order,
68 'options' => $table->options || [],
69 $table->comments ? ('comments' => [ $table->comments ]) : (),
70 'constraints' => [
71 5 map { view_constraint($_) } $table->get_constraints
72 ],
73 'indices' => [
74 0 map { view_index($_) } $table->get_indices
75 ],
76 'fields' => {
77 10 ▶ map { ($_->name => view_field($_)) }
Branch: 1 branch
BranchTrueFalseTotal
keys %{$table->extra;} ? : 0 2 2
78 $table->get_fields
79 },
80 2 ▶ keys %{ $table->extra } ? ('extra' => { $table->extra }) : (),
Branch: 1 branch
BranchTrueFalseTotal
$table->comments ? : 1 1 2
Condition50%1/2
$table->options || []
l!l
20
MC/DC0%0/1
$table->options || []
$table->options
Truth table50%1/2
A
A= $table->options
Aresult
11
00
81 };
82 }
83  
84 5 ▶sub view_constraint {
Subroutine view_constraint: called CC=4 SCAR=14.6
Pod undocumented
85 5 my $constraint = shift;
86  
87 return {
88 'deferrable' => scalar $constraint->deferrable,
89 'expression' => scalar $constraint->expression,
90 5 ▶ 'fields' => [ map { ref $_ ? $_->name : $_ } $constraint->field_names ],
Branch: 1 branch
BranchTrueFalseTotal
ref $_ ? : 0 5 5
91 'match_type' => scalar $constraint->match_type,
92 'name' => scalar $constraint->name,
93 'options' => scalar $constraint->options,
94 'on_delete' => scalar $constraint->on_delete,
95 'on_update' => scalar $constraint->on_update,
96 1 ▶ 'reference_fields' => [ map { ref $_ ? $_->name : $_ } $constraint->reference_fields ],
Branch: 2 branches
BranchTrueFalseTotal
ref $_ ? : 0 1 1
keys %{$constraint->extra;} ? : 0 5 5
97 'reference_table' => scalar $constraint->reference_table,
98 'type' => scalar $constraint->type,
99 5 keys %{ $constraint->extra }
100 ? ('extra' => { $constraint->extra })
101 : (),
102 };
103 }
104  
105 10 ▶sub view_field {
Subroutine view_field: called CC=4 SCAR=13.9
Pod undocumented
106 10 my $field = shift;
107  
108 return {
109 'order' => scalar $field->order,
110 'name' => scalar $field->name,
111 'data_type' => scalar $field->data_type,
112 'size' => [ $field->size ],
113 'default_value' => scalar $field->default_value,
114 'is_nullable' => scalar $field->is_nullable,
115 'is_primary_key' => scalar $field->is_primary_key,
116 'is_unique' => scalar $field->is_unique,
117 $field->is_auto_increment ? ('is_auto_increment' => 1) : (),
118 $field->comments ? ('comments' => [ $field->comments ]) : (),
119 10 ▶ keys %{ $field->extra } ? ('extra' => { $field->extra }) : (),
Branch: 3 branches
BranchTrueFalseTotal
$field->is_auto_increment ? : 1 9 10
$field->comments ? : 1 9 10
keys %{$field->extra;} ? : 1 9 10
120 };
121 }
122  
123 0 ▶sub view_procedure {
Subroutine view_procedure: not called CC=3 SCAR=24.8
Pod undocumented
124 0 my $procedure = shift;
125  
126 return {
127 'order' => scalar $procedure->order,
128 'name' => scalar $procedure->name,
129 'sql' => scalar $procedure->sql,
130 'parameters' => scalar $procedure->parameters,
131 'owner' => scalar $procedure->owner,
132 $procedure->comments ? ('comments' => [ $procedure->comments ]) : (),
133 0 ▶ keys %{ $procedure->extra } ? ('extra' => { $procedure->extra }) : (),
Branch: 2 branches
BranchTrueFalseTotal
$procedure->comments ? : 0 0 0
keys %{$procedure->extra;} ? : 0 0 0
134 };
135 }
136  
137 1 ▶sub view_trigger {
Subroutine view_trigger: called CC=3 SCAR=11.7
Pod undocumented
138 1 my $trigger = shift;
139  
140 return {
141 'order' => scalar $trigger->order,
142 'name' => scalar $trigger->name,
143 'perform_action_when' => scalar $trigger->perform_action_when,
144 'database_events' => scalar $trigger->database_events,
145 'fields' => scalar $trigger->fields,
146 'on_table' => scalar $trigger->on_table,
147 'action' => scalar $trigger->action,
148 (
149 defined $trigger->scope
150 ? ('scope' => scalar $trigger->scope,)
151 : ()
152 ),
153 1 ▶ keys %{ $trigger->extra } ? ('extra' => { $trigger->extra }) : (),
Branch: 2 branches
BranchTrueFalseTotal
defined $trigger->scope ? : 1 0 1
keys %{$trigger->extra;} ? : 0 1 1
154 };
155 }
156  
157 1 ▶sub view_view {
Subroutine view_view: called CC=2 SCAR=7.1
Pod undocumented
158 1 my $view = shift;
159  
160 return {
161 'order' => scalar $view->order,
162 'name' => scalar $view->name,
163 'sql' => scalar $view->sql,
164 'fields' => scalar $view->fields,
165 1 ▶ keys %{ $view->extra } ? ('extra' => { $view->extra }) : (),
Branch: 1 branch
BranchTrueFalseTotal
keys %{$view->extra;} ? : 0 1 1
166 };
167 }
168  
169 0 ▶sub view_index {
Subroutine view_index: not called CC=5 SCAR=34.0
Pod undocumented
170 0 my $index = shift;
171  
172 return {
173 'name' => scalar $index->name,
174 'type' => scalar $index->type,
175 'fields' => [
176 0 ▶ map { ref($_) && $_->extra && keys %{ $_->extra } ? { name => $_->name, %{ $_->extra } } : "$_" }
Branch: 2 branches
BranchTrueFalseTotal
ref $_ && $_->extra && keys %{$_->extra;} ? : 0 0 0
keys %{$index->extra;} ? : 0 0 0
Condition0%0/6
ref $_ and $_->extra and keys %{$_->extra;}
!ll&&!rl&&r
000
ref $_ and $_->extra
!ll&&!rl&&r
000
MC/DC0%0/3
ref $_ and $_->extra and keys %{$_->extra;}
ref $_ $_->extra keys %{$_->extra;}
Truth table0%0/4
A and B and C
A= ref $_B= $_->extraC= keys %{$_->extra;}
ABCresult
0XX0
10X0
1100
1111
177 $index->fields
178 ],
179 'options' => scalar $index->options,
180 0 keys %{ $index->extra } ? ('extra' => { $index->extra }) : (),
181 };
182 }
183  
184 1 1;
185  
186 =head1 SEE ALSO
187  
188 SQL::Translator, JSON::MaybeXS, http://www.json.org/.
189  
190 =head1 AUTHORS
191  
192 darren chamberlain E<lt>darren@cpan.orgE<gt>,
193 Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>.
194 Jon Jensen E<lt>jonj@cpan.orgE<gt>.
195  
196 =cut
« blib/lib/SQL/Translator/Schema/Field.pm ↑ Summary blib/lib/SQL/Translator/Producer.pm »