Implement EpochMilli with milliseconds precision.

Used for decimal(x,x) formats like start_time and init_time in CDRs.
agranig/2.004-ramoptimized
Andreas Granig 12 years ago committed by Lars Dieckow
parent c0182df990
commit 503442ab63

@ -6,6 +6,7 @@ my $builder = Module::Build->new(
dist_version_from => 'lib/NGCP/Schema.pm',
requires => {
'aliased' => 0,
'DateTime' => 0,
'DateTime::Format::MySQL' => 0, #required by DBIx::Class
'DBD::mysql' => 0,
'DBIx::Class::EncodedColumn' => 0,

@ -0,0 +1,44 @@
package NGCP::Schema::InflateColumn::DateTime::EpochMilli;
use Sipwise::Base;
use DateTime qw();
extends 'DBIx::Class';
__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_milli } ) {
$info->{ inflate_datetime } = 'epoch_milli';
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} eq "decimal"
|| (exists $info->{inflate_datetime}
&& $info->{inflate_datetime} eq 'epoch_milli');
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} eq "decimal"
|| (exists $info->{inflate_datetime}
&& $info->{inflate_datetime} eq 'epoch_milli');
return $value->epoch;
}

@ -12,7 +12,11 @@ our $VERSION = '2.000';
extends 'DBIx::Class::Core';
__PACKAGE__->load_components("InflateColumn::DateTime", "Helper::Row::ToJSON");
__PACKAGE__->load_components(
"InflateColumn::DateTime",
"Helper::Row::ToJSON",
"+NGCP::Schema::InflateColumn::DateTime::EpochMilli",
);
__PACKAGE__->table("accounting.cdr");
@ -635,7 +639,13 @@ NGCP::Schema::Result::cdr
# Created by DBIx::Class::Schema::Loader v0.07035 @ 2013-06-27 12:51:39
# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:rNMfsyIh9LAGJGoe/or06w
for my $col (qw/init_time start_time/) {
if(__PACKAGE__->has_column($col)) {
__PACKAGE__->remove_column($col);
__PACKAGE__->add_column($col =>
{ data_type => "decimal", is_nullable => 0, size => [13, 3], inflate_datetime => 'epoch_milli' }
);
}
}
# You can replace this text with custom code or comments, and it will be preserved on regeneration
__PACKAGE__->meta->make_immutable;
1;

Loading…
Cancel
Save