« blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Request.pm
↑ Summary
blib/lib/auto/share/dist/Cmd-Dwarf/app/lib/Dwarf/Util/DateTime.pm »
| 1 | package App::DB; | ||
| 2 | use Dwarf::Pragma; | ||
| 3 | use parent 'Teng'; | ||
| 4 | use Class::Method::Modifiers; | ||
| 5 | use DateTime; | ||
| 6 | use DateTime::Format::Pg; | ||
| 7 | |||
| 8 | #__PACKAGE__->load_plugin('BulkInsert'); | ||
| 9 | __PACKAGE__->load_plugin('Count'); | ||
| 10 | |||
| 11 | before insert => \&will_insert; | ||
| 12 | before fast_insert => \&will_insert; | ||
| 13 | sub will_insert { | ||
| 14 | my ($self, $table_name, $row_data) = @_; | ||
| 15 | my $table = $self->schema->get_table($table_name); | ||
| 16 | if (grep /^created_at$/, @{ $table->columns }) { | ||
| 17 | $row_data->{created_at} ||= \'NOW()'; | ||
| 18 | } | ||
| 19 | if (grep /^updated_at$/, @{ $table->columns }) { | ||
| 20 | $row_data->{updated_at} ||= \'NOW()'; | ||
| 21 | } | ||
| 22 | } | ||
| 23 | |||
| 24 | before update => \&will_update; | ||
| 25 | sub will_update { | ||
| 26 | my ($self, $table_name, $update_row_data, $update_condition) = @_; | ||
| 27 | my $table = $self->schema->get_table($table_name); | ||
| 28 | if (grep /^updated_at$/, @{ $table->columns }) { | ||
| 29 | $update_row_data->{updated_at} ||= \'NOW()'; | ||
| 30 | } | ||
| 31 | } | ||
| 32 | |||
| 33 | sub inflate_dt { | ||
| 34 | my ($self, $value) = @_; | ||
| 35 | return unless defined $value; | ||
| 36 | my $dt = DateTime::Format::Pg->parse_datetime($value); | ||
| 37 | $dt->set_time_zone('Asia/Tokyo'); | ||
| 38 | return $dt; | ||
| 39 | } | ||
| 40 | |||
| 41 | sub deflate_dt { | ||
| 42 | my ($self, $dt) = @_; | ||
| 43 | return if ref $dt ne 'DateTime'; | ||
| 44 | return sprintf "%s %s", $dt->ymd('-'), $dt->hms; | ||
| 45 | } | ||
| 46 | |||
| 47 | 1; |