| 1 | package Fey::Column; | ||||||||||
| 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 Scalar::Util qw( blessed weaken ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 25 | ▶ | use Fey::Column::Alias; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 25 | ▶ | use Fey::Exceptions qw( param_error object_state_error ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | 25 | ▶ | use Fey::Literal; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 14 | 25 | ▶ | use Fey::Table; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | 25 | ▶ | use Fey::Table::Alias; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 25 | use Fey::Types qw( | |||||||||
| 17 | Bool | ||||||||||
| 18 | DefaultValue | ||||||||||
| 19 | GenericTypeName | ||||||||||
| 20 | PosInteger | ||||||||||
| 21 | PosOrZeroInteger | ||||||||||
| 22 | Str | ||||||||||
| 23 | 25 | ▶ | ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 24 | |||||||||||
| 25 | 25 | ▶ | use Moose 0.90; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 26 | 25 | ▶ | use MooseX::SemiAffordanceAccessor 0.03; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 27 | 25 | ▶ | use MooseX::StrictConstructor 0.07; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 28 | |||||||||||
| 29 | 25 | with 'Fey::Role::ColumnLike'; | |||||||||
| 30 | |||||||||||
| 31 | 25 | with 'Fey::Role::MakesAliasObjects' => { | |||||||||
| 32 | self_param => 'column', | ||||||||||
| 33 | alias_class => 'Fey::Column::Alias', | ||||||||||
| 34 | }; | ||||||||||
| 35 | |||||||||||
| 36 | 25 | has 'id' => ( | |||||||||
| 37 | is => 'ro', | ||||||||||
| 38 | lazy_build => 1, | ||||||||||
| 39 | init_arg => undef, | ||||||||||
| 40 | clearer => '_clear_id', | ||||||||||
| 41 | ); | ||||||||||
| 42 | |||||||||||
| 43 | 25 | has 'name' => ( | |||||||||
| 44 | is => 'ro', | ||||||||||
| 45 | isa => Str, | ||||||||||
| 46 | required => 1, | ||||||||||
| 47 | ); | ||||||||||
| 48 | |||||||||||
| 49 | 25 | has 'generic_type' => ( | |||||||||
| 50 | is => 'ro', | ||||||||||
| 51 | isa => GenericTypeName, | ||||||||||
| 52 | lazy_build => 1, | ||||||||||
| 53 | ); | ||||||||||
| 54 | |||||||||||
| 55 | 25 | has type => ( | |||||||||
| 56 | is => 'ro', | ||||||||||
| 57 | isa => Str, | ||||||||||
| 58 | required => 1, | ||||||||||
| 59 | ); | ||||||||||
| 60 | |||||||||||
| 61 | 25 | has length => ( | |||||||||
| 62 | is => 'ro', | ||||||||||
| 63 | isa => PosInteger, | ||||||||||
| 64 | required => 0 | ||||||||||
| 65 | ); | ||||||||||
| 66 | |||||||||||
| 67 | # How to say that precision requires length as well? | ||||||||||
| 68 | 25 | has precision => ( | |||||||||
| 69 | is => 'ro', | ||||||||||
| 70 | isa => PosOrZeroInteger, | ||||||||||
| 71 | required => 0 | ||||||||||
| 72 | ); | ||||||||||
| 73 | |||||||||||
| 74 | 25 | has is_auto_increment => ( | |||||||||
| 75 | is => 'ro', | ||||||||||
| 76 | isa => Bool, | ||||||||||
| 77 | default => 0, | ||||||||||
| 78 | ); | ||||||||||
| 79 | |||||||||||
| 80 | 25 | has is_nullable => ( | |||||||||
| 81 | is => 'ro', | ||||||||||
| 82 | isa => Bool, | ||||||||||
| 83 | default => 0, | ||||||||||
| 84 | ); | ||||||||||
| 85 | |||||||||||
| 86 | 25 | has default => ( | |||||||||
| 87 | is => 'ro', | ||||||||||
| 88 | isa => DefaultValue, | ||||||||||
| 89 | coerce => 1, | ||||||||||
| 90 | ); | ||||||||||
| 91 | |||||||||||
| 92 | 25 | has 'table' => ( | |||||||||
| 93 | is => 'rw', | ||||||||||
| 94 | does => 'Fey::Role::TableLike', | ||||||||||
| 95 | weak_ref => 1, | ||||||||||
| 96 | predicate => 'has_table', | ||||||||||
| 97 | writer => '_set_table', | ||||||||||
| 98 | clearer => '_clear_table', | ||||||||||
| 99 | ); | ||||||||||
| 100 | |||||||||||
| 101 | 673 | ▶ | after '_set_table', '_clear_table' => sub { $_[0]->_clear_id() }; | ||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||
| 102 | |||||||||||
| 103 | 25 | with 'Fey::Role::Named'; | |||||||||
| 104 | |||||||||||
| 105 | { | ||||||||||
| 106 | 25 | my @TypesRe = ( | |||||||||
| 107 | [ text => qr/(?:text|char(?:acter)?)\b/xism ], | ||||||||||
| 108 | [ blob => qr/blob\b|bytea\b/xism ], | ||||||||||
| 109 | |||||||||||
| 110 | # The year type comes from MySQL | ||||||||||
| 111 | [ integer => qr/(?:int(?:eger)?\d*|year)\b/xism ], | ||||||||||
| 112 | [ float => qr/(?:float\d*|decimal|real|double|money|numeric)\b/xism ], | ||||||||||
| 113 | |||||||||||
| 114 | # MySQL's timestamp is not always a datetime, it depends on | ||||||||||
| 115 | # the length of the column, but this is the best _guess_. | ||||||||||
| 116 | [ datetime => qr/datetime\b|^timestamp/xism ], | ||||||||||
| 117 | [ date => qr/date\b/xism ], | ||||||||||
| 118 | [ time => qr/^time|time\b/xism ], | ||||||||||
| 119 | [ boolean => qr/\bbool/xism ], | ||||||||||
| 120 | ); | ||||||||||
| 121 | |||||||||||
| 122 | sub _build_generic_type { | ||||||||||
| 123 | 62 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_generic_type: called CC=3 SCAR=11.0
| |||||||||||
| 124 | 62 | my $type = $self->type(); | |||||||||
| 125 | |||||||||||
| 126 | 62 | for my $p (@TypesRe) { | |||||||||
| 127 | 259 | ▶ | return $p->[0] if $type =~ /$p->[1]/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 128 | } | ||||||||||
| 129 | |||||||||||
| 130 | 9 | return 'other'; | |||||||||
| 131 | } | ||||||||||
| 132 | } | ||||||||||
| 133 | |||||||||||
| 134 | 25 | sub _clone { | |||||||||
| 135 | 5 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_clone: called CC=1 SCAR=0.0
| |||||||||||
| 136 | |||||||||||
| 137 | 5 | my %clone = %{$self}; | |||||||||
| 138 | |||||||||||
| 139 | 5 | return bless \%clone, ref $self; | |||||||||
| 140 | } | ||||||||||
| 141 | |||||||||||
| 142 | 1 | ▶ | sub is_alias { return 0 } | ||||||||
|
Subroutine
is_alias: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 143 | |||||||||||
| 144 | sub sql { | ||||||||||
| 145 | 139 | ▶ | my $self = shift; | ||||||||
|
Subroutine
sql: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 146 | 139 | my $dbh = shift; | |||||||||
| 147 | |||||||||||
| 148 | 139 | return $dbh->quote_identifier( | |||||||||
| 149 | undef, | ||||||||||
| 150 | $self->_containing_table_name_or_alias(), | ||||||||||
| 151 | $self->name(), | ||||||||||
| 152 | ); | ||||||||||
| 153 | } | ||||||||||
| 154 | |||||||||||
| 155 | 25 | ▶ | sub sql_with_alias { goto &sql } | ||||||||
|
Subroutine
sql_with_alias: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 156 | |||||||||||
| 157 | 100 | ▶ | sub sql_or_alias { goto &sql } | ||||||||
|
Subroutine
sql_or_alias: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 158 | |||||||||||
| 159 | sub _build_id { | ||||||||||
| 160 | 3 | ▶ | my $self = shift; | ||||||||
|
Subroutine
_build_id: called CC=2 SCAR=6.9
| |||||||||||
| 161 | |||||||||||
| 162 | 3 | my $table = $self->table(); | |||||||||
| 163 | |||||||||||
| 164 | 3 | ▶ | object_state_error | ||||||||
|
Branch: 1 branch
| |||||||||||
| 165 | 'The id attribute cannot be determined for a column object which has no table.' | ||||||||||
| 166 | unless $table; | ||||||||||
| 167 | |||||||||||
| 168 | 1 | return $table->id() . q{.} . $self->name(); | |||||||||
| 169 | } | ||||||||||
| 170 | |||||||||||
| 171 | 25 | __PACKAGE__->meta()->make_immutable(); | |||||||||
| 172 | |||||||||||
| 173 | 25 | 1; | |||||||||
| 174 | |||||||||||
| 175 | # ABSTRACT: Represents a column | ||||||||||
| 176 | |||||||||||
| 177 | __END__ | ||||||||||
| 178 | |||||||||||
| 179 | =pod | ||||||||||
| 180 | |||||||||||
| 181 | =head1 NAME | ||||||||||
| 182 | |||||||||||
| 183 | Fey::Column - Represents a column | ||||||||||
| 184 | |||||||||||
| 185 | =head1 VERSION | ||||||||||
| 186 | |||||||||||
| 187 | version 0.42 | ||||||||||
| 188 | |||||||||||
| 189 | =head1 SYNOPSIS | ||||||||||
| 190 | |||||||||||
| 191 | my $column = Fey::Column->new( name => 'user_id', | ||||||||||
| 192 | type => 'integer', | ||||||||||
| 193 | is_auto_increment => 1, | ||||||||||
| 194 | ); | ||||||||||
| 195 | |||||||||||
| 196 | =head1 DESCRIPTION | ||||||||||
| 197 | |||||||||||
| 198 | This class represents a column in a table. | ||||||||||
| 199 | |||||||||||
| 200 | =head1 METHODS | ||||||||||
| 201 | |||||||||||
| 202 | This class provides the following methods: | ||||||||||
| 203 | |||||||||||
| 204 | =head2 Fey::Column->new() | ||||||||||
| 205 | |||||||||||
| 206 | This method constructs a new C<Fey::Column> object. It takes the | ||||||||||
| 207 | following parameters: | ||||||||||
| 208 | |||||||||||
| 209 | =over 4 | ||||||||||
| 210 | |||||||||||
| 211 | =item * name - required | ||||||||||
| 212 | |||||||||||
| 213 | The name of the column. | ||||||||||
| 214 | |||||||||||
| 215 | =item * type - required | ||||||||||
| 216 | |||||||||||
| 217 | The type of the column. This should be a string. Do not include | ||||||||||
| 218 | modifiers like length or precision. | ||||||||||
| 219 | |||||||||||
| 220 | =item * generic_type - optional | ||||||||||
| 221 | |||||||||||
| 222 | This should be one of the following types: | ||||||||||
| 223 | |||||||||||
| 224 | =over 8 | ||||||||||
| 225 | |||||||||||
| 226 | =item * text | ||||||||||
| 227 | |||||||||||
| 228 | =item * blob | ||||||||||
| 229 | |||||||||||
| 230 | =item * integer | ||||||||||
| 231 | |||||||||||
| 232 | =item * float | ||||||||||
| 233 | |||||||||||
| 234 | =item * date | ||||||||||
| 235 | |||||||||||
| 236 | =item * datetime | ||||||||||
| 237 | |||||||||||
| 238 | =item * time | ||||||||||
| 239 | |||||||||||
| 240 | =item * boolean | ||||||||||
| 241 | |||||||||||
| 242 | =item * other | ||||||||||
| 243 | |||||||||||
| 244 | =back | ||||||||||
| 245 | |||||||||||
| 246 | This indicate a generic type for the column, which is intended to | ||||||||||
| 247 | allow for a common description of column types across different DBMS | ||||||||||
| 248 | platforms. | ||||||||||
| 249 | |||||||||||
| 250 | If this parameter is not specified, then the constructor code will | ||||||||||
| 251 | attempt to determine a reasonable value, defaulting to "other" if | ||||||||||
| 252 | necessary. | ||||||||||
| 253 | |||||||||||
| 254 | =item * length - optional | ||||||||||
| 255 | |||||||||||
| 256 | The length of the column. This must be a positive integer. | ||||||||||
| 257 | |||||||||||
| 258 | =item * precision - optional | ||||||||||
| 259 | |||||||||||
| 260 | The precision of the column, for float-type columns. This must be an | ||||||||||
| 261 | integer >= 0. | ||||||||||
| 262 | |||||||||||
| 263 | =item * is_auto_increment - defaults to 0 | ||||||||||
| 264 | |||||||||||
| 265 | This indicates whether or not the column is auto-incremented. | ||||||||||
| 266 | |||||||||||
| 267 | =item * is_nullable - defaults to 0 | ||||||||||
| 268 | |||||||||||
| 269 | A boolean indicating whether the column is nullable. | ||||||||||
| 270 | |||||||||||
| 271 | =item * default - optional | ||||||||||
| 272 | |||||||||||
| 273 | This must be either a scalar (including undef) or a C<Fey::Literal> | ||||||||||
| 274 | object. If a scalar is provided, it is turned into a C<Fey::Literal> | ||||||||||
| 275 | object via C<< Fey::Literal->new_from_scalar() >>. | ||||||||||
| 276 | |||||||||||
| 277 | =back | ||||||||||
| 278 | |||||||||||
| 279 | =head2 $column->name() | ||||||||||
| 280 | |||||||||||
| 281 | =head2 $column->type() | ||||||||||
| 282 | |||||||||||
| 283 | =head2 $column->generic_type() | ||||||||||
| 284 | |||||||||||
| 285 | =head2 $column->length() | ||||||||||
| 286 | |||||||||||
| 287 | =head2 $column->precision() | ||||||||||
| 288 | |||||||||||
| 289 | =head2 $column->is_auto_increment() | ||||||||||
| 290 | |||||||||||
| 291 | =head2 $column->is_nullable() | ||||||||||
| 292 | |||||||||||
| 293 | =head2 $column->default() | ||||||||||
| 294 | |||||||||||
| 295 | Returns the specified attribute. | ||||||||||
| 296 | |||||||||||
| 297 | =head2 $column->table() | ||||||||||
| 298 | |||||||||||
| 299 | Returns the C<Fey::Table> object to which the column belongs, if any. | ||||||||||
| 300 | |||||||||||
| 301 | =head2 $column->alias(%p) | ||||||||||
| 302 | |||||||||||
| 303 | =head2 $column->alias($alias_name) | ||||||||||
| 304 | |||||||||||
| 305 | This method returns a new C<Fey::Column::Alias> object based on the | ||||||||||
| 306 | column. Any parameters passed to this method will be passed through to | ||||||||||
| 307 | C<< Fey::Column::Alias->new() >>. | ||||||||||
| 308 | |||||||||||
| 309 | As a shortcut, if you pass a single argument to this method, it will | ||||||||||
| 310 | be passed as the "alias_name" parameter to C<< | ||||||||||
| 311 | Fey::Table::Column->new() >>. | ||||||||||
| 312 | |||||||||||
| 313 | =head2 $column->is_alias() | ||||||||||
| 314 | |||||||||||
| 315 | Always returns false. | ||||||||||
| 316 | |||||||||||
| 317 | =head2 $column->sql() | ||||||||||
| 318 | |||||||||||
| 319 | =head2 $column->sql_with_alias() | ||||||||||
| 320 | |||||||||||
| 321 | =head2 $column->sql_or_alias() | ||||||||||
| 322 | |||||||||||
| 323 | Returns the appropriate SQL snippet for the column. | ||||||||||
| 324 | |||||||||||
| 325 | =head2 $column->id() | ||||||||||
| 326 | |||||||||||
| 327 | Returns a unique identifier for the column. | ||||||||||
| 328 | |||||||||||
| 329 | =head1 ROLES | ||||||||||
| 330 | |||||||||||
| 331 | This class does the L<Fey::Role::ColumnLike>, L<Fey::Role::MakesAliasObjects>, | ||||||||||
| 332 | and L<Fey::Role::Named> roles. | ||||||||||
| 333 | |||||||||||
| 334 | =head1 BUGS | ||||||||||
| 335 | |||||||||||
| 336 | See L<Fey> for details on how to report bugs. | ||||||||||
| 337 | |||||||||||
| 338 | =head1 AUTHOR | ||||||||||
| 339 | |||||||||||
| 340 | Dave Rolsky <autarch@urth.org> | ||||||||||
| 341 | |||||||||||
| 342 | =head1 COPYRIGHT AND LICENSE | ||||||||||
| 343 | |||||||||||
| 344 | This software is Copyright (c) 2011 - 2015 by Dave Rolsky. | ||||||||||
| 345 | |||||||||||
| 346 | This is free software, licensed under: | ||||||||||
| 347 | |||||||||||
| 348 | The Artistic License 2.0 (GPL Compatible) | ||||||||||
| 349 | |||||||||||
| 350 | =cut | ||||||||||