| 1 | |||
| 2 | package SQL::Translator::Parser::XML::Xmldoom; | ||
| 3 | use base qw(SQL::Translator::Parser); | ||
| 4 | |||
| 5 | use Xmldoom::Schema::SAXHandler; | ||
| 6 | use SQL::Translator; | ||
| 7 | use SQL::Translator::Schema::Constants; | ||
| 8 | use XML::SAX; | ||
| 9 | use strict; | ||
| 10 | |||
| 11 | sub parse | ||
| 12 | { | ||
| 13 | my ($translator, $data) = @_; | ||
| 14 | |||
| 15 | my $sqlfairy_parser = SQL::Translator::Parser::XML::Xmldoom->new( $translator->schema ); | ||
| 16 | my $handler = Xmldoom::Schema::SAXHandler->new({ parser => $sqlfairy_parser }); | ||
| 17 | my $parser = XML::SAX::ParserFactory->parser(Handler => $handler); | ||
| 18 | |||
| 19 | $parser->parse_string($data); | ||
| 20 | |||
| 21 | return 1; | ||
| 22 | } | ||
| 23 | |||
| 24 | sub new | ||
| 25 | { | ||
| 26 | my $class = shift; | ||
| 27 | my $args = shift; | ||
| 28 | |||
| 29 | my $schema; | ||
| 30 | |||
| 31 | if ( ref($args) eq 'HASH' ) | ||
| 32 | { | ||
| 33 | $schema = $args->{schema}; | ||
| 34 | } | ||
| 35 | else | ||
| 36 | { | ||
| 37 | $schema = $args; | ||
| 38 | } | ||
| 39 | |||
| 40 | my $self = { | ||
| 41 | schema => $schema | ||
| 42 | }; | ||
| 43 | |||
| 44 | bless $self, $class; | ||
| 45 | return $self; | ||
| 46 | } | ||
| 47 | |||
| 48 | sub setup_database | ||
| 49 | { | ||
| 50 | my ($self, $args) = @_; | ||
| 51 | |||
| 52 | if ( defined $args->{name} ) | ||
| 53 | { | ||
| 54 | $self->{schema}->name( $args->{name} ); | ||
| 55 | } | ||
| 56 | if ( defined $args->{defaultIdMethod} ) | ||
| 57 | { | ||
| 58 | $self->{schema}->extra( defaultIdMethod => $args->{defaultIdMethod} ); | ||
| 59 | } | ||
| 60 | } | ||
| 61 | |||
| 62 | sub add_table | ||
| 63 | { | ||
| 64 | my ($self, $args) = @_; | ||
| 65 | |||
| 66 | my $table = $self->{schema}->add_table(name => $args->{name}); | ||
| 67 | |||
| 68 | if ( defined $args->{description} ) | ||
| 69 | { | ||
| 70 | $table->extra( description => $args->{description} ); | ||
| 71 | } | ||
| 72 | |||
| 73 | return $table; | ||
| 74 | } | ||
| 75 | |||
| 76 | sub finish_table | ||
| 77 | { | ||
| 78 | my ($self, $table) = @_; | ||
| 79 | |||
| 80 | my @primary_key; | ||
| 81 | |||
| 82 | foreach my $field ( $table->get_fields() ) | ||
| 83 | { | ||
| 84 | if ( $field->is_primary_key ) | ||
| 85 | { | ||
| 86 | push @primary_key, $field->name; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | |||
| 90 | # setup the primary key constraint | ||
| 91 | $table->add_constraint( | ||
| 92 | type => PRIMARY_KEY, | ||
| 93 | fields => \@primary_key | ||
| 94 | ); | ||
| 95 | } | ||
| 96 | |||
| 97 | sub add_column | ||
| 98 | { | ||
| 99 | my ($self, $table, $args) = @_; | ||
| 100 | |||
| 101 | my $sqlfairy = { | ||
| 102 | name => $args->{name}, | ||
| 103 | data_type => $args->{type}, | ||
| 104 | }; | ||
| 105 | |||
| 106 | if ( defined $args->{required} ) | ||
| 107 | { | ||
| 108 | $sqlfairy->{is_nullable} = not $args->{required}; | ||
| 109 | } | ||
| 110 | if ( defined $args->{primary_key} ) | ||
| 111 | { | ||
| 112 | $sqlfairy->{is_primary_key} = $args->{primary_key}; | ||
| 113 | } | ||
| 114 | if ( defined $args->{size} ) | ||
| 115 | { | ||
| 116 | $sqlfairy->{size} = $args->{size}; | ||
| 117 | } | ||
| 118 | if ( defined $args->{auto_increment} ) | ||
| 119 | { | ||
| 120 | $sqlfairy->{is_auto_increment} = $args->{auto_increment}; | ||
| 121 | } | ||
| 122 | if ( defined $args->{default} ) | ||
| 123 | { | ||
| 124 | $sqlfairy->{default_value} = $args->{default} | ||
| 125 | } | ||
| 126 | |||
| 127 | my $field = $table->add_field( %$sqlfairy ); | ||
| 128 | |||
| 129 | if ( defined $args->{description} ) | ||
| 130 | { | ||
| 131 | $field->extra( description => $args->{description} ); | ||
| 132 | } | ||
| 133 | if ( defined $args->{id_generator} ) | ||
| 134 | { | ||
| 135 | $field->extra( idGenerator => $args->{id_generator} ); | ||
| 136 | } | ||
| 137 | if ( defined $args->{timestamp} ) | ||
| 138 | { | ||
| 139 | $field->extra( timestamp => $args->{timestamp} ); | ||
| 140 | } | ||
| 141 | if ( defined $args->{options} ) | ||
| 142 | { | ||
| 143 | $field->extra( list => $args->{options} ); | ||
| 144 | } | ||
| 145 | } | ||
| 146 | |||
| 147 | sub add_foreign_key | ||
| 148 | { | ||
| 149 | my ($self, $table, $args) = @_; | ||
| 150 | |||
| 151 | $table->add_constraint( | ||
| 152 | type => FOREIGN_KEY, | ||
| 153 | fields => $args->{local_columns}, | ||
| 154 | reference_fields => $args->{foreign_columns}, | ||
| 155 | reference_table => $args->{foreign_table} | ||
| 156 | ); | ||
| 157 | } | ||
| 158 | |||
| 159 | 1; | ||
| 160 | |||
| 161 | __END__ |