| 1 | |||
| 2 | package Xmldoom::Schema::Parser; | ||
| 3 | |||
| 4 | use Xmldoom::Schema; | ||
| 5 | use Xmldoom::Schema::SAXHandler; | ||
| 6 | use XML::SAX; | ||
| 7 | use strict; | ||
| 8 | |||
| 9 | sub parse | ||
| 10 | { | ||
| 11 | my $args = shift; | ||
| 12 | |||
| 13 | my $schema; | ||
| 14 | my $uri; | ||
| 15 | my $data; | ||
| 16 | my $shared; | ||
| 17 | |||
| 18 | if ( ref($args) eq 'HASH' ) | ||
| 19 | { | ||
| 20 | $schema = $args->{schema}; | ||
| 21 | $uri = $args->{uri}; | ||
| 22 | $data = $args->{data}; | ||
| 23 | } | ||
| 24 | else | ||
| 25 | { | ||
| 26 | $schema = $args; | ||
| 27 | $data = shift; | ||
| 28 | } | ||
| 29 | |||
| 30 | if ( not defined $schema ) | ||
| 31 | { | ||
| 32 | $schema = Xmldoom::Schema->new({ shared => $shared }); | ||
| 33 | } | ||
| 34 | |||
| 35 | my $xmldoom_parser = Xmldoom::Schema::Parser->new( $schema ); | ||
| 36 | my $handler = Xmldoom::Schema::SAXHandler->new({ parser => $xmldoom_parser }); | ||
| 37 | my $parser = XML::SAX::ParserFactory->parser(Handler => $handler); | ||
| 38 | |||
| 39 | if ( defined $uri ) | ||
| 40 | { | ||
| 41 | $parser->parse_uri($uri); | ||
| 42 | } | ||
| 43 | elsif ( defined $data ) | ||
| 44 | { | ||
| 45 | $parser->parse_string($data); | ||
| 46 | } | ||
| 47 | |||
| 48 | return $schema; | ||
| 49 | } | ||
| 50 | |||
| 51 | sub new | ||
| 52 | { | ||
| 53 | my $class = shift; | ||
| 54 | my $args = shift; | ||
| 55 | |||
| 56 | my $schema; | ||
| 57 | |||
| 58 | if ( ref($args) eq 'HASH' ) | ||
| 59 | { | ||
| 60 | $schema = $args->{schema}; | ||
| 61 | } | ||
| 62 | else | ||
| 63 | { | ||
| 64 | $schema = $args; | ||
| 65 | } | ||
| 66 | |||
| 67 | my $self = { | ||
| 68 | schema => $schema, | ||
| 69 | }; | ||
| 70 | |||
| 71 | bless $self, $class; | ||
| 72 | return $self; | ||
| 73 | } | ||
| 74 | |||
| 75 | sub setup_database | ||
| 76 | { | ||
| 77 | my ($self, $args) = @_; | ||
| 78 | } | ||
| 79 | |||
| 80 | sub add_table | ||
| 81 | { | ||
| 82 | my ($self, $args) = @_; | ||
| 83 | |||
| 84 | my $table = $self->{schema}->create_table( $args->{name} ); | ||
| 85 | |||
| 86 | return $table; | ||
| 87 | } | ||
| 88 | |||
| 89 | sub finish_table | ||
| 90 | { | ||
| 91 | my ($self, $table) = @_; | ||
| 92 | } | ||
| 93 | |||
| 94 | sub add_column | ||
| 95 | { | ||
| 96 | my ($self, $table, $args) = @_; | ||
| 97 | |||
| 98 | $table->add_column( $args ); | ||
| 99 | } | ||
| 100 | |||
| 101 | sub add_foreign_key | ||
| 102 | { | ||
| 103 | my ($self, $table, $args) = @_; | ||
| 104 | |||
| 105 | $table->add_foreign_key({ | ||
| 106 | reference_table => $args->{foreign_table}, | ||
| 107 | local_columns => $args->{local_columns}, | ||
| 108 | foreign_columns => $args->{foreign_columns} | ||
| 109 | }); | ||
| 110 | } | ||
| 111 | |||
| 112 | 1; | ||
| 113 |