blib/lib/SQL/Translator/Parser/XML/SQLFairy.pm

filter: Statementstmt 96.3% 105 / 109 Branchbran 68.0% 34 / 50 Conditioncond 58.8% 10 / 17 MC/DCmcdc 30.0% 3 / 10 Subroutinesub 100% 12 / 12 Podpod 0.0% 0 / 2 totaltotal 82.0% 164 / 200 CC 42 SCAR 31.9
CC
42
Cov
85%
parse
37.1
get_tagfields
14.5
BEGIN
1.0
CRAP 24.3
« blib/lib/SQL/Translator/Parser/xSV.pm ↑ Summary blib/lib/SQL/Translator/Parser/YAML.pm »
1 package SQL::Translator::Parser::XML::SQLFairy;
2  
3 =head1 NAME
4  
5 SQL::Translator::Parser::XML::SQLFairy - parser for SQL::Translator's XML.
6  
7 =head1 SYNOPSIS
8  
9 use SQL::Translator;
10  
11 my $translator = SQL::Translator->new( show_warnings => 1 );
12  
13 my $out = $obj->translate(
14 from => 'XML-SQLFairy',
15 to => 'MySQL',
16 filename => 'schema.xml',
17 ) or die $translator->error;
18  
19 print $out;
20  
21 =head1 DESCRIPTION
22  
23 This parser handles the flavor of XML used natively by the SQLFairy
24 project (L<SQL::Translator>). The XML must be in the XML namespace
25 C<http://sqlfairy.sourceforge.net/sqlfairy.xml>.
26 See L<SQL::Translator::Producer::XML::SQLFairy> for details of this format.
27  
28 You do not need to specify every attribute of the Schema objects as any missing
29 from the XML will be set to their default values. e.g. A field could be written
30 using only;
31  
32 <sqlf:field name="email" data_type="varchar" size="255" />
33  
34 Instead of the full;
35  
36 <sqlf:field name="email" data_type="varchar" size="255" is_nullable="1"
37 is_auto_increment="0" is_primary_key="0" is_foreign_key="0" order="4">
38 <sqlf:comments></sqlf:comments>
39 </sqlf:field>
40  
41 If you do not explicitly set the order of items using order attributes on the
42 tags then the order the tags appear in the XML will be used.
43  
44 =head2 default_value
45  
46 Leave the attribute out all together to use the default in
47 L<SQL::Translator::Schema::Field>. Use empty quotes or 'EMPTY_STRING'
48 for a zero length string. 'NULL' for an explicit null (currently sets
49 default_value to undef in the field object).
50  
51 <sqlf:field default_value="" /> <!-- Empty string -->
52 <sqlf:field default_value="EMPTY_STRING" /> <!-- Empty string -->
53 <sqlf:field default_value="NULL" /> <!-- NULL -->
54  
55 =head2 ARGS
56  
57 Doesn't take any extra parser args at the moment.
58  
59 =head1 LEGACY FORMAT
60  
61 The previous version of the SQLFairy XML allowed the attributes of the
62 schema objects to be written as either xml attributes or as data elements, in
63 any combination. While this allows for lots of flexibility in writing the XML
64 the result is a great many possible XML formats, not so good for DTD writing,
65 XPathing etc! So we have moved to a fixed version described in
66 L<SQL::Translator::Producer::XML::SQLFairy>.
67  
68 This version of the parser will still parse the old formats and emit warnings
69 when it sees them being used but they should be considered B<heavily
70 depreciated>.
71  
72 To convert your old format files simply pass them through the translator :)
73  
74 $ sqlt -f XML-SQLFairy -t XML-SQLFairy schema-old.xml > schema-new.xml
75  
76 =cut
77  
78 14 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
79 14 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
80  
81 14 our ($DEBUG, @EXPORT_OK);
82 14 our $VERSION = '1.66';
83 14 ▶$DEBUG = 0 unless defined $DEBUG;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $SQL::Translator::Parser::XML::SQLFairy::DEBUG 14 0 14
84  
85 14 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
86 14 ▶use Carp::Clan qw/^SQL::Translator/;
Subroutine BEGIN: called CC=1 SCAR=0.0
87 14 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
88 14 ▶use base qw(Exporter);
Subroutine BEGIN: called CC=1 SCAR=0.0
89 14 @EXPORT_OK = qw(parse);
90  
91 14 ▶use base qw/SQL::Translator::Parser/; # Doesnt do anything at the mo!
Subroutine BEGIN: called CC=1 SCAR=0.0
92 14 ▶use SQL::Translator::Utils 'debug';
Subroutine BEGIN: called CC=1 SCAR=0.0
93 14 ▶use XML::LibXML;
Subroutine BEGIN: called CC=1 SCAR=0.0
94 14 ▶use XML::LibXML::XPathContext;
Subroutine BEGIN: called CC=1 SCAR=0.0
95  
96 15 ▶sub parse {
Subroutine parse: called CC=29 SCAR=36.1
Pod undocumented
97 15 my ($translator, $data) = @_;
98 15 my $schema = $translator->schema;
99 15 local $DEBUG = $translator->debug;
100 15 my $doc = XML::LibXML->new->parse_string($data);
101 15 my $xp = XML::LibXML::XPathContext->new($doc);
102  
103 15 $xp->registerNs("sqlf", "http://sqlfairy.sourceforge.net/sqlfairy.xml");
104  
105 #
106 # Work our way through the tables
107 #
108 15 my @nodes = $xp->findnodes('/sqlf:schema/sqlf:table|/sqlf:schema/sqlf:tables/sqlf:table');
109 15 for my $tblnode (
110 sort {
111 21 ▶ ("" . $xp->findvalue('sqlf:order|@order', $a) || 0) <=> ("" . $xp->findvalue('sqlf:order|@order', $b) || 0)
Condition50%2/4
'' . $xp->findvalue('sqlf:order|@order', $a) || 0
l!l
210
'' . $xp->findvalue('sqlf:order|@order', $b) || 0
l!l
210
MC/DC0%0/1
'' . $xp->findvalue('sqlf:order|@order', $a) || 0
'' . $xp->findvalue('sqlf:order|@order', $a)
MC/DC0%0/1
'' . $xp->findvalue('sqlf:order|@order', $b) || 0
'' . $xp->findvalue('sqlf:order|@order', $b)
Truth table50%1/2
A
A= '' . $xp->findvalue('sqlf:order|@order', $a)
Aresult
11
00
Truth table50%1/2
A
A= '' . $xp->findvalue('sqlf:order|@order', $b)
Aresult
11
00
112 } @nodes
113 ) {
114 34 debug "Adding table:" . $xp->findvalue('sqlf:name', $tblnode);
115  
116 34 ▶ my $table = $schema->add_table(get_tagfields($xp, $tblnode, "sqlf:" => qw/name order extra/))
Branch: 1 branch
BranchTrueFalseTotal
my $table = $schema->add_table(get_tagfields($xp, $tblnode, "sqlf:", ("name", "order", "extra"))) or die $schema->error 34 0 34
117 or die $schema->error;
118  
119 #
120 # Fields
121 #
122 34 my @nodes = $xp->findnodes('sqlf:fields/sqlf:field', $tblnode);
123 34 ▶ foreach (sort { ("" . $xp->findvalue('sqlf:order', $a) || 0) <=> ("" . $xp->findvalue('sqlf:order', $b) || 0) }
Condition50%2/4
'' . $xp->findvalue('sqlf:order', $a) || 0
l!l
0274
'' . $xp->findvalue('sqlf:order', $b) || 0
l!l
0274
MC/DC0%0/1
'' . $xp->findvalue('sqlf:order', $a) || 0
'' . $xp->findvalue('sqlf:order', $a)
MC/DC0%0/1
'' . $xp->findvalue('sqlf:order', $b) || 0
'' . $xp->findvalue('sqlf:order', $b)
Truth table50%1/2
A
A= '' . $xp->findvalue('sqlf:order', $a)
Aresult
11
00
Truth table50%1/2
A
A= '' . $xp->findvalue('sqlf:order', $b)
Aresult
11
00
124 @nodes) {
125 162 my %fdata = get_tagfields(
126 $xp, $_, "sqlf:",
127 qw/name data_type size default_value is_nullable extra
128 is_auto_increment is_primary_key is_foreign_key comments/
129 );
130  
131 162 ▶ if (exists $fdata{'default_value'}
Branch: 1 branch
BranchTrueFalseTotal
if (exists $fdata{"default_value"} and defined $fdata{"default_value"}) 76 86 162
Condition66%2/3
exists $fdata{"default_value"} and defined $fdata{"default_value"}
!ll&&!rl&&r
86076
MC/DC50%1/2
exists $fdata{"default_value"} and defined $fdata{"default_value"}
exists $fdata{"default_value"} defined $fdata{"default_value"}
Truth table66%2/3
A and B
A= exists $fdata{"default_value"}B= defined $fdata{"default_value"}
ABresult
0X0
100
111
132 and defined $fdata{'default_value'}) {
133 76 ▶ if ($fdata{'default_value'} =~ /^\s*NULL\s*$/) {
Branch: 2 branches
BranchTrueFalseTotal
if ($fdata{'default_value'} =~ /^\s*NULL\s*$/) { } 0 76 76
elsif ($fdata{'default_value'} =~ /^\s*EMPTY_STRING\s*$/) { } 0 76 76
134 0 $fdata{'default_value'} = undef;
135 } elsif ($fdata{'default_value'} =~ /^\s*EMPTY_STRING\s*$/) {
136 0 $fdata{'default_value'} = "";
137 }
138 }
139  
140 162 ▶ my $field = $table->add_field(%fdata) or die $table->error;
Branch: 1 branch
BranchTrueFalseTotal
my $field = $table->add_field(%fdata) or die $table->error 162 0 162
141  
142 162 ▶ $table->primary_key($field->name) if $fdata{'is_primary_key'};
Branch: 1 branch
BranchTrueFalseTotal
if $fdata{"is_primary_key"} 32 130 162
143  
144 #
145 # TODO:
146 # - We should be able to make the table obj spot this when
147 # we use add_field.
148 #
149 }
150  
151 #
152 # Constraints
153 #
154 34 @nodes = $xp->findnodes('sqlf:constraints/sqlf:constraint', $tblnode);
155 34 foreach (@nodes) {
156 69 my %data = get_tagfields(
157 $xp, $_, "sqlf:",
158 qw/name type table fields reference_fields reference_table
159 match_type on_delete on_update extra/
160 );
161 69 ▶ $table->add_constraint(%data) or die $table->error;
Branch: 1 branch
BranchTrueFalseTotal
$table->add_constraint(%data) or die $table->error 69 0 69
162 }
163  
164 #
165 # Indexes
166 #
167 34 @nodes = $xp->findnodes('sqlf:indices/sqlf:index', $tblnode);
168 34 foreach (@nodes) {
169 14 my %data = get_tagfields($xp, $_, "sqlf:", qw/name type fields options extra/);
170 14 ▶ $table->add_index(%data) or die $table->error;
Branch: 1 branch
BranchTrueFalseTotal
$table->add_index(%data) or die $table->error 14 0 14
171 }
172  
173 #
174 # Comments
175 #
176 34 @nodes = $xp->findnodes('sqlf:comments/sqlf:comment', $tblnode);
177 34 foreach (@nodes) {
178 0 my $data = $_->string_value;
179 0 $table->comments($data);
180 }
181  
182 } # tables loop
183  
184 #
185 # Views
186 #
187 15 @nodes = $xp->findnodes('/sqlf:schema/sqlf:view|/sqlf:schema/sqlf:views/sqlf:view');
188 15 foreach (@nodes) {
189 14 my %data = get_tagfields($xp, $_, "sqlf:", qw/name sql fields order extra/);
190 14 ▶ $schema->add_view(%data) or die $schema->error;
Branch: 1 branch
BranchTrueFalseTotal
$schema->add_view(%data) or die $schema->error 14 0 14
191 }
192  
193 #
194 # Triggers
195 #
196 15 @nodes = $xp->findnodes('/sqlf:schema/sqlf:trigger|/sqlf:schema/sqlf:triggers/sqlf:trigger');
197 15 foreach (@nodes) {
198 30 my %data = get_tagfields(
199 $xp, $_, "sqlf:", qw/
200 name perform_action_when database_event database_events fields
201 on_table action order extra scope
202 /
203 );
204  
205 # back compat
206 30 ▶ if (my $evt = $data{database_event} and $translator->{show_warnings}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $evt = $data{"database_event"} and $translator->{"show_warnings"}) 1 29 30
Condition100%3/3
my $evt = $data{"database_event"} and $translator->{"show_warnings"}
!ll&&!rl&&r
17121
MC/DC100%2/2
my $evt = $data{"database_event"} and $translator->{"show_warnings"}
my $evt = $data{"database_event"} $translator->{"show_warnings"}
Truth table100%3/3
A and B
A= my $evt = $data{"database_event"}B= $translator->{"show_warnings"}
ABresult
0X0
100
111
207 1 carp 'The database_event tag is deprecated - please use '
208 . 'database_events (which can take one or more comma separated '
209 . 'event names)';
210 1 ▶ $data{database_events} = join(', ', $data{database_events} || (), $evt,);
Condition33%1/3
$data{'database_events'} || ()
l!l&&r!l&&!r
010
MC/DC0%0/2
$data{'database_events'} || ()
$data{'database_events'} ()
Truth table33%1/3
A || B
A= $data{'database_events'}B= ()
ABresult
1X1
011
000
211 }
212  
213 # split into arrayref
214 30 ▶ if (my $evts = $data{database_events}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $evts = $data{"database_events"}) 18 12 30
215 18 $data{database_events} = [ split(/\s*,\s*/, $evts) ];
216 }
217  
218 30 ▶ $schema->add_trigger(%data) or die $schema->error;
Branch: 1 branch
BranchTrueFalseTotal
$schema->add_trigger(%data) or die $schema->error 30 0 30
219 }
220  
221 #
222 # Procedures
223 #
224 15 @nodes = $xp->findnodes('/sqlf:schema/sqlf:procedure|/sqlf:schema/sqlf:procedures/sqlf:procedure');
225 15 foreach (@nodes) {
226 14 my %data = get_tagfields($xp, $_, "sqlf:", qw/name sql parameters owner comments order extra/);
227 14 ▶ $schema->add_procedure(%data) or die $schema->error;
Branch: 1 branch
BranchTrueFalseTotal
$schema->add_procedure(%data) or die $schema->error 14 0 14
228 }
229  
230 15 return 1;
231 }
232  
233 337 ▶sub get_tagfields {
Subroutine get_tagfields: called CC=14 SCAR=26.7
Pod undocumented
234 #
235 # get_tagfields XP, NODE, NAMESPACE => qw/TAGNAMES/;
236 # get_tagfields $node, "sqlf:" => qw/name type fields reference/;
237 #
238 # Returns hash of data.
239 # TODO - Add handling of an explicit NULL value.
240 #
241  
242 337 my ($xp, $node, @names) = @_;
243 337 my (%data, $ns);
244 337 foreach (@names) {
245 3287 ▶ if (m/:$/) { $ns = $_; next; } # Set def namespace
Branch: 1 branch
BranchTrueFalseTotal
if (/:$/) 337 2950 3287
246 2950 ▶ my $thisns = (s/(^.*?:)// ? $1 : $ns);
Branch: 1 branch
BranchTrueFalseTotal
s/(^.*?:)// ? : 0 2950 2950
247  
248 2950 ▶ my $is_attrib = m/^(sql|comments|action|extra)$/ ? 0 : 1;
Branch: 1 branch
BranchTrueFalseTotal
/^(sql|comments|action|extra)$/ ? : 571 2379 2950
249  
250 2950 my $attrib_path = "\@$_";
251 2950 my $tag_path = "$thisns$_";
252 2950 ▶ if (my $found = $xp->find($attrib_path, $node)) {
Branch: 2 branches
BranchTrueFalseTotal
if (my $found = $xp->find($attrib_path, $node)) { } 1569 1381 2950
elsif ($found = $xp->find($tag_path, $node)) { } 258 1123 1381
253 1569 $data{$_} = "" . $found->to_literal;
254 1569 ▶ warn "Use of '$_' as an attribute is depricated."
Branch: 1 branch
BranchTrueFalseTotal
unless $is_attrib 0 1569 1569
255 . " Use a child tag instead."
256 . " To convert your file to the new version see the Docs.\n"
257 unless $is_attrib;
258 1569 ▶ debug "Got $_=" . (defined $data{$_} ? $data{$_} : 'UNDEF');
Branch: 1 branch
BranchTrueFalseTotal
defined $data{$_} ? : 1569 0 1569
259 } elsif ($found = $xp->find($tag_path, $node)) {
260 258 ▶ if ($_ eq "extra") {
Branch: 1 branch
BranchTrueFalseTotal
if ($_ eq 'extra') { } 159 99 258
261 159 my %extra;
262 159 foreach ($found->pop->getAttributes) {
263 392 $extra{ $_->getName } = $_->getData;
264 }
265 159 $data{$_} = \%extra;
266 } else {
267 99 $data{$_} = "" . $found->to_literal;
268 }
269 258 ▶ warn "Use of '$_' as a child tag is depricated."
Branch: 1 branch
BranchTrueFalseTotal
if $is_attrib 0 258 258
270 . " Use an attribute instead."
271 . " To convert your file to the new version see the Docs.\n"
272 if $is_attrib;
273 258 ▶ debug "Got $_=" . (defined $data{$_} ? $data{$_} : 'UNDEF');
Branch: 1 branch
BranchTrueFalseTotal
defined $data{$_} ? : 258 0 258
274 }
275 }
276  
277 337 ▶ return wantarray ? %data : \%data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 337 0 337
278 }
279  
280 14 1;
281  
282 =pod
283  
284 =head1 BUGS
285  
286 Ignores the order attribute for Constraints, Views, Indices, Views, Triggers
287 and Procedures, using the tag order instead. (This is the order output by the
288 SQLFairy XML producer).
289  
290 =head1 SEE ALSO
291  
292 L<perl>, L<SQL::Translator>, L<SQL::Translator::Producer::XML::SQLFairy>,
293 L<SQL::Translator::Schema>.
294  
295 =head1 TODO
296  
297 =over 4
298  
299 =item *
300  
301 Support options attribute.
302  
303 =item *
304  
305 Test foreign keys are parsed ok.
306  
307 =item *
308  
309 Control over defaulting.
310  
311 =back
312  
313 =head1 AUTHOR
314  
315 Mark D. Addison E<lt>mark.addison@itn.co.ukE<gt>,
316 Jonathan Yu E<lt>frequency@cpan.orgE<gt>
317  
318 =cut
« blib/lib/SQL/Translator/Parser/xSV.pm ↑ Summary blib/lib/SQL/Translator/Parser/YAML.pm »