Fey::Column - Represents a column


Fey documentation Contained in the Fey distribution.

Index


Code Index:

NAME

Top

Fey::Column - Represents a column

VERSION

Top

version 0.40

SYNOPSIS

Top

  my $column = Fey::Column->new( name              => 'user_id',
                                 type              => 'integer',
                                 is_auto_increment => 1,
                               );

DESCRIPTION

Top

This class represents a column in a table.

METHODS

Top

This class provides the following methods:

Fey::Column->new()

This method constructs a new Fey::Column object. It takes the following parameters:

* name - required

The name of the column.

* type - required

The type of the column. This should be a string. Do not include modifiers like length or precision.

* generic_type - optional

This should be one of the following types:

* text
* blob
* integer
* float
* date
* datetime
* time
* boolean
* other

This indicate a generic type for the column, which is intended to allow for a common description of column types across different DBMS platforms.

If this parameter is not specified, then the constructor code will attempt to determine a reasonable value, defaulting to "other" if necessary.

* length - optional

The length of the column. This must be a positive integer.

* precision - optional

The precision of the column, for float-type columns. This must be an integer >= 0.

* is_auto_increment - defaults to 0

This indicates whether or not the column is auto-incremented.

* is_nullable - defaults to 0

A boolean indicating whether the column is nullable.

* default - optional

This must be either a scalar (including undef) or a Fey::Literal object. If a scalar is provided, it is turned into a Fey::Literal object via Fey::Literal->new_from_scalar().

$column->name()

$column->type()

$column->generic_type()

$column->length()

$column->precision()

$column->is_auto_increment()

$column->is_nullable()

$column->default()

Returns the specified attribute.

$column->table()

Returns the Fey::Table object to which the column belongs, if any.

$column->alias(%p)

$column->alias($alias_name)

This method returns a new Fey::Column::Alias object based on the column. Any parameters passed to this method will be passed through to Fey::Column::Alias->new().

As a shortcut, if you pass a single argument to this method, it will be passed as the "alias_name" parameter to Fey::Table::Column->new().

$column->is_alias()

Always returns false.

$column->sql()

$column->sql_with_alias()

$column->sql_or_alias()

Returns the appropriate SQL snippet for the column.

$column->id()

Returns a unique identifier for the column.

ROLES

Top

This class does the Fey::Role::ColumnLike, Fey::Role::MakesAliasObjects, and Fey::Role::Named roles.

BUGS

Top

See Fey for details on how to report bugs.

AUTHOR

Top

Dave Rolsky <autarch@urth.org>

COPYRIGHT AND LICENSE

Top


Fey documentation Contained in the Fey distribution.

package Fey::Column;
BEGIN {
  $Fey::Column::VERSION = '0.40';
}

use strict;
use warnings;
use namespace::autoclean;

use Scalar::Util qw( blessed weaken );

use Fey::Column::Alias;
use Fey::Exceptions qw(  param_error object_state_error );
use Fey::Literal;
use Fey::Table;
use Fey::Table::Alias;
use Fey::Types qw(
    Bool
    DefaultValue
    GenericTypeName
    PosInteger
    PosOrZeroInteger
    Str
);

use Moose;
use MooseX::SemiAffordanceAccessor;
use MooseX::StrictConstructor;

with 'Fey::Role::ColumnLike';

with 'Fey::Role::MakesAliasObjects' => {
    self_param  => 'column',
    alias_class => 'Fey::Column::Alias',
};

has 'id' => (
    is         => 'ro',
    lazy_build => 1,
    init_arg   => undef,
    clearer    => '_clear_id',
);

has 'name' => (
    is       => 'ro',
    isa      => Str,
    required => 1,
);

has 'generic_type' => (
    is         => 'ro',
    isa        => GenericTypeName,
    lazy_build => 1,
);

has type => (
    is       => 'ro',
    isa      => Str,
    required => 1,
);

has length => (
    is       => 'ro',
    isa      => PosInteger,
    required => 0
);

# How to say that precision requires length as well?
has precision => (
    is       => 'ro',
    isa      => PosOrZeroInteger,
    required => 0
);

has is_auto_increment => (
    is      => 'ro',
    isa     => Bool,
    default => 0,
);

has is_nullable => (
    is      => 'ro',
    isa     => Bool,
    default => 0,
);

has default => (
    is     => 'ro',
    isa    => DefaultValue,
    coerce => 1,
);

has 'table' => (
    is        => 'rw',
    does      => 'Fey::Role::TableLike',
    weak_ref  => 1,
    predicate => 'has_table',
    writer    => '_set_table',
    clearer   => '_clear_table',
);

after '_set_table', '_clear_table' => sub { $_[0]->_clear_id() };

with 'Fey::Role::Named';

{
    my @TypesRe = (
        [ text => qr/(?:text|char(?:acter)?)\b/xism ],
        [ blob => qr/blob\b|bytea\b/xism ],

        # The year type comes from MySQL
        [ integer => qr/(?:int(?:eger)?\d*|year)\b/xism ],
        [ float => qr/(?:float\d*|decimal|real|double|money|numeric)\b/xism ],

        # MySQL's timestamp is not always a datetime, it depends on
        # the length of the column, but this is the best _guess_.
        [ datetime => qr/datetime\b|^timestamp/xism ],
        [ date     => qr/date\b/xism ],
        [ time     => qr/^time|time\b/xism ],
        [ boolean  => qr/\bbool/xism ],
    );

    sub _build_generic_type {
        my $self = shift;
        my $type = $self->type();

        for my $p (@TypesRe) {
            return $p->[0] if $type =~ /$p->[1]/;
        }

        return 'other';
    }
}

sub _clone {
    my $self = shift;

    my %clone = %{$self};

    return bless \%clone, ref $self;
}

sub is_alias { return 0 }

sub sql {
    my $self = shift;
    my $dbh  = shift;

    return $dbh->quote_identifier(
        undef,
        $self->_containing_table_name_or_alias(),
        $self->name(),
    );
}

sub sql_with_alias { goto &sql }

sub sql_or_alias { goto &sql }

sub _build_id {
    my $self = shift;

    my $table = $self->table();

    object_state_error
        'The id attribute cannot be determined for a column object which has no table.'
        unless $table;

    return $table->id() . q{.} . $self->name();
}

__PACKAGE__->meta()->make_immutable();

1;

# ABSTRACT: Represents a column




__END__