| 1 | package SQL::Translator::Producer::XML::SQLFairy; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | =pod | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | =head1 NAME | ||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | SQL::Translator::Producer::XML::SQLFairy - SQLFairy's default XML format | ||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||
| 11 | use SQL::Translator; | ||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | my $t = SQL::Translator->new( | ||||||||||||||||||||||||||||
| 14 | from => 'MySQL', | ||||||||||||||||||||||||||||
| 15 | to => 'XML-SQLFairy', | ||||||||||||||||||||||||||||
| 16 | filename => 'schema.sql', | ||||||||||||||||||||||||||||
| 17 | show_warnings => 1, | ||||||||||||||||||||||||||||
| 18 | ); | ||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | print $t->translate; | ||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | Creates XML output of a schema, in the flavor of XML used natively by the | ||||||||||||||||||||||||||||
| 25 | SQLFairy project (L<SQL::Translator>). This format is detailed here. | ||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | The XML lives in the C<http://sqlfairy.sourceforge.net/sqlfairy.xml> namespace. | ||||||||||||||||||||||||||||
| 28 | With a root element of <schema>. | ||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||
| 30 | Objects in the schema are mapped to tags of the same name as the objects class | ||||||||||||||||||||||||||||
| 31 | (all lowercase). | ||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||
| 33 | The attributes of the objects (e.g. $field->name) are mapped to attributes of | ||||||||||||||||||||||||||||
| 34 | the tag, except for sql, comments and action, which get mapped to child data | ||||||||||||||||||||||||||||
| 35 | elements. | ||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||
| 37 | List valued attributes (such as the list of fields in an index) | ||||||||||||||||||||||||||||
| 38 | get mapped to comma separated lists of values in the attribute. | ||||||||||||||||||||||||||||
| 39 | |||||||||||||||||||||||||||||
| 40 | Child objects, such as a tables fields, get mapped to child tags wrapped in a | ||||||||||||||||||||||||||||
| 41 | set of container tags using the plural of their contained classes name. | ||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | An objects' extra attribute (a hash of arbitrary data) is | ||||||||||||||||||||||||||||
| 44 | mapped to a tag called extra, with the hash of data as attributes, sorted into | ||||||||||||||||||||||||||||
| 45 | alphabetical order. | ||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||
| 47 | e.g. | ||||||||||||||||||||||||||||
| 48 | |||||||||||||||||||||||||||||
| 49 | <schema name="" database="" | ||||||||||||||||||||||||||||
| 50 | xmlns="http://sqlfairy.sourceforge.net/sqlfairy.xml"> | ||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||
| 52 | <tables> | ||||||||||||||||||||||||||||
| 53 | <table name="Story" order="1"> | ||||||||||||||||||||||||||||
| 54 | <fields> | ||||||||||||||||||||||||||||
| 55 | <field name="id" data_type="BIGINT" size="20" | ||||||||||||||||||||||||||||
| 56 | is_nullable="0" is_auto_increment="1" is_primary_key="1" | ||||||||||||||||||||||||||||
| 57 | is_foreign_key="0" order="3"> | ||||||||||||||||||||||||||||
| 58 | <extra ZEROFILL="1" /> | ||||||||||||||||||||||||||||
| 59 | <comments></comments> | ||||||||||||||||||||||||||||
| 60 | </field> | ||||||||||||||||||||||||||||
| 61 | <field name="created" data_type="datetime" size="0" | ||||||||||||||||||||||||||||
| 62 | is_nullable="1" is_auto_increment="0" is_primary_key="0" | ||||||||||||||||||||||||||||
| 63 | is_foreign_key="0" order="1"> | ||||||||||||||||||||||||||||
| 64 | <extra /> | ||||||||||||||||||||||||||||
| 65 | <comments></comments> | ||||||||||||||||||||||||||||
| 66 | </field> | ||||||||||||||||||||||||||||
| 67 | ... | ||||||||||||||||||||||||||||
| 68 | </fields> | ||||||||||||||||||||||||||||
| 69 | <indices> | ||||||||||||||||||||||||||||
| 70 | <index name="foobar" type="NORMAL" fields="foo,bar" options="" /> | ||||||||||||||||||||||||||||
| 71 | </indices> | ||||||||||||||||||||||||||||
| 72 | </table> | ||||||||||||||||||||||||||||
| 73 | </tables> | ||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||
| 75 | <views> | ||||||||||||||||||||||||||||
| 76 | <view name="email_list" fields="email" order="1"> | ||||||||||||||||||||||||||||
| 77 | <sql>SELECT email FROM Basic WHERE email IS NOT NULL</sql> | ||||||||||||||||||||||||||||
| 78 | </view> | ||||||||||||||||||||||||||||
| 79 | </views> | ||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||
| 81 | </schema> | ||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||
| 83 | To see a complete example of the XML translate one of your schema :) | ||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | $ sqlt -f MySQL -t XML-SQLFairy schema.sql | ||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | =head1 ARGS | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | =over 4 | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | =item add_prefix | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | Set to true to use the default namespace prefix of 'sqlf', instead of using | ||||||||||||||||||||||||||||
| 94 | the default namespace for | ||||||||||||||||||||||||||||
| 95 | C<http://sqlfairy.sourceforge.net/sqlfairy.xml namespace> | ||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||
| 97 | e.g. | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | <!-- add_prefix=0 --> | ||||||||||||||||||||||||||||
| 100 | <field name="foo" /> | ||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||
| 102 | <!-- add_prefix=1 --> | ||||||||||||||||||||||||||||
| 103 | <sqlf:field name="foo" /> | ||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||
| 105 | =item prefix | ||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | Set to the namespace prefix you want to use for the | ||||||||||||||||||||||||||||
| 108 | C<http://sqlfairy.sourceforge.net/sqlfairy.xml namespace> | ||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | e.g. | ||||||||||||||||||||||||||||
| 111 | |||||||||||||||||||||||||||||
| 112 | <!-- prefix='foo' --> | ||||||||||||||||||||||||||||
| 113 | <foo:field name="foo" /> | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | =item newlines | ||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||
| 117 | If true (the default) inserts newlines around the XML, otherwise the schema is | ||||||||||||||||||||||||||||
| 118 | written on one line. | ||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||
| 120 | =item indent | ||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | When using newlines the number of whitespace characters to use as the indent. | ||||||||||||||||||||||||||||
| 123 | Default is 2, set to 0 to turn off indenting. | ||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||
| 125 | =back | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | =head1 LEGACY FORMAT | ||||||||||||||||||||||||||||
| 128 | |||||||||||||||||||||||||||||
| 129 | The previous version of the SQLFairy XML allowed the attributes of the | ||||||||||||||||||||||||||||
| 130 | schema objects to be written as either xml attributes or as data elements, in | ||||||||||||||||||||||||||||
| 131 | any combination. The old producer could produce attribute only or data element | ||||||||||||||||||||||||||||
| 132 | only versions. While this allowed for lots of flexibility in writing the XML | ||||||||||||||||||||||||||||
| 133 | the result is a great many possible XML formats, not so good for DTD writing, | ||||||||||||||||||||||||||||
| 134 | XPathing etc! So we have moved to a fixed version described above. | ||||||||||||||||||||||||||||
| 135 | |||||||||||||||||||||||||||||
| 136 | This version of the producer will now only produce the new style XML. | ||||||||||||||||||||||||||||
| 137 | To convert your old format files simply pass them through the translator :) | ||||||||||||||||||||||||||||
| 138 | |||||||||||||||||||||||||||||
| 139 | $ sqlt -f XML-SQLFairy -t XML-SQLFairy schema-old.xml > schema-new.xml | ||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||
| 141 | =cut | ||||||||||||||||||||||||||||
| 142 | |||||||||||||||||||||||||||||
| 143 | 5 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 144 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 145 | 5 | our @EXPORT_OK; | |||||||||||||||||||||||||||
| 146 | 5 | our $VERSION = '1.66'; | |||||||||||||||||||||||||||
| 147 | |||||||||||||||||||||||||||||
| 148 | 5 | ▶ | use Exporter; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 149 | 5 | ▶ | use base qw(Exporter); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 150 | 5 | @EXPORT_OK = qw(produce); | |||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||
| 152 | 5 | ▶ | use SQL::Translator::Utils qw(header_comment debug); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 153 | |||||||||||||||||||||||||||||
| 154 | 5 | ▶ | BEGIN { | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 155 | # Will someone fix XML::Writer already? | ||||||||||||||||||||||||||||
| 156 | 5 | local $^W = 0; | |||||||||||||||||||||||||||
| 157 | 5 | require XML::Writer; | |||||||||||||||||||||||||||
| 158 | 5 | import XML::Writer; | |||||||||||||||||||||||||||
| 159 | } | ||||||||||||||||||||||||||||
| 160 | |||||||||||||||||||||||||||||
| 161 | # Which schema object attributes (methods) to write as xml elements rather than | ||||||||||||||||||||||||||||
| 162 | # as attributes. e.g. <comments>blah, blah...</comments> | ||||||||||||||||||||||||||||
| 163 | 5 | my @MAP_AS_ELEMENTS = qw/sql comments action extra/; | |||||||||||||||||||||||||||
| 164 | |||||||||||||||||||||||||||||
| 165 | 5 | my $Namespace = 'http://sqlfairy.sourceforge.net/sqlfairy.xml'; | |||||||||||||||||||||||||||
| 166 | 5 | my $Name = 'sqlf'; | |||||||||||||||||||||||||||
| 167 | 5 | my $PArgs = {}; | |||||||||||||||||||||||||||
| 168 | 5 | my $no_comments; | |||||||||||||||||||||||||||
| 169 | |||||||||||||||||||||||||||||
| 170 | 10 | ▶ | sub produce { | ||||||||||||||||||||||||||
|
Subroutine
produce: called CC=7 SCAR=19.5
Pod
undocumented
| |||||||||||||||||||||||||||||
| 171 | 10 | my $translator = shift; | |||||||||||||||||||||||||||
| 172 | 10 | my $schema = $translator->schema; | |||||||||||||||||||||||||||
| 173 | 10 | $no_comments = $translator->no_comments; | |||||||||||||||||||||||||||
| 174 | 10 | $PArgs = $translator->producer_args; | |||||||||||||||||||||||||||
| 175 | 10 | ▶ | my $newlines = defined $PArgs->{newlines} ? $PArgs->{newlines} : 1; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 176 | 10 | ▶ | my $indent = defined $PArgs->{indent} ? $PArgs->{indent} : 2; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||
| 178 | # Setup the XML::Writer and set the namespace | ||||||||||||||||||||||||||||
| 179 | 10 | my $io; | |||||||||||||||||||||||||||
| 180 | 10 | my $prefix = ""; | |||||||||||||||||||||||||||
| 181 | 10 | ▶ | $prefix = $Name if $PArgs->{add_prefix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 182 | 10 | ▶ | $prefix = $PArgs->{prefix} if $PArgs->{prefix}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 183 | 10 | my $xml = XML::Writer->new( | |||||||||||||||||||||||||||
| 184 | OUTPUT => \$io, | ||||||||||||||||||||||||||||
| 185 | NAMESPACES => 1, | ||||||||||||||||||||||||||||
| 186 | PREFIX_MAP => { $Namespace => $prefix }, | ||||||||||||||||||||||||||||
| 187 | DATA_MODE => $newlines, | ||||||||||||||||||||||||||||
| 188 | DATA_INDENT => $indent, | ||||||||||||||||||||||||||||
| 189 | ); | ||||||||||||||||||||||||||||
| 190 | |||||||||||||||||||||||||||||
| 191 | # Start the document | ||||||||||||||||||||||||||||
| 192 | 10 | $xml->xmlDecl('UTF-8'); | |||||||||||||||||||||||||||
| 193 | |||||||||||||||||||||||||||||
| 194 | 10 | ▶ | $xml->comment(header_comment('', '')) | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 195 | unless $no_comments; | ||||||||||||||||||||||||||||
| 196 | |||||||||||||||||||||||||||||
| 197 | 10 | xml_obj( | |||||||||||||||||||||||||||
| 198 | $xml, $schema, | ||||||||||||||||||||||||||||
| 199 | tag => "schema", | ||||||||||||||||||||||||||||
| 200 | methods => [qw/name database extra/], | ||||||||||||||||||||||||||||
| 201 | end_tag => 0 | ||||||||||||||||||||||||||||
| 202 | ); | ||||||||||||||||||||||||||||
| 203 | |||||||||||||||||||||||||||||
| 204 | # | ||||||||||||||||||||||||||||
| 205 | # Table | ||||||||||||||||||||||||||||
| 206 | # | ||||||||||||||||||||||||||||
| 207 | 10 | $xml->startTag([ $Namespace => "tables" ]); | |||||||||||||||||||||||||||
| 208 | 10 | for my $table ($schema->get_tables) { | |||||||||||||||||||||||||||
| 209 | 29 | debug "Table:", $table->name; | |||||||||||||||||||||||||||
| 210 | 29 | xml_obj( | |||||||||||||||||||||||||||
| 211 | $xml, $table, | ||||||||||||||||||||||||||||
| 212 | tag => "table", | ||||||||||||||||||||||||||||
| 213 | methods => [qw/name order extra/], | ||||||||||||||||||||||||||||
| 214 | end_tag => 0 | ||||||||||||||||||||||||||||
| 215 | ); | ||||||||||||||||||||||||||||
| 216 | |||||||||||||||||||||||||||||
| 217 | # | ||||||||||||||||||||||||||||
| 218 | # Fields | ||||||||||||||||||||||||||||
| 219 | # | ||||||||||||||||||||||||||||
| 220 | 29 | xml_obj_children( | |||||||||||||||||||||||||||
| 221 | $xml, $table, | ||||||||||||||||||||||||||||
| 222 | tag => 'field', | ||||||||||||||||||||||||||||
| 223 | methods => [ | ||||||||||||||||||||||||||||
| 224 | qw/ | ||||||||||||||||||||||||||||
| 225 | name data_type size is_nullable default_value is_auto_increment | ||||||||||||||||||||||||||||
| 226 | is_primary_key is_foreign_key extra comments order | ||||||||||||||||||||||||||||
| 227 | / | ||||||||||||||||||||||||||||
| 228 | ], | ||||||||||||||||||||||||||||
| 229 | ); | ||||||||||||||||||||||||||||
| 230 | |||||||||||||||||||||||||||||
| 231 | # | ||||||||||||||||||||||||||||
| 232 | # Indices | ||||||||||||||||||||||||||||
| 233 | # | ||||||||||||||||||||||||||||
| 234 | 29 | xml_obj_children( | |||||||||||||||||||||||||||
| 235 | $xml, $table, | ||||||||||||||||||||||||||||
| 236 | tag => 'index', | ||||||||||||||||||||||||||||
| 237 | collection_tag => "indices", | ||||||||||||||||||||||||||||
| 238 | methods => [qw/name type fields options extra/], | ||||||||||||||||||||||||||||
| 239 | ); | ||||||||||||||||||||||||||||
| 240 | |||||||||||||||||||||||||||||
| 241 | # | ||||||||||||||||||||||||||||
| 242 | # Constraints | ||||||||||||||||||||||||||||
| 243 | # | ||||||||||||||||||||||||||||
| 244 | 29 | xml_obj_children( | |||||||||||||||||||||||||||
| 245 | $xml, $table, | ||||||||||||||||||||||||||||
| 246 | tag => 'constraint', | ||||||||||||||||||||||||||||
| 247 | methods => [ | ||||||||||||||||||||||||||||
| 248 | qw/ | ||||||||||||||||||||||||||||
| 249 | name type fields reference_table reference_fields | ||||||||||||||||||||||||||||
| 250 | on_delete on_update match_type expression options deferrable | ||||||||||||||||||||||||||||
| 251 | extra | ||||||||||||||||||||||||||||
| 252 | / | ||||||||||||||||||||||||||||
| 253 | ], | ||||||||||||||||||||||||||||
| 254 | ); | ||||||||||||||||||||||||||||
| 255 | |||||||||||||||||||||||||||||
| 256 | # | ||||||||||||||||||||||||||||
| 257 | # Comments | ||||||||||||||||||||||||||||
| 258 | # | ||||||||||||||||||||||||||||
| 259 | 29 | xml_obj_children( | |||||||||||||||||||||||||||
| 260 | $xml, $table, | ||||||||||||||||||||||||||||
| 261 | tag => 'comment', | ||||||||||||||||||||||||||||
| 262 | collection_tag => "comments", | ||||||||||||||||||||||||||||
| 263 | methods => [ | ||||||||||||||||||||||||||||
| 264 | qw/ | ||||||||||||||||||||||||||||
| 265 | comments | ||||||||||||||||||||||||||||
| 266 | / | ||||||||||||||||||||||||||||
| 267 | ], | ||||||||||||||||||||||||||||
| 268 | ); | ||||||||||||||||||||||||||||
| 269 | |||||||||||||||||||||||||||||
| 270 | 29 | $xml->endTag([ $Namespace => 'table' ]); | |||||||||||||||||||||||||||
| 271 | } | ||||||||||||||||||||||||||||
| 272 | 10 | $xml->endTag([ $Namespace => 'tables' ]); | |||||||||||||||||||||||||||
| 273 | |||||||||||||||||||||||||||||
| 274 | # | ||||||||||||||||||||||||||||
| 275 | # Views | ||||||||||||||||||||||||||||
| 276 | # | ||||||||||||||||||||||||||||
| 277 | 10 | xml_obj_children( | |||||||||||||||||||||||||||
| 278 | $xml, $schema, | ||||||||||||||||||||||||||||
| 279 | tag => 'view', | ||||||||||||||||||||||||||||
| 280 | methods => [qw/name sql fields order extra/], | ||||||||||||||||||||||||||||
| 281 | ); | ||||||||||||||||||||||||||||
| 282 | |||||||||||||||||||||||||||||
| 283 | # | ||||||||||||||||||||||||||||
| 284 | # Tiggers | ||||||||||||||||||||||||||||
| 285 | # | ||||||||||||||||||||||||||||
| 286 | 10 | xml_obj_children( | |||||||||||||||||||||||||||
| 287 | $xml, $schema, | ||||||||||||||||||||||||||||
| 288 | tag => 'trigger', | ||||||||||||||||||||||||||||
| 289 | methods => [ | ||||||||||||||||||||||||||||
| 290 | qw/name database_events action on_table perform_action_when | ||||||||||||||||||||||||||||
| 291 | fields order extra scope/ | ||||||||||||||||||||||||||||
| 292 | ], | ||||||||||||||||||||||||||||
| 293 | ); | ||||||||||||||||||||||||||||
| 294 | |||||||||||||||||||||||||||||
| 295 | # | ||||||||||||||||||||||||||||
| 296 | # Procedures | ||||||||||||||||||||||||||||
| 297 | # | ||||||||||||||||||||||||||||
| 298 | 10 | xml_obj_children( | |||||||||||||||||||||||||||
| 299 | $xml, $schema, | ||||||||||||||||||||||||||||
| 300 | tag => 'procedure', | ||||||||||||||||||||||||||||
| 301 | methods => [qw/name sql parameters owner comments order extra/], | ||||||||||||||||||||||||||||
| 302 | ); | ||||||||||||||||||||||||||||
| 303 | |||||||||||||||||||||||||||||
| 304 | 10 | $xml->endTag([ $Namespace => 'schema' ]); | |||||||||||||||||||||||||||
| 305 | 10 | $xml->end; | |||||||||||||||||||||||||||
| 306 | |||||||||||||||||||||||||||||
| 307 | 10 | return $io; | |||||||||||||||||||||||||||
| 308 | } | ||||||||||||||||||||||||||||
| 309 | |||||||||||||||||||||||||||||
| 310 | # | ||||||||||||||||||||||||||||
| 311 | # Takes and XML::Write object, Schema::* parent object, the tag name, | ||||||||||||||||||||||||||||
| 312 | # the collection name and a list of methods (of the children) to write as XML. | ||||||||||||||||||||||||||||
| 313 | # The collection name defaults to the name with an s on the end and is used to | ||||||||||||||||||||||||||||
| 314 | # work out the method to get the children with. eg a name of 'foo' gives a | ||||||||||||||||||||||||||||
| 315 | # collection of foos and gets the members using ->get_foos. | ||||||||||||||||||||||||||||
| 316 | # | ||||||||||||||||||||||||||||
| 317 | 146 | ▶ | sub xml_obj_children { | ||||||||||||||||||||||||||
|
Subroutine
xml_obj_children: called CC=5 SCAR=16.1
Pod
undocumented
| |||||||||||||||||||||||||||||
| 318 | 146 | my ($xml, $parent) = (shift, shift); | |||||||||||||||||||||||||||
| 319 | 146 | my %args = @_; | |||||||||||||||||||||||||||
| 320 | 146 | my ($name, $collection_name, $methods) = @args{qw/tag collection_tag methods/}; | |||||||||||||||||||||||||||
| 321 | 146 | ▶ | $collection_name ||= "${name}s"; | ||||||||||||||||||||||||||
|
Condition100%2/2
$collection_name ||= "${name}s"
MC/DC100%1/1
$collection_name ||= "${name}s"
$collection_name
Truth table100%2/2
A
A= $collection_name
| |||||||||||||||||||||||||||||
| 322 | |||||||||||||||||||||||||||||
| 323 | 146 | my $meth; | |||||||||||||||||||||||||||
| 324 | 146 | ▶ | if ($collection_name eq 'comments') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 325 | 29 | $meth = 'comments'; | |||||||||||||||||||||||||||
| 326 | } else { | ||||||||||||||||||||||||||||
| 327 | 117 | $meth = "get_$collection_name"; | |||||||||||||||||||||||||||
| 328 | } | ||||||||||||||||||||||||||||
| 329 | |||||||||||||||||||||||||||||
| 330 | 146 | my @kids = $parent->$meth; | |||||||||||||||||||||||||||
| 331 | |||||||||||||||||||||||||||||
| 332 | #@kids || return; | ||||||||||||||||||||||||||||
| 333 | 146 | $xml->startTag([ $Namespace => $collection_name ]); | |||||||||||||||||||||||||||
| 334 | |||||||||||||||||||||||||||||
| 335 | 146 | for my $obj (@kids) { | |||||||||||||||||||||||||||
| 336 | 202 | ▶ | if ($collection_name eq 'comments') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 337 | 0 | $xml->dataElement([ $Namespace => 'comment' ], $obj); | |||||||||||||||||||||||||||
| 338 | } else { | ||||||||||||||||||||||||||||
| 339 | 202 | xml_obj( | |||||||||||||||||||||||||||
| 340 | $xml, $obj, | ||||||||||||||||||||||||||||
| 341 | tag => "$name", | ||||||||||||||||||||||||||||
| 342 | end_tag => 1, | ||||||||||||||||||||||||||||
| 343 | methods => $methods, | ||||||||||||||||||||||||||||
| 344 | ); | ||||||||||||||||||||||||||||
| 345 | } | ||||||||||||||||||||||||||||
| 346 | } | ||||||||||||||||||||||||||||
| 347 | 146 | $xml->endTag([ $Namespace => $collection_name ]); | |||||||||||||||||||||||||||
| 348 | } | ||||||||||||||||||||||||||||
| 349 | |||||||||||||||||||||||||||||
| 350 | # | ||||||||||||||||||||||||||||
| 351 | # Takes an XML::Writer, Schema::* object and list of method names | ||||||||||||||||||||||||||||
| 352 | # and writes the object out as XML. All methods values are written as attributes | ||||||||||||||||||||||||||||
| 353 | # except for the methods listed in @MAP_AS_ELEMENTS which get written as child | ||||||||||||||||||||||||||||
| 354 | # data elements. | ||||||||||||||||||||||||||||
| 355 | # | ||||||||||||||||||||||||||||
| 356 | # The attributes/tags are written in the same order as the method names are | ||||||||||||||||||||||||||||
| 357 | # passed. | ||||||||||||||||||||||||||||
| 358 | # | ||||||||||||||||||||||||||||
| 359 | # TODO | ||||||||||||||||||||||||||||
| 360 | # - Should the Namespace be passed in instead of global? Pass in the same | ||||||||||||||||||||||||||||
| 361 | # as Writer ie [ NS => TAGNAME ] | ||||||||||||||||||||||||||||
| 362 | # | ||||||||||||||||||||||||||||
| 363 | 5 | my $elements_re = join("|", @MAP_AS_ELEMENTS); | |||||||||||||||||||||||||||
| 364 | 5 | $elements_re = qr/^($elements_re)$/; | |||||||||||||||||||||||||||
| 365 | |||||||||||||||||||||||||||||
| 366 | 241 | ▶ | sub xml_obj { | ||||||||||||||||||||||||||
|
Subroutine
xml_obj: called CC=13 SCAR=25.7
Pod
undocumented
| |||||||||||||||||||||||||||||
| 367 | 241 | my ($xml, $obj, %args) = @_; | |||||||||||||||||||||||||||
| 368 | 241 | ▶ | my $tag = $args{'tag'} || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
$args{'tag'} || ''
MC/DC0%0/1
$args{'tag'} || ''
$args{'tag'}
Truth table50%1/2
A
A= $args{'tag'}
| |||||||||||||||||||||||||||||
| 369 | 241 | ▶ | my $end_tag = $args{'end_tag'} || ''; | ||||||||||||||||||||||||||
|
Condition100%2/2
$args{'end_tag'} || ''
MC/DC100%1/1
$args{'end_tag'} || ''
$args{'end_tag'}
Truth table100%2/2
A
A= $args{'end_tag'}
| |||||||||||||||||||||||||||||
| 370 | 241 | my @meths = @{ $args{'methods'} }; | |||||||||||||||||||||||||||
| 371 | 241 | my $empty_tag = 0; | |||||||||||||||||||||||||||
| 372 | |||||||||||||||||||||||||||||
| 373 | # Use array to ensure consistent (ie not hash) ordering of attribs | ||||||||||||||||||||||||||||
| 374 | # The order comes from the meths list passed in. | ||||||||||||||||||||||||||||
| 375 | 241 | my @tags; | |||||||||||||||||||||||||||
| 376 | my @attr; | ||||||||||||||||||||||||||||
| 377 | 241 | foreach (grep { defined $obj->$_ } @meths) { | |||||||||||||||||||||||||||
| 378 | 2130 | ▶ | my $what = m/$elements_re/ ? \@tags : \@attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 379 | 2130 | ▶ | my $val | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 380 | = $_ eq 'extra' | ||||||||||||||||||||||||||||
| 381 | ? { $obj->$_ } | ||||||||||||||||||||||||||||
| 382 | : $obj->$_; | ||||||||||||||||||||||||||||
| 383 | 2130 | ▶ | $val = ref $val eq 'ARRAY' ? join(',', @$val) : $val; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 384 | 2130 | push @$what, $_ => $val; | |||||||||||||||||||||||||||
| 385 | } | ||||||||||||||||||||||||||||
| 386 | 241 | my $child_tags = @tags; | |||||||||||||||||||||||||||
| 387 | 241 | ▶ | $end_tag && !$child_tags | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$end_tag and not $child_tags
MC/DC0%0/2
$end_tag and not $child_tags
$end_tag
not $child_tags
Truth table66%2/3
A and not(B)
A= $end_tagB= not $child_tags
| |||||||||||||||||||||||||||||
| 388 | ? $xml->emptyTag([ $Namespace => $tag ], @attr) | ||||||||||||||||||||||||||||
| 389 | : $xml->startTag([ $Namespace => $tag ], @attr); | ||||||||||||||||||||||||||||
| 390 | 241 | ▶ | while (my ($name, $val) = splice @tags, 0, 2) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 391 | 401 | ▶ | if (ref $val eq 'HASH') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 392 | 241 | $xml->emptyTag([ $Namespace => $name ], map { ($_, $val->{$_}) } sort keys %$val); | |||||||||||||||||||||||||||
| 393 | } else { | ||||||||||||||||||||||||||||
| 394 | 160 | $xml->dataElement([ $Namespace => $name ], $val); | |||||||||||||||||||||||||||
| 395 | } | ||||||||||||||||||||||||||||
| 396 | } | ||||||||||||||||||||||||||||
| 397 | 241 | ▶ | $xml->endTag([ $Namespace => $tag ]) if $child_tags && $end_tag; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
$child_tags and $end_tag
MC/DC50%1/2
$child_tags and $end_tag
$child_tags
$end_tag
Truth table66%2/3
A and B
A= $child_tagsB= $end_tag
| |||||||||||||||||||||||||||||
| 398 | } | ||||||||||||||||||||||||||||
| 399 | |||||||||||||||||||||||||||||
| 400 | 5 | 1; | |||||||||||||||||||||||||||
| 401 | |||||||||||||||||||||||||||||
| 402 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 403 | # The eyes of fire, the nostrils of air, | ||||||||||||||||||||||||||||
| 404 | # The mouth of water, the beard of earth. | ||||||||||||||||||||||||||||
| 405 | # William Blake | ||||||||||||||||||||||||||||
| 406 | # ------------------------------------------------------------------- | ||||||||||||||||||||||||||||
| 407 | |||||||||||||||||||||||||||||
| 408 | =pod | ||||||||||||||||||||||||||||
| 409 | |||||||||||||||||||||||||||||
| 410 | =head1 AUTHORS | ||||||||||||||||||||||||||||
| 411 | |||||||||||||||||||||||||||||
| 412 | Ken Youens-Clark E<lt>kclark@cpan.orgE<gt>, | ||||||||||||||||||||||||||||
| 413 | Darren Chamberlain E<lt>darren@cpan.orgE<gt>, | ||||||||||||||||||||||||||||
| 414 | Mark Addison E<lt>mark.addison@itn.co.ukE<gt>. | ||||||||||||||||||||||||||||
| 415 | |||||||||||||||||||||||||||||
| 416 | =head1 SEE ALSO | ||||||||||||||||||||||||||||
| 417 | |||||||||||||||||||||||||||||
| 418 | C<perl(1)>, L<SQL::Translator>, L<SQL::Translator::Parser::XML::SQLFairy>, | ||||||||||||||||||||||||||||
| 419 | L<SQL::Translator::Schema>, L<XML::Writer>. | ||||||||||||||||||||||||||||
| 420 | |||||||||||||||||||||||||||||
| 421 | =cut | ||||||||||||||||||||||||||||