DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based columns to/from DateTime objects


DBIx-Class-DateTime-Epoch documentation Contained in the DBIx-Class-DateTime-Epoch distribution.

Index


Code Index:

NAME

Top

DBIx::Class::DateTime::Epoch - Automatic inflation/deflation of epoch-based columns to/from DateTime objects

SYNOPSIS

Top

    package MySchema::Foo;

    use base qw( DBIx::Class );

    __PACKAGE__->load_components( qw( DateTime::Epoch TimeStamp Core ) );
    __PACKAGE__->add_columns(
        name => {
            data_type => 'varchar',
            size      => 10,
        },
        bar => { # epoch stored as an int
            data_type        => 'bigint',
            inflate_datetime => 1,
        },
        baz => { # epoch stored as a string
            data_type        => 'varchar',
            size             => 50,
            inflate_datetime => 'epoch',
        },
        # working in conjunction with DBIx::Class::TimeStamp
        creation_time => {
            data_type        => 'bigint',
            inflate_datetime => 1,
            set_on_create    => 1,
        },
        modification_time => {
            data_type        => 'bigint',
            inflate_datetime => 1,
            set_on_create    => 1,
            set_on_update    => 1,
        }
    );

DESCRIPTION

Top

This module automatically inflates/deflates DateTime objects from/to epoch values for the specified columns. This module is essentially an extension to DBIx::Class::InflateColumn::DateTime so all of the settings, including locale and timezone, are also valid.

A column will be recognized as an epoch time given one of the following scenarios:

* data_type is an int of some sort and inflate_datetime is also set to a true value
* data_type is some other value (e.g. varchar) and inflate_datetime is explicitly set to epoch.

DBIx::Class::TimeStamp can also be used in conjunction with this module to support epoch-based columns that are automatically set on creation of a row and updated subsequent modifications.

METHODS

Top

add_columns( )

Provides backwards compatibility with the older DateTime::Epoch API.

_inflate_to_datetime( )

Overrides column inflation to use Datetime->from_epoch.

_deflate_from_datetime( )

Overrides column deflation to call epoch() on the column value.

SEE ALSO

Top

* DBIx::Class
* DBIx::Class::TimeStamp
* DateTime

AUTHORS

Top

Brian Cassidy <bricas@cpan.org>

Adam Paynter <adapay@cpan.org>

COPYRIGHT AND LICENSE

Top


DBIx-Class-DateTime-Epoch documentation Contained in the DBIx-Class-DateTime-Epoch distribution.

package DBIx::Class::DateTime::Epoch;

use strict;
use warnings;

our $VERSION = '0.07';

use base qw( DBIx::Class );

use DateTime;

__PACKAGE__->load_components( qw( InflateColumn::DateTime ) );

# back compat
sub add_columns {
    my( $class, @cols ) = @_;
    my @columns;

    while (my $col = shift @cols) {
        my $info = ref $cols[0] ? shift @cols : {};

        if( my $type = delete $info->{ epoch } ) {
            $info->{ inflate_datetime } = 'epoch';

            if( $type =~ m{^[cm]time$} ) {
                __PACKAGE__->load_components( 'TimeStamp' );
                $info->{ set_on_create } = 1;
                $info->{ set_on_update } = 1 if $type eq 'mtime';
            }
        }

        push @columns, $col => $info;

    }

    $class->next::method( @columns );
}

sub _inflate_to_datetime {
    my( $self, $value, $info, @rest ) = @_;
    return $self->next::method( $value, $info, @rest )
        unless $info->{ data_type } =~ m{int}i || $info->{ inflate_datetime } eq 'epoch';

    return DateTime->from_epoch( epoch => $value );
}

sub _deflate_from_datetime {
    my( $self, $value, $info, @rest ) = @_;
    return $self->next::method( $value, $info, @rest )
        unless $info->{ data_type } =~ m{int}i || $info->{ inflate_datetime } eq 'epoch';

    return $value->epoch;
}

1;

__END__