| 1 | |||
| 2 | package Xmldoom::Schema::Table; | ||
| 3 | |||
| 4 | use Xmldoom::Schema::Column; | ||
| 5 | use Xmldoom::Schema::ForeignKey; | ||
| 6 | use Xmldoom::Threads; | ||
| 7 | use strict; | ||
| 8 | |||
| 9 | sub new | ||
| 10 | { | ||
| 11 | my $class = shift; | ||
| 12 | my $args = shift; | ||
| 13 | |||
| 14 | my $parent; | ||
| 15 | my $name; | ||
| 16 | |||
| 17 | if ( ref($args) eq 'HASH' ) | ||
| 18 | { | ||
| 19 | $parent = $args->{parent}; | ||
| 20 | $name = $args->{name}; | ||
| 21 | } | ||
| 22 | else | ||
| 23 | { | ||
| 24 | $parent = $args; | ||
| 25 | $name = shift; | ||
| 26 | } | ||
| 27 | |||
| 28 | my $self = { | ||
| 29 | parent => $parent, | ||
| 30 | name => $name, | ||
| 31 | columns => [ ], | ||
| 32 | foreign_keys => [ ] | ||
| 33 | }; | ||
| 34 | |||
| 35 | bless $self, $class; | ||
| 36 | return Xmldoom::Threads::make_shared($self, $args->{shared}); | ||
| 37 | } | ||
| 38 | |||
| 39 | sub DESTROY | ||
| 40 | { | ||
| 41 | my $self = shift; | ||
| 42 | |||
| 43 | # we don't need no stinking weak references! | ||
| 44 | $self->{parent} = undef; | ||
| 45 | } | ||
| 46 | |||
| 47 | sub get_schema { return shift->{parent}; } | ||
| 48 | sub get_name { return shift->{name}; } | ||
| 49 | |||
| 50 | sub get_columns | ||
| 51 | { | ||
| 52 | my $self = shift; | ||
| 53 | my $args = shift; | ||
| 54 | |||
| 55 | # deal with the simplest case | ||
| 56 | if ( not defined $args ) | ||
| 57 | { | ||
| 58 | return $self->{columns}; | ||
| 59 | } | ||
| 60 | |||
| 61 | my $names; | ||
| 62 | my $primary_key = 0; | ||
| 63 | my $data_only = 0; | ||
| 64 | |||
| 65 | if ( ref($args) eq 'ARRAY' ) | ||
| 66 | { | ||
| 67 | $names = $args; | ||
| 68 | } | ||
| 69 | elsif ( ref($args) eq 'HASH' ) | ||
| 70 | { | ||
| 71 | $primary_key = $args->{primary_key} if defined $args->{primary_key}; | ||
| 72 | $data_only = $args->{data_only} if defined $args->{data_only}; | ||
| 73 | } | ||
| 74 | |||
| 75 | my @cols; | ||
| 76 | foreach my $col ( @{$self->{columns}} ) | ||
| 77 | { | ||
| 78 | if ( defined $names ) | ||
| 79 | { | ||
| 80 | # only add those colums matched in the names list | ||
| 81 | foreach my $name ( @$names ) | ||
| 82 | { | ||
| 83 | if ( $col->get_name() eq $name ) | ||
| 84 | { | ||
| 85 | push @cols, $col; | ||
| 86 | last; | ||
| 87 | } | ||
| 88 | } | ||
| 89 | } | ||
| 90 | else | ||
| 91 | { | ||
| 92 | if ( $primary_key and $col->is_primary_key() ) | ||
| 93 | { | ||
| 94 | # only the primary key | ||
| 95 | push @cols, $col; | ||
| 96 | } | ||
| 97 | elsif ( $data_only and not $col->is_primary_key() ) | ||
| 98 | { | ||
| 99 | # only the data | ||
| 100 | push @cols, $col; | ||
| 101 | } | ||
| 102 | } | ||
| 103 | } | ||
| 104 | |||
| 105 | return \@cols; | ||
| 106 | } | ||
| 107 | |||
| 108 | sub get_column_names | ||
| 109 | { | ||
| 110 | my $self = shift; | ||
| 111 | |||
| 112 | my $columns = $self->get_columns(@_); | ||
| 113 | my @ret = map { $_->get_name() } @$columns; | ||
| 114 | |||
| 115 | return \@ret; | ||
| 116 | } | ||
| 117 | |||
| 118 | sub get_column | ||
| 119 | { | ||
| 120 | my ($self, $name) = @_; | ||
| 121 | |||
| 122 | my @cols = grep { $_->{name} eq $name } @{$self->{columns}}; | ||
| 123 | if ( scalar @cols != 1 ) | ||
| 124 | { | ||
| 125 | return undef; | ||
| 126 | } | ||
| 127 | |||
| 128 | return $cols[0]; | ||
| 129 | } | ||
| 130 | |||
| 131 | sub get_primary_key | ||
| 132 | { | ||
| 133 | my $self = shift; | ||
| 134 | |||
| 135 | my @columns; | ||
| 136 | |||
| 137 | foreach my $col ( @{$self->{columns}} ) | ||
| 138 | { | ||
| 139 | if ( $col->{primary_key} ) | ||
| 140 | { | ||
| 141 | push @columns, $col; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | |||
| 145 | return \@columns; | ||
| 146 | } | ||
| 147 | |||
| 148 | sub get_column_type | ||
| 149 | { | ||
| 150 | my ($self, $name) = @_; | ||
| 151 | |||
| 152 | my $column = $self->get_column($name); | ||
| 153 | if ( defined $column ) | ||
| 154 | { | ||
| 155 | return $column->get_data_type(); | ||
| 156 | } | ||
| 157 | return undef; | ||
| 158 | } | ||
| 159 | |||
| 160 | sub get_foreign_keys { return shift->{foreign_keys}; } | ||
| 161 | |||
| 162 | # TODO: this function operates like before but before it was broken! | ||
| 163 | sub get_foreign_key | ||
| 164 | { | ||
| 165 | my ($self, $name) = @_; | ||
| 166 | |||
| 167 | foreach my $key ( @{$self->get_foreign_keys()} ) | ||
| 168 | { | ||
| 169 | my $pair_name = $key->get_foreign_pair_name( $name ); | ||
| 170 | |||
| 171 | if ( defined $pair_name ) | ||
| 172 | { | ||
| 173 | return { | ||
| 174 | foreign_table => $key->get_reference_table_name(), | ||
| 175 | local_column => $name, | ||
| 176 | foreign_column => $pair_name | ||
| 177 | }; | ||
| 178 | } | ||
| 179 | } | ||
| 180 | |||
| 181 | return undef; | ||
| 182 | } | ||
| 183 | |||
| 184 | sub add_column | ||
| 185 | { | ||
| 186 | my $self; | ||
| 187 | my @args; | ||
| 188 | |||
| 189 | # sneak the parent argument in. | ||
| 190 | if ( ref($_[1]) eq 'HASH' ) | ||
| 191 | { | ||
| 192 | $self = shift; | ||
| 193 | @args = ( $_[1] ); | ||
| 194 | $args[0]->{parent} = $self; | ||
| 195 | } | ||
| 196 | else | ||
| 197 | { | ||
| 198 | $self = $_[0]; | ||
| 199 | @args = @_; | ||
| 200 | } | ||
| 201 | |||
| 202 | my $col = Xmldoom::Schema::Column->new(@_); | ||
| 203 | |||
| 204 | if ( $self->get_column( $col->{name} ) ) | ||
| 205 | { | ||
| 206 | die "Table already has a column named \"$col->{name}\""; | ||
| 207 | } | ||
| 208 | |||
| 209 | push @{$self->{columns}}, $col; | ||
| 210 | } | ||
| 211 | |||
| 212 | sub add_foreign_key | ||
| 213 | { | ||
| 214 | my $self = shift; | ||
| 215 | my $args = shift; | ||
| 216 | |||
| 217 | if ( ref($args) eq 'HASH' ) | ||
| 218 | { | ||
| 219 | $args->{parent} = $self; | ||
| 220 | } | ||
| 221 | else | ||
| 222 | { | ||
| 223 | $args = { | ||
| 224 | parent => $self, | ||
| 225 | reference_table => $args, | ||
| 226 | local_columns => shift, | ||
| 227 | foreign_columns => shift | ||
| 228 | }; | ||
| 229 | } | ||
| 230 | |||
| 231 | push @{$self->{foreign_keys}}, Xmldoom::Schema::ForeignKey->new($args); | ||
| 232 | } | ||
| 233 | |||
| 234 | 1; | ||
| 235 |