| 1 | package Fey::Table; | ||||||||||||
| 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::Column; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | 25 | ▶ | use Fey::Exceptions qw( param_error ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 11 | 25 | ▶ | use Fey::NamedObjectSet; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 12 | 25 | ▶ | use Fey::Schema; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | 25 | ▶ | use Fey::Table::Alias; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | 25 | use Fey::Types qw( | |||||||||||
| 15 | ArrayRef Bool HashRef Str Undef Column ColumnOrName NamedObjectSet Schema | ||||||||||||
| 16 | 25 | ▶ | ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 25 | ▶ | use List::AllUtils qw( any all first_index ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | 25 | ▶ | use Scalar::Util qw( blessed weaken ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 19 | |||||||||||||
| 20 | 25 | ▶ | use Moose 0.90; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 21 | 25 | ▶ | use MooseX::Params::Validate 0.08 qw( pos_validated_list ); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 22 | 25 | ▶ | use MooseX::SemiAffordanceAccessor 0.03; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 23 | 25 | ▶ | use MooseX::StrictConstructor 0.07; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 24 | 25 | ▶ | use Moose::Util::TypeConstraints; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 25 | |||||||||||||
| 26 | 25 | with 'Fey::Role::TableLike'; | |||||||||||
| 27 | |||||||||||||
| 28 | 25 | with 'Fey::Role::MakesAliasObjects' => { | |||||||||||
| 29 | self_param => 'table', | ||||||||||||
| 30 | alias_class => 'Fey::Table::Alias', | ||||||||||||
| 31 | }; | ||||||||||||
| 32 | |||||||||||||
| 33 | 25 | has 'id' => ( | |||||||||||
| 34 | is => 'ro', | ||||||||||||
| 35 | lazy_build => 1, | ||||||||||||
| 36 | init_arg => undef, | ||||||||||||
| 37 | ); | ||||||||||||
| 38 | |||||||||||||
| 39 | 25 | has 'name' => ( | |||||||||||
| 40 | is => 'ro', | ||||||||||||
| 41 | isa => Str, | ||||||||||||
| 42 | required => 1, | ||||||||||||
| 43 | ); | ||||||||||||
| 44 | |||||||||||||
| 45 | 25 | has 'is_view' => ( | |||||||||||
| 46 | is => 'ro', | ||||||||||||
| 47 | isa => Bool, | ||||||||||||
| 48 | default => 0, | ||||||||||||
| 49 | ); | ||||||||||||
| 50 | |||||||||||||
| 51 | has '_keys' => ( | ||||||||||||
| 52 | traits => ['Array'], | ||||||||||||
| 53 | is => 'bare', | ||||||||||||
| 54 | isa => ArrayRef [NamedObjectSet], | ||||||||||||
| 55 | 209 | ▶ | default => sub { [] }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 25 | handles => { | |||||||||||
| 57 | _keys => 'elements', | ||||||||||||
| 58 | _add_key => 'push', | ||||||||||||
| 59 | _delete_key => 'splice', | ||||||||||||
| 60 | }, | ||||||||||||
| 61 | |||||||||||||
| 62 | ); | ||||||||||||
| 63 | |||||||||||||
| 64 | has '_columns' => ( | ||||||||||||
| 65 | is => 'ro', | ||||||||||||
| 66 | isa => NamedObjectSet, | ||||||||||||
| 67 | 209 | ▶ | default => sub { return Fey::NamedObjectSet->new() }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 68 | 25 | handles => { | |||||||||||
| 69 | columns => 'objects', | ||||||||||||
| 70 | column => 'object', | ||||||||||||
| 71 | }, | ||||||||||||
| 72 | ); | ||||||||||||
| 73 | |||||||||||||
| 74 | 25 | has 'schema' => ( | |||||||||||
| 75 | is => 'rw', | ||||||||||||
| 76 | isa => Undef | Schema, | ||||||||||||
| 77 | weak_ref => 1, | ||||||||||||
| 78 | writer => '_set_schema', | ||||||||||||
| 79 | clearer => '_clear_schema', | ||||||||||||
| 80 | predicate => 'has_schema', | ||||||||||||
| 81 | ); | ||||||||||||
| 82 | |||||||||||||
| 83 | 25 | has 'candidate_keys' => ( | |||||||||||
| 84 | is => 'ro', | ||||||||||||
| 85 | isa => ArrayRef [ ArrayRef [Column] ], | ||||||||||||
| 86 | clearer => '_clear_candidate_keys', | ||||||||||||
| 87 | lazy_build => 1, | ||||||||||||
| 88 | init_arg => undef, | ||||||||||||
| 89 | ); | ||||||||||||
| 90 | |||||||||||||
| 91 | 309 | ▶ | after '_add_key', '_delete_key' => sub { $_[0]->_clear_candidate_keys() }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 92 | |||||||||||||
| 93 | 25 | has 'primary_key' => ( | |||||||||||
| 94 | is => 'ro', | ||||||||||||
| 95 | isa => ArrayRef [Column], | ||||||||||||
| 96 | clearer => '_clear_primary_key', | ||||||||||||
| 97 | lazy_build => 1, | ||||||||||||
| 98 | init_arg => undef, | ||||||||||||
| 99 | ); | ||||||||||||
| 100 | |||||||||||||
| 101 | 309 | ▶ | after '_clear_candidate_keys' => sub { $_[0]->_clear_primary_key() }; | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 102 | |||||||||||||
| 103 | has '_aliased_tables' => ( | ||||||||||||
| 104 | traits => ['Hash'], | ||||||||||||
| 105 | is => 'bare', | ||||||||||||
| 106 | isa => HashRef, | ||||||||||||
| 107 | lazy => 1, | ||||||||||||
| 108 | 4 | ▶ | default => sub { {} }, | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 109 | 25 | handles => { | |||||||||||
| 110 | _aliased_table => 'get', | ||||||||||||
| 111 | _store_aliased_table => 'set', | ||||||||||||
| 112 | _has_aliased_table => 'exists', | ||||||||||||
| 113 | }, | ||||||||||||
| 114 | ); | ||||||||||||
| 115 | |||||||||||||
| 116 | 25 | with 'Fey::Role::Named'; | |||||||||||
| 117 | |||||||||||||
| 118 | sub add_column { | ||||||||||||
| 119 | 664 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
add_column: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 120 | 664 | my ($col) = pos_validated_list( \@_, { isa => Column } ); | |||||||||||
| 121 | |||||||||||||
| 122 | 664 | my $name = $col->name(); | |||||||||||
| 123 | 664 | ▶ | param_error "The table already has a column named $name." | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 124 | if $self->column($name); | ||||||||||||
| 125 | |||||||||||||
| 126 | 663 | $self->_columns()->add($col); | |||||||||||
| 127 | |||||||||||||
| 128 | 663 | $col->_set_table($self); | |||||||||||
| 129 | |||||||||||||
| 130 | 663 | return $self; | |||||||||||
| 131 | } | ||||||||||||
| 132 | |||||||||||||
| 133 | sub remove_column { | ||||||||||||
| 134 | 5 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
remove_column: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||||
| 135 | 5 | my ($col) | |||||||||||
| 136 | = pos_validated_list( \@_, { isa => ColumnOrName } ); | ||||||||||||
| 137 | |||||||||||||
| 138 | 5 | ▶ | $col = $self->column($col) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 139 | unless blessed $col; | ||||||||||||
| 140 | |||||||||||||
| 141 | 5 | ▶ | if ( my $schema = $self->schema() ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 142 | 1 | for my $fk ( grep { $_->has_column($col) } | |||||||||||
| 143 | $schema->foreign_keys_for_table($self) ) { | ||||||||||||
| 144 | 1 | $schema->remove_foreign_key($fk); | |||||||||||
| 145 | } | ||||||||||||
| 146 | } | ||||||||||||
| 147 | |||||||||||||
| 148 | 5 | my $name = $col->name(); | |||||||||||
| 149 | |||||||||||||
| 150 | 5 | for my $k ( $self->_keys() ) { | |||||||||||
| 151 | 4 | ▶ | $self->remove_candidate_key( $k->objects() ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 152 | if $k->object($name); | ||||||||||||
| 153 | } | ||||||||||||
| 154 | |||||||||||||
| 155 | 5 | $self->_columns()->delete($col); | |||||||||||
| 156 | |||||||||||||
| 157 | 5 | $col->_clear_table(); | |||||||||||
| 158 | |||||||||||||
| 159 | 5 | return $self; | |||||||||||
| 160 | } | ||||||||||||
| 161 | |||||||||||||
| 162 | sub _build_candidate_keys { | ||||||||||||
| 163 | 7 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_build_candidate_keys: called CC=1 SCAR=0.0
| |||||||||||||
| 164 | |||||||||||||
| 165 | 7 | return [ map { [ $_->objects() ] } $self->_keys() ]; | |||||||||||
| 166 | } | ||||||||||||
| 167 | |||||||||||||
| 168 | sub _build_primary_key { | ||||||||||||
| 169 | 5 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
_build_primary_key: called CC=2 SCAR=6.9
| |||||||||||||
| 170 | |||||||||||||
| 171 | 5 | my $keys = $self->candidate_keys(); | |||||||||||
| 172 | |||||||||||||
| 173 | 5 | ▶ | return $keys->[0] || []; | ||||||||||
|
Condition100%2/2
$keys->[0] || []
MC/DC100%1/1
$keys->[0] || []
$keys->[0]
Truth table100%2/2
A
A= $keys->[0]
| |||||||||||||
| 174 | } | ||||||||||||
| 175 | |||||||||||||
| 176 | sub add_candidate_key { | ||||||||||||
| 177 | 307 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
add_candidate_key: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 178 | |||||||||||||
| 179 | 307 | ▶ | my $count = @_ ? @_ : 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 180 | 307 | my (@cols) = pos_validated_list( | |||||||||||
| 181 | \@_, | ||||||||||||
| 182 | ( ( { isa => ColumnOrName } ) x $count ), | ||||||||||||
| 183 | MX_PARAMS_VALIDATE_NO_CACHE => 1, | ||||||||||||
| 184 | ); | ||||||||||||
| 185 | |||||||||||||
| 186 | 306 | ▶ | for my $name ( map { blessed $_ ? $_->name() : $_ } @cols ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 187 | 357 | ▶ | param_error "The column $name is not part of the " | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 188 | . $self->name() | ||||||||||||
| 189 | . ' table.' | ||||||||||||
| 190 | unless $self->column($name); | ||||||||||||
| 191 | } | ||||||||||||
| 192 | |||||||||||||
| 193 | 305 | $_ = $self->column($_) for grep { !blessed $_ } @cols; | |||||||||||
| 194 | |||||||||||||
| 195 | 305 | ▶ | return if $self->has_candidate_key(@cols); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 196 | |||||||||||||
| 197 | 304 | $self->_add_key( Fey::NamedObjectSet->new(@cols) ); | |||||||||||
| 198 | |||||||||||||
| 199 | 304 | return; | |||||||||||
| 200 | } | ||||||||||||
| 201 | |||||||||||||
| 202 | sub remove_candidate_key { | ||||||||||||
| 203 | 8 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
remove_candidate_key: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 204 | |||||||||||||
| 205 | 8 | ▶ | my $count = @_ ? @_ : 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 206 | 8 | my (@cols) = pos_validated_list( | |||||||||||
| 207 | \@_, | ||||||||||||
| 208 | ( ( { isa => ColumnOrName } ) x $count ), | ||||||||||||
| 209 | MX_PARAMS_VALIDATE_NO_CACHE => 1, | ||||||||||||
| 210 | ); | ||||||||||||
| 211 | |||||||||||||
| 212 | 7 | ▶ | for my $name ( map { blessed $_ ? $_->name() : $_ } @cols ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 213 | 7 | ▶ | param_error "The column $name is not part of the " | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 214 | . $self->name() | ||||||||||||
| 215 | . ' table.' | ||||||||||||
| 216 | unless $self->column($name); | ||||||||||||
| 217 | } | ||||||||||||
| 218 | |||||||||||||
| 219 | 6 | $_ = $self->column($_) for grep { !blessed $_ } @cols; | |||||||||||
| 220 | |||||||||||||
| 221 | 6 | my $set = Fey::NamedObjectSet->new(@cols); | |||||||||||
| 222 | |||||||||||||
| 223 | 6 | ▶ | my $idx = first_index { $_->is_same_as($set) } $self->_keys(); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 224 | |||||||||||||
| 225 | 6 | ▶ | $self->_delete_key( $idx, 1 ) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 226 | if $idx >= 0; | ||||||||||||
| 227 | |||||||||||||
| 228 | 6 | return; | |||||||||||
| 229 | } | ||||||||||||
| 230 | |||||||||||||
| 231 | sub has_candidate_key { | ||||||||||||
| 232 | 310 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
has_candidate_key: called CC=7 SCAR=19.5
Pod
documented
| |||||||||||||
| 233 | |||||||||||||
| 234 | 310 | ▶ | my $count = @_ ? @_ : 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 235 | 310 | my (@cols) = pos_validated_list( | |||||||||||
| 236 | \@_, | ||||||||||||
| 237 | ( ( { isa => ColumnOrName } ) x $count ), | ||||||||||||
| 238 | MX_PARAMS_VALIDATE_NO_CACHE => 1, | ||||||||||||
| 239 | ); | ||||||||||||
| 240 | |||||||||||||
| 241 | 309 | ▶ | for my $name ( map { blessed $_ ? $_->name() : $_ } @cols ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 242 | 361 | ▶ | param_error "The column $name is not part of the " | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 243 | . $self->name() | ||||||||||||
| 244 | . ' table.' | ||||||||||||
| 245 | unless $self->column($name); | ||||||||||||
| 246 | } | ||||||||||||
| 247 | |||||||||||||
| 248 | 308 | $_ = $self->column($_) for grep { !blessed $_ } @cols; | |||||||||||
| 249 | |||||||||||||
| 250 | 308 | my $set = Fey::NamedObjectSet->new(@cols); | |||||||||||
| 251 | |||||||||||||
| 252 | return 1 | ||||||||||||
| 253 | 308 | ▶ | if any { $_->is_same_as($set) } $self->_keys(); | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
| |||||||||||||
| 254 | |||||||||||||
| 255 | 305 | return 0; | |||||||||||
| 256 | } | ||||||||||||
| 257 | |||||||||||||
| 258 | # Caching the objects by name prevents a weird bug where we have two | ||||||||||||
| 259 | # aliases of the same name, and one disappears because of weak | ||||||||||||
| 260 | # references, causing weird errors. | ||||||||||||
| 261 | around 'alias' => sub { | ||||||||||||
| 262 | 7 | ▶ | my $orig = shift; | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||
| 263 | 7 | my $self = shift; | |||||||||||
| 264 | |||||||||||||
| 265 | # bleh, duplicating code from Aliasable | ||||||||||||
| 266 | 7 | ▶ | my %p = @_ == 1 ? ( alias_name => $_[0] ) : @_; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 267 | |||||||||||||
| 268 | 7 | ▶ | if ( defined $p{alias_name} ) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 269 | return $self->_aliased_table( $p{alias_name} ) | ||||||||||||
| 270 | 4 | ▶ | if $self->_has_aliased_table( $p{alias_name} ); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 271 | } | ||||||||||||
| 272 | |||||||||||||
| 273 | 7 | my $alias = $orig->( $self, %p ); | |||||||||||
| 274 | |||||||||||||
| 275 | 7 | $self->_store_aliased_table( $alias->alias_name() => $alias ); | |||||||||||
| 276 | |||||||||||||
| 277 | 7 | return $alias; | |||||||||||
| 278 | 25 | }; | |||||||||||
| 279 | |||||||||||||
| 280 | 193 | ▶ | sub is_alias {0} | ||||||||||
|
Subroutine
is_alias: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 281 | |||||||||||||
| 282 | sub aliased_column { | ||||||||||||
| 283 | 6 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
aliased_column: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||
| 284 | 6 | my $prefix = shift; | |||||||||||
| 285 | 6 | my $name = shift; | |||||||||||
| 286 | |||||||||||||
| 287 | 6 | ▶ | my $col = $self->column($name) | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 288 | or return; | ||||||||||||
| 289 | |||||||||||||
| 290 | 6 | return $col->alias( alias_name => $prefix . $col->name() ); | |||||||||||
| 291 | } | ||||||||||||
| 292 | |||||||||||||
| 293 | sub aliased_columns { | ||||||||||||
| 294 | 2 | ▶ | my $self = shift; | ||||||||||
|
Subroutine
aliased_columns: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||
| 295 | 2 | my $prefix = shift; | |||||||||||
| 296 | |||||||||||||
| 297 | 2 | ▶ | my @names = @_ ? @_ : map { $_->name() } $self->columns(); | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 298 | |||||||||||||
| 299 | 2 | return map { $self->aliased_column( $prefix, $_ ) } @names; | |||||||||||
| 300 | } | ||||||||||||
| 301 | |||||||||||||
| 302 | sub sql { | ||||||||||||
| 303 | 74 | ▶ | return $_[1]->quote_identifier( $_[0]->name() ); | ||||||||||
|
Subroutine
sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 304 | } | ||||||||||||
| 305 | |||||||||||||
| 306 | 3 | ▶ | sub sql_for_select_clause { $_[0]->sql( $_[1] ) . '.*' } | ||||||||||
|
Subroutine
sql_for_select_clause: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 307 | |||||||||||||
| 308 | 71 | ▶ | sub sql_with_alias { goto &sql } | ||||||||||
|
Subroutine
sql_with_alias: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 309 | |||||||||||||
| 310 | 12 | ▶ | sub _build_id { $_[0]->name() } | ||||||||||
|
Subroutine
_build_id: called CC=1 SCAR=0.0
| |||||||||||||
| 311 | |||||||||||||
| 312 | 25 | __PACKAGE__->meta()->make_immutable(); | |||||||||||
| 313 | |||||||||||||
| 314 | 25 | 1; | |||||||||||
| 315 | |||||||||||||
| 316 | # ABSTRACT: Represents a table (or view) | ||||||||||||
| 317 | |||||||||||||
| 318 | __END__ | ||||||||||||
| 319 | |||||||||||||
| 320 | =pod | ||||||||||||
| 321 | |||||||||||||
| 322 | =head1 NAME | ||||||||||||
| 323 | |||||||||||||
| 324 | Fey::Table - Represents a table (or view) | ||||||||||||
| 325 | |||||||||||||
| 326 | =head1 VERSION | ||||||||||||
| 327 | |||||||||||||
| 328 | version 0.42 | ||||||||||||
| 329 | |||||||||||||
| 330 | =head1 SYNOPSIS | ||||||||||||
| 331 | |||||||||||||
| 332 | my $table = Fey::Table->new( name => 'User' ); | ||||||||||||
| 333 | |||||||||||||
| 334 | =head1 DESCRIPTION | ||||||||||||
| 335 | |||||||||||||
| 336 | This class represents a table or view in a schema. From the standpoint | ||||||||||||
| 337 | of SQL construction in Fey, a table and a view are basically the same | ||||||||||||
| 338 | thing. | ||||||||||||
| 339 | |||||||||||||
| 340 | =head1 METHODS | ||||||||||||
| 341 | |||||||||||||
| 342 | This class provides the following methods: | ||||||||||||
| 343 | |||||||||||||
| 344 | =head2 Fey::Table->new() | ||||||||||||
| 345 | |||||||||||||
| 346 | my $table = Fey::Table->new( name => 'User' ); | ||||||||||||
| 347 | |||||||||||||
| 348 | my $table = Fey::Table->new( name => 'ActiveUser', | ||||||||||||
| 349 | is_view => 1, | ||||||||||||
| 350 | ); | ||||||||||||
| 351 | |||||||||||||
| 352 | This method constructs a new C<Fey::Table> object. It takes the | ||||||||||||
| 353 | following parameters: | ||||||||||||
| 354 | |||||||||||||
| 355 | =over 4 | ||||||||||||
| 356 | |||||||||||||
| 357 | =item * name - required | ||||||||||||
| 358 | |||||||||||||
| 359 | The name of the table. | ||||||||||||
| 360 | |||||||||||||
| 361 | =item * is_view - defaults to 0 | ||||||||||||
| 362 | |||||||||||||
| 363 | A boolean indicating whether this table is a view. | ||||||||||||
| 364 | |||||||||||||
| 365 | =back | ||||||||||||
| 366 | |||||||||||||
| 367 | =head2 $table->name() | ||||||||||||
| 368 | |||||||||||||
| 369 | Returns the name of the table. | ||||||||||||
| 370 | |||||||||||||
| 371 | =head2 $table->is_view() | ||||||||||||
| 372 | |||||||||||||
| 373 | Returns a boolean indicating whether the object is a view. | ||||||||||||
| 374 | |||||||||||||
| 375 | =head2 $table->schema() | ||||||||||||
| 376 | |||||||||||||
| 377 | Returns the C<Fey::Schema> object that this table belongs to. This is | ||||||||||||
| 378 | set when the table is added to a schema via the C<< | ||||||||||||
| 379 | Fey::Schema->add_table() >> method. | ||||||||||||
| 380 | |||||||||||||
| 381 | =head2 $table->add_column($column) | ||||||||||||
| 382 | |||||||||||||
| 383 | This adds a new column to the schema. The column must be a | ||||||||||||
| 384 | C<Fey::Column> object. Adding the column to the table sets the table | ||||||||||||
| 385 | for the column, so that C<< $column->table() >> returns the correct | ||||||||||||
| 386 | object. | ||||||||||||
| 387 | |||||||||||||
| 388 | If the table already has a column with the same name, an exception is | ||||||||||||
| 389 | thrown. | ||||||||||||
| 390 | |||||||||||||
| 391 | =head2 $table->remove_column($column) | ||||||||||||
| 392 | |||||||||||||
| 393 | Remove the specified column from the table. If the column was part of | ||||||||||||
| 394 | any foreign keys, these are removed from the schema. If this column is | ||||||||||||
| 395 | part of any keys for the table, those keys will be removed. Removing | ||||||||||||
| 396 | the column unsets the table for the column. | ||||||||||||
| 397 | |||||||||||||
| 398 | The table can be specified either by name or by passing in a | ||||||||||||
| 399 | C<Fey::Column> object. | ||||||||||||
| 400 | |||||||||||||
| 401 | =head2 $table->column($name) | ||||||||||||
| 402 | |||||||||||||
| 403 | Given a column name, this method returns the matching column object, | ||||||||||||
| 404 | if one exists. | ||||||||||||
| 405 | |||||||||||||
| 406 | =head2 $table->columns | ||||||||||||
| 407 | |||||||||||||
| 408 | =head2 $table->columns(@names) | ||||||||||||
| 409 | |||||||||||||
| 410 | When this method is called with no arguments, it returns all of the columns in | ||||||||||||
| 411 | the table. Columns are returned in the order with which they were added to the | ||||||||||||
| 412 | table. | ||||||||||||
| 413 | |||||||||||||
| 414 | If given a list of names, it returns only the specified columns. If a name is | ||||||||||||
| 415 | given which doesn't match a column in the table, then it is ignored. | ||||||||||||
| 416 | |||||||||||||
| 417 | =head2 $table->candidate_keys() | ||||||||||||
| 418 | |||||||||||||
| 419 | Returns all of the candidate keys for the table as an array | ||||||||||||
| 420 | reference. Each element of the reference is in turn an array reference | ||||||||||||
| 421 | containing one or more columns. | ||||||||||||
| 422 | |||||||||||||
| 423 | =head2 $table->has_candidate_key(@columns) | ||||||||||||
| 424 | |||||||||||||
| 425 | This method returns true if the table has the given key. A key is | ||||||||||||
| 426 | identified as a list of names or C<Fey::Column> objects. | ||||||||||||
| 427 | |||||||||||||
| 428 | =head2 $table->add_candidate_key(@columns) | ||||||||||||
| 429 | |||||||||||||
| 430 | This method adds a new candidate key to the table. The list of columns | ||||||||||||
| 431 | can contain either names or C<Fey::Column> objects. | ||||||||||||
| 432 | |||||||||||||
| 433 | A candidate key is one or more columns which uniquely identify a row | ||||||||||||
| 434 | in that table. | ||||||||||||
| 435 | |||||||||||||
| 436 | If a name or column is specified which doesn't belong to the table, an | ||||||||||||
| 437 | exception will be thrown. | ||||||||||||
| 438 | |||||||||||||
| 439 | =head2 $table->remove_candidate_key(@columns) | ||||||||||||
| 440 | |||||||||||||
| 441 | This method removes a candidate key for the table. The list of columns | ||||||||||||
| 442 | can contain either names or C<Fey::Column> objects. | ||||||||||||
| 443 | |||||||||||||
| 444 | If a name or column is specified which doesn't belong to the table, an | ||||||||||||
| 445 | exception will be thrown. | ||||||||||||
| 446 | |||||||||||||
| 447 | =head2 $table->primary_key() | ||||||||||||
| 448 | |||||||||||||
| 449 | This is a convenience method that simply returns the first candidate | ||||||||||||
| 450 | key added to the table. The key is returned as an array reference of | ||||||||||||
| 451 | column objects. | ||||||||||||
| 452 | |||||||||||||
| 453 | =head2 $table->alias(%p) | ||||||||||||
| 454 | |||||||||||||
| 455 | =head2 $table->alias($alias_name) | ||||||||||||
| 456 | |||||||||||||
| 457 | This method returns a new C<Fey::Table::Alias> object based on the | ||||||||||||
| 458 | table. Any parameters passed to this method will be passed through to | ||||||||||||
| 459 | C<< Fey::Table::Alias->new() >>. | ||||||||||||
| 460 | |||||||||||||
| 461 | As a shortcut, if you pass a single argument to this method, it will | ||||||||||||
| 462 | be passed as the "alias_name" parameter to C<< | ||||||||||||
| 463 | Fey::Table::Alias->new() >>. | ||||||||||||
| 464 | |||||||||||||
| 465 | =head2 $table->is_alias() | ||||||||||||
| 466 | |||||||||||||
| 467 | Always returns false. | ||||||||||||
| 468 | |||||||||||||
| 469 | =head2 $table->aliased_column( $prefix, $column_name ) | ||||||||||||
| 470 | |||||||||||||
| 471 | This method returns a new L<Fey::Column::Alias> object. The alias's | ||||||||||||
| 472 | name is generated by concatenating the specified prefix and the | ||||||||||||
| 473 | column's real name. | ||||||||||||
| 474 | |||||||||||||
| 475 | =head2 $table->aliased_columns( $prefix, @column_names ) | ||||||||||||
| 476 | |||||||||||||
| 477 | This method returns a list of new L<Fey::Column::Alias> objects. The | ||||||||||||
| 478 | alias names are generated by concatenating the specified prefix and | ||||||||||||
| 479 | the column's real name. | ||||||||||||
| 480 | |||||||||||||
| 481 | If you omit the list of column names, it returns aliases for I<all> of the | ||||||||||||
| 482 | columns in table, in same order as returned by C<< $table->columns() >>. | ||||||||||||
| 483 | |||||||||||||
| 484 | =head2 $table->sql() | ||||||||||||
| 485 | |||||||||||||
| 486 | =head2 $table->sql_with_alias() | ||||||||||||
| 487 | |||||||||||||
| 488 | =head2 $table->sql_for_select_clause() | ||||||||||||
| 489 | |||||||||||||
| 490 | Returns the appropriate SQL snippet for the table. | ||||||||||||
| 491 | |||||||||||||
| 492 | =head2 $table->id() | ||||||||||||
| 493 | |||||||||||||
| 494 | Returns a unique identifier for the table. | ||||||||||||
| 495 | |||||||||||||
| 496 | =head1 ROLES | ||||||||||||
| 497 | |||||||||||||
| 498 | This class does the L<Fey::Role::TableLike>, L<Fey::Role::MakesAliasObjects>, | ||||||||||||
| 499 | and L<Fey::Role::Named> roles. | ||||||||||||
| 500 | |||||||||||||
| 501 | =head1 BUGS | ||||||||||||
| 502 | |||||||||||||
| 503 | See L<Fey> for details on how to report bugs. | ||||||||||||
| 504 | |||||||||||||
| 505 | =head1 AUTHOR | ||||||||||||
| 506 | |||||||||||||
| 507 | Dave Rolsky <autarch@urth.org> | ||||||||||||
| 508 | |||||||||||||
| 509 | =head1 COPYRIGHT AND LICENSE | ||||||||||||
| 510 | |||||||||||||
| 511 | This software is Copyright (c) 2011 - 2015 by Dave Rolsky. | ||||||||||||
| 512 | |||||||||||||
| 513 | This is free software, licensed under: | ||||||||||||
| 514 | |||||||||||||
| 515 | The Artistic License 2.0 (GPL Compatible) | ||||||||||||
| 516 | |||||||||||||
| 517 | =cut | ||||||||||||