| 1 | package Fey::Schema; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 25 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | 25 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 25 | ▶ | use namespace::autoclean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | |||||||||||||||
| 7 | 25 | our $VERSION = '0.42'; | |||||||||||||
| 8 | |||||||||||||||
| 9 | 25 | ▶ | use Fey::Exceptions qw( param_error ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 25 | ▶ | use Fey::NamedObjectSet; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 25 | ▶ | use Fey::SQL; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | 25 | ▶ | use Fey::Table; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | use Fey::Types | ||||||||||||||
| 14 | 25 | ▶ | qw( FK HashRef NamedObjectSet Str Table TableLikeOrName TableOrName ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 15 | 25 | ▶ | use Scalar::Util qw( blessed ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 16 | |||||||||||||||
| 17 | 25 | ▶ | use Moose 0.90; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 18 | 25 | ▶ | use MooseX::Params::Validate 0.08 qw( pos_validated_list ); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 19 | 25 | ▶ | use MooseX::SemiAffordanceAccessor 0.03; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 20 | 25 | ▶ | use MooseX::StrictConstructor 0.07; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | 25 | has 'name' => ( | |||||||||||||
| 23 | is => 'rw', | ||||||||||||||
| 24 | isa => Str, | ||||||||||||||
| 25 | required => 1, | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | has '_tables' => ( | ||||||||||||||
| 29 | is => 'ro', | ||||||||||||||
| 30 | isa => NamedObjectSet, | ||||||||||||||
| 31 | 52 | ▶ | default => sub { return Fey::NamedObjectSet->new() }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 32 | 25 | handles => { | |||||||||||||
| 33 | tables => 'objects', | ||||||||||||||
| 34 | table => 'object', | ||||||||||||||
| 35 | }, | ||||||||||||||
| 36 | init_arg => undef, | ||||||||||||||
| 37 | ); | ||||||||||||||
| 38 | |||||||||||||||
| 39 | has '_fks' => ( | ||||||||||||||
| 40 | is => 'ro', | ||||||||||||||
| 41 | isa => HashRef, | ||||||||||||||
| 42 | 52 | ▶ | default => sub { {} }, | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 43 | 25 | init_arg => undef, | |||||||||||||
| 44 | ); | ||||||||||||||
| 45 | |||||||||||||||
| 46 | sub add_table { | ||||||||||||||
| 47 | 205 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
add_table: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||
| 48 | 205 | my ($table) = pos_validated_list( \@_, { isa => Table } ); | |||||||||||||
| 49 | |||||||||||||||
| 50 | 205 | my $name = $table->name(); | |||||||||||||
| 51 | 205 | ▶ | param_error "The schema already contains a table named $name." | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | if $self->table($name); | ||||||||||||||
| 53 | |||||||||||||||
| 54 | 204 | $self->_tables->add($table); | |||||||||||||
| 55 | |||||||||||||||
| 56 | 204 | $table->_set_schema($self); | |||||||||||||
| 57 | |||||||||||||||
| 58 | 204 | return $self; | |||||||||||||
| 59 | } | ||||||||||||||
| 60 | |||||||||||||||
| 61 | sub remove_table { | ||||||||||||||
| 62 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
remove_table: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 63 | 3 | my ($table) | |||||||||||||
| 64 | = pos_validated_list( \@_, { isa => TableOrName } ); | ||||||||||||||
| 65 | |||||||||||||||
| 66 | 3 | ▶ | $table = $self->table($table) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | unless blessed $table; | ||||||||||||||
| 68 | |||||||||||||||
| 69 | 3 | for my $fk ( $self->foreign_keys_for_table($table) ) { | |||||||||||||
| 70 | 1 | $self->remove_foreign_key($fk); | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | |||||||||||||||
| 73 | 3 | $self->_tables()->delete($table); | |||||||||||||
| 74 | |||||||||||||||
| 75 | 3 | $table->_set_schema(undef); | |||||||||||||
| 76 | |||||||||||||||
| 77 | 3 | return $self; | |||||||||||||
| 78 | } | ||||||||||||||
| 79 | |||||||||||||||
| 80 | sub add_foreign_key { | ||||||||||||||
| 81 | 125 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
add_foreign_key: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 82 | 125 | my ($fk) = pos_validated_list( \@_, { isa => FK } ); | |||||||||||||
| 83 | |||||||||||||||
| 84 | 125 | my $fk_id = $fk->id(); | |||||||||||||
| 85 | |||||||||||||||
| 86 | 125 | my $source_table_name = $fk->source_table()->name(); | |||||||||||||
| 87 | |||||||||||||||
| 88 | 125 | for my $col_name ( map { $_->name() } @{ $fk->source_columns() } ) { | |||||||||||||
| 89 | 125 | $self->_fks()->{$source_table_name}{$col_name}{$fk_id} = $fk; | |||||||||||||
| 90 | } | ||||||||||||||
| 91 | |||||||||||||||
| 92 | 125 | my $target_table_name = $fk->target_table()->name(); | |||||||||||||
| 93 | |||||||||||||||
| 94 | 125 | for my $col_name ( map { $_->name() } @{ $fk->target_columns() } ) { | |||||||||||||
| 95 | 125 | $self->_fks()->{$target_table_name}{$col_name}{$fk_id} = $fk; | |||||||||||||
| 96 | } | ||||||||||||||
| 97 | |||||||||||||||
| 98 | 125 | return $self; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | sub remove_foreign_key { | ||||||||||||||
| 102 | 3 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
remove_foreign_key: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 103 | 3 | my ($fk) = pos_validated_list( \@_, { isa => FK } ); | |||||||||||||
| 104 | |||||||||||||||
| 105 | 3 | my $fk_id = $fk->id(); | |||||||||||||
| 106 | |||||||||||||||
| 107 | 3 | my $source_table_name = $fk->source_table()->name(); | |||||||||||||
| 108 | 3 | for my $col_name ( map { $_->name() } @{ $fk->source_columns() } ) { | |||||||||||||
| 109 | 3 | delete $self->_fks()->{$source_table_name}{$col_name}{$fk_id}; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | 3 | my $target_table_name = $fk->target_table()->name(); | |||||||||||||
| 113 | 3 | for my $col_name ( map { $_->name() } @{ $fk->target_columns() } ) { | |||||||||||||
| 114 | 3 | delete $self->_fks()->{$target_table_name}{$col_name}{$fk_id}; | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | 3 | return $self; | |||||||||||||
| 118 | } | ||||||||||||||
| 119 | |||||||||||||||
| 120 | sub foreign_keys_for_table { | ||||||||||||||
| 121 | 12 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
foreign_keys_for_table: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 122 | 12 | my ($table) | |||||||||||||
| 123 | = pos_validated_list( \@_, { isa => TableOrName } ); | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 12 | ▶ | my $name = blessed $table ? $table->name() : $table; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | |||||||||||||||
| 127 | my %fks = ( | ||||||||||||||
| 128 | 8 | map { $_->id() => $_ } | |||||||||||||
| 129 | 11 | map { values %{ $self->_fks()->{$name}{$_} } } | |||||||||||||
| 130 | 12 | ▶ | keys %{ $self->_fks()->{$name} || {} } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | ); | ||||||||||||||
| 132 | |||||||||||||||
| 133 | 12 | return values %fks; | |||||||||||||
| 134 | } | ||||||||||||||
| 135 | |||||||||||||||
| 136 | sub foreign_keys_between_tables { | ||||||||||||||
| 137 | 10 | ▶ | my $self = shift; | ||||||||||||
|
Subroutine
foreign_keys_between_tables: called CC=12 SCAR=25.4
Pod
documented
| |||||||||||||||
| 138 | 10 | my ( $table1, $table2 ) = pos_validated_list( | |||||||||||||
| 139 | \@_, | ||||||||||||||
| 140 | { isa => TableLikeOrName }, | ||||||||||||||
| 141 | { isa => TableLikeOrName } | ||||||||||||||
| 142 | ); | ||||||||||||||
| 143 | |||||||||||||||
| 144 | 10 | ▶ | my $name1 | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 145 | = !blessed $table1 ? $table1 | ||||||||||||||
| 146 | : $table1->isa('Fey::Table') ? $table1->name() | ||||||||||||||
| 147 | : $table1->table()->name(); | ||||||||||||||
| 148 | |||||||||||||||
| 149 | 10 | ▶ | my $name2 | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 150 | = !blessed $table2 ? $table2 | ||||||||||||||
| 151 | : $table2->isa('Fey::Table') ? $table2->name() | ||||||||||||||
| 152 | : $table2->table()->name(); | ||||||||||||||
| 153 | |||||||||||||||
| 154 | my %fks = ( | ||||||||||||||
| 155 | 7 | map { $_->id() => $_ } | |||||||||||||
| 156 | 9 | grep { $_->has_tables( $name1, $name2 ) } | |||||||||||||
| 157 | 10 | map { values %{ $self->_fks()->{$name1}{$_} } } | |||||||||||||
| 158 | 10 | ▶ | keys %{ $self->_fks()->{$name1} || {} } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | ); | ||||||||||||||
| 160 | |||||||||||||||
| 161 | return values %fks | ||||||||||||||
| 162 | 10 | ▶ | unless grep { blessed $_ && $_->is_alias() } $table1, $table2; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 163 | |||||||||||||||
| 164 | 1 | ▶ | $table1 = $self->table($name1) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | unless blessed $table1; | ||||||||||||||
| 166 | |||||||||||||||
| 167 | 1 | ▶ | $table2 = $self->table($name2) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 168 | unless blessed $table2; | ||||||||||||||
| 169 | |||||||||||||||
| 170 | 1 | my @fks; | |||||||||||||
| 171 | |||||||||||||||
| 172 | 1 | for my $fk ( values %fks ) { | |||||||||||||
| 173 | my %p | ||||||||||||||
| 174 | = $table1->name() eq $fk->source_table()->name() | ||||||||||||||
| 175 | ? ( | ||||||||||||||
| 176 | source_columns => [ | ||||||||||||||
| 177 | $table1->columns( | ||||||||||||||
| 178 | 1 | map { $_->name() } @{ $fk->source_columns() } | |||||||||||||
| 179 | ) | ||||||||||||||
| 180 | ], | ||||||||||||||
| 181 | target_columns => [ | ||||||||||||||
| 182 | $table2->columns( | ||||||||||||||
| 183 | 1 | map { $_->name() } @{ $fk->target_columns() } | |||||||||||||
| 184 | ) | ||||||||||||||
| 185 | ], | ||||||||||||||
| 186 | ) | ||||||||||||||
| 187 | : ( | ||||||||||||||
| 188 | source_columns => [ | ||||||||||||||
| 189 | $table2->columns( | ||||||||||||||
| 190 | 0 | map { $_->name() } @{ $fk->source_columns() } | |||||||||||||
| 191 | ) | ||||||||||||||
| 192 | ], | ||||||||||||||
| 193 | target_columns => [ | ||||||||||||||
| 194 | $table1->columns( | ||||||||||||||
| 195 | 1 | ▶ | map { $_->name() } @{ $fk->target_columns() } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 196 | ) | ||||||||||||||
| 197 | ], | ||||||||||||||
| 198 | ); | ||||||||||||||
| 199 | |||||||||||||||
| 200 | 1 | push @fks, Fey::FK->new(%p); | |||||||||||||
| 201 | } | ||||||||||||||
| 202 | |||||||||||||||
| 203 | 1 | return @fks; | |||||||||||||
| 204 | } | ||||||||||||||
| 205 | |||||||||||||||
| 206 | 25 | __PACKAGE__->meta()->make_immutable(); | |||||||||||||
| 207 | |||||||||||||||
| 208 | 25 | 1; | |||||||||||||
| 209 | |||||||||||||||
| 210 | # ABSTRACT: Represents a schema and contains tables and foreign keys | ||||||||||||||
| 211 | |||||||||||||||
| 212 | __END__ | ||||||||||||||
| 213 | |||||||||||||||
| 214 | =pod | ||||||||||||||
| 215 | |||||||||||||||
| 216 | =head1 NAME | ||||||||||||||
| 217 | |||||||||||||||
| 218 | Fey::Schema - Represents a schema and contains tables and foreign keys | ||||||||||||||
| 219 | |||||||||||||||
| 220 | =head1 VERSION | ||||||||||||||
| 221 | |||||||||||||||
| 222 | version 0.42 | ||||||||||||||
| 223 | |||||||||||||||
| 224 | =head1 SYNOPSIS | ||||||||||||||
| 225 | |||||||||||||||
| 226 | my $schema = Fey::Schema->new( name => 'MySchema' ); | ||||||||||||||
| 227 | |||||||||||||||
| 228 | $schema->add_table(...); | ||||||||||||||
| 229 | |||||||||||||||
| 230 | $schema->add_foreign_key(...); | ||||||||||||||
| 231 | |||||||||||||||
| 232 | =head1 DESCRIPTION | ||||||||||||||
| 233 | |||||||||||||||
| 234 | This class represents a schema, which is a set of tables and foreign | ||||||||||||||
| 235 | keys. | ||||||||||||||
| 236 | |||||||||||||||
| 237 | =head1 METHODS | ||||||||||||||
| 238 | |||||||||||||||
| 239 | This class provides the following methods: | ||||||||||||||
| 240 | |||||||||||||||
| 241 | =head2 Fey::Schema->new() | ||||||||||||||
| 242 | |||||||||||||||
| 243 | my $schema = Fey::Schema->new( name => 'MySchema' ); | ||||||||||||||
| 244 | |||||||||||||||
| 245 | This method constructs a new C<Fey::Schema> object. It takes the | ||||||||||||||
| 246 | following parameters: | ||||||||||||||
| 247 | |||||||||||||||
| 248 | =over 4 | ||||||||||||||
| 249 | |||||||||||||||
| 250 | =item * name - required | ||||||||||||||
| 251 | |||||||||||||||
| 252 | The name of the schema. | ||||||||||||||
| 253 | |||||||||||||||
| 254 | =back | ||||||||||||||
| 255 | |||||||||||||||
| 256 | =head2 $schema->name() | ||||||||||||||
| 257 | |||||||||||||||
| 258 | Returns the name of the schema. | ||||||||||||||
| 259 | |||||||||||||||
| 260 | =head2 $schema->add_table($table) | ||||||||||||||
| 261 | |||||||||||||||
| 262 | Adds the specified table to the schema. The table must be a | ||||||||||||||
| 263 | C<Fey::Table> object. Adding the table to the schema sets the schema | ||||||||||||||
| 264 | for the table, so that C<< $table->schema() >> returns the correct | ||||||||||||||
| 265 | object. | ||||||||||||||
| 266 | |||||||||||||||
| 267 | If the table is already part of the schema, an exception will be | ||||||||||||||
| 268 | thrown. | ||||||||||||||
| 269 | |||||||||||||||
| 270 | =head2 $schema->remove_table($table) | ||||||||||||||
| 271 | |||||||||||||||
| 272 | Remove the specified table from the schema. Removing the table also | ||||||||||||||
| 273 | removes any foreign keys which reference the table. Removing the table | ||||||||||||||
| 274 | unsets the schema for the table. | ||||||||||||||
| 275 | |||||||||||||||
| 276 | The table can be specified either by name or by passing in a | ||||||||||||||
| 277 | C<Fey::Table> object. | ||||||||||||||
| 278 | |||||||||||||||
| 279 | =head2 $schema->table($name) | ||||||||||||||
| 280 | |||||||||||||||
| 281 | Returns the table with the specified name. If no such table exists, | ||||||||||||||
| 282 | this method returns false. | ||||||||||||||
| 283 | |||||||||||||||
| 284 | =head2 $schema->tables() | ||||||||||||||
| 285 | |||||||||||||||
| 286 | =head2 $schema->tables(@names) | ||||||||||||||
| 287 | |||||||||||||||
| 288 | When this method is called with no arguments, it returns all of the tables in | ||||||||||||||
| 289 | the schema. Tables are returned in the order with which they were added to the | ||||||||||||||
| 290 | schema. | ||||||||||||||
| 291 | |||||||||||||||
| 292 | If given a list of names, it returns only the specified tables. If a name is | ||||||||||||||
| 293 | given which doesn't match a table in the schema, then it is ignored. | ||||||||||||||
| 294 | |||||||||||||||
| 295 | =head2 $schema->add_foreign_key($fk) | ||||||||||||||
| 296 | |||||||||||||||
| 297 | Adds the specified to the schema. The foreign key must be a C<Fey::FK> | ||||||||||||||
| 298 | object. | ||||||||||||||
| 299 | |||||||||||||||
| 300 | If the foreign key references tables which are not in the schema, an | ||||||||||||||
| 301 | exception will be thrown. | ||||||||||||||
| 302 | |||||||||||||||
| 303 | =head2 $schema->remove_foreign_key($fk) | ||||||||||||||
| 304 | |||||||||||||||
| 305 | Removes the specified foreign key from the schema. The foreign key | ||||||||||||||
| 306 | must be a C<Fey::FK> object. | ||||||||||||||
| 307 | |||||||||||||||
| 308 | =head2 $schema->foreign_keys_for_table($table) | ||||||||||||||
| 309 | |||||||||||||||
| 310 | Returns all the foreign keys which reference the specified table. The | ||||||||||||||
| 311 | table can be specified as a name or a C<Fey::Table> object. | ||||||||||||||
| 312 | |||||||||||||||
| 313 | =head2 $schema->foreign_keys_between_tables( $source_table, $target_table ) | ||||||||||||||
| 314 | |||||||||||||||
| 315 | Returns all the foreign keys which reference both tables. The tables | ||||||||||||||
| 316 | can be specified as names, C<Fey::Table> objects, or | ||||||||||||||
| 317 | C<Fey::Table::Alias> objects. If you provide any aliases, the foreign | ||||||||||||||
| 318 | keys returned will contain columns from those aliases, not the real | ||||||||||||||
| 319 | tables. This provides support for joining an alias in a SQL statement. | ||||||||||||||
| 320 | |||||||||||||||
| 321 | =head1 BUGS | ||||||||||||||
| 322 | |||||||||||||||
| 323 | See L<Fey> for details on how to report bugs. | ||||||||||||||
| 324 | |||||||||||||||
| 325 | =head1 AUTHOR | ||||||||||||||
| 326 | |||||||||||||||
| 327 | Dave Rolsky <autarch@urth.org> | ||||||||||||||
| 328 | |||||||||||||||
| 329 | =head1 COPYRIGHT AND LICENSE | ||||||||||||||
| 330 | |||||||||||||||
| 331 | This software is Copyright (c) 2011 - 2015 by Dave Rolsky. | ||||||||||||||
| 332 | |||||||||||||||
| 333 | This is free software, licensed under: | ||||||||||||||
| 334 | |||||||||||||||
| 335 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||||
| 336 | |||||||||||||||
| 337 | =cut | ||||||||||||||