| 1 | =head1 NAME | ||
| 2 | |||
| 3 | DBIx::SQLEngine::Schema::TableSet - Array of Schema::Table objects | ||
| 4 | |||
| 5 | =head1 SYNOPSIS | ||
| 6 | |||
| 7 | use DBIx::SQLEngine::Schema::TableSet; | ||
| 8 | my $tables = DBIx::SQLEngine::Schema::TableSet->new( $table1, $table2 ); | ||
| 9 | |||
| 10 | print $tables->count; | ||
| 11 | |||
| 12 | foreach my $table ( $tables->tables ) { | ||
| 13 | print $table->name; | ||
| 14 | } | ||
| 15 | |||
| 16 | $table = $tables->table_named( $name ); | ||
| 17 | |||
| 18 | $ts->create_tables; | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | DBIx::SQLEngine::Schema::TableSet objects contain an array of DBIx::SQLEngine::Schema::Table objects. | ||
| 23 | |||
| 24 | =cut | ||
| 25 | |||
| 26 | package DBIx::SQLEngine::Schema::TableSet; | ||
| 27 | |||
| 28 | use strict; | ||
| 29 | use Carp; | ||
| 30 | use Class::MakeMethods; | ||
| 31 | |||
| 32 | use DBIx::SQLEngine::Schema::Table; | ||
| 33 | |||
| 34 | ######################################################################## | ||
| 35 | |||
| 36 | =head2 Creation | ||
| 37 | |||
| 38 | =over 4 | ||
| 39 | |||
| 40 | =item new() | ||
| 41 | |||
| 42 | DBIx::SQLEngine::Schema::TableSet->new( @tables ) : $tableset | ||
| 43 | |||
| 44 | Creates a new instance. | ||
| 45 | |||
| 46 | =back | ||
| 47 | |||
| 48 | =cut | ||
| 49 | |||
| 50 | sub new { | ||
| 51 | my $package = shift; | ||
| 52 | my @tables = map { | ||
| 53 | ( ref($_) eq 'HASH' ) ? DBIx::SQLEngine::Schema::Table->new_from_hash(%$_) | ||
| 54 | : $_ | ||
| 55 | } @_; | ||
| 56 | bless \@tables, $package; | ||
| 57 | } | ||
| 58 | |||
| 59 | ######################################################################## | ||
| 60 | |||
| 61 | =head2 Access to Tables | ||
| 62 | |||
| 63 | =over 4 | ||
| 64 | |||
| 65 | =item tables() | ||
| 66 | |||
| 67 | $tableset->tables : @table_objects | ||
| 68 | |||
| 69 | Returns a list of tables contained in this set. | ||
| 70 | |||
| 71 | =item call_method_on_tables() | ||
| 72 | |||
| 73 | $tableset->call_method_on_tables( $method, @args ) : @results | ||
| 74 | |||
| 75 | Calls the provided method on each of the tables in this set. | ||
| 76 | |||
| 77 | =back | ||
| 78 | |||
| 79 | =cut | ||
| 80 | |||
| 81 | sub tables { | ||
| 82 | my $tables = shift; | ||
| 83 | @$tables | ||
| 84 | } | ||
| 85 | |||
| 86 | sub call_method_on_tables { | ||
| 87 | my $tables = shift; | ||
| 88 | my $method = shift; | ||
| 89 | return map { $_->$method( @_ ) } @$tables; | ||
| 90 | } | ||
| 91 | |||
| 92 | |||
| 93 | ######################################################################## | ||
| 94 | |||
| 95 | =head2 Table Names | ||
| 96 | |||
| 97 | =over 4 | ||
| 98 | |||
| 99 | =item table_names() | ||
| 100 | |||
| 101 | $tableset->table_names : @table_names | ||
| 102 | |||
| 103 | Returns a list of the names of each of the tables in this set. | ||
| 104 | |||
| 105 | =item table_named() | ||
| 106 | |||
| 107 | $tableset->table_named( $table_name ); : $table_object | ||
| 108 | |||
| 109 | Searches through the tables in the set until it finds one with the given name. Throws an exception if none are found. | ||
| 110 | |||
| 111 | =back | ||
| 112 | |||
| 113 | =cut | ||
| 114 | |||
| 115 | # @colnames = $tables->table_names; | ||
| 116 | sub table_names { | ||
| 117 | (shift)->call_method_on_tables( 'name' ) | ||
| 118 | } | ||
| 119 | |||
| 120 | # $table = $tables->table_named( $table_name ); | ||
| 121 | # $table = $tables->table_named( $table_name ); | ||
| 122 | sub table_named { | ||
| 123 | my $tables = shift; | ||
| 124 | my $table_name = shift; | ||
| 125 | foreach ( @$tables ) { | ||
| 126 | return $_ if ( $_->name eq $table_name ); | ||
| 127 | } | ||
| 128 | croak( | ||
| 129 | "No table named $table_name in this set\n" . | ||
| 130 | " (Perhaps you meant one of these: ".join(', ',$tables->table_names)."?)" | ||
| 131 | ); | ||
| 132 | } | ||
| 133 | |||
| 134 | ######################################################################## | ||
| 135 | |||
| 136 | =head2 Schema Definition | ||
| 137 | |||
| 138 | =over 4 | ||
| 139 | |||
| 140 | =item create_tables() | ||
| 141 | |||
| 142 | $tableset->create_tables : () | ||
| 143 | |||
| 144 | Calls create_table() on each table in the set. | ||
| 145 | |||
| 146 | =item ensure_tables_exist() | ||
| 147 | |||
| 148 | $tableset->ensure_tables_exist : () | ||
| 149 | |||
| 150 | Calls ensure_table_exists() on each table in the set. | ||
| 151 | |||
| 152 | =item recreate_tables() | ||
| 153 | |||
| 154 | $tableset->recreate_tables : () | ||
| 155 | |||
| 156 | Calls recreate_table_with_rows() on each table in the set. | ||
| 157 | |||
| 158 | =item drop_tables() | ||
| 159 | |||
| 160 | $tableset->drop_tables : () | ||
| 161 | |||
| 162 | Calls drop_table() on each table in the set. | ||
| 163 | |||
| 164 | =back | ||
| 165 | |||
| 166 | =cut | ||
| 167 | |||
| 168 | sub create_tables { | ||
| 169 | (shift)->call_method_on_tables( 'create_table' ) | ||
| 170 | } | ||
| 171 | |||
| 172 | sub ensure_tables_exist { | ||
| 173 | (shift)->call_method_on_tables( 'ensure_table_exists' ) | ||
| 174 | } | ||
| 175 | |||
| 176 | sub recreate_tables { | ||
| 177 | (shift)->call_method_on_tables( 'recreate_table_with_rows' ) | ||
| 178 | } | ||
| 179 | |||
| 180 | sub drop_tables { | ||
| 181 | (shift)->call_method_on_tables( 'drop_table' ) | ||
| 182 | } | ||
| 183 | |||
| 184 | ######################################################################## | ||
| 185 | |||
| 186 | =head1 SEE ALSO | ||
| 187 | |||
| 188 | See L<DBIx::SQLEngine> for the overall interface and developer documentation. | ||
| 189 | |||
| 190 | See L<DBIx::SQLEngine::Docs::ReadMe> for general information about | ||
| 191 | this distribution, including installation and license information. | ||
| 192 | |||
| 193 | =cut | ||
| 194 | |||
| 195 | ######################################################################## | ||
| 196 | |||
| 197 | 1; |