| 1 | package Eve::PgSqlType::Interval; | ||
| 2 | |||
| 3 | 2 | ▶ | use parent qw(Eve::PgSqlType); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | |||
| 8 | use DateTime::Format::Pg; | ||
| 9 | use DBD::Pg (); | ||
| 10 | |||
| 11 | =head1 NAME | ||
| 12 | |||
| 13 | B<Eve::PgSqlType::Interval> - a PostgreSQL interval type. | ||
| 14 | |||
| 15 | =head1 SYNOPSIS | ||
| 16 | |||
| 17 | my $interval = Eve::PgSqlType::Interval->new(); | ||
| 18 | $interval->serialize(value => $duration); | ||
| 19 | |||
| 20 | =head1 DESCRIPTION | ||
| 21 | |||
| 22 | B<Eve::PgSqlType::Interval> is a PostgreSQL interval type adapter | ||
| 23 | class. | ||
| 24 | |||
| 25 | =head1 METHODS | ||
| 26 | |||
| 27 | =head2 B<get_type()> | ||
| 28 | |||
| 29 | =head3 Returns | ||
| 30 | |||
| 31 | The PG_TIMESTAMP type. | ||
| 32 | |||
| 33 | =cut | ||
| 34 | |||
| 35 | sub get_type { | ||
| 36 | return DBD::Pg::PG_INTERVAL; | ||
| 37 | } | ||
| 38 | |||
| 39 | =head2 B<wrap()> | ||
| 40 | |||
| 41 | Wraps an expression with CAST statement. | ||
| 42 | |||
| 43 | =head3 Arguments | ||
| 44 | |||
| 45 | =over 4 | ||
| 46 | |||
| 47 | =item C<expression> | ||
| 48 | |||
| 49 | =back | ||
| 50 | |||
| 51 | =head3 Returns | ||
| 52 | |||
| 53 | CAST (C<expression> AS interval) | ||
| 54 | |||
| 55 | =cut | ||
| 56 | |||
| 57 | sub wrap { | ||
| 58 | my ($self, %arg_hash) = @_; | ||
| 59 | Eve::Support::arguments(\%arg_hash, my $expression); | ||
| 60 | |||
| 61 | return 'CAST ('.$expression.' AS interval)'; | ||
| 62 | } | ||
| 63 | |||
| 64 | =head2 B<serialize()> | ||
| 65 | |||
| 66 | Formats a B<DateTime::Duration> object into the appropriate string interval | ||
| 67 | representation with the B<DateTime::Format::Pg> module. | ||
| 68 | |||
| 69 | =head3 Arguments | ||
| 70 | |||
| 71 | =over 4 | ||
| 72 | |||
| 73 | =item C<value> | ||
| 74 | |||
| 75 | =back | ||
| 76 | |||
| 77 | =head3 Returns | ||
| 78 | |||
| 79 | The string like '1 day 1 hour 1 minute 1 second'. | ||
| 80 | |||
| 81 | =cut | ||
| 82 | |||
| 83 | sub serialize { | ||
| 84 | my ($self, %arg_hash) = @_; | ||
| 85 | Eve::Support::arguments(\%arg_hash, my $value); | ||
| 86 | |||
| 87 | return DateTime::Format::Pg->format_interval($value); | ||
| 88 | } | ||
| 89 | |||
| 90 | =head2 B<deserialize()> | ||
| 91 | |||
| 92 | Parses an interval string representation into the appropriate | ||
| 93 | B<DateTime::Duration> object with the B<DateTime::Format::Pg> module. | ||
| 94 | |||
| 95 | =head3 Arguments | ||
| 96 | |||
| 97 | =over 4 | ||
| 98 | |||
| 99 | =item C<value> | ||
| 100 | |||
| 101 | =back | ||
| 102 | |||
| 103 | =head3 Returns | ||
| 104 | |||
| 105 | A B<DateTime::Duration> object. | ||
| 106 | |||
| 107 | =cut | ||
| 108 | |||
| 109 | sub deserialize { | ||
| 110 | my ($self, %arg_hash) = @_; | ||
| 111 | Eve::Support::arguments(\%arg_hash, my $value); | ||
| 112 | |||
| 113 | my $result; | ||
| 114 | if (defined $value) { | ||
| 115 | $result = DateTime::Format::Pg->parse_interval($value); | ||
| 116 | } | ||
| 117 | |||
| 118 | return $result; | ||
| 119 | } | ||
| 120 | |||
| 121 | =head1 SEE ALSO | ||
| 122 | |||
| 123 | =over 4 | ||
| 124 | |||
| 125 | =item L<DateTime> | ||
| 126 | |||
| 127 | =item L<DateTime::Duration> | ||
| 128 | |||
| 129 | =item L<DateTime::Format::Pg> | ||
| 130 | |||
| 131 | =item L<DBD::Pg> | ||
| 132 | |||
| 133 | =item L<Eve::PgSqlType> | ||
| 134 | |||
| 135 | =back | ||
| 136 | |||
| 137 | =head1 LICENSE AND COPYRIGHT | ||
| 138 | |||
| 139 | Copyright 2012 Igor Zinovyev. | ||
| 140 | |||
| 141 | This program is free software; you can redistribute it and/or modify it | ||
| 142 | under the terms of either: the GNU General Public License as published | ||
| 143 | by the Free Software Foundation; or the Artistic License. | ||
| 144 | |||
| 145 | See http://dev.perl.org/licenses/ for more information. | ||
| 146 | |||
| 147 | |||
| 148 | =head1 AUTHOR | ||
| 149 | |||
| 150 | =over 4 | ||
| 151 | |||
| 152 | =item L<Sergey Konoplev|mailto:gray.ru@gmail.com> | ||
| 153 | |||
| 154 | =item L<Igor Zinovyev|mailto:zinigor@gmail.com> | ||
| 155 | |||
| 156 | =back | ||
| 157 | |||
| 158 | =cut | ||
| 159 | |||
| 160 | 1; | ||