| 1 | # $Id: DateField.pm,v 1.5 2005/10/05 20:39:34 mjb47 Exp $ | ||
| 2 | |||
| 3 | package DBIx::XMLServer::DateField; | ||
| 4 | use XML::LibXML; | ||
| 5 | use Date::Manip qw( Date_Init UnixDate ); | ||
| 6 | our @ISA = ('DBIx::XMLServer::Field'); | ||
| 7 | |||
| 8 | sub BEGIN { Date_Init("DateFormat = EU"); } | ||
| 9 | |||
| 10 | =head1 NAME | ||
| 11 | |||
| 12 | DBIx::XMLServer::DateField - date field type | ||
| 13 | |||
| 14 | =head1 DESCRIPTION | ||
| 15 | |||
| 16 | This class implements the built-in date field type of | ||
| 17 | DBIx::XMLServer. The B<where> and B<value> methods are overridden | ||
| 18 | from the base class. | ||
| 19 | |||
| 20 | To use this field type, you must have the C<Date::Manip> package installed. | ||
| 21 | |||
| 22 | =head2 B<where> method | ||
| 23 | |||
| 24 | $sql_expression = $date_field->where($condition); | ||
| 25 | |||
| 26 | The condition may consist of one of the comparison operators '=', '<', '>', | ||
| 27 | '>=' or '<=' followed by a date. The date may be in any format understood | ||
| 28 | by the C<Date::Manip> package, such as '1976-02-28' or 'two months ago'. | ||
| 29 | |||
| 30 | Alternatively, the condition may be empty, in which case the SQL expression | ||
| 31 | is | ||
| 32 | |||
| 33 | <field> IS NOT NULL . | ||
| 34 | |||
| 35 | If the condition is the character '!', then the SQL expression is | ||
| 36 | |||
| 37 | <field> IS NULL . | ||
| 38 | |||
| 39 | =cut | ||
| 40 | |||
| 41 | sub where { | ||
| 42 | my $self = shift; | ||
| 43 | my $cond = shift; | ||
| 44 | my $column = $self->select; | ||
| 45 | return "$column IS NOT NULL" if $cond eq ''; | ||
| 46 | return "$column IS NULL" if $cond eq '!'; | ||
| 47 | my ($comp, $date) = ($cond =~ /([=<>]+)(.*)/); | ||
| 48 | defined $comp or die "Unrecognised date condition: $cond\n"; | ||
| 49 | $comp =~ /^(=|[<>]=?)$/ or die "Unrecognised date comparison: $comp\n"; | ||
| 50 | my $date1 = UnixDate($date, '%Q') or die "Unrecognised date: $date\n"; | ||
| 51 | return "$column $comp " . $date1; | ||
| 52 | } | ||
| 53 | |||
| 54 | =head2 B<value> method | ||
| 55 | |||
| 56 | $date = $date_field->value(\@results); | ||
| 57 | |||
| 58 | The date is returned as 'YYYY-mm-dd', as required by the B<xsd:date> type | ||
| 59 | of XML Schema. | ||
| 60 | |||
| 61 | =cut | ||
| 62 | |||
| 63 | sub value { | ||
| 64 | shift; | ||
| 65 | return UnixDate(shift @{shift()}, '%Y-%m-%d'); | ||
| 66 | } | ||
| 67 | |||
| 68 | 1; | ||
| 69 | |||
| 70 | __END__ |