package FabForce::DBDesigner4::XML; use 5.006001; use strict; use warnings; use XML::Twig; use XML::Writer; use IO::File; use FabForce::DBDesigner4::Table qw(:const); our $VERSION = '0.04'; sub new{ my ($class) = @_; my $self = {}; bless $self,$class; $self->_reset_tables; $self->_is_fabforce( 0 ); $self->_id( 0 ); $self->_reset_columns; $self->_reset_tableids; $self->_reset_relationsid; return $self; }# new sub writeXML{ my ($self,$structref,$file) = @_; return unless(ref($structref) eq 'ARRAY'); $self->_reset_tableids; $self->_id( 0 ); $self->_reset_relationsid; my $fh = (defined $file) ? IO::File->new(">$file") : \*STDOUT; my $xml = XML::Writer->new(OUTPUT => $fh, UNSAFE => 1, NEWLINE => 1); $xml->xmlDecl('ISO-8859-1','yes'); $xml->startTag("DBMODEL",version => "4.0"); # general settings for DBDesigner $xml->startTag("SETTINGS"); $xml->raw(_constants('DATATYPEGROUPS')); $xml->raw(_constants('DATATYPES')); $xml->raw(_constants('REGIONCOLORS')); $xml->endTag("SETTINGS"); # metadata $xml->startTag("METADATA"); $xml->startTag("TABLES"); $xml->raw(_printTables($self,$structref)); $xml->endTag("TABLES"); $xml->startTag("RELATIONS"); $xml->raw(_printRelations($self,$structref)); $xml->endTag("RELATIONS"); $xml->endTag("METADATA"); # plugindata $xml->startTag("PLUGINDATA"); $xml->endTag("PLUGINDATA"); # querydata $xml->startTag("QUERYDATA"); $xml->endTag("QUERYDATA"); # linked models $xml->startTag("LINKEDMODELS"); $xml->endTag("LINKEDMODELS"); $xml->endTag("DBMODEL"); $xml->end(); $fh->close() if(ref($fh) ne 'GLOB'); }# writeXML sub parsefile{ my ($self,$filename) = @_; return unless $filename; $self->_reset_tables; $self->_is_fabforce( 0 ); my $parser = XML::Twig->new(twig_handlers => { 'TABLE' => sub{_tables($self,@_)}, 'COLUMN' => sub{_column($self,@_)}, 'RELATION' => sub{_relation($self,@_)}, 'INDEXCOLUMN' => sub{_index($self,@_)}, 'DBMODEL' => sub{$self->_is_fabforce(1)}, }); $parser->parsefile($filename); my $root = $parser->root; return unless($self->_is_fabforce); for my $table( $self->_all_tables ){ $table->columns( $self->_table_columns( $table->name ) ); $table->key( $self->_key( $table->name ) ); } return [ $self->_all_tables ]; }# parsefile sub _tables{ my ($self,$t,$table) = @_; my $name = $table->{att}->{Tablename}; my $xPos = $table->{att}->{XPos}; my $yPos = $table->{att}->{YPos}; my $tableobj = FabForce::DBDesigner4::Table->new(); $tableobj->name($name); $tableobj->coords([$xPos,$yPos,0,0]); $self->_add_table( $tableobj ); $self->_tableid( $table->{att}->{ID}, $name ); }# _tables sub _column{ my ($self,$t,$col) = @_; my $parent_table = $col->{parent}->{parent}->{att}->{Tablename}; my $name = $col->{att}->{ColName}; my $datatype = _datatypes('id2name',$col->{att}->{idDatatype}); my $notnull = $col->{att}->{NotNull} ? 'NOT NULL' : ''; my $default = $col->{att}->{DefaultValue}; my $autoinc = $col->{att}->{AutoInc} ? 'AUTOINCREMENT' : ''; if( $datatype !~ m!INT! ){ $autoinc = ""; } my $quotes = ( defined $default and $default =~ m!^\d+(?:\.\d*)?$! ) ? "" : "'"; my $info = ''; $info .= $notnull.' ' if $notnull; $info .= sprintf "DEFAULT %s%s%s ", $quotes,$default,$quotes if defined $default and $default ne ''; $info .= $autoinc if $autoinc; $info =~ s!\s+\z!!; $self->_add_columns( $parent_table, {$name => [$datatype,$info]} ); $self->_key( $parent_table, $name ) if $col->{att}->{PrimaryKey}; }# _column sub _relation{ my ($self,$t,$rel) = @_; my $src = $self->_tableid( $rel->{att}->{SrcTable} ); my @relations = split(/\\n/,$rel->{att}->{FKFields}); my ($obj) = grep{$_->name() eq $src}$self->_all_tables; my $f_id = $self->_tableid( $rel->{att}->{DestTable} ); my ($f_table) = grep{$_->name() eq $f_id}$self->_all_tables; my $type = $rel->{att}->{Kind}; for my $relation(@relations){ my ($owncol,$foreign) = split(/=/,$relation,2); $obj->addRelation( [ 1, $f_id.'.'.$foreign, $src.'.'.$owncol, $type ]); $f_table->addRelation([ 1, $f_id.'.'.$foreign, $src.'.'.$owncol, $type ]); } }# _relation sub _index{ my ($self) = @_; }# _index sub _printTables{ my ($self,$struct) = @_; my %optionselected = (0 => [1,5,6,6,19,20,33,34,35], 1 => [1,2,3,4,5], ); my $string = ''; # table attributes my @att_order = qw(ID Tablename PrevTableName XPos YPos TableType TablePrefix nmTable Temporary UseStandardInserts StandardInserts TableOptions Comments Collapsed IsLinkedObject IDLinkedModel Obj_id_Linked OrderPos); # column attributes my @att_names = qw(ID ColName PrevColName Pos idDatatype DatatypeParams Width Prec PrimaryKey NotNull AutoInc IsForeignKey DefaultValue Comments); for my $table(@{$struct}){ my $id = $self->_id; $self->_id( $id+1 ); my $attributes = ''; my $tablename = $table->name(); $self->_tableid( $self->_id, $tablename ); for my $att(@att_order){ my $value = ''; if($att eq 'ID'){ $value = $self->_id; } elsif($att eq 'Tablename'){ $value = $table->name(); } elsif($att eq 'XPos'){ $value = ($table->coords())[0] || $self->_id * 20; } elsif($att eq 'YPos'){ $value = ($table->coords())[1] || $self->_id * 30; } elsif($att eq 'TableType'){ $value = 'MyISAM'; } elsif($att eq 'PrevTableName'){ $value = 'Table_'.sprintf("%02d",$self->_id); } $attributes .= ' '.$att.'="'.$value.'"'; } $string .= '